@atproto/lex-client 0.0.4 → 0.0.6
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 +42 -0
- package/dist/agent.d.ts +10 -9
- package/dist/agent.d.ts.map +1 -1
- package/dist/agent.js +3 -0
- package/dist/agent.js.map +1 -1
- package/dist/client.d.ts +51 -113
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +38 -42
- package/dist/client.js.map +1 -1
- package/dist/errors.d.ts +82 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +132 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/lexicons/com/atproto/repo/createRecord.defs.d.ts +7 -7
- package/dist/lexicons/com/atproto/repo/createRecord.defs.d.ts.map +1 -1
- package/dist/lexicons/com/atproto/repo/createRecord.defs.js +8 -12
- package/dist/lexicons/com/atproto/repo/createRecord.defs.js.map +1 -1
- package/dist/lexicons/com/atproto/repo/deleteRecord.defs.d.ts +7 -7
- package/dist/lexicons/com/atproto/repo/deleteRecord.defs.d.ts.map +1 -1
- package/dist/lexicons/com/atproto/repo/deleteRecord.defs.js +8 -12
- package/dist/lexicons/com/atproto/repo/deleteRecord.defs.js.map +1 -1
- package/dist/lexicons/com/atproto/repo/getRecord.defs.d.ts +5 -6
- package/dist/lexicons/com/atproto/repo/getRecord.defs.d.ts.map +1 -1
- package/dist/lexicons/com/atproto/repo/getRecord.defs.js +6 -10
- package/dist/lexicons/com/atproto/repo/getRecord.defs.js.map +1 -1
- package/dist/lexicons/com/atproto/repo/listRecords.defs.d.ts +5 -6
- package/dist/lexicons/com/atproto/repo/listRecords.defs.d.ts.map +1 -1
- package/dist/lexicons/com/atproto/repo/listRecords.defs.js +5 -8
- package/dist/lexicons/com/atproto/repo/listRecords.defs.js.map +1 -1
- package/dist/lexicons/com/atproto/repo/putRecord.defs.d.ts +7 -7
- package/dist/lexicons/com/atproto/repo/putRecord.defs.d.ts.map +1 -1
- package/dist/lexicons/com/atproto/repo/putRecord.defs.js +8 -12
- package/dist/lexicons/com/atproto/repo/putRecord.defs.js.map +1 -1
- package/dist/lexicons/com/atproto/repo/uploadBlob.defs.d.ts +7 -7
- package/dist/lexicons/com/atproto/repo/uploadBlob.defs.d.ts.map +1 -1
- package/dist/lexicons/com/atproto/repo/uploadBlob.defs.js +6 -9
- package/dist/lexicons/com/atproto/repo/uploadBlob.defs.js.map +1 -1
- package/dist/lexicons/com/atproto/sync/getBlob.d.ts +3 -0
- package/dist/lexicons/com/atproto/sync/getBlob.d.ts.map +1 -0
- package/dist/lexicons/com/atproto/sync/getBlob.defs.d.ts +25 -0
- package/dist/lexicons/com/atproto/sync/getBlob.defs.d.ts.map +1 -0
- package/dist/lexicons/com/atproto/sync/getBlob.defs.js +27 -0
- package/dist/lexicons/com/atproto/sync/getBlob.defs.js.map +1 -0
- package/dist/lexicons/com/atproto/sync/getBlob.js +10 -0
- package/dist/lexicons/com/atproto/sync/getBlob.js.map +1 -0
- package/dist/lexicons/com/atproto/sync.d.ts +2 -0
- package/dist/lexicons/com/atproto/sync.d.ts.map +1 -0
- package/dist/lexicons/com/atproto/sync.js +9 -0
- package/dist/lexicons/com/atproto/sync.js.map +1 -0
- package/dist/lexicons/com/atproto.d.ts +1 -0
- package/dist/lexicons/com/atproto.d.ts.map +1 -1
- package/dist/lexicons/com/atproto.js +2 -1
- package/dist/lexicons/com/atproto.js.map +1 -1
- package/dist/lexicons.d.ts +2 -0
- package/dist/lexicons.d.ts.map +1 -0
- package/dist/lexicons.js +6 -0
- package/dist/lexicons.js.map +1 -0
- package/dist/response.d.ts +25 -8
- package/dist/response.d.ts.map +1 -1
- package/dist/response.js +123 -10
- package/dist/response.js.map +1 -1
- package/dist/types.d.ts +18 -4
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +0 -4
- package/dist/types.js.map +1 -1
- package/dist/util.d.ts +14 -0
- package/dist/util.d.ts.map +1 -0
- package/dist/util.js +65 -0
- package/dist/util.js.map +1 -0
- package/dist/xrpc.d.ts +35 -32
- package/dist/xrpc.d.ts.map +1 -1
- package/dist/xrpc.js +116 -124
- package/dist/xrpc.js.map +1 -1
- package/package.json +10 -10
- package/src/agent.ts +18 -14
- package/src/client.ts +135 -114
- package/src/errors.ts +206 -0
- package/src/index.ts +1 -1
- package/src/lexicons/com/atproto/repo/createRecord.defs.ts +31 -36
- package/src/lexicons/com/atproto/repo/deleteRecord.defs.ts +27 -32
- package/src/lexicons/com/atproto/repo/getRecord.defs.ts +12 -17
- package/src/lexicons/com/atproto/repo/listRecords.defs.ts +13 -15
- package/src/lexicons/com/atproto/repo/putRecord.defs.ts +32 -37
- package/src/lexicons/com/atproto/repo/uploadBlob.defs.ts +13 -15
- package/src/lexicons/com/atproto/sync/getBlob.defs.ts +37 -0
- package/src/lexicons/com/atproto/sync/getBlob.ts +6 -0
- package/src/lexicons/com/atproto/sync.ts +5 -0
- package/src/lexicons/com/atproto.ts +1 -0
- package/src/lexicons.ts +1 -0
- package/src/response.ts +201 -15
- package/src/types.ts +26 -5
- package/src/util.ts +84 -0
- package/src/xrpc.ts +220 -232
- package/tsconfig.tests.json +4 -7
- package/dist/error.d.ts +0 -66
- package/dist/error.d.ts.map +0 -1
- package/dist/error.js +0 -100
- package/dist/error.js.map +0 -1
- package/src/error.ts +0 -145
package/dist/response.js
CHANGED
|
@@ -1,31 +1,144 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.LexRpcResponse = void 0;
|
|
4
|
+
const lex_json_1 = require("@atproto/lex-json");
|
|
5
|
+
const errors_js_1 = require("./errors.js");
|
|
4
6
|
/**
|
|
5
7
|
* Small container for XRPC response data.
|
|
6
8
|
*
|
|
7
|
-
* @implements {ResultSuccess<
|
|
9
|
+
* @implements {ResultSuccess<LexRpcResponse<M>>} for convenience in result handling contexts.
|
|
8
10
|
*/
|
|
9
|
-
class
|
|
11
|
+
class LexRpcResponse {
|
|
10
12
|
method;
|
|
11
13
|
status;
|
|
12
14
|
headers;
|
|
13
|
-
|
|
15
|
+
payload;
|
|
14
16
|
/** @see {@link ResultSuccess.success} */
|
|
15
17
|
success = true;
|
|
16
18
|
/** @see {@link ResultSuccess.value} */
|
|
17
19
|
get value() {
|
|
18
20
|
return this;
|
|
19
21
|
}
|
|
20
|
-
|
|
21
|
-
return this.method.output?.encoding;
|
|
22
|
-
}
|
|
23
|
-
constructor(method, status, headers, body) {
|
|
22
|
+
constructor(method, status, headers, payload) {
|
|
24
23
|
this.method = method;
|
|
25
24
|
this.status = status;
|
|
26
25
|
this.headers = headers;
|
|
27
|
-
this.
|
|
26
|
+
this.payload = payload;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Whether the response payload was parsed as {@link LexValue} (`true`) or is
|
|
30
|
+
* in binary form {@link Uint8Array} (`false`).
|
|
31
|
+
*/
|
|
32
|
+
get isParsed() {
|
|
33
|
+
return this.encoding === 'application/json' && shouldParse(this.method);
|
|
34
|
+
}
|
|
35
|
+
get encoding() {
|
|
36
|
+
return this.payload?.encoding;
|
|
37
|
+
}
|
|
38
|
+
get body() {
|
|
39
|
+
return this.payload?.body;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* @throws {LexRpcResponseError} in case of (valid) XRPC error responses. Use
|
|
43
|
+
* {@link LexRpcResponseError.matchesSchema} to narrow the error type based on
|
|
44
|
+
* the method's declared error schema.
|
|
45
|
+
* @throws {LexRpcUpstreamError} when the response is not a valid XRPC
|
|
46
|
+
* response, or if the response does not conform to the method's schema.
|
|
47
|
+
*/
|
|
48
|
+
static async fromFetchResponse(method, response, options) {
|
|
49
|
+
// @NOTE The body MUST either be read or canceled to avoid resource leaks.
|
|
50
|
+
// Since nothing should cause an exception before "readPayload" is
|
|
51
|
+
// called, we can safely not use a try/finally here.
|
|
52
|
+
// @NOTE redirect is set to 'follow', so we shouldn't get 3xx responses here
|
|
53
|
+
if (response.status < 200 || response.status >= 300) {
|
|
54
|
+
// Always parse json for error responses
|
|
55
|
+
const payload = await readPayload(response, { parse: true });
|
|
56
|
+
if (response.status >= 400 && (0, errors_js_1.isLexRpcErrorPayload)(payload)) {
|
|
57
|
+
throw new errors_js_1.LexRpcResponseError(method, response.status, response.headers, payload);
|
|
58
|
+
}
|
|
59
|
+
if (response.status >= 500) {
|
|
60
|
+
throw new errors_js_1.LexRpcUpstreamError('UpstreamFailure', `Upstream server encountered an error`, response, payload);
|
|
61
|
+
}
|
|
62
|
+
throw new errors_js_1.LexRpcUpstreamError('InvalidResponse', response.status >= 400
|
|
63
|
+
? `Upstream server returned an invalid response payload`
|
|
64
|
+
: `Upstream server returned an invalid status code`, response, payload);
|
|
65
|
+
}
|
|
66
|
+
// Only parse json if the schema expects it
|
|
67
|
+
const payload = await readPayload(response, {
|
|
68
|
+
parse: shouldParse(method),
|
|
69
|
+
});
|
|
70
|
+
// Response is successful (2xx). Validate payload (data and encoding) against schema.
|
|
71
|
+
if (method.output.encoding == null) {
|
|
72
|
+
// Schema expects no payload
|
|
73
|
+
if (payload) {
|
|
74
|
+
throw new errors_js_1.LexRpcUpstreamError('InvalidResponse', `Expected response with no body, got ${payload.encoding}`, response, payload);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
else {
|
|
78
|
+
// Schema expects a payload
|
|
79
|
+
if (!payload || !method.output.matchesEncoding(payload.encoding)) {
|
|
80
|
+
throw new errors_js_1.LexRpcUpstreamError('InvalidResponse', payload
|
|
81
|
+
? `Expected ${method.output.encoding} response, got ${payload.encoding}`
|
|
82
|
+
: `Expected non-empty response with content-type ${method.output.encoding}`, response, payload);
|
|
83
|
+
}
|
|
84
|
+
// Assert valid response body.
|
|
85
|
+
if (method.output.schema && options?.validateResponse !== false) {
|
|
86
|
+
const result = method.output.schema.safeParse(payload.body, {
|
|
87
|
+
allowTransform: false,
|
|
88
|
+
});
|
|
89
|
+
if (!result.success) {
|
|
90
|
+
throw new errors_js_1.LexRpcUpstreamError('InvalidResponse', `Response validation failed: ${result.reason.message}`, response, payload, { cause: result.reason });
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return new LexRpcResponse(method, response.status, response.headers, payload);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
exports.LexRpcResponse = LexRpcResponse;
|
|
98
|
+
function shouldParse(method) {
|
|
99
|
+
return method.output.encoding === 'application/json';
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* @note this function always consumes the response body
|
|
103
|
+
*/
|
|
104
|
+
async function readPayload(response, options) {
|
|
105
|
+
// @TODO Should we limit the maximum response size here (this could also be
|
|
106
|
+
// done by the FetchHandler)?
|
|
107
|
+
const encoding = response.headers
|
|
108
|
+
.get('content-type')
|
|
109
|
+
?.split(';')[0]
|
|
110
|
+
.trim()
|
|
111
|
+
.toLowerCase();
|
|
112
|
+
// Response content-type is undefined
|
|
113
|
+
if (!encoding) {
|
|
114
|
+
// If the body is empty, return null (= no payload)
|
|
115
|
+
const body = await response.arrayBuffer();
|
|
116
|
+
if (body.byteLength === 0)
|
|
117
|
+
return null;
|
|
118
|
+
// If we got data despite no content-type, treat it as binary
|
|
119
|
+
return {
|
|
120
|
+
encoding: 'application/octet-stream',
|
|
121
|
+
body: new Uint8Array(body),
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
if (options?.parse && encoding === 'application/json') {
|
|
125
|
+
// @NOTE It might be worth returning the raw bytes here (Uint8Array) and
|
|
126
|
+
// perform the lex parsing using cborg/json, allowing to do
|
|
127
|
+
// bytes->LexValue in one step instead of bytes->text->JSON->LexValue.
|
|
128
|
+
// This would require adding encode/decode utilities to lex-json (similar
|
|
129
|
+
// to @ipld/dag-json)
|
|
130
|
+
const text = await response.text();
|
|
131
|
+
try {
|
|
132
|
+
// @NOTE Using `lexParse(text)` (instead of `jsonToLex(json)`) here as
|
|
133
|
+
// using a reviver function during JSON.parse should be faster than
|
|
134
|
+
// parsing to JSON then converting to Lex (?)
|
|
135
|
+
// @TODO verify statement above
|
|
136
|
+
return { encoding, body: (0, lex_json_1.lexParse)(text) };
|
|
137
|
+
}
|
|
138
|
+
catch (cause) {
|
|
139
|
+
throw new errors_js_1.LexRpcUpstreamError('InvalidResponse', 'Invalid JSON response body', response, null, { cause });
|
|
140
|
+
}
|
|
28
141
|
}
|
|
142
|
+
return { encoding, body: new Uint8Array(await response.arrayBuffer()) };
|
|
29
143
|
}
|
|
30
|
-
exports.XrpcResponse = XrpcResponse;
|
|
31
144
|
//# sourceMappingURL=response.js.map
|
package/dist/response.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"response.js","sourceRoot":"","sources":["../src/response.ts"],"names":[],"mappings":";;;AAeA;;;;GAIG;AACH,MAAa,YAAY;IAgBZ;IACA;IACA;IACA;IAhBX,yCAAyC;IAChC,OAAO,GAAG,IAAa,CAAA;IAEhC,uCAAuC;IACvC,IAAI,KAAK;QACP,OAAO,IAAI,CAAA;IACb,CAAC;IAED,IAAI,QAAQ;QACV,OAAO,IAAI,CAAC,MAAM,CAAC,MAAM,EAAE,QAAQ,CAAA;IACrC,CAAC;IAED,YACW,MAAS,EACT,MAAc,EACd,OAAgB,EAChB,IAAyB;QAHzB,WAAM,GAAN,MAAM,CAAG;QACT,WAAM,GAAN,MAAM,CAAQ;QACd,YAAO,GAAP,OAAO,CAAS;QAChB,SAAI,GAAJ,IAAI,CAAqB;IACjC,CAAC;CACL;AArBD,oCAqBC","sourcesContent":["import {\n InferPayloadBody,\n InferPayloadEncoding,\n Procedure,\n Query,\n ResultSuccess,\n} from '@atproto/lex-schema'\n\nexport type XrpcResponseEncoding<M extends Procedure | Query> =\n InferPayloadEncoding<M['output']>\n\nexport type XrpcResponseBody<M extends Procedure | Query> = InferPayloadBody<\n M['output']\n>\n\n/**\n * Small container for XRPC response data.\n *\n * @implements {ResultSuccess<XrpcResponse<M>>} for convenience in result handling contexts.\n */\nexport class XrpcResponse<M extends Procedure | Query>\n implements ResultSuccess<XrpcResponse<M>>\n{\n /** @see {@link ResultSuccess.success} */\n readonly success = true as const\n\n /** @see {@link ResultSuccess.value} */\n get value(): this {\n return this\n }\n\n get encoding(): XrpcResponseEncoding<M> {\n return this.method.output?.encoding\n }\n\n constructor(\n readonly method: M,\n readonly status: number,\n readonly headers: Headers,\n readonly body: XrpcResponseBody<M>,\n ) {}\n}\n"]}
|
|
1
|
+
{"version":3,"file":"response.js","sourceRoot":"","sources":["../src/response.ts"],"names":[],"mappings":";;;AAAA,gDAA4C;AAQ5C,2CAIoB;AAWpB;;;;GAIG;AACH,MAAa,cAAc;IAYd;IACA;IACA;IACA;IAZX,yCAAyC;IAChC,OAAO,GAAG,IAAa,CAAA;IAEhC,uCAAuC;IACvC,IAAI,KAAK;QACP,OAAO,IAAI,CAAA;IACb,CAAC;IAED,YACW,MAAS,EACT,MAAc,EACd,OAAgB,EAChB,OAAiC;QAHjC,WAAM,GAAN,MAAM,CAAG;QACT,WAAM,GAAN,MAAM,CAAQ;QACd,YAAO,GAAP,OAAO,CAAS;QAChB,YAAO,GAAP,OAAO,CAA0B;IACzC,CAAC;IAEJ;;;OAGG;IACH,IAAI,QAAQ;QACV,OAAO,IAAI,CAAC,QAAQ,KAAK,kBAAkB,IAAI,WAAW,CAAC,IAAI,CAAC,MAAM,CAAC,CAAA;IACzE,CAAC;IAED,IAAI,QAAQ;QACV,OAAO,IAAI,CAAC,OAAO,EAAE,QAAwC,CAAA;IAC/D,CAAC;IAED,IAAI,IAAI;QACN,OAAO,IAAI,CAAC,OAAO,EAAE,IAA6B,CAAA;IACpD,CAAC;IAED;;;;;;OAMG;IACH,MAAM,CAAC,KAAK,CAAC,iBAAiB,CAC5B,MAAS,EACT,QAAkB,EAClB,OAAwC;QAExC,0EAA0E;QAC1E,kEAAkE;QAClE,oDAAoD;QAEpD,4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AAG,CAAC,cAAc,CAAC;QACpB,EAAE,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;SACd,IAAI,EAAE;SACN,WAAW,EAAE,CAAA;IAEhB,qCAAqC;IACrC,IAAI,CAAC,QAAQ,EAAE,CAAC;QACd,mDAAmD;QACnD,MAAM,IAAI,GAAG,MAAM,QAAQ,CAAC,WAAW,EAAE,CAAA;QACzC,IAAI,IAAI,CAAC,UAAU,KAAK,CAAC;YAAE,OAAO,IAAI,CAAA;QAEtC,6DAA6D;QAC7D,OAAO;YACL,QAAQ,EAAE,0BAA0B;YACpC,IAAI,EAAE,IAAI,UAAU,CAAC,IAAI,CAAC;SAC3B,CAAA;IACH,CAAC;IAED,IAAI,OAAO,EAAE,KAAK,IAAI,QAAQ,KAAK,kBAAkB,EAAE,CAAC;QACtD,wEAAwE;QACxE,2DAA2D;QAC3D,sEAAsE;QACtE,yEAAyE;QACzE,qBAAqB;QACrB,MAAM,IAAI,GAAG,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAA;QAElC,IAAI,CAAC;YACH,sEAAsE;YACtE,mEAAmE;YACnE,6CAA6C;YAE7C,+BAA+B;YAC/B,OAAO,EAAE,QAAQ,EAAE,IAAI,EAAE,IAAA,mBAAQ,EAAC,IAAI,CAAC,EAAE,CAAA;QAC3C,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,MAAM,IAAI,+BAAmB,CAC3B,iBAAiB,EACjB,4BAA4B,EAC5B,QAAQ,EACR,IAAI,EACJ,EAAE,KAAK,EAAE,CACV,CAAA;QACH,CAAC;IACH,CAAC;IAED,OAAO,EAAE,QAAQ,EAAE,IAAI,EAAE,IAAI,UAAU,CAAC,MAAM,QAAQ,CAAC,WAAW,EAAE,CAAC,EAAE,CAAA;AACzE,CAAC","sourcesContent":["import { lexParse } from '@atproto/lex-json'\nimport {\n InferMethodOutputBody,\n InferMethodOutputEncoding,\n Procedure,\n Query,\n ResultSuccess,\n} from '@atproto/lex-schema'\nimport {\n LexRpcResponseError,\n LexRpcUpstreamError,\n isLexRpcErrorPayload,\n} from './errors.js'\nimport { Payload } from './util.js'\n\nexport type LexRpcResponseBody<M extends Procedure | Query> =\n InferMethodOutputBody<M, Uint8Array>\n\nexport type LexRpcResponsePayload<M extends Procedure | Query> =\n InferMethodOutputEncoding<M> extends infer E extends string\n ? Payload<LexRpcResponseBody<M>, E>\n : null\n\n/**\n * Small container for XRPC response data.\n *\n * @implements {ResultSuccess<LexRpcResponse<M>>} for convenience in result handling contexts.\n */\nexport class LexRpcResponse<const M extends Procedure | Query>\n implements ResultSuccess<LexRpcResponse<M>>\n{\n /** @see {@link ResultSuccess.success} */\n readonly success = true as const\n\n /** @see {@link ResultSuccess.value} */\n get value(): this {\n return this\n }\n\n constructor(\n readonly method: M,\n readonly status: number,\n readonly headers: Headers,\n readonly payload: LexRpcResponsePayload<M>,\n ) {}\n\n /**\n * Whether the response payload was parsed as {@link LexValue} (`true`) or is\n * in binary form {@link Uint8Array} (`false`).\n */\n get isParsed() {\n return this.encoding === 'application/json' && shouldParse(this.method)\n }\n\n get encoding() {\n return this.payload?.encoding as InferMethodOutputEncoding<M>\n }\n\n get body() {\n return this.payload?.body as LexRpcResponseBody<M>\n }\n\n /**\n * @throws {LexRpcResponseError} in case of (valid) XRPC error responses. Use\n * {@link LexRpcResponseError.matchesSchema} to narrow the error type based on\n * the method's declared error schema.\n * @throws {LexRpcUpstreamError} when the response is not a valid XRPC\n * response, or if the response does not conform to the method's schema.\n */\n static async fromFetchResponse<const M extends Procedure | Query>(\n method: M,\n response: Response,\n options?: { validateResponse?: boolean },\n ): Promise<LexRpcResponse<M>> {\n // @NOTE The body MUST either be read or canceled to avoid resource leaks.\n // Since nothing should cause an exception before \"readPayload\" is\n // called, we can safely not use a try/finally here.\n\n // @NOTE redirect is set to 'follow', so we shouldn't get 3xx responses here\n if (response.status < 200 || response.status >= 300) {\n // Always parse json for error responses\n const payload = await readPayload(response, { parse: true })\n\n if (response.status >= 400 && isLexRpcErrorPayload(payload)) {\n throw new LexRpcResponseError(\n method,\n response.status,\n response.headers,\n payload,\n )\n }\n\n if (response.status >= 500) {\n throw new LexRpcUpstreamError(\n 'UpstreamFailure',\n `Upstream server encountered an error`,\n response,\n payload,\n )\n }\n\n throw new LexRpcUpstreamError(\n 'InvalidResponse',\n response.status >= 400\n ? `Upstream server returned an invalid response payload`\n : `Upstream server returned an invalid status code`,\n response,\n payload,\n )\n }\n\n // Only parse json if the schema expects it\n const payload = await readPayload(response, {\n parse: shouldParse(method),\n })\n\n // Response is successful (2xx). Validate payload (data and encoding) against schema.\n if (method.output.encoding == null) {\n // Schema expects no payload\n if (payload) {\n throw new LexRpcUpstreamError(\n 'InvalidResponse',\n `Expected response with no body, got ${payload.encoding}`,\n response,\n payload,\n )\n }\n } else {\n // Schema expects a payload\n if (!payload || !method.output.matchesEncoding(payload.encoding)) {\n throw new LexRpcUpstreamError(\n 'InvalidResponse',\n payload\n ? `Expected ${method.output.encoding} response, got ${payload.encoding}`\n : `Expected non-empty response with content-type ${method.output.encoding}`,\n response,\n payload,\n )\n }\n\n // Assert valid response body.\n if (method.output.schema && options?.validateResponse !== false) {\n const result = method.output.schema.safeParse(payload.body, {\n allowTransform: false,\n })\n\n if (!result.success) {\n throw new LexRpcUpstreamError(\n 'InvalidResponse',\n `Response validation failed: ${result.reason.message}`,\n response,\n payload,\n { cause: result.reason },\n )\n }\n }\n }\n\n return new LexRpcResponse<M>(\n method,\n response.status,\n response.headers,\n payload as LexRpcResponsePayload<M>,\n )\n }\n}\n\nfunction shouldParse(method: Procedure | Query) {\n return method.output.encoding === 'application/json'\n}\n\n/**\n * @note this function always consumes the response body\n */\nasync function readPayload(\n response: Response,\n options?: { parse?: boolean },\n): Promise<Payload | null> {\n // @TODO Should we limit the maximum response size here (this could also be\n // done by the FetchHandler)?\n\n const encoding = response.headers\n .get('content-type')\n ?.split(';')[0]\n .trim()\n .toLowerCase()\n\n // Response content-type is undefined\n if (!encoding) {\n // If the body is empty, return null (= no payload)\n const body = await response.arrayBuffer()\n if (body.byteLength === 0) return null\n\n // If we got data despite no content-type, treat it as binary\n return {\n encoding: 'application/octet-stream',\n body: new Uint8Array(body),\n }\n }\n\n if (options?.parse && encoding === 'application/json') {\n // @NOTE It might be worth returning the raw bytes here (Uint8Array) and\n // perform the lex parsing using cborg/json, allowing to do\n // bytes->LexValue in one step instead of bytes->text->JSON->LexValue.\n // This would require adding encode/decode utilities to lex-json (similar\n // to @ipld/dag-json)\n const text = await response.text()\n\n try {\n // @NOTE Using `lexParse(text)` (instead of `jsonToLex(json)`) here as\n // using a reviver function during JSON.parse should be faster than\n // parsing to JSON then converting to Lex (?)\n\n // @TODO verify statement above\n return { encoding, body: lexParse(text) }\n } catch (cause) {\n throw new LexRpcUpstreamError(\n 'InvalidResponse',\n 'Invalid JSON response body',\n response,\n null,\n { cause },\n )\n }\n }\n\n return { encoding, body: new Uint8Array(await response.arrayBuffer()) }\n}\n"]}
|
package/dist/types.d.ts
CHANGED
|
@@ -7,11 +7,25 @@ export type CallOptions = {
|
|
|
7
7
|
signal?: AbortSignal;
|
|
8
8
|
headers?: HeadersInit;
|
|
9
9
|
service?: Service;
|
|
10
|
+
/**
|
|
11
|
+
* Whether to validate the request against the method's input schema. Enabling
|
|
12
|
+
* this can help catch errors early but may have a performance cost. This
|
|
13
|
+
* would typically only be set to `true` in development or debugging
|
|
14
|
+
* scenarios.
|
|
15
|
+
*
|
|
16
|
+
* @default false
|
|
17
|
+
*/
|
|
10
18
|
validateRequest?: boolean;
|
|
19
|
+
/**
|
|
20
|
+
* Whether to validate the response against the method's output schema.
|
|
21
|
+
* Disabling this can improve performance but may lead to runtime errors if
|
|
22
|
+
* the response does not conform to the expected schema. Only set this to
|
|
23
|
+
* `false` if you are certain that the upstream service will always return
|
|
24
|
+
* valid responses.
|
|
25
|
+
*
|
|
26
|
+
* @default true
|
|
27
|
+
*/
|
|
11
28
|
validateResponse?: boolean;
|
|
12
29
|
};
|
|
13
|
-
export type
|
|
14
|
-
main: T;
|
|
15
|
-
};
|
|
16
|
-
export declare function getMain<T extends object>(ns: Namespace<T>): T;
|
|
30
|
+
export type BinaryBodyInit = Uint8Array | ArrayBuffer | Blob | ReadableStream<Uint8Array> | AsyncIterable<Uint8Array> | string;
|
|
17
31
|
//# sourceMappingURL=types.d.ts.map
|
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAA;AAE9D,YAAY,EAAE,SAAS,EAAE,aAAa,EAAE,CAAA;AAExC,MAAM,MAAM,oBAAoB,GAAG,iBAAiB,GAAG,aAAa,CAAA;AACpE,MAAM,MAAM,OAAO,GAAG,GAAG,SAAS,IAAI,oBAAoB,EAAE,CAAA;AAE5D,MAAM,MAAM,WAAW,GAAG;IACxB,QAAQ,CAAC,EAAE,QAAQ,CAAC,SAAS,CAAC,CAAA;IAC9B,MAAM,CAAC,EAAE,WAAW,CAAA;IACpB,OAAO,CAAC,EAAE,WAAW,CAAA;IACrB,OAAO,CAAC,EAAE,OAAO,CAAA;
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAA;AAE9D,YAAY,EAAE,SAAS,EAAE,aAAa,EAAE,CAAA;AAExC,MAAM,MAAM,oBAAoB,GAAG,iBAAiB,GAAG,aAAa,CAAA;AACpE,MAAM,MAAM,OAAO,GAAG,GAAG,SAAS,IAAI,oBAAoB,EAAE,CAAA;AAE5D,MAAM,MAAM,WAAW,GAAG;IACxB,QAAQ,CAAC,EAAE,QAAQ,CAAC,SAAS,CAAC,CAAA;IAC9B,MAAM,CAAC,EAAE,WAAW,CAAA;IACpB,OAAO,CAAC,EAAE,WAAW,CAAA;IACrB,OAAO,CAAC,EAAE,OAAO,CAAA;IAEjB;;;;;;;OAOG;IACH,eAAe,CAAC,EAAE,OAAO,CAAA;IAEzB;;;;;;;;OAQG;IACH,gBAAgB,CAAC,EAAE,OAAO,CAAA;CAC3B,CAAA;AAED,MAAM,MAAM,cAAc,GACtB,UAAU,GACV,WAAW,GACX,IAAI,GACJ,cAAc,CAAC,UAAU,CAAC,GAC1B,aAAa,CAAC,UAAU,CAAC,GACzB,MAAM,CAAA"}
|
package/dist/types.js
CHANGED
package/dist/types.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"","sourcesContent":["import { DidString, UnknownString } from '@atproto/lex-schema'\n\nexport type { DidString, UnknownString }\n\nexport type DidServiceIdentifier = 'atproto_labeler' | UnknownString\nexport type Service = `${DidString}#${DidServiceIdentifier}`\n\nexport type CallOptions = {\n labelers?: Iterable<DidString>\n signal?: AbortSignal\n headers?: HeadersInit\n service?: Service\n\n /**\n * Whether to validate the request against the method's input schema. Enabling\n * this can help catch errors early but may have a performance cost. This\n * would typically only be set to `true` in development or debugging\n * scenarios.\n *\n * @default false\n */\n validateRequest?: boolean\n\n /**\n * Whether to validate the response against the method's output schema.\n * Disabling this can improve performance but may lead to runtime errors if\n * the response does not conform to the expected schema. Only set this to\n * `false` if you are certain that the upstream service will always return\n * valid responses.\n *\n * @default true\n */\n validateResponse?: boolean\n}\n\nexport type BinaryBodyInit =\n | Uint8Array\n | ArrayBuffer\n | Blob\n | ReadableStream<Uint8Array>\n | AsyncIterable<Uint8Array>\n | string\n"]}
|
package/dist/util.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { DidString } from '@atproto/lex-schema';
|
|
2
|
+
export type Payload<B = unknown, E extends string = string> = {
|
|
3
|
+
body: B;
|
|
4
|
+
encoding: E;
|
|
5
|
+
};
|
|
6
|
+
export declare function isBlobLike(value: unknown): value is Blob;
|
|
7
|
+
export declare function isAsyncIterable<T>(value: T): value is unknown extends T ? T & AsyncIterable<unknown> : Extract<T, AsyncIterable<any>>;
|
|
8
|
+
export declare function buildAtprotoHeaders(options: {
|
|
9
|
+
headers?: HeadersInit;
|
|
10
|
+
service?: `${DidString}#${string}`;
|
|
11
|
+
labelers?: Iterable<DidString>;
|
|
12
|
+
}): Headers;
|
|
13
|
+
export declare function toReadableStream(data: AsyncIterable<Uint8Array>): ReadableStream<Uint8Array>;
|
|
14
|
+
//# sourceMappingURL=util.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"util.d.ts","sourceRoot":"","sources":["../src/util.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAA;AAE/C,MAAM,MAAM,OAAO,CAAC,CAAC,GAAG,OAAO,EAAE,CAAC,SAAS,MAAM,GAAG,MAAM,IAAI;IAC5D,IAAI,EAAE,CAAC,CAAA;IACP,QAAQ,EAAE,CAAC,CAAA;CACZ,CAAA;AAED,wBAAgB,UAAU,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,IAAI,CAexD;AAED,wBAAgB,eAAe,CAAC,CAAC,EAC/B,KAAK,EAAE,CAAC,GACP,KAAK,IAAI,OAAO,SAAS,CAAC,GACzB,CAAC,GAAG,aAAa,CAAC,OAAO,CAAC,GAC1B,OAAO,CAAC,CAAC,EAAE,aAAa,CAAC,GAAG,CAAC,CAAC,CAIjC;AAED,wBAAgB,mBAAmB,CAAC,OAAO,EAAE;IAC3C,OAAO,CAAC,EAAE,WAAW,CAAA;IACrB,OAAO,CAAC,EAAE,GAAG,SAAS,IAAI,MAAM,EAAE,CAAA;IAClC,QAAQ,CAAC,EAAE,QAAQ,CAAC,SAAS,CAAC,CAAA;CAC/B,GAAG,OAAO,CAiBV;AAED,wBAAgB,gBAAgB,CAC9B,IAAI,EAAE,aAAa,CAAC,UAAU,CAAC,GAC9B,cAAc,CAAC,UAAU,CAAC,CAwB5B"}
|
package/dist/util.js
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isBlobLike = isBlobLike;
|
|
4
|
+
exports.isAsyncIterable = isAsyncIterable;
|
|
5
|
+
exports.buildAtprotoHeaders = buildAtprotoHeaders;
|
|
6
|
+
exports.toReadableStream = toReadableStream;
|
|
7
|
+
function isBlobLike(value) {
|
|
8
|
+
if (value == null)
|
|
9
|
+
return false;
|
|
10
|
+
if (typeof value !== 'object')
|
|
11
|
+
return false;
|
|
12
|
+
if (typeof Blob === 'function' && value instanceof Blob)
|
|
13
|
+
return true;
|
|
14
|
+
// Support for Blobs provided by libraries that don't use the native Blob
|
|
15
|
+
// (e.g. fetch-blob from node-fetch).
|
|
16
|
+
// https://github.com/node-fetch/fetch-blob/blob/a1a182e5978811407bef4ea1632b517567dda01f/index.js#L233-L244
|
|
17
|
+
const tag = value[Symbol.toStringTag];
|
|
18
|
+
if (tag === 'Blob' || tag === 'File') {
|
|
19
|
+
return 'stream' in value && typeof value.stream === 'function';
|
|
20
|
+
}
|
|
21
|
+
return false;
|
|
22
|
+
}
|
|
23
|
+
function isAsyncIterable(value) {
|
|
24
|
+
return (value != null && typeof value[Symbol.asyncIterator] === 'function');
|
|
25
|
+
}
|
|
26
|
+
function buildAtprotoHeaders(options) {
|
|
27
|
+
const headers = new Headers(options?.headers);
|
|
28
|
+
if (options.service && !headers.has('atproto-proxy')) {
|
|
29
|
+
headers.set('atproto-proxy', options.service);
|
|
30
|
+
}
|
|
31
|
+
if (options.labelers) {
|
|
32
|
+
headers.set('atproto-accept-labelers', [...options.labelers, headers.get('atproto-accept-labelers')?.trim()]
|
|
33
|
+
.filter(Boolean)
|
|
34
|
+
.join(', '));
|
|
35
|
+
}
|
|
36
|
+
return headers;
|
|
37
|
+
}
|
|
38
|
+
function toReadableStream(data) {
|
|
39
|
+
// Use the native ReadableStream.from() if available.
|
|
40
|
+
if ('from' in ReadableStream && typeof ReadableStream.from === 'function') {
|
|
41
|
+
return ReadableStream.from(data);
|
|
42
|
+
}
|
|
43
|
+
let iterator;
|
|
44
|
+
return new ReadableStream({
|
|
45
|
+
async pull(controller) {
|
|
46
|
+
try {
|
|
47
|
+
iterator ??= data[Symbol.asyncIterator]();
|
|
48
|
+
const result = await iterator.next();
|
|
49
|
+
if (result.done)
|
|
50
|
+
controller.close();
|
|
51
|
+
else
|
|
52
|
+
controller.enqueue(result.value);
|
|
53
|
+
}
|
|
54
|
+
catch (err) {
|
|
55
|
+
controller.error(err);
|
|
56
|
+
iterator = undefined;
|
|
57
|
+
}
|
|
58
|
+
},
|
|
59
|
+
async cancel() {
|
|
60
|
+
await iterator?.return?.();
|
|
61
|
+
iterator = undefined;
|
|
62
|
+
},
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
//# sourceMappingURL=util.js.map
|
package/dist/util.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"util.js","sourceRoot":"","sources":["../src/util.ts"],"names":[],"mappings":";;AAOA,gCAeC;AAED,0CAQC;AAED,kDAqBC;AAED,4CA0BC;AA5ED,SAAgB,UAAU,CAAC,KAAc;IACvC,IAAI,KAAK,IAAI,IAAI;QAAE,OAAO,KAAK,CAAA;IAC/B,IAAI,OAAO,KAAK,KAAK,QAAQ;QAAE,OAAO,KAAK,CAAA;IAC3C,IAAI,OAAO,IAAI,KAAK,UAAU,IAAI,KAAK,YAAY,IAAI;QAAE,OAAO,IAAI,CAAA;IAEpE,yEAAyE;IACzE,qCAAqC;IACrC,4GAA4G;IAE5G,MAAM,GAAG,GAAI,KAAa,CAAC,MAAM,CAAC,WAAW,CAAC,CAAA;IAC9C,IAAI,GAAG,KAAK,MAAM,IAAI,GAAG,KAAK,MAAM,EAAE,CAAC;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AAA;gBACzC,MAAM,MAAM,GAAG,MAAM,QAAS,CAAC,IAAI,EAAE,CAAA;gBACrC,IAAI,MAAM,CAAC,IAAI;oBAAE,UAAU,CAAC,KAAK,EAAE,CAAA;;oBAC9B,UAAU,CAAC,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,CAAA;YACvC,CAAC;YAAC,OAAO,GAAG,EAAE,CAAC;gBACb,UAAU,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;gBACrB,QAAQ,GAAG,SAAS,CAAA;YACtB,CAAC;QACH,CAAC;QACD,KAAK,CAAC,MAAM;YACV,MAAM,QAAQ,EAAE,MAAM,EAAE,EAAE,CAAA;YAC1B,QAAQ,GAAG,SAAS,CAAA;QACtB,CAAC;KACF,CAAC,CAAA;AACJ,CAAC","sourcesContent":["import { DidString } from '@atproto/lex-schema'\n\nexport type Payload<B = unknown, E extends string = string> = {\n body: B\n encoding: E\n}\n\nexport function isBlobLike(value: unknown): value is Blob {\n if (value == null) return false\n if (typeof value !== 'object') return false\n if (typeof Blob === 'function' && value instanceof Blob) return true\n\n // Support for Blobs provided by libraries that don't use the native Blob\n // (e.g. fetch-blob from node-fetch).\n // https://github.com/node-fetch/fetch-blob/blob/a1a182e5978811407bef4ea1632b517567dda01f/index.js#L233-L244\n\n const tag = (value as any)[Symbol.toStringTag]\n if (tag === 'Blob' || tag === 'File') {\n return 'stream' in value && typeof value.stream === 'function'\n }\n\n return false\n}\n\nexport function isAsyncIterable<T>(\n value: T,\n): value is unknown extends T\n ? T & AsyncIterable<unknown>\n : Extract<T, AsyncIterable<any>> {\n return (\n value != null && typeof (value as any)[Symbol.asyncIterator] === 'function'\n )\n}\n\nexport function buildAtprotoHeaders(options: {\n headers?: HeadersInit\n service?: `${DidString}#${string}`\n labelers?: Iterable<DidString>\n}): Headers {\n const headers = new Headers(options?.headers)\n\n if (options.service && !headers.has('atproto-proxy')) {\n headers.set('atproto-proxy', options.service)\n }\n\n if (options.labelers) {\n headers.set(\n 'atproto-accept-labelers',\n [...options.labelers, headers.get('atproto-accept-labelers')?.trim()]\n .filter(Boolean)\n .join(', '),\n )\n }\n\n return headers\n}\n\nexport function toReadableStream(\n data: AsyncIterable<Uint8Array>,\n): ReadableStream<Uint8Array> {\n // Use the native ReadableStream.from() if available.\n if ('from' in ReadableStream && typeof ReadableStream.from === 'function') {\n return ReadableStream.from(data)\n }\n\n let iterator: AsyncIterator<Uint8Array> | undefined\n return new ReadableStream({\n async pull(controller) {\n try {\n iterator ??= data[Symbol.asyncIterator]()\n const result = await iterator!.next()\n if (result.done) controller.close()\n else controller.enqueue(result.value)\n } catch (err) {\n controller.error(err)\n iterator = undefined\n }\n },\n async cancel() {\n await iterator?.return?.()\n iterator = undefined\n },\n })\n}\n"]}
|
package/dist/xrpc.d.ts
CHANGED
|
@@ -1,37 +1,40 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { DidString, InferParamsSchema, InferPayloadBody, Params, ParamsSchema, Procedure, Query, Restricted, Subscription } from '@atproto/lex-schema';
|
|
1
|
+
import { InferMethodInput, InferMethodParams, Main, Params, Procedure, Query, Restricted } from '@atproto/lex-schema';
|
|
3
2
|
import { Agent } from './agent.js';
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
export type XrpcRequestUrlOptions<M extends Query | Procedure | Subscription> = CallOptions & (undefined extends InferParamsSchema<M['parameters']> ? {
|
|
10
|
-
params?: InferParamsSchema<M['parameters']>;
|
|
3
|
+
import { LexRpcResponseError, LexRpcUnexpectedError, LexRpcUpstreamError } from './errors.js';
|
|
4
|
+
import { LexRpcResponse } from './response.js';
|
|
5
|
+
import { BinaryBodyInit, CallOptions } from './types.js';
|
|
6
|
+
type LexRpcParamsOptions<P extends Params> = NonNullable<unknown> extends P ? {
|
|
7
|
+
params?: P;
|
|
11
8
|
} : {
|
|
12
|
-
params:
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
}
|
|
19
|
-
body:
|
|
9
|
+
params: P;
|
|
10
|
+
};
|
|
11
|
+
type LexRpcRequestPayload<M extends Procedure | Query> = InferMethodInput<M, BinaryBodyInit>;
|
|
12
|
+
type LexRpcInputOptions<In> = In extends {
|
|
13
|
+
body: infer B;
|
|
14
|
+
encoding: infer E;
|
|
15
|
+
} ? {
|
|
16
|
+
body: B;
|
|
17
|
+
encoding?: E;
|
|
20
18
|
} : {
|
|
21
|
-
body?:
|
|
22
|
-
|
|
23
|
-
export declare function xrpcRequestInit<T extends Procedure | Query>(schema: T, options: XrpcRequestInitOptions<T>): RequestInit & {
|
|
24
|
-
duplex?: 'half';
|
|
19
|
+
body?: undefined;
|
|
20
|
+
encoding?: undefined;
|
|
25
21
|
};
|
|
26
|
-
export
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
export declare function
|
|
36
|
-
|
|
22
|
+
export type LexRpcOptions<M extends Procedure | Query = Procedure | Query> = CallOptions & LexRpcInputOptions<LexRpcRequestPayload<M>> & LexRpcParamsOptions<InferMethodParams<M>>;
|
|
23
|
+
export type LexRpcFailure<M extends Procedure | Query> = LexRpcResponseError<M> | LexRpcUpstreamError | LexRpcUnexpectedError;
|
|
24
|
+
export type LexRpcResult<M extends Procedure | Query> = LexRpcResponse<M> | LexRpcFailure<M>;
|
|
25
|
+
/**
|
|
26
|
+
* Utility method to type cast the error thrown by {@link xrpc} to an
|
|
27
|
+
* {@link LexRpcFailure} matching the provided method. Only use this function
|
|
28
|
+
* inside a catch block right after calling {@link xrpc}, and use the same
|
|
29
|
+
* method type parameter as used in the {@link xrpc} call.
|
|
30
|
+
*/
|
|
31
|
+
export declare function asLexRpcFailure<M extends Procedure | Query = Procedure | Query>(err: unknown): LexRpcFailure<M>;
|
|
32
|
+
/**
|
|
33
|
+
* @throws LexRpcFailure<M>
|
|
34
|
+
*/
|
|
35
|
+
export declare function xrpc<const M extends Query | Procedure>(agent: Agent, ns: NonNullable<unknown> extends LexRpcOptions<M> ? Main<M> : Restricted<'This XRPC method requires an "options" argument'>): Promise<LexRpcResponse<M>>;
|
|
36
|
+
export declare function xrpc<const M extends Query | Procedure>(agent: Agent, ns: Main<M>, options: LexRpcOptions<M>): Promise<LexRpcResponse<M>>;
|
|
37
|
+
export declare function xrpcSafe<const M extends Query | Procedure>(agent: Agent, ns: NonNullable<unknown> extends LexRpcOptions<M> ? Main<M> : Restricted<'This XRPC method requires an "options" argument'>): Promise<LexRpcResult<M>>;
|
|
38
|
+
export declare function xrpcSafe<const M extends Query | Procedure>(agent: Agent, ns: Main<M>, options: LexRpcOptions<M>): Promise<LexRpcResult<M>>;
|
|
39
|
+
export {};
|
|
37
40
|
//# sourceMappingURL=xrpc.d.ts.map
|
package/dist/xrpc.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"xrpc.d.ts","sourceRoot":"","sources":["../src/xrpc.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"xrpc.d.ts","sourceRoot":"","sources":["../src/xrpc.ts"],"names":[],"mappings":"AAEA,OAAO,EACL,gBAAgB,EAChB,iBAAiB,EACjB,IAAI,EACJ,MAAM,EAGN,SAAS,EACT,KAAK,EACL,UAAU,EAGX,MAAM,qBAAqB,CAAA;AAC5B,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAA;AAClC,OAAO,EACL,mBAAmB,EACnB,qBAAqB,EACrB,mBAAmB,EACpB,MAAM,aAAa,CAAA;AACpB,OAAO,EAAE,cAAc,EAAE,MAAM,eAAe,CAAA;AAC9C,OAAO,EAAE,cAAc,EAAE,WAAW,EAAE,MAAM,YAAY,CAAA;AAUxD,KAAK,mBAAmB,CAAC,CAAC,SAAS,MAAM,IACvC,WAAW,CAAC,OAAO,CAAC,SAAS,CAAC,GAAG;IAAE,MAAM,CAAC,EAAE,CAAC,C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