0.7.0 • Published 8 months ago
@atproto/xrpc v0.7.0
@atproto/xrpc: atproto HTTP API Client
TypeScript client library for talking to atproto services, with Lexicon schema validation.
Usage
import { LexiconDoc } from '@atproto/lexicon'
import { XrpcClient } from '@atproto/xrpc'
const pingLexicon = {
lexicon: 1,
id: 'io.example.ping',
defs: {
main: {
type: 'query',
description: 'Ping the server',
parameters: {
type: 'params',
properties: { message: { type: 'string' } },
},
output: {
encoding: 'application/json',
schema: {
type: 'object',
required: ['message'],
properties: { message: { type: 'string' } },
},
},
},
},
} satisfies LexiconDoc
const xrpc = new XrpcClient('https://ping.example.com', [
// Any number of lexicon here
pingLexicon,
])
const res1 = await xrpc.call('io.example.ping', {
message: 'hello world',
})
res1.encoding // => 'application/json'
res1.body // => {message: 'hello world'}With a custom fetch handler
import { XrpcClient } from '@atproto/xrpc'
const session = {
serviceUrl: 'https://ping.example.com',
token: '<my-token>',
async refreshToken() {
const { token } = await fetch('https://auth.example.com/refresh', {
method: 'POST',
headers: { Authorization: `Bearer ${this.token}` },
}).then((res) => res.json())
this.token = token
return token
},
}
const sessionBasedFetch: FetchHandler = async (
url: string,
init: RequestInit,
) => {
const headers = new Headers(init.headers)
headers.set('Authorization', `Bearer ${session.token}`)
const response = await fetch(new URL(url, session.serviceUrl), {
...init,
headers,
})
if (response.status === 401) {
// Refresh token, then try again.
const newToken = await session.refreshToken()
headers.set('Authorization', `Bearer ${newToken}`)
return fetch(new URL(url, session.serviceUrl), { ...init, headers })
}
return response
}
const xrpc = new XrpcClient(sessionBasedFetch, [
// Any number of lexicon here
pingLexicon,
])
//License
This project is dual-licensed under MIT and Apache 2.0 terms:
- MIT license (LICENSE-MIT.txt or http://opensource.org/licenses/MIT)
- Apache License, Version 2.0, (LICENSE-APACHE.txt or http://www.apache.org/licenses/LICENSE-2.0)
Downstream projects and end users may chose either license individually, or both together, at their discretion. The motivation for this dual-licensing is the additional software patent assurance provided by Apache 2.0.
0.6.7
11 months ago
0.6.6
12 months ago
0.6.9
11 months ago
0.6.8
11 months ago
0.6.10
10 months ago
0.6.12
9 months ago
0.6.11
9 months ago
0.6.5
1 year ago
0.6.4
1 year ago
0.7.0
8 months ago
0.6.3
1 year ago
0.6.2
1 year ago
0.6.1
1 year ago
0.5.1-rc.0
1 year ago
0.6.0-rc.0
1 year ago
0.6.0
1 year ago
0.5.0
2 years ago
0.4.3
2 years ago
0.4.3-next.0
2 years ago
0.4.2
2 years ago
0.4.1
2 years ago
0.4.0
2 years ago
0.3.3
2 years ago
0.3.0
2 years ago
0.2.0
2 years ago
0.3.2
2 years ago
0.3.1
2 years ago
0.1.0
3 years ago
0.0.4
3 years ago
0.0.3
3 years ago
0.0.2
3 years ago
0.0.1
3 years ago