@atproto/xrpc 0.7.6 → 0.8.0-next.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 +116 -90
- package/LICENSE.txt +1 -1
- package/dist/client.d.ts +3 -3
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +8 -29
- package/dist/client.js.map +1 -1
- package/dist/fetch-handler.d.ts +1 -1
- package/dist/fetch-handler.d.ts.map +1 -1
- package/dist/fetch-handler.js +3 -6
- package/dist/fetch-handler.js.map +1 -1
- package/dist/index.d.ts +6 -6
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +8 -26
- package/dist/index.js.map +1 -1
- package/dist/types.js +24 -84
- package/dist/types.js.map +1 -1
- package/dist/util.d.ts +1 -1
- package/dist/util.d.ts.map +1 -1
- package/dist/util.js +22 -35
- package/dist/util.js.map +1 -1
- package/dist/xrpc-client.d.ts +2 -2
- package/dist/xrpc-client.d.ts.map +1 -1
- package/dist/xrpc-client.js +22 -43
- package/dist/xrpc-client.js.map +1 -1
- package/package.json +13 -5
- package/src/client.ts +3 -3
- package/src/fetch-handler.ts +2 -2
- package/src/index.ts +6 -8
- package/src/util.ts +1 -1
- package/src/xrpc-client.ts +3 -3
- package/tsconfig.build.tsbuildinfo +1 -1
package/dist/xrpc-client.js
CHANGED
|
@@ -1,35 +1,15 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
const types_1 = require("./types");
|
|
7
|
-
const util_1 = require("./util");
|
|
8
|
-
class XrpcClient {
|
|
1
|
+
import { Lexicons, ValidationError } from '@atproto/lexicon';
|
|
2
|
+
import { buildFetchHandler, } from './fetch-handler.js';
|
|
3
|
+
import { ResponseType, XRPCError, XRPCInvalidResponseError, XRPCResponse, httpResponseCodeToEnum, } from './types.js';
|
|
4
|
+
import { combineHeaders, constructMethodCallHeaders, constructMethodCallUrl, encodeMethodCallBody, getMethodSchemaHTTPMethod, httpResponseBodyParse, isErrorResponseBody, } from './util.js';
|
|
5
|
+
export class XrpcClient {
|
|
9
6
|
constructor(fetchHandlerOpts,
|
|
10
7
|
// "Lexicons" is redundant here (because that class implements
|
|
11
8
|
// "Iterable<LexiconDoc>") but we keep it for explicitness:
|
|
12
9
|
lex) {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
writable: true,
|
|
17
|
-
value: void 0
|
|
18
|
-
});
|
|
19
|
-
Object.defineProperty(this, "headers", {
|
|
20
|
-
enumerable: true,
|
|
21
|
-
configurable: true,
|
|
22
|
-
writable: true,
|
|
23
|
-
value: new Map()
|
|
24
|
-
});
|
|
25
|
-
Object.defineProperty(this, "lex", {
|
|
26
|
-
enumerable: true,
|
|
27
|
-
configurable: true,
|
|
28
|
-
writable: true,
|
|
29
|
-
value: void 0
|
|
30
|
-
});
|
|
31
|
-
this.fetchHandler = (0, fetch_handler_1.buildFetchHandler)(fetchHandlerOpts);
|
|
32
|
-
this.lex = lex instanceof lexicon_1.Lexicons ? lex : new lexicon_1.Lexicons(lex);
|
|
10
|
+
this.headers = new Map();
|
|
11
|
+
this.fetchHandler = buildFetchHandler(fetchHandlerOpts);
|
|
12
|
+
this.lex = lex instanceof Lexicons ? lex : new Lexicons(lex);
|
|
33
13
|
}
|
|
34
14
|
setHeader(key, value) {
|
|
35
15
|
this.headers.set(key.toLowerCase(), value);
|
|
@@ -50,15 +30,15 @@ class XrpcClient {
|
|
|
50
30
|
// if (data !== undefined) {
|
|
51
31
|
// this.lex.assertValidXrpcInput(methodNsid, data)
|
|
52
32
|
// }
|
|
53
|
-
const reqUrl =
|
|
54
|
-
const reqMethod =
|
|
55
|
-
const reqHeaders =
|
|
56
|
-
const reqBody =
|
|
33
|
+
const reqUrl = constructMethodCallUrl(methodNsid, def, params);
|
|
34
|
+
const reqMethod = getMethodSchemaHTTPMethod(def);
|
|
35
|
+
const reqHeaders = constructMethodCallHeaders(def, data, opts);
|
|
36
|
+
const reqBody = encodeMethodCallBody(reqHeaders, data);
|
|
57
37
|
// The duplex field is required for streaming bodies, but not yet reflected
|
|
58
38
|
// anywhere in docs or types. See whatwg/fetch#1438, nodejs/node#46221.
|
|
59
39
|
const init = {
|
|
60
40
|
method: reqMethod,
|
|
61
|
-
headers:
|
|
41
|
+
headers: combineHeaders(reqHeaders, this.headers),
|
|
62
42
|
body: reqBody,
|
|
63
43
|
duplex: 'half',
|
|
64
44
|
redirect: 'follow',
|
|
@@ -69,27 +49,26 @@ class XrpcClient {
|
|
|
69
49
|
const resStatus = response.status;
|
|
70
50
|
const resHeaders = Object.fromEntries(response.headers.entries());
|
|
71
51
|
const resBodyBytes = await response.arrayBuffer();
|
|
72
|
-
const resBody =
|
|
73
|
-
const resCode =
|
|
74
|
-
if (resCode !==
|
|
75
|
-
const { error = undefined, message = undefined } = resBody &&
|
|
76
|
-
throw new
|
|
52
|
+
const resBody = httpResponseBodyParse(response.headers.get('content-type'), resBodyBytes);
|
|
53
|
+
const resCode = httpResponseCodeToEnum(resStatus);
|
|
54
|
+
if (resCode !== ResponseType.Success) {
|
|
55
|
+
const { error = undefined, message = undefined } = resBody && isErrorResponseBody(resBody) ? resBody : {};
|
|
56
|
+
throw new XRPCError(resCode, error, message, resHeaders);
|
|
77
57
|
}
|
|
78
58
|
try {
|
|
79
59
|
this.lex.assertValidXrpcOutput(methodNsid, resBody);
|
|
80
60
|
}
|
|
81
61
|
catch (e) {
|
|
82
|
-
if (e instanceof
|
|
83
|
-
throw new
|
|
62
|
+
if (e instanceof ValidationError) {
|
|
63
|
+
throw new XRPCInvalidResponseError(methodNsid, e, resBody);
|
|
84
64
|
}
|
|
85
65
|
throw e;
|
|
86
66
|
}
|
|
87
|
-
return new
|
|
67
|
+
return new XRPCResponse(resBody, resHeaders);
|
|
88
68
|
}
|
|
89
69
|
catch (err) {
|
|
90
|
-
throw
|
|
70
|
+
throw XRPCError.from(err);
|
|
91
71
|
}
|
|
92
72
|
}
|
|
93
73
|
}
|
|
94
|
-
exports.XrpcClient = XrpcClient;
|
|
95
74
|
//# sourceMappingURL=xrpc-client.js.map
|
package/dist/xrpc-client.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"xrpc-client.js","sourceRoot":"","sources":["../src/xrpc-client.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"xrpc-client.js","sourceRoot":"","sources":["../src/xrpc-client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAc,QAAQ,EAAE,eAAe,EAAE,MAAM,kBAAkB,CAAA;AACxE,OAAO,EAIL,iBAAiB,GAClB,MAAM,oBAAoB,CAAA;AAC3B,OAAO,EAIL,YAAY,EACZ,SAAS,EACT,wBAAwB,EACxB,YAAY,EACZ,sBAAsB,GACvB,MAAM,YAAY,CAAA;AACnB,OAAO,EACL,cAAc,EACd,0BAA0B,EAC1B,sBAAsB,EACtB,oBAAoB,EACpB,yBAAyB,EACzB,qBAAqB,EACrB,mBAAmB,GACpB,MAAM,WAAW,CAAA;AAElB,MAAM,OAAO,UAAU;IAKrB,YACE,gBAAyE;IACzE,8DAA8D;IAC9D,2DAA2D;IAC3D,GAAoC;QAP7B,YAAO,GAAG,IAAI,GAAG,EAAmC,CAAA;QAS3D,IAAI,CAAC,YAAY,GAAG,iBAAiB,CAAC,gBAAgB,CAAC,CAAA;QAEvD,IAAI,CAAC,GAAG,GAAG,GAAG,YAAY,QAAQ,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,QAAQ,CAAC,GAAG,CAAC,CAAA;IAC9D,CAAC;IAED,SAAS,CAAC,GAAW,EAAE,KAA8B;QACnD,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,WAAW,EAAE,EAAE,KAAK,CAAC,CAAA;IAC5C,CAAC;IAED,WAAW,CAAC,GAAW;QACrB,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,GAAG,CAAC,WAAW,EAAE,CAAC,CAAA;IACxC,CAAC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AM,UAAU,GAAG,MAAM,CAAC,WAAW,CAAC,QAAQ,CAAC,OAAO,CAAC,OAAO,EAAE,CAAC,CAAA;YACjE,MAAM,YAAY,GAAG,MAAM,QAAQ,CAAC,WAAW,EAAE,CAAA;YACjD,MAAM,OAAO,GAAG,qBAAqB,CACnC,QAAQ,CAAC,OAAO,CAAC,GAAG,CAAC,cAAc,CAAC,EACpC,YAAY,CACb,CAAA;YAED,MAAM,OAAO,GAAG,sBAAsB,CAAC,SAAS,CAAC,CAAA;YACjD,IAAI,OAAO,KAAK,YAAY,CAAC,OAAO,EAAE,CAAC;gBACrC,MAAM,EAAE,KAAK,GAAG,SAAS,EAAE,OAAO,GAAG,SAAS,EAAE,GAC9C,OAAO,IAAI,mBAAmB,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,CAAA;gBACxD,MAAM,IAAI,SAAS,CAAC,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE,UAAU,CAAC,CAAA;YAC1D,CAAC;YAED,IAAI,CAAC;gBACH,IAAI,CAAC,GAAG,CAAC,qBAAqB,CAAC,UAAU,EAAE,OAAO,CAAC,CAAA;YACrD,CAAC;YAAC,OAAO,CAAU,EAAE,CAAC;gBACpB,IAAI,CAAC,YAAY,eAAe,EAAE,CAAC;oBACjC,MAAM,IAAI,wBAAwB,CAAC,UAAU,EAAE,CAAC,EAAE,OAAO,CAAC,CAAA;gBAC5D,CAAC;gBAED,MAAM,CAAC,CAAA;YACT,CAAC;YAED,OAAO,IAAI,YAAY,CAAC,OAAO,EAAE,UAAU,CAAC,CAAA;QAC9C,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACb,MAAM,SAAS,CAAC,IAAI,CAAC,GAAG,CAAC,CAAA;QAC3B,CAAC;IACH,CAAC;CACF","sourcesContent":["import { LexiconDoc, Lexicons, ValidationError } from '@atproto/lexicon'\nimport {\n FetchHandler,\n FetchHandlerObject,\n FetchHandlerOptions,\n buildFetchHandler,\n} from './fetch-handler.js'\nimport {\n CallOptions,\n Gettable,\n QueryParams,\n ResponseType,\n XRPCError,\n XRPCInvalidResponseError,\n XRPCResponse,\n httpResponseCodeToEnum,\n} from './types.js'\nimport {\n combineHeaders,\n constructMethodCallHeaders,\n constructMethodCallUrl,\n encodeMethodCallBody,\n getMethodSchemaHTTPMethod,\n httpResponseBodyParse,\n isErrorResponseBody,\n} from './util.js'\n\nexport class XrpcClient {\n readonly fetchHandler: FetchHandler\n readonly headers = new Map<string, Gettable<null | string>>()\n readonly lex: Lexicons\n\n constructor(\n fetchHandlerOpts: FetchHandler | FetchHandlerObject | FetchHandlerOptions,\n // \"Lexicons\" is redundant here (because that class implements\n // \"Iterable<LexiconDoc>\") but we keep it for explicitness:\n lex: Lexicons | Iterable<LexiconDoc>,\n ) {\n this.fetchHandler = buildFetchHandler(fetchHandlerOpts)\n\n this.lex = lex instanceof Lexicons ? lex : new Lexicons(lex)\n }\n\n setHeader(key: string, value: Gettable<null | string>): void {\n this.headers.set(key.toLowerCase(), value)\n }\n\n unsetHeader(key: string): void {\n this.headers.delete(key.toLowerCase())\n }\n\n clearHeaders(): void {\n this.headers.clear()\n }\n\n async call(\n methodNsid: string,\n params?: QueryParams,\n data?: unknown,\n opts?: CallOptions,\n ): Promise<XRPCResponse> {\n const def = this.lex.getDefOrThrow(methodNsid)\n if (!def || (def.type !== 'query' && def.type !== 'procedure')) {\n throw new TypeError(\n `Invalid lexicon: ${methodNsid}. Must be a query or procedure.`,\n )\n }\n\n // @TODO: should we validate the params and data here?\n // this.lex.assertValidXrpcParams(methodNsid, params)\n // if (data !== undefined) {\n // this.lex.assertValidXrpcInput(methodNsid, data)\n // }\n\n const reqUrl = constructMethodCallUrl(methodNsid, def, params)\n const reqMethod = getMethodSchemaHTTPMethod(def)\n const reqHeaders = constructMethodCallHeaders(def, data, opts)\n const reqBody = encodeMethodCallBody(reqHeaders, data)\n\n // The duplex field is required for streaming bodies, but not yet reflected\n // anywhere in docs or types. See whatwg/fetch#1438, nodejs/node#46221.\n const init: RequestInit & { duplex: 'half' } = {\n method: reqMethod,\n headers: combineHeaders(reqHeaders, this.headers),\n body: reqBody,\n duplex: 'half',\n redirect: 'follow',\n signal: opts?.signal,\n }\n\n try {\n const response = await this.fetchHandler.call(undefined, reqUrl, init)\n\n const resStatus = response.status\n const resHeaders = Object.fromEntries(response.headers.entries())\n const resBodyBytes = await response.arrayBuffer()\n const resBody = httpResponseBodyParse(\n response.headers.get('content-type'),\n resBodyBytes,\n )\n\n const resCode = httpResponseCodeToEnum(resStatus)\n if (resCode !== ResponseType.Success) {\n const { error = undefined, message = undefined } =\n resBody && isErrorResponseBody(resBody) ? resBody : {}\n throw new XRPCError(resCode, error, message, resHeaders)\n }\n\n try {\n this.lex.assertValidXrpcOutput(methodNsid, resBody)\n } catch (e: unknown) {\n if (e instanceof ValidationError) {\n throw new XRPCInvalidResponseError(methodNsid, e, resBody)\n }\n\n throw e\n }\n\n return new XRPCResponse(resBody, resHeaders)\n } catch (err) {\n throw XRPCError.from(err)\n }\n }\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@atproto/xrpc",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.0-next.0",
|
|
4
|
+
"engines": {
|
|
5
|
+
"node": ">=22"
|
|
6
|
+
},
|
|
4
7
|
"license": "MIT",
|
|
5
8
|
"description": "atproto HTTP API (XRPC) client library",
|
|
6
9
|
"keywords": [
|
|
@@ -13,14 +16,19 @@
|
|
|
13
16
|
"url": "https://github.com/bluesky-social/atproto",
|
|
14
17
|
"directory": "packages/xrpc"
|
|
15
18
|
},
|
|
16
|
-
"main": "dist/index.js",
|
|
17
|
-
"types": "dist/index.d.ts",
|
|
18
19
|
"dependencies": {
|
|
19
20
|
"zod": "^3.23.8",
|
|
20
|
-
"@atproto/lexicon": "^0.
|
|
21
|
+
"@atproto/lexicon": "^0.7.0-next.0"
|
|
21
22
|
},
|
|
22
23
|
"devDependencies": {
|
|
23
|
-
"typescript": "^
|
|
24
|
+
"typescript": "^6.0.3"
|
|
25
|
+
},
|
|
26
|
+
"type": "module",
|
|
27
|
+
"exports": {
|
|
28
|
+
".": {
|
|
29
|
+
"types": "./dist/index.d.ts",
|
|
30
|
+
"default": "./dist/index.js"
|
|
31
|
+
}
|
|
24
32
|
},
|
|
25
33
|
"scripts": {
|
|
26
34
|
"build": "tsc --build tsconfig.build.json"
|
package/src/client.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { LexiconDoc, Lexicons } from '@atproto/lexicon'
|
|
2
|
-
import { CallOptions, QueryParams } from './types'
|
|
3
|
-
import { combineHeaders } from './util'
|
|
4
|
-
import { XrpcClient } from './xrpc-client'
|
|
2
|
+
import { CallOptions, QueryParams } from './types.js'
|
|
3
|
+
import { combineHeaders } from './util.js'
|
|
4
|
+
import { XrpcClient } from './xrpc-client.js'
|
|
5
5
|
|
|
6
6
|
/** @deprecated Use {@link XrpcClient} instead */
|
|
7
7
|
export class Client {
|
package/src/fetch-handler.ts
CHANGED
package/src/index.ts
CHANGED
|
@@ -1,12 +1,10 @@
|
|
|
1
|
-
export * from './client'
|
|
2
|
-
export * from './fetch-handler'
|
|
3
|
-
export * from './types'
|
|
4
|
-
export * from './util'
|
|
5
|
-
export * from './xrpc-client'
|
|
1
|
+
export * from './client.js'
|
|
2
|
+
export * from './fetch-handler.js'
|
|
3
|
+
export * from './types.js'
|
|
4
|
+
export * from './util.js'
|
|
5
|
+
export * from './xrpc-client.js'
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
import { Client } from './client'
|
|
7
|
+
import { Client } from './client.js'
|
|
9
8
|
/** @deprecated create a local {@link XrpcClient} instance instead */
|
|
10
9
|
const defaultInst = new Client()
|
|
11
10
|
export default defaultInst
|
|
12
|
-
/* eslint-enable import/no-deprecated */
|
package/src/util.ts
CHANGED
package/src/xrpc-client.ts
CHANGED
|
@@ -4,7 +4,7 @@ import {
|
|
|
4
4
|
FetchHandlerObject,
|
|
5
5
|
FetchHandlerOptions,
|
|
6
6
|
buildFetchHandler,
|
|
7
|
-
} from './fetch-handler'
|
|
7
|
+
} from './fetch-handler.js'
|
|
8
8
|
import {
|
|
9
9
|
CallOptions,
|
|
10
10
|
Gettable,
|
|
@@ -14,7 +14,7 @@ import {
|
|
|
14
14
|
XRPCInvalidResponseError,
|
|
15
15
|
XRPCResponse,
|
|
16
16
|
httpResponseCodeToEnum,
|
|
17
|
-
} from './types'
|
|
17
|
+
} from './types.js'
|
|
18
18
|
import {
|
|
19
19
|
combineHeaders,
|
|
20
20
|
constructMethodCallHeaders,
|
|
@@ -23,7 +23,7 @@ import {
|
|
|
23
23
|
getMethodSchemaHTTPMethod,
|
|
24
24
|
httpResponseBodyParse,
|
|
25
25
|
isErrorResponseBody,
|
|
26
|
-
} from './util'
|
|
26
|
+
} from './util.js'
|
|
27
27
|
|
|
28
28
|
export class XrpcClient {
|
|
29
29
|
readonly fetchHandler: FetchHandler
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"root":["./src/client.ts","./src/fetch-handler.ts","./src/index.ts","./src/types.ts","./src/util.ts","./src/xrpc-client.ts"],"version":"
|
|
1
|
+
{"root":["./src/client.ts","./src/fetch-handler.ts","./src/index.ts","./src/types.ts","./src/util.ts","./src/xrpc-client.ts"],"version":"6.0.3"}
|