@atproto/xrpc 0.4.3 → 0.5.1-rc.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,16 @@
1
1
  # @atproto/xrpc
2
2
 
3
+ ## 0.5.0
4
+
5
+ ### Minor Changes
6
+
7
+ - [#2169](https://github.com/bluesky-social/atproto/pull/2169) [`f689bd51a`](https://github.com/bluesky-social/atproto/commit/f689bd51a2f4e02d4eca40eb2568a1fcb95494e9) Thanks [@matthieusieben](https://github.com/matthieusieben)! - Build system rework, stop bundling dependencies.
8
+
9
+ ### Patch Changes
10
+
11
+ - Updated dependencies [[`f689bd51a`](https://github.com/bluesky-social/atproto/commit/f689bd51a2f4e02d4eca40eb2568a1fcb95494e9)]:
12
+ - @atproto/lexicon@0.4.0
13
+
3
14
  ## 0.4.3
4
15
 
5
16
  ### Patch Changes
package/README.md CHANGED
@@ -9,9 +9,9 @@ TypeScript client library for talking to [atproto](https://atproto.com) services
9
9
 
10
10
  ```typescript
11
11
  import { LexiconDoc } from '@atproto/lexicon'
12
- import xrpc from '@atproto/xrpc'
12
+ import { XrpcClient } from '@atproto/xrpc'
13
13
 
14
- const pingLexicon: LexiconDoc = {
14
+ const pingLexicon = {
15
15
  lexicon: 1,
16
16
  id: 'io.example.ping',
17
17
  defs: {
@@ -32,44 +32,69 @@ const pingLexicon: LexiconDoc = {
32
32
  },
33
33
  },
34
34
  },
35
- }
36
- xrpc.addLexicon(pingLexicon)
35
+ } satisfies LexiconDoc
36
+
37
+ const xrpc = new XrpcClient('https://ping.example.com', [
38
+ // Any number of lexicon here
39
+ pingLexicon,
40
+ ])
37
41
 
38
- const res1 = await xrpc.call('https://example.com', 'io.example.ping', {
42
+ const res1 = await xrpc.call('io.example.ping', {
39
43
  message: 'hello world',
40
44
  })
41
45
  res1.encoding // => 'application/json'
42
46
  res1.body // => {message: 'hello world'}
43
- const res2 = await xrpc
44
- .service('https://example.com')
45
- .call('io.example.ping', { message: 'hello world' })
46
- res2.encoding // => 'application/json'
47
- res2.body // => {message: 'hello world'}
47
+ ```
48
48
 
49
- const writeJsonLexicon: LexiconDoc = {
50
- lexicon: 1,
51
- id: 'io.example.writeJsonFile',
52
- defs: {
53
- main: {
54
- type: 'procedure',
55
- description: 'Write a JSON file',
56
- parameters: {
57
- type: 'params',
58
- properties: { fileName: { type: 'string' } },
59
- },
60
- input: {
61
- encoding: 'application/json',
62
- },
63
- },
49
+ ### With a custom fetch handler
50
+
51
+ ```typescript
52
+ import { XrpcClient } from '@atproto/xrpc'
53
+
54
+ const session = {
55
+ serviceUrl: 'https://ping.example.com',
56
+ token: '<my-token>',
57
+ async refreshToken() {
58
+ const { token } = await fetch('https://auth.example.com/refresh', {
59
+ method: 'POST',
60
+ headers: { Authorization: `Bearer ${this.token}` },
61
+ }).then((res) => res.json())
62
+
63
+ this.token = token
64
+
65
+ return token
64
66
  },
65
67
  }
66
- xrpc.addLexicon(writeJsonLexicon)
67
68
 
68
- const res3 = await xrpc.service('https://example.com').call(
69
- 'io.example.writeJsonFile',
70
- { fileName: 'foo.json' }, // query parameters
71
- { hello: 'world', thisIs: 'the file to write' }, // input body
72
- )
69
+ const sessionBasedFetch: FetchHandler = async (
70
+ url: string,
71
+ init: RequestInit,
72
+ ) => {
73
+ const headers = new Headers(init.headers)
74
+
75
+ headers.set('Authorization', `Bearer ${session.token}`)
76
+
77
+ const response = await fetch(new URL(url, session.serviceUrl), {
78
+ ...init,
79
+ headers,
80
+ })
81
+
82
+ if (response.status === 401) {
83
+ // Refresh token, then try again.
84
+ const newToken = await session.refreshToken()
85
+ headers.set('Authorization', `Bearer ${newToken}`)
86
+ return fetch(new URL(url, session.serviceUrl), { ...init, headers })
87
+ }
88
+
89
+ return response
90
+ }
91
+
92
+ const xrpc = new XrpcClient(sessionBasedFetch, [
93
+ // Any number of lexicon here
94
+ pingLexicon,
95
+ ])
96
+
97
+ //
73
98
  ```
74
99
 
75
100
  ## License
package/dist/client.d.ts CHANGED
@@ -1,21 +1,26 @@
1
1
  import { LexiconDoc, Lexicons } from '@atproto/lexicon';
2
- import { FetchHandler, FetchHandlerResponse, Headers, CallOptions, QueryParams, XRPCResponse } from './types';
2
+ import { CallOptions, QueryParams } from './types';
3
+ import { XrpcClient } from './xrpc-client';
4
+ /** @deprecated Use {@link XrpcClient} instead */
3
5
  export declare class Client {
4
- fetch: FetchHandler;
6
+ /** @deprecated */
7
+ get fetch(): never;
8
+ /** @deprecated */
9
+ set fetch(_: never);
5
10
  lex: Lexicons;
6
- call(serviceUri: string | URL, methodNsid: string, params?: QueryParams, data?: unknown, opts?: CallOptions): Promise<XRPCResponse>;
11
+ call(serviceUri: string | URL, methodNsid: string, params?: QueryParams, data?: BodyInit | null, opts?: CallOptions): Promise<import("./types").XRPCResponse>;
7
12
  service(serviceUri: string | URL): ServiceClient;
8
13
  addLexicon(doc: LexiconDoc): void;
9
14
  addLexicons(docs: LexiconDoc[]): void;
10
15
  removeLexicon(uri: string): void;
11
16
  }
12
- export declare class ServiceClient {
17
+ /** @deprecated Use {@link XrpcClient} instead */
18
+ export declare class ServiceClient extends XrpcClient {
13
19
  baseClient: Client;
14
20
  uri: URL;
15
21
  headers: Record<string, string>;
16
22
  constructor(baseClient: Client, serviceUri: string | URL);
17
23
  setHeader(key: string, value: string): void;
18
24
  unsetHeader(key: string): void;
19
- call(methodNsid: string, params?: QueryParams, data?: unknown, opts?: CallOptions): Promise<XRPCResponse>;
20
25
  }
21
- export declare function defaultFetchHandler(httpUri: string, httpMethod: string, httpHeaders: Headers, httpReqBody: unknown): Promise<FetchHandlerResponse>;
26
+ //# sourceMappingURL=client.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAA;AACvD,OAAO,EAAE,WAAW,EAAE,WAAW,EAAE,MAAM,SAAS,CAAA;AAClD,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAA;AAG1C,iDAAiD;AACjD,qBAAa,MAAM;IACjB,kBAAkB;IAClB,IAAI,KAAK,IAAI,KAAK,CAIjB;IAED,kBAAkB;IAClB,IAAI,KAAK,CAAC,CAAC,EAAE,KAAK,EAIjB;IAED,GAAG,WAAiB;IAKd,IAAI,CACR,UAAU,EAAE,MAAM,GAAG,GAAG,EACxB,UAAU,EAAE,MAAM,EAClB,MAAM,CAAC,EAAE,WAAW,EACpB,IAAI,CAAC,EAAE,QAAQ,GAAG,IAAI,EACtB,IAAI,CAAC,EAAE,WAAW;IAKpB,OAAO,CAAC,UAAU,EAAE,MAAM,GAAG,GAAG;IAOhC,UAAU,CAAC,GAAG,EAAE,UAAU;IAI1B,WAAW,CAAC,IAAI,EAAE,UAAU,EAAE;IAM9B,aAAa,CAAC,GAAG,EAAE,MAAM;CAG1B;AAED,iDAAiD;AACjD,qBAAa,aAAc,SAAQ,UAAU;IAKlC,UAAU,EAAE,MAAM;IAJ3B,GAAG,EAAE,GAAG,CAAA;IACR,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAK;gBAG3B,UAAU,EAAE,MAAM,EACzB,UAAU,EAAE,MAAM,GAAG,GAAG;IAS1B,SAAS,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,IAAI;IAI3C,WAAW,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI;CAG/B"}
package/dist/client.js ADDED
@@ -0,0 +1,83 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ServiceClient = exports.Client = void 0;
4
+ const lexicon_1 = require("@atproto/lexicon");
5
+ const xrpc_client_1 = require("./xrpc-client");
6
+ const util_1 = require("./util");
7
+ /** @deprecated Use {@link XrpcClient} instead */
8
+ class Client {
9
+ constructor() {
10
+ Object.defineProperty(this, "lex", {
11
+ enumerable: true,
12
+ configurable: true,
13
+ writable: true,
14
+ value: new lexicon_1.Lexicons()
15
+ });
16
+ }
17
+ /** @deprecated */
18
+ get fetch() {
19
+ throw new Error('Client.fetch is no longer supported. Use an XrpcClient instead.');
20
+ }
21
+ /** @deprecated */
22
+ set fetch(_) {
23
+ throw new Error('Client.fetch is no longer supported. Use an XrpcClient instead.');
24
+ }
25
+ // method calls
26
+ //
27
+ async call(serviceUri, methodNsid, params, data, opts) {
28
+ return this.service(serviceUri).call(methodNsid, params, data, opts);
29
+ }
30
+ service(serviceUri) {
31
+ return new ServiceClient(this, serviceUri);
32
+ }
33
+ // schemas
34
+ // =
35
+ addLexicon(doc) {
36
+ this.lex.add(doc);
37
+ }
38
+ addLexicons(docs) {
39
+ for (const doc of docs) {
40
+ this.addLexicon(doc);
41
+ }
42
+ }
43
+ removeLexicon(uri) {
44
+ this.lex.remove(uri);
45
+ }
46
+ }
47
+ exports.Client = Client;
48
+ /** @deprecated Use {@link XrpcClient} instead */
49
+ class ServiceClient extends xrpc_client_1.XrpcClient {
50
+ constructor(baseClient, serviceUri) {
51
+ super(async (input, init) => {
52
+ const headers = (0, util_1.combineHeaders)(init.headers, Object.entries(this.headers));
53
+ return fetch(new URL(input, this.uri), { ...init, headers });
54
+ }, baseClient.lex);
55
+ Object.defineProperty(this, "baseClient", {
56
+ enumerable: true,
57
+ configurable: true,
58
+ writable: true,
59
+ value: baseClient
60
+ });
61
+ Object.defineProperty(this, "uri", {
62
+ enumerable: true,
63
+ configurable: true,
64
+ writable: true,
65
+ value: void 0
66
+ });
67
+ Object.defineProperty(this, "headers", {
68
+ enumerable: true,
69
+ configurable: true,
70
+ writable: true,
71
+ value: {}
72
+ });
73
+ this.uri = typeof serviceUri === 'string' ? new URL(serviceUri) : serviceUri;
74
+ }
75
+ setHeader(key, value) {
76
+ this.headers[key] = value;
77
+ }
78
+ unsetHeader(key) {
79
+ delete this.headers[key];
80
+ }
81
+ }
82
+ exports.ServiceClient = ServiceClient;
83
+ //# sourceMappingURL=client.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"client.js","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":";;;AAAA,8CAAuD;AAEvD,+CAA0C;AAC1C,iCAAuC;AAEvC,iDAAiD;AACjD,MAAa,MAAM;IAAnB;QAeE;;;;mBAAM,IAAI,kBAAQ,EAAE;WAAA;IAmCtB,CAAC;IAjDC,kBAAkB;IAClB,IAAI,KAAK;QACP,MAAM,IAAI,KAAK,CACb,iEAAiE,CAClE,CAAA;IACH,CAAC;IAED,kBAAkB;IAClB,IAAI,KAAK,CAAC,CAAQ;QAChB,MAAM,IAAI,KAAK,CACb,iEAAiE,CAClE,CAAA;IACH,CAAC;IAID,eAAe;IACf,EAAE;IAEF,KAAK,CAAC,IAAI,CACR,UAAwB,EACxB,UAAkB,EAClB,MAAoB,EACpB,IAAsB,EACtB,IAAkB;QAElB,OAAO,IAAI,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC,IAAI,CAAC,UAAU,EAAE,MAAM,EAAE,IAAI,EAAE,IAAI,CAAC,CAAA;IACtE,CAAC;IAED,OAAO,CAAC,UAAwB;QAC9B,OAAO,IAAI,aAAa,CAAC,IAAI,EAAE,UAAU,CAAC,CAAA;IAC5C,CAAC;IAED,UAAU;IACV,IAAI;IAEJ,UAAU,CAAC,GAAe;QACxB,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,CAAA;IACnB,CAAC;IAED,WAAW,CAAC,IAAkB;QAC5B,KAAK,MAAM,GAAG,IAAI,IAAI,EAAE,CAAC;YACvB,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,CAAA;QACtB,CAAC;IACH,CAAC;IAED,aAAa,CAAC,GAAW;QACvB,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAA;IACtB,CAAC;CACF;AAlDD,wBAkDC;AAED,iDAAiD;AACjD,MAAa,aAAc,SAAQ,wBAAU;IAI3C,YACS,UAAkB,EACzB,UAAwB;QAExB,KAAK,CAAC,KAAK,EAAE,KAAK,EAAE,IAAI,EAAE,EAAE;YAC1B,MAAM,OAAO,GAAG,IAAA,qBAAc,EAAC,IAAI,CAAC,OAAO,EAAE,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,CAAA;YAC1E,OAAO,KAAK,CAAC,IAAI,GAAG,CAAC,KAAK,EAAE,IAAI,CAAC,GAAG,CAAC,EAAE,EAAE,GAAG,IAAI,EAAE,OAAO,EAAE,CAAC,CAAA;QAC9D,CAAC,EAAE,UAAU,CAAC,GAAG,CAAC,CAAA;QANlB;;;;mBAAO,UAAU;WAAQ;QAJ3B;;;;;WAAQ;QACR;;;;mBAAkC,EAAE;WAAA;QAUlC,IAAI,CAAC,GAAG,GAAG,OAAO,UAAU,KAAK,QAAQ,CAAC,CAAC,CAAC,IAAI,GAAG,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,UAAU,CAAA;IAC9E,CAAC;IAED,SAAS,CAAC,GAAW,EAAE,KAAa;QAClC,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,KAAK,CAAA;IAC3B,CAAC;IAED,WAAW,CAAC,GAAW;QACrB,OAAO,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,CAAA;IAC1B,CAAC;CACF;AAtBD,sCAsBC"}
@@ -0,0 +1,33 @@
1
+ import { Gettable } from './types';
2
+ export type FetchHandler = (this: void,
3
+ /**
4
+ * The URL (pathname + query parameters) to make the request to, without the
5
+ * origin. The origin (protocol, hostname, and port) must be added by this
6
+ * {@link FetchHandler}, typically based on authentication or other factors.
7
+ */
8
+ url: string, init: RequestInit) => Promise<Response>;
9
+ export type FetchHandlerOptions = BuildFetchHandlerOptions | string | URL;
10
+ export type BuildFetchHandlerOptions = {
11
+ /**
12
+ * The service URL to make requests to. This can be a string, URL, or a
13
+ * function that returns a string or URL. This is useful for dynamic URLs,
14
+ * such as a service URL that changes based on authentication.
15
+ */
16
+ service: Gettable<string | URL>;
17
+ /**
18
+ * Headers to be added to every request. If a function is provided, it will be
19
+ * called on each request to get the headers. This is useful for dynamic
20
+ * headers, such as authentication tokens that may expire.
21
+ */
22
+ headers?: {
23
+ [_ in string]?: Gettable<null | string>;
24
+ };
25
+ /**
26
+ * Bring your own fetch implementation. Typically useful for testing, logging,
27
+ * mocking, or adding retries, session management, signatures, proof of
28
+ * possession (DPoP), etc. Defaults to the global `fetch` function.
29
+ */
30
+ fetch?: typeof globalThis.fetch;
31
+ };
32
+ export declare function buildFetchHandler(options: FetchHandler | FetchHandlerOptions): FetchHandler;
33
+ //# sourceMappingURL=fetch-handler.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"fetch-handler.d.ts","sourceRoot":"","sources":["../src/fetch-handler.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAA;AAGlC,MAAM,MAAM,YAAY,GAAG,CACzB,IAAI,EAAE,IAAI;AACV;;;;GAIG;AACH,GAAG,EAAE,MAAM,EACX,IAAI,EAAE,WAAW,KACd,OAAO,CAAC,QAAQ,CAAC,CAAA;AAEtB,MAAM,MAAM,mBAAmB,GAAG,wBAAwB,GAAG,MAAM,GAAG,GAAG,CAAA;AAEzE,MAAM,MAAM,wBAAwB,GAAG;IACrC;;;;OAIG;IACH,OAAO,EAAE,QAAQ,CAAC,MAAM,GAAG,GAAG,CAAC,CAAA;IAE/B;;;;OAIG;IACH,OAAO,CAAC,EAAE;SACP,CAAC,IAAI,MAAM,CAAC,CAAC,EAAE,QAAQ,CAAC,IAAI,GAAG,MAAM,CAAC;KACxC,CAAA;IAED;;;;OAIG;IACH,KAAK,CAAC,EAAE,OAAO,UAAU,CAAC,KAAK,CAAA;CAChC,CAAA;AAED,wBAAgB,iBAAiB,CAC/B,OAAO,EAAE,YAAY,GAAG,mBAAmB,GAC1C,YAAY,CA6Bd"}
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.buildFetchHandler = void 0;
4
+ const util_1 = require("./util");
5
+ function buildFetchHandler(options) {
6
+ // Already a fetch handler (allowed for convenience)
7
+ if (typeof options === 'function')
8
+ return options;
9
+ const { service, headers: defaultHeaders = undefined, fetch = globalThis.fetch, } = typeof options === 'string' || options instanceof URL
10
+ ? { service: options }
11
+ : options;
12
+ if (typeof fetch !== 'function') {
13
+ throw new TypeError('XrpcDispatcher requires fetch() to be available in your environment.');
14
+ }
15
+ const defaultHeadersEntries = defaultHeaders != null ? Object.entries(defaultHeaders) : undefined;
16
+ return async function (url, init) {
17
+ const base = typeof service === 'function' ? service() : service;
18
+ const fullUrl = new URL(url, base);
19
+ const headers = (0, util_1.combineHeaders)(init.headers, defaultHeadersEntries);
20
+ return fetch(fullUrl, { ...init, headers });
21
+ };
22
+ }
23
+ exports.buildFetchHandler = buildFetchHandler;
24
+ //# sourceMappingURL=fetch-handler.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"fetch-handler.js","sourceRoot":"","sources":["../src/fetch-handler.ts"],"names":[],"mappings":";;;AACA,iCAAuC;AAwCvC,SAAgB,iBAAiB,CAC/B,OAA2C;IAE3C,oDAAoD;IACpD,IAAI,OAAO,OAAO,KAAK,UAAU;QAAE,OAAO,OAAO,CAAA;IAEjD,MAAM,EACJ,OAAO,EACP,OAAO,EAAE,cAAc,GAAG,SAAS,EACnC,KAAK,GAAG,UAAU,CAAC,KAAK,GACzB,GAAG,OAAO,OAAO,KAAK,QAAQ,IAAI,OAAO,YAAY,GAAG;QACvD,CAAC,CAAC,EAAE,OAAO,EAAE,OAAO,EAAE;QACtB,CAAC,CAAC,OAAO,CAAA;IAEX,IAAI,OAAO,KAAK,KAAK,UAAU,EAAE,CAAC;QAChC,MAAM,IAAI,SAAS,CACjB,sEAAsE,CACvE,CAAA;IACH,CAAC;IAED,MAAM,qBAAqB,GACzB,cAAc,IAAI,IAAI,CAAC,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC,SAAS,CAAA;IAErE,OAAO,KAAK,WAAW,GAAG,EAAE,IAAI;QAC9B,MAAM,IAAI,GAAG,OAAO,OAAO,KAAK,UAAU,CAAC,CAAC,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC,OAAO,CAAA;QAChE,MAAM,OAAO,GAAG,IAAI,GAAG,CAAC,GAAG,EAAE,IAAI,CAAC,CAAA;QAElC,MAAM,OAAO,GAAG,IAAA,qBAAc,EAAC,IAAI,CAAC,OAAO,EAAE,qBAAqB,CAAC,CAAA;QAEnE,OAAO,KAAK,CAAC,OAAO,EAAE,EAAE,GAAG,IAAI,EAAE,OAAO,EAAE,CAAC,CAAA;IAC7C,CAAC,CAAA;AACH,CAAC;AA/BD,8CA+BC"}
package/dist/index.d.ts CHANGED
@@ -1,5 +1,10 @@
1
- export * from './types';
2
1
  export * from './client';
2
+ export * from './fetch-handler';
3
+ export * from './types';
4
+ export * from './util';
5
+ export * from './xrpc-client';
3
6
  import { Client } from './client';
7
+ /** @deprecated create a local {@link XrpcClient} instance instead */
4
8
  declare const defaultInst: Client;
5
9
  export default defaultInst;
10
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAA;AACxB,cAAc,iBAAiB,CAAA;AAC/B,cAAc,SAAS,CAAA;AACvB,cAAc,QAAQ,CAAA;AACtB,cAAc,eAAe,CAAA;AAE7B,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAA;AACjC,qEAAqE;AACrE,QAAA,MAAM,WAAW,QAAe,CAAA;AAChC,eAAe,WAAW,CAAA"}