@atproto/lex-client 0.3.7 → 0.4.2
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 +38 -0
- package/README.md +4 -3
- package/dist/client.d.ts +4 -11
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +6 -12
- package/dist/client.js.map +1 -1
- package/dist/index.d.ts +2 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -5
- package/dist/index.js.map +1 -1
- package/dist/lexicons/com/atproto/repo/applyWrites.d.ts +2 -2
- package/dist/lexicons/com/atproto/repo/applyWrites.d.ts.map +1 -1
- package/dist/lexicons/com/atproto/repo/applyWrites.defs.d.ts +23 -23
- package/dist/lexicons/com/atproto/repo/applyWrites.defs.d.ts.map +1 -1
- package/dist/lexicons/com/atproto/repo/applyWrites.defs.js +13 -64
- package/dist/lexicons/com/atproto/repo/applyWrites.defs.js.map +1 -1
- package/dist/lexicons/com/atproto/repo/applyWrites.js +2 -2
- package/dist/lexicons/com/atproto/repo/applyWrites.js.map +1 -1
- package/dist/lexicons/com/atproto/repo/createRecord.d.ts +2 -2
- package/dist/lexicons/com/atproto/repo/createRecord.d.ts.map +1 -1
- package/dist/lexicons/com/atproto/repo/createRecord.defs.d.ts +2 -2
- package/dist/lexicons/com/atproto/repo/createRecord.defs.d.ts.map +1 -1
- package/dist/lexicons/com/atproto/repo/createRecord.defs.js +6 -24
- package/dist/lexicons/com/atproto/repo/createRecord.defs.js.map +1 -1
- package/dist/lexicons/com/atproto/repo/createRecord.js +2 -2
- package/dist/lexicons/com/atproto/repo/createRecord.js.map +1 -1
- package/dist/lexicons/com/atproto/repo/defs.d.ts +1 -1
- package/dist/lexicons/com/atproto/repo/defs.d.ts.map +1 -1
- package/dist/lexicons/com/atproto/repo/defs.defs.d.ts +4 -4
- package/dist/lexicons/com/atproto/repo/defs.defs.d.ts.map +1 -1
- package/dist/lexicons/com/atproto/repo/defs.defs.js +3 -7
- package/dist/lexicons/com/atproto/repo/defs.defs.js.map +1 -1
- package/dist/lexicons/com/atproto/repo/defs.js +1 -1
- package/dist/lexicons/com/atproto/repo/defs.js.map +1 -1
- package/dist/lexicons/com/atproto/repo/deleteRecord.d.ts +2 -2
- package/dist/lexicons/com/atproto/repo/deleteRecord.d.ts.map +1 -1
- package/dist/lexicons/com/atproto/repo/deleteRecord.defs.d.ts +2 -2
- package/dist/lexicons/com/atproto/repo/deleteRecord.defs.d.ts.map +1 -1
- package/dist/lexicons/com/atproto/repo/deleteRecord.defs.js +6 -19
- package/dist/lexicons/com/atproto/repo/deleteRecord.defs.js.map +1 -1
- package/dist/lexicons/com/atproto/repo/deleteRecord.js +2 -2
- package/dist/lexicons/com/atproto/repo/deleteRecord.js.map +1 -1
- package/dist/lexicons/com/atproto/repo/getRecord.d.ts +2 -2
- package/dist/lexicons/com/atproto/repo/getRecord.d.ts.map +1 -1
- package/dist/lexicons/com/atproto/repo/getRecord.defs.d.ts +1 -1
- package/dist/lexicons/com/atproto/repo/getRecord.defs.d.ts.map +1 -1
- package/dist/lexicons/com/atproto/repo/getRecord.defs.js +5 -14
- package/dist/lexicons/com/atproto/repo/getRecord.defs.js.map +1 -1
- package/dist/lexicons/com/atproto/repo/getRecord.js +2 -2
- package/dist/lexicons/com/atproto/repo/getRecord.js.map +1 -1
- package/dist/lexicons/com/atproto/repo/listRecords.d.ts +2 -2
- package/dist/lexicons/com/atproto/repo/listRecords.d.ts.map +1 -1
- package/dist/lexicons/com/atproto/repo/listRecords.defs.d.ts +5 -5
- package/dist/lexicons/com/atproto/repo/listRecords.defs.d.ts.map +1 -1
- package/dist/lexicons/com/atproto/repo/listRecords.defs.js +5 -22
- package/dist/lexicons/com/atproto/repo/listRecords.defs.js.map +1 -1
- package/dist/lexicons/com/atproto/repo/listRecords.js +2 -2
- package/dist/lexicons/com/atproto/repo/listRecords.js.map +1 -1
- package/dist/lexicons/com/atproto/repo/putRecord.d.ts +2 -2
- package/dist/lexicons/com/atproto/repo/putRecord.d.ts.map +1 -1
- package/dist/lexicons/com/atproto/repo/putRecord.defs.d.ts +2 -2
- package/dist/lexicons/com/atproto/repo/putRecord.defs.d.ts.map +1 -1
- package/dist/lexicons/com/atproto/repo/putRecord.defs.js +6 -25
- package/dist/lexicons/com/atproto/repo/putRecord.defs.js.map +1 -1
- package/dist/lexicons/com/atproto/repo/putRecord.js +2 -2
- package/dist/lexicons/com/atproto/repo/putRecord.js.map +1 -1
- package/dist/lexicons/com/atproto/repo/uploadBlob.d.ts +2 -2
- package/dist/lexicons/com/atproto/repo/uploadBlob.d.ts.map +1 -1
- package/dist/lexicons/com/atproto/repo/uploadBlob.defs.d.ts +1 -1
- package/dist/lexicons/com/atproto/repo/uploadBlob.defs.d.ts.map +1 -1
- package/dist/lexicons/com/atproto/repo/uploadBlob.defs.js +4 -6
- package/dist/lexicons/com/atproto/repo/uploadBlob.defs.js.map +1 -1
- package/dist/lexicons/com/atproto/repo/uploadBlob.js +2 -2
- package/dist/lexicons/com/atproto/repo/uploadBlob.js.map +1 -1
- package/dist/lexicons/com/atproto/repo.d.ts +8 -8
- package/dist/lexicons/com/atproto/repo.d.ts.map +1 -1
- package/dist/lexicons/com/atproto/repo.js +8 -8
- package/dist/lexicons/com/atproto/repo.js.map +1 -1
- package/dist/lexicons/com/atproto/sync/getBlob.d.ts +2 -2
- package/dist/lexicons/com/atproto/sync/getBlob.d.ts.map +1 -1
- package/dist/lexicons/com/atproto/sync/getBlob.defs.d.ts +1 -1
- package/dist/lexicons/com/atproto/sync/getBlob.defs.d.ts.map +1 -1
- package/dist/lexicons/com/atproto/sync/getBlob.defs.js +5 -14
- package/dist/lexicons/com/atproto/sync/getBlob.defs.js.map +1 -1
- package/dist/lexicons/com/atproto/sync/getBlob.js +2 -2
- package/dist/lexicons/com/atproto/sync/getBlob.js.map +1 -1
- package/dist/lexicons/com/atproto/sync.d.ts +1 -1
- package/dist/lexicons/com/atproto/sync.d.ts.map +1 -1
- package/dist/lexicons/com/atproto/sync.js +1 -1
- package/dist/lexicons/com/atproto/sync.js.map +1 -1
- package/dist/lexicons/com/atproto.d.ts +2 -2
- package/dist/lexicons/com/atproto.d.ts.map +1 -1
- package/dist/lexicons/com/atproto.js +2 -2
- package/dist/lexicons/com/atproto.js.map +1 -1
- package/dist/lexicons/com.d.ts +1 -1
- package/dist/lexicons/com.d.ts.map +1 -1
- package/dist/lexicons/com.js +1 -1
- package/dist/lexicons/com.js.map +1 -1
- package/dist/lexicons/index.d.ts +1 -1
- package/dist/lexicons/index.d.ts.map +1 -1
- package/dist/lexicons/index.js +1 -1
- package/dist/lexicons/index.js.map +1 -1
- package/dist/write-operation-builder.d.ts +1 -1
- package/dist/write-operation-builder.d.ts.map +1 -1
- package/dist/write-operation-builder.js +4 -4
- package/dist/write-operation-builder.js.map +1 -1
- package/package.json +16 -9
- package/dist/agent.d.ts +0 -127
- package/dist/agent.d.ts.map +0 -1
- package/dist/agent.js +0 -41
- package/dist/agent.js.map +0 -1
- package/dist/errors.d.ts +0 -277
- package/dist/errors.d.ts.map +0 -1
- package/dist/errors.js +0 -388
- package/dist/errors.js.map +0 -1
- package/dist/response.d.ts +0 -124
- package/dist/response.d.ts.map +0 -1
- package/dist/response.js +0 -178
- package/dist/response.js.map +0 -1
- package/dist/types.d.ts +0 -54
- package/dist/types.d.ts.map +0 -1
- package/dist/types.js +0 -16
- package/dist/types.js.map +0 -1
- package/dist/util.d.ts +0 -90
- package/dist/util.d.ts.map +0 -1
- package/dist/util.js +0 -210
- package/dist/util.js.map +0 -1
- package/dist/www-authenticate.d.ts +0 -35
- package/dist/www-authenticate.d.ts.map +0 -1
- package/dist/www-authenticate.js +0 -54
- package/dist/www-authenticate.js.map +0 -1
- package/dist/xrpc.d.ts +0 -194
- package/dist/xrpc.d.ts.map +0 -1
- package/dist/xrpc.js +0 -235
- package/dist/xrpc.js.map +0 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"atproto.js","sourceRoot":"","sources":["../../../src/lexicons/com/atproto.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,IAAI,MAAM,mBAAmB,
|
|
1
|
+
{"version":3,"file":"atproto.js","sourceRoot":"","sources":["../../../src/lexicons/com/atproto.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,IAAI,MAAM,mBAAmB,CAAC;AAC1C,OAAO,KAAK,IAAI,MAAM,mBAAmB,CAAC","sourcesContent":["/*\n * THIS FILE WAS GENERATED BY \"@atproto/lex\". DO NOT EDIT.\n */\n\nexport * as repo from \"./atproto/repo.js\";\nexport * as sync from \"./atproto/sync.js\";\n"]}
|
package/dist/lexicons/com.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export * as atproto from
|
|
1
|
+
export * as atproto from "./com/atproto.js";
|
|
2
2
|
//# sourceMappingURL=com.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"com.d.ts","sourceRoot":"","sources":["../../src/lexicons/com.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,OAAO,MAAM,kBAAkB,
|
|
1
|
+
{"version":3,"file":"com.d.ts","sourceRoot":"","sources":["../../src/lexicons/com.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,OAAO,MAAM,kBAAkB,CAAC"}
|
package/dist/lexicons/com.js
CHANGED
package/dist/lexicons/com.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"com.js","sourceRoot":"","sources":["../../src/lexicons/com.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,OAAO,MAAM,kBAAkB,
|
|
1
|
+
{"version":3,"file":"com.js","sourceRoot":"","sources":["../../src/lexicons/com.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,OAAO,MAAM,kBAAkB,CAAC","sourcesContent":["/*\n * THIS FILE WAS GENERATED BY \"@atproto/lex\". DO NOT EDIT.\n */\n\nexport * as atproto from \"./com/atproto.js\";\n"]}
|
package/dist/lexicons/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export * as com from
|
|
1
|
+
export * as com from "./com.js";
|
|
2
2
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/lexicons/index.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,GAAG,MAAM,UAAU,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/lexicons/index.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,GAAG,MAAM,UAAU,CAAC"}
|
package/dist/lexicons/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/lexicons/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,GAAG,MAAM,UAAU,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/lexicons/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,GAAG,MAAM,UAAU,CAAC","sourcesContent":["/*\n * THIS FILE WAS GENERATED BY \"@atproto/lex\". DO NOT EDIT.\n */\n\nexport * as com from \"./com.js\";\n"]}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
+
import { type RecordKeyOptions } from '@atproto/lex-rpc';
|
|
1
2
|
import { type $Typed, type InferInput, type Main, type RecordSchema, type Restricted } from '@atproto/lex-schema';
|
|
2
3
|
import { com } from './lexicons/index.js';
|
|
3
|
-
import type { RecordKeyOptions } from './util.js';
|
|
4
4
|
export type WriteOperation = $Typed<com.atproto.repo.applyWrites.Create> | $Typed<com.atproto.repo.applyWrites.Update> | $Typed<com.atproto.repo.applyWrites.Delete>;
|
|
5
5
|
export type WriteOperationCreateOptions<T extends RecordSchema> = RecordKeyOptions<T, 'tid' | 'any'>;
|
|
6
6
|
export type WriteOperationUpdateOptions<T extends RecordSchema> = RecordKeyOptions<T>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"write-operation-builder.d.ts","sourceRoot":"","sources":["../src/write-operation-builder.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,MAAM,EACX,KAAK,UAAU,EACf,KAAK,IAAI,EACT,KAAK,YAAY,EACjB,KAAK,UAAU,EAEhB,MAAM,qBAAqB,CAAA;AAC5B,OAAO,EAAE,GAAG,EAAE,MAAM,qBAAqB,CAAA;
|
|
1
|
+
{"version":3,"file":"write-operation-builder.d.ts","sourceRoot":"","sources":["../src/write-operation-builder.ts"],"names":[],"mappings":"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"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
+
import { rkeyFor, rkeyForCreate } from '@atproto/lex-rpc';
|
|
1
2
|
import { getMain, } from '@atproto/lex-schema';
|
|
2
3
|
import { com } from './lexicons/index.js';
|
|
3
|
-
import { getDefaultRecordKey, getLiteralRecordKey } from './util.js';
|
|
4
4
|
export class WriteOperationHelper {
|
|
5
5
|
constructor() { }
|
|
6
6
|
create(ns, input, options = {}) {
|
|
@@ -9,7 +9,7 @@ export class WriteOperationHelper {
|
|
|
9
9
|
return com.atproto.repo.applyWrites.create.$build({
|
|
10
10
|
collection: schema.$type,
|
|
11
11
|
value,
|
|
12
|
-
rkey: options?.rkey
|
|
12
|
+
rkey: rkeyForCreate(schema, options?.rkey),
|
|
13
13
|
});
|
|
14
14
|
}
|
|
15
15
|
update(ns, input, options = {}) {
|
|
@@ -18,14 +18,14 @@ export class WriteOperationHelper {
|
|
|
18
18
|
return com.atproto.repo.applyWrites.update.$build({
|
|
19
19
|
collection: schema.$type,
|
|
20
20
|
value,
|
|
21
|
-
rkey: options?.rkey
|
|
21
|
+
rkey: rkeyFor(schema, options?.rkey),
|
|
22
22
|
});
|
|
23
23
|
}
|
|
24
24
|
delete(ns, options = {}) {
|
|
25
25
|
const schema = getMain(ns);
|
|
26
26
|
return com.atproto.repo.applyWrites.delete.$build({
|
|
27
27
|
collection: schema.$type,
|
|
28
|
-
rkey: options?.rkey
|
|
28
|
+
rkey: rkeyFor(schema, options?.rkey),
|
|
29
29
|
});
|
|
30
30
|
}
|
|
31
31
|
static build(factory) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"write-operation-builder.js","sourceRoot":"","sources":["../src/write-operation-builder.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"write-operation-builder.js","sourceRoot":"","sources":["../src/write-operation-builder.ts"],"names":[],"mappings":"AAAA,OAAO,EAAyB,OAAO,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAA;AAChF,OAAO,EAML,OAAO,GACR,MAAM,qBAAqB,CAAA;AAC5B,OAAO,EAAE,GAAG,EAAE,MAAM,qBAAqB,CAAA;AAoBzC,MAAM,OAAO,oBAAoB;IAC/B,gBAAuB,CAAC;IAaxB,MAAM,CACJ,EAAW,EACX,KAAmC,EACnC,OAAO,GAAmC,EAAoC;QAE9E,MAAM,MAAM,GAAM,OAAO,CAAC,EAAE,CAAC,CAAA;QAC7B,MAAM,KAAK,GAAG,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,CAAA;QACjC,OAAO,GAAG,CAAC,OAAO,CAAC,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC,MAAM,CAAC;YAChD,UAAU,EAAE,MAAM,CAAC,KAAK;YACxB,KAAK;YACL,IAAI,EAAE,aAAa,CAAC,MAAM,EAAE,OAAO,EAAE,IAAI,CAAC;SAC3C,CAAC,CAAA;IACJ,CAAC;IAaD,MAAM,CACJ,EAAW,EACX,KAAmC,EACnC,OAAO,GAAmC,EAAoC;QAE9E,MAAM,MAAM,GAAM,OAAO,CAAC,EAAE,CAAC,CAAA;QAC7B,MAAM,KAAK,GAAG,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,CAAA;QACjC,OAAO,GAAG,CAAC,OAAO,CAAC,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC,MAAM,CAAC;YAChD,UAAU,EAAE,MAAM,CAAC,KAAK;YACxB,KAAK;YACL,IAAI,EAAE,OAAO,CAAC,MAAM,EAAE,OAAO,EAAE,IAAI,CAAC;SACrC,CAAC,CAAA;IACJ,CAAC;IAWD,MAAM,CACJ,EAAW,EACX,OAAO,GAAmC,EAAoC;QAE9E,MAAM,MAAM,GAAM,OAAO,CAAC,EAAE,CAAC,CAAA;QAC7B,OAAO,GAAG,CAAC,OAAO,CAAC,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC,MAAM,CAAC;YAChD,UAAU,EAAE,MAAM,CAAC,KAAK;YACxB,IAAI,EAAE,OAAO,CAAC,MAAM,EAAE,OAAO,EAAE,IAAI,CAAC;SACrC,CAAC,CAAA;IACJ,CAAC;IAED,MAAM,CAAC,KAAK,CAAC,OAA+B;QAC1C,OAAO,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI,oBAAoB,EAAE,CAAC,CAAC,CAAA;IACxD,CAAC;CACF","sourcesContent":["import { type RecordKeyOptions, rkeyFor, rkeyForCreate } from '@atproto/lex-rpc'\nimport {\n type $Typed,\n type InferInput,\n type Main,\n type RecordSchema,\n type Restricted,\n getMain,\n} from '@atproto/lex-schema'\nimport { com } from './lexicons/index.js'\n\nexport type WriteOperation =\n | $Typed<com.atproto.repo.applyWrites.Create>\n | $Typed<com.atproto.repo.applyWrites.Update>\n | $Typed<com.atproto.repo.applyWrites.Delete>\n\nexport type WriteOperationCreateOptions<T extends RecordSchema> =\n RecordKeyOptions<T, 'tid' | 'any'>\n\nexport type WriteOperationUpdateOptions<T extends RecordSchema> =\n RecordKeyOptions<T>\n\nexport type WriteOperationDeleteOptions<T extends RecordSchema> =\n RecordKeyOptions<T>\n\nexport type WriteOperationsFactory = (\n helper: WriteOperationHelper,\n) => Iterable<WriteOperation>\n\nexport class WriteOperationHelper {\n private constructor() {}\n\n create<const T extends RecordSchema>(\n ns: NonNullable<unknown> extends WriteOperationCreateOptions<T>\n ? Main<T>\n : Restricted<'This record type requires an \"options\" argument'>,\n input: Omit<InferInput<T>, '$type'>,\n ): $Typed<com.atproto.repo.applyWrites.Create>\n create<const T extends RecordSchema>(\n ns: Main<T>,\n input: Omit<InferInput<T>, '$type'>,\n options: WriteOperationCreateOptions<T>,\n ): $Typed<com.atproto.repo.applyWrites.Create>\n create<const T extends RecordSchema>(\n ns: Main<T>,\n input: Omit<InferInput<T>, '$type'>,\n options: WriteOperationCreateOptions<T> = {} as WriteOperationCreateOptions<T>,\n ): $Typed<com.atproto.repo.applyWrites.Create> {\n const schema: T = getMain(ns)\n const value = schema.build(input)\n return com.atproto.repo.applyWrites.create.$build({\n collection: schema.$type,\n value,\n rkey: rkeyForCreate(schema, options?.rkey),\n })\n }\n\n update<const T extends RecordSchema>(\n ns: NonNullable<unknown> extends WriteOperationUpdateOptions<T>\n ? Main<T>\n : Restricted<'This record type requires an \"options\" argument'>,\n input: Omit<InferInput<T>, '$type'>,\n ): $Typed<com.atproto.repo.applyWrites.Update>\n update<const T extends RecordSchema>(\n ns: Main<T>,\n input: Omit<InferInput<T>, '$type'>,\n options: WriteOperationUpdateOptions<T>,\n ): $Typed<com.atproto.repo.applyWrites.Update>\n update<const T extends RecordSchema>(\n ns: Main<T>,\n input: Omit<InferInput<T>, '$type'>,\n options: WriteOperationUpdateOptions<T> = {} as WriteOperationUpdateOptions<T>,\n ): $Typed<com.atproto.repo.applyWrites.Update> {\n const schema: T = getMain(ns)\n const value = schema.build(input)\n return com.atproto.repo.applyWrites.update.$build({\n collection: schema.$type,\n value,\n rkey: rkeyFor(schema, options?.rkey),\n })\n }\n\n delete<const T extends RecordSchema>(\n ns: NonNullable<unknown> extends WriteOperationDeleteOptions<T>\n ? Main<T>\n : Restricted<'This record type requires an \"options\" argument'>,\n ): $Typed<com.atproto.repo.applyWrites.Delete>\n delete<const T extends RecordSchema>(\n ns: Main<T>,\n options: WriteOperationDeleteOptions<T>,\n ): $Typed<com.atproto.repo.applyWrites.Delete>\n delete<const T extends RecordSchema>(\n ns: Main<T>,\n options: WriteOperationDeleteOptions<T> = {} as WriteOperationDeleteOptions<T>,\n ): $Typed<com.atproto.repo.applyWrites.Delete> {\n const schema: T = getMain(ns)\n return com.atproto.repo.applyWrites.delete.$build({\n collection: schema.$type,\n rkey: rkeyFor(schema, options?.rkey),\n })\n }\n\n static build(factory: WriteOperationsFactory): WriteOperation[] {\n return Array.from(factory(new WriteOperationHelper()))\n }\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@atproto/lex-client",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.2",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"description": "HTTP client for interacting with Lexicon based APIs",
|
|
6
6
|
"keywords": [
|
|
@@ -32,18 +32,25 @@
|
|
|
32
32
|
"node": ">=22"
|
|
33
33
|
},
|
|
34
34
|
"dependencies": {
|
|
35
|
-
"
|
|
36
|
-
"@atproto/lex-
|
|
37
|
-
"@atproto/lex-schema": "^0.2.
|
|
38
|
-
"
|
|
35
|
+
"@atproto/lex-data": "^0.1.10",
|
|
36
|
+
"@atproto/lex-rpc": "^0.0.3",
|
|
37
|
+
"@atproto/lex-schema": "^0.2.11",
|
|
38
|
+
"tslib": "^2.8.1"
|
|
39
39
|
},
|
|
40
40
|
"devDependencies": {
|
|
41
|
-
"
|
|
42
|
-
"@atproto/lex-
|
|
43
|
-
"@atproto/lex-
|
|
41
|
+
"@atproto/lex-cbor": "^0.1.9",
|
|
42
|
+
"@atproto/lex-json": "^0.1.9",
|
|
43
|
+
"@atproto/lex-builder": "^0.1.20",
|
|
44
|
+
"@atproto/lex-installer": "^0.2.7",
|
|
45
|
+
"vitest": "^4.1.10"
|
|
44
46
|
},
|
|
45
47
|
"scripts": {
|
|
46
|
-
"
|
|
48
|
+
"lex:build": "lex-builder build --out ./src/lexicons --clear --lib @atproto/lex-schema --index-file --default-export",
|
|
49
|
+
"lex:install": "lex-installer install",
|
|
50
|
+
"postlex:install": "pnpm run lex:build",
|
|
51
|
+
"lex:update": "lex-installer update",
|
|
52
|
+
"postlex:update": "pnpm run lex:build",
|
|
53
|
+
"codegen:lex": "pnpm run lex:install --ci",
|
|
47
54
|
"prebuild": "pnpm run '/^(codegen:.+)$/'",
|
|
48
55
|
"build": "tsc --build tsconfig.build.json",
|
|
49
56
|
"test": "vitest run"
|
package/dist/agent.d.ts
DELETED
|
@@ -1,127 +0,0 @@
|
|
|
1
|
-
import type { DidString } from '@atproto/lex-schema';
|
|
2
|
-
/**
|
|
3
|
-
* A function that performs HTTP requests towards a service endpoint.
|
|
4
|
-
*
|
|
5
|
-
* The handler is responsible for adding the origin (protocol, hostname, and
|
|
6
|
-
* port) to the provided path, typically based on authentication or service
|
|
7
|
-
* configuration. The handler can also be responsible for adding any necessary
|
|
8
|
-
* headers, such as authorization tokens.
|
|
9
|
-
*
|
|
10
|
-
* @param path - The URL path (pathname + query parameters) without the origin
|
|
11
|
-
* @param init - Standard fetch RequestInit options
|
|
12
|
-
* @returns A Promise resolving to the HTTP Response
|
|
13
|
-
*/
|
|
14
|
-
export type FetchHandler = (
|
|
15
|
-
/**
|
|
16
|
-
* The URL (pathname + query parameters) to make the request to, without the
|
|
17
|
-
* origin. The origin (protocol, hostname, and port) must be added by this
|
|
18
|
-
* {@link FetchHandler}, typically based on authentication or other factors.
|
|
19
|
-
*/
|
|
20
|
-
path: `/${string}`, init: RequestInit) => Promise<Response>;
|
|
21
|
-
/**
|
|
22
|
-
* Core interface for making XRPC requests towards a specific service.
|
|
23
|
-
*
|
|
24
|
-
* An {@link Agent} encapsulates an identity and request handling for AT
|
|
25
|
-
* Protocol operations. Agents will typically handle authentication, service URL
|
|
26
|
-
* resolution, and other request configuration, allowing client code to make
|
|
27
|
-
* requests without needing to manage these details directly. The key component
|
|
28
|
-
* of an Agent is the {@link FetchHandler}, which is responsible for
|
|
29
|
-
* constructing the full request URL and adding any necessary headers or
|
|
30
|
-
* authentication tokens. The Agent's `did` property represents the
|
|
31
|
-
* authenticated user's DID, if available, and can be used for operations that
|
|
32
|
-
* require knowledge of the user's identity (such as creating AT Protocol
|
|
33
|
-
* records).
|
|
34
|
-
*
|
|
35
|
-
* @see {@link buildAgent} for creating (simple) Agent instances.
|
|
36
|
-
*
|
|
37
|
-
* @example
|
|
38
|
-
* ```typescript
|
|
39
|
-
* const agent: Agent = {
|
|
40
|
-
* did: 'did:plc:example123',
|
|
41
|
-
* fetchHandler: async (path, init) => {
|
|
42
|
-
* const url = new URL(path, 'https://bsky.social')
|
|
43
|
-
* return fetch(url, init)
|
|
44
|
-
* }
|
|
45
|
-
* }
|
|
46
|
-
* ```
|
|
47
|
-
*/
|
|
48
|
-
export interface Agent {
|
|
49
|
-
/** The DID of the authenticated user, or `undefined` if unauthenticated. */
|
|
50
|
-
readonly did?: DidString;
|
|
51
|
-
/** The fetch handler used to make HTTP requests. */
|
|
52
|
-
fetchHandler: FetchHandler;
|
|
53
|
-
}
|
|
54
|
-
export declare function isAgent(value: unknown): value is Agent;
|
|
55
|
-
export type AgentConfig = {
|
|
56
|
-
/**
|
|
57
|
-
* The identifier (DID) of the user represented by this agent.
|
|
58
|
-
*/
|
|
59
|
-
did?: DidString;
|
|
60
|
-
/**
|
|
61
|
-
* The service URL to make requests to. This can be a string, URL, or a
|
|
62
|
-
* function that returns a string or URL. This is useful for dynamic URLs,
|
|
63
|
-
* such as a service URL that changes based on authentication.
|
|
64
|
-
*/
|
|
65
|
-
service: string | URL;
|
|
66
|
-
/**
|
|
67
|
-
* Optional headers to include with every request made by this agent, unless
|
|
68
|
-
* overridden by the request-specific headers provided to the fetch handler.
|
|
69
|
-
*/
|
|
70
|
-
headers?: HeadersInit;
|
|
71
|
-
/**
|
|
72
|
-
* Bring your own fetch implementation. Typically useful for testing, logging,
|
|
73
|
-
* mocking, or adding retries, session management, signatures, proof of
|
|
74
|
-
* possession (DPoP), SSRF protection, etc. Defaults to the global `fetch`
|
|
75
|
-
* function.
|
|
76
|
-
*/
|
|
77
|
-
fetch?: typeof globalThis.fetch;
|
|
78
|
-
fetchHandler?: never;
|
|
79
|
-
};
|
|
80
|
-
/**
|
|
81
|
-
* Options for creating an Agent.
|
|
82
|
-
*
|
|
83
|
-
* Can be a full {@link AgentConfig} object, or a simple service URL string/{@link URL}.
|
|
84
|
-
*/
|
|
85
|
-
export type AgentOptions = AgentConfig | FetchHandler | string | URL;
|
|
86
|
-
/**
|
|
87
|
-
* Creates an {@link Agent} from various input types.
|
|
88
|
-
*
|
|
89
|
-
* This factory function accepts an existing Agent (returned as-is), a service
|
|
90
|
-
* URL, or a full configuration object. It handles the common case of creating
|
|
91
|
-
* an unauthenticated agent from just a service URL.
|
|
92
|
-
*
|
|
93
|
-
* @param options - Agent instance, configuration object, or service URL
|
|
94
|
-
* @returns A configured Agent ready for making requests
|
|
95
|
-
* @throws {TypeError} If fetch() is not available in the environment
|
|
96
|
-
*
|
|
97
|
-
* @example // From URL string
|
|
98
|
-
* ```typescript
|
|
99
|
-
* const agent = buildAgent('https://public.api.bsky.app')
|
|
100
|
-
* ```
|
|
101
|
-
*
|
|
102
|
-
* @example // From configuration
|
|
103
|
-
* ```typescript
|
|
104
|
-
* const agent = buildAgent({
|
|
105
|
-
* did: 'did:plc:example',
|
|
106
|
-
* service: 'https://bsky.social',
|
|
107
|
-
* headers: { 'Authorization': 'Bearer ...' }
|
|
108
|
-
* })
|
|
109
|
-
* ```
|
|
110
|
-
*
|
|
111
|
-
* @example // From existing Agent
|
|
112
|
-
* ```typescript
|
|
113
|
-
* const existingAgent: Agent = { ... }
|
|
114
|
-
* const agent = buildAgent(existingAgent)
|
|
115
|
-
* ```
|
|
116
|
-
*
|
|
117
|
-
* @example // From agent like object with fetchHandler
|
|
118
|
-
* ```typescript
|
|
119
|
-
* const agent = buildAgent({
|
|
120
|
-
* did: 'did:plc:example',
|
|
121
|
-
* fetchHandler: async (path, init) => { ... }
|
|
122
|
-
* // @NOTE "headers" and "service" are not allowed when providing a fetchHandler, because the input will returned "as is" if it matched the "Agent" interface.
|
|
123
|
-
* })
|
|
124
|
-
* ```
|
|
125
|
-
*/
|
|
126
|
-
export declare function buildAgent<O extends Agent | AgentOptions>(options: O): O extends Agent ? O : Agent;
|
|
127
|
-
//# sourceMappingURL=agent.d.ts.map
|
package/dist/agent.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"agent.d.ts","sourceRoot":"","sources":["../src/agent.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAA;AAGpD;;;;;;;;;;;GAWG;AACH,MAAM,MAAM,YAAY,GAAG;AACzB;;;;GAIG;AACH,IAAI,EAAE,IAAI,MAAM,EAAE,EAClB,IAAI,EAAE,WAAW,KACd,OAAO,CAAC,QAAQ,CAAC,CAAA;AAEtB;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,MAAM,WAAW,KAAK;IACpB,4EAA4E;IAC5E,QAAQ,CAAC,GAAG,CAAC,EAAE,SAAS,CAAA;IACxB,oDAAoD;IACpD,YAAY,EAAE,YAAY,CAAA;CAC3B;AAED,wBAAgB,OAAO,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,KAAK,CAUtD;AAED,MAAM,MAAM,WAAW,GAAG;IACxB;;OAEG;IACH,GAAG,CAAC,EAAE,SAAS,CAAA;IAEf;;;;OAIG;IACH,OAAO,EAAE,MAAM,GAAG,GAAG,CAAA;IAErB;;;OAGG;IACH,OAAO,CAAC,EAAE,WAAW,CAAA;IAErB;;;;;OAKG;IACH,KAAK,CAAC,EAAE,OAAO,UAAU,CAAC,KAAK,CAAA;IAK/B,YAAY,CAAC,EAAE,KAAK,CAAA;CACrB,CAAA;AAED;;;;GAIG;AACH,MAAM,MAAM,YAAY,GAAG,WAAW,GAAG,YAAY,GAAG,MAAM,GAAG,GAAG,CAAA;AAEpE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuCG;AACH,wBAAgB,UAAU,CAAC,CAAC,SAAS,KAAK,GAAG,YAAY,EACvD,OAAO,EAAE,CAAC,GACT,CAAC,SAAS,KAAK,GAAG,CAAC,GAAG,KAAK,CAAA"}
|
package/dist/agent.js
DELETED
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
import { mergeHeaders } from './util.js';
|
|
2
|
-
export function isAgent(value) {
|
|
3
|
-
return (typeof value === 'object' &&
|
|
4
|
-
value !== null &&
|
|
5
|
-
'fetchHandler' in value &&
|
|
6
|
-
typeof value.fetchHandler === 'function' &&
|
|
7
|
-
(!('did' in value) ||
|
|
8
|
-
value.did === undefined ||
|
|
9
|
-
typeof value.did === 'string'));
|
|
10
|
-
}
|
|
11
|
-
export function buildAgent(options) {
|
|
12
|
-
if (typeof options === 'function') {
|
|
13
|
-
return { did: undefined, fetchHandler: options };
|
|
14
|
-
}
|
|
15
|
-
const config = typeof options === 'string' || options instanceof URL
|
|
16
|
-
? { did: undefined, service: options }
|
|
17
|
-
: options;
|
|
18
|
-
// Prevent mismatch between `fetchHandler` and `headers` in the config, since
|
|
19
|
-
// `headers` would be ignored if `fetchHandler` is provided.
|
|
20
|
-
if ('fetchHandler' in config) {
|
|
21
|
-
if (isAgent(config))
|
|
22
|
-
return config;
|
|
23
|
-
throw new TypeError('fetchHandler must not be provided when using AgentConfig');
|
|
24
|
-
}
|
|
25
|
-
const { did, service, fetch = globalThis.fetch, headers: defaultHeaders, } = config;
|
|
26
|
-
if (typeof fetch !== 'function') {
|
|
27
|
-
throw new TypeError('fetch() is not available in this environment');
|
|
28
|
-
}
|
|
29
|
-
return {
|
|
30
|
-
get did() {
|
|
31
|
-
return did;
|
|
32
|
-
},
|
|
33
|
-
async fetchHandler(path, init) {
|
|
34
|
-
const headers = defaultHeaders != null && init.headers != null
|
|
35
|
-
? mergeHeaders(defaultHeaders, init.headers)
|
|
36
|
-
: defaultHeaders || init.headers;
|
|
37
|
-
return fetch(new URL(path, service), headers !== init.headers ? { ...init, headers } : init);
|
|
38
|
-
},
|
|
39
|
-
};
|
|
40
|
-
}
|
|
41
|
-
//# sourceMappingURL=agent.js.map
|
package/dist/agent.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"agent.js","sourceRoot":"","sources":["../src/agent.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAE,MAAM,WAAW,CAAA;AA0DxC,MAAM,UAAU,OAAO,CAAC,KAAc;IACpC,OAAO,CACL,OAAO,KAAK,KAAK,QAAQ;QACzB,KAAK,KAAK,IAAI;QACd,cAAc,IAAI,KAAK;QACvB,OAAO,KAAK,CAAC,YAAY,KAAK,UAAU;QACxC,CAAC,CAAC,CAAC,KAAK,IAAI,KAAK,CAAC;YAChB,KAAK,CAAC,GAAG,KAAK,SAAS;YACvB,OAAO,KAAK,CAAC,GAAG,KAAK,QAAQ,CAAC,CACjC,CAAA;AACH,CAAC;AAqFD,MAAM,UAAU,UAAU,CAAC,OAA6B;IACtD,IAAI,OAAO,OAAO,KAAK,UAAU,EAAE,CAAC;QAClC,OAAO,EAAE,GAAG,EAAE,SAAS,EAAE,YAAY,EAAE,OAAO,EAAE,CAAA;IAClD,CAAC;IAED,MAAM,MAAM,GACV,OAAO,OAAO,KAAK,QAAQ,IAAI,OAAO,YAAY,GAAG;QACnD,CAAC,CAAC,EAAE,GAAG,EAAE,SAAS,EAAE,OAAO,EAAE,OAAO,EAAE;QACtC,CAAC,CAAC,OAAO,CAAA;IAEb,6EAA6E;IAC7E,4DAA4D;IAC5D,IAAI,cAAc,IAAI,MAAM,EAAE,CAAC;QAC7B,IAAI,OAAO,CAAC,MAAM,CAAC;YAAE,OAAO,MAAM,CAAA;QAElC,MAAM,IAAI,SAAS,CACjB,0DAA0D,CAC3D,CAAA;IACH,CAAC;IAED,MAAM,EACJ,GAAG,EACH,OAAO,EACP,KAAK,GAAG,UAAU,CAAC,KAAK,EACxB,OAAO,EAAE,cAAc,GACxB,GAAG,MAAM,CAAA;IAEV,IAAI,OAAO,KAAK,KAAK,UAAU,EAAE,CAAC;QAChC,MAAM,IAAI,SAAS,CAAC,8CAA8C,CAAC,CAAA;IACrE,CAAC;IAED,OAAO;QACL,IAAI,GAAG;YACL,OAAO,GAAG,CAAA;QACZ,CAAC;QAED,KAAK,CAAC,YAAY,CAAC,IAAI,EAAE,IAAI;YAC3B,MAAM,OAAO,GACX,cAAc,IAAI,IAAI,IAAI,IAAI,CAAC,OAAO,IAAI,IAAI;gBAC5C,CAAC,CAAC,YAAY,CAAC,cAAc,EAAE,IAAI,CAAC,OAAO,CAAC;gBAC5C,CAAC,CAAC,cAAc,IAAI,IAAI,CAAC,OAAO,CAAA;YAEpC,OAAO,KAAK,CACV,IAAI,GAAG,CAAC,IAAI,EAAE,OAAO,CAAC,EACtB,OAAO,KAAK,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,GAAG,IAAI,EAAE,OAAO,EAAE,CAAC,CAAC,CAAC,IAAI,CACvD,CAAA;QACH,CAAC;KACF,CAAA;AACH,CAAC","sourcesContent":["import type { DidString } from '@atproto/lex-schema'\nimport { mergeHeaders } from './util.js'\n\n/**\n * A function that performs HTTP requests towards a service endpoint.\n *\n * The handler is responsible for adding the origin (protocol, hostname, and\n * port) to the provided path, typically based on authentication or service\n * configuration. The handler can also be responsible for adding any necessary\n * headers, such as authorization tokens.\n *\n * @param path - The URL path (pathname + query parameters) without the origin\n * @param init - Standard fetch RequestInit options\n * @returns A Promise resolving to the HTTP Response\n */\nexport type FetchHandler = (\n /**\n * The URL (pathname + query parameters) to make the request to, without the\n * origin. The origin (protocol, hostname, and port) must be added by this\n * {@link FetchHandler}, typically based on authentication or other factors.\n */\n path: `/${string}`,\n init: RequestInit,\n) => Promise<Response>\n\n/**\n * Core interface for making XRPC requests towards a specific service.\n *\n * An {@link Agent} encapsulates an identity and request handling for AT\n * Protocol operations. Agents will typically handle authentication, service URL\n * resolution, and other request configuration, allowing client code to make\n * requests without needing to manage these details directly. The key component\n * of an Agent is the {@link FetchHandler}, which is responsible for\n * constructing the full request URL and adding any necessary headers or\n * authentication tokens. The Agent's `did` property represents the\n * authenticated user's DID, if available, and can be used for operations that\n * require knowledge of the user's identity (such as creating AT Protocol\n * records).\n *\n * @see {@link buildAgent} for creating (simple) Agent instances.\n *\n * @example\n * ```typescript\n * const agent: Agent = {\n * did: 'did:plc:example123',\n * fetchHandler: async (path, init) => {\n * const url = new URL(path, 'https://bsky.social')\n * return fetch(url, init)\n * }\n * }\n * ```\n */\nexport interface Agent {\n /** The DID of the authenticated user, or `undefined` if unauthenticated. */\n readonly did?: DidString\n /** The fetch handler used to make HTTP requests. */\n fetchHandler: FetchHandler\n}\n\nexport function isAgent(value: unknown): value is Agent {\n return (\n typeof value === 'object' &&\n value !== null &&\n 'fetchHandler' in value &&\n typeof value.fetchHandler === 'function' &&\n (!('did' in value) ||\n value.did === undefined ||\n typeof value.did === 'string')\n )\n}\n\nexport type AgentConfig = {\n /**\n * The identifier (DID) of the user represented by this agent.\n */\n did?: DidString\n\n /**\n * The service URL to make requests to. This can be a string, URL, or a\n * function that returns a string or URL. This is useful for dynamic URLs,\n * such as a service URL that changes based on authentication.\n */\n service: string | URL\n\n /**\n * Optional headers to include with every request made by this agent, unless\n * overridden by the request-specific headers provided to the fetch handler.\n */\n headers?: HeadersInit\n\n /**\n * Bring your own fetch implementation. Typically useful for testing, logging,\n * mocking, or adding retries, session management, signatures, proof of\n * possession (DPoP), SSRF protection, etc. Defaults to the global `fetch`\n * function.\n */\n fetch?: typeof globalThis.fetch\n\n // Because this type get unioned with `Agent`, we need to prevent TypeScript\n // from accepting `headers` when `fetchHandler` is provided, since `headers`\n // would not be used (while the user might expect it to be).\n fetchHandler?: never\n}\n\n/**\n * Options for creating an Agent.\n *\n * Can be a full {@link AgentConfig} object, or a simple service URL string/{@link URL}.\n */\nexport type AgentOptions = AgentConfig | FetchHandler | string | URL\n\n/**\n * Creates an {@link Agent} from various input types.\n *\n * This factory function accepts an existing Agent (returned as-is), a service\n * URL, or a full configuration object. It handles the common case of creating\n * an unauthenticated agent from just a service URL.\n *\n * @param options - Agent instance, configuration object, or service URL\n * @returns A configured Agent ready for making requests\n * @throws {TypeError} If fetch() is not available in the environment\n *\n * @example // From URL string\n * ```typescript\n * const agent = buildAgent('https://public.api.bsky.app')\n * ```\n *\n * @example // From configuration\n * ```typescript\n * const agent = buildAgent({\n * did: 'did:plc:example',\n * service: 'https://bsky.social',\n * headers: { 'Authorization': 'Bearer ...' }\n * })\n * ```\n *\n * @example // From existing Agent\n * ```typescript\n * const existingAgent: Agent = { ... }\n * const agent = buildAgent(existingAgent)\n * ```\n *\n * @example // From agent like object with fetchHandler\n * ```typescript\n * const agent = buildAgent({\n * did: 'did:plc:example',\n * fetchHandler: async (path, init) => { ... }\n * // @NOTE \"headers\" and \"service\" are not allowed when providing a fetchHandler, because the input will returned \"as is\" if it matched the \"Agent\" interface.\n * })\n * ```\n */\nexport function buildAgent<O extends Agent | AgentOptions>(\n options: O,\n): O extends Agent ? O : Agent\nexport function buildAgent(options: Agent | AgentOptions): Agent {\n if (typeof options === 'function') {\n return { did: undefined, fetchHandler: options }\n }\n\n const config: Agent | AgentConfig =\n typeof options === 'string' || options instanceof URL\n ? { did: undefined, service: options }\n : options\n\n // Prevent mismatch between `fetchHandler` and `headers` in the config, since\n // `headers` would be ignored if `fetchHandler` is provided.\n if ('fetchHandler' in config) {\n if (isAgent(config)) return config\n\n throw new TypeError(\n 'fetchHandler must not be provided when using AgentConfig',\n )\n }\n\n const {\n did,\n service,\n fetch = globalThis.fetch,\n headers: defaultHeaders,\n } = config\n\n if (typeof fetch !== 'function') {\n throw new TypeError('fetch() is not available in this environment')\n }\n\n return {\n get did() {\n return did\n },\n\n async fetchHandler(path, init) {\n const headers =\n defaultHeaders != null && init.headers != null\n ? mergeHeaders(defaultHeaders, init.headers)\n : defaultHeaders || init.headers\n\n return fetch(\n new URL(path, service),\n headers !== init.headers ? { ...init, headers } : init,\n )\n },\n }\n}\n"]}
|