@atproto/xrpc 0.0.2 → 0.0.4

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.
@@ -1,5 +1,5 @@
1
1
  import { Lexicons } from '@atproto/lexicon';
2
- import { FetchHandler, CallOptions, QueryParams, XRPCResponse } from './types';
2
+ import { FetchHandler, FetchHandlerResponse, Headers, CallOptions, QueryParams, XRPCResponse } from './types';
3
3
  export declare class Client {
4
4
  fetch: FetchHandler;
5
5
  lex: Lexicons;
@@ -18,3 +18,4 @@ export declare class ServiceClient {
18
18
  unsetHeader(key: string): void;
19
19
  call(methodNsid: string, params?: QueryParams, data?: unknown, opts?: CallOptions): Promise<XRPCResponse>;
20
20
  }
21
+ export declare function defaultFetchHandler(httpUri: string, httpMethod: string, httpHeaders: Headers, httpReqBody: unknown): Promise<FetchHandlerResponse>;
File without changes
package/dist/index.js CHANGED
@@ -1,8 +1,13 @@
1
1
  "use strict";
2
+ var __create = Object.create;
2
3
  var __defProp = Object.defineProperty;
3
4
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
5
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
5
7
  var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __commonJS = (cb, mod2) => function __require() {
9
+ return mod2 || (0, cb[__getOwnPropNames(cb)[0]])((mod2 = { exports: {} }).exports, mod2), mod2.exports;
10
+ };
6
11
  var __export = (target, all) => {
7
12
  for (var name in all)
8
13
  __defProp(target, name, { get: all[name], enumerable: true });
@@ -15,8 +20,61 @@ var __copyProps = (to, from, except, desc) => {
15
20
  }
16
21
  return to;
17
22
  };
23
+ var __toESM = (mod2, isNodeMode, target) => (target = mod2 != null ? __create(__getProtoOf(mod2)) : {}, __copyProps(
24
+ isNodeMode || !mod2 || !mod2.__esModule ? __defProp(target, "default", { value: mod2, enumerable: true }) : target,
25
+ mod2
26
+ ));
18
27
  var __toCommonJS = (mod2) => __copyProps(__defProp({}, "__esModule", { value: true }), mod2);
19
28
 
29
+ // ../../node_modules/iso-datestring-validator/dist/index.js
30
+ var require_dist = __commonJS({
31
+ "../../node_modules/iso-datestring-validator/dist/index.js"(exports) {
32
+ (() => {
33
+ "use strict";
34
+ var e = { d: (t2, r2) => {
35
+ for (var n2 in r2)
36
+ e.o(r2, n2) && !e.o(t2, n2) && Object.defineProperty(t2, n2, { enumerable: true, get: r2[n2] });
37
+ }, o: (e2, t2) => Object.prototype.hasOwnProperty.call(e2, t2), r: (e2) => {
38
+ "undefined" != typeof Symbol && Symbol.toStringTag && Object.defineProperty(e2, Symbol.toStringTag, { value: "Module" }), Object.defineProperty(e2, "__esModule", { value: true });
39
+ } }, t = {};
40
+ function r(e2, t2) {
41
+ return void 0 === t2 && (t2 = "-"), new RegExp("^(?!0{4}" + t2 + "0{2}" + t2 + "0{2})((?=[0-9]{4}" + t2 + "(((0[^2])|1[0-2])|02(?=" + t2 + "(([0-1][0-9])|2[0-8])))" + t2 + "[0-9]{2})|(?=((([13579][26])|([2468][048])|(0[48]))0{2})|([0-9]{2}((((0|[2468])[48])|[2468][048])|([13579][26])))" + t2 + "02" + t2 + "29))([0-9]{4})" + t2 + "(?!((0[469])|11)" + t2 + "31)((0[1,3-9]|1[0-2])|(02(?!" + t2 + "3)))" + t2 + "(0[1-9]|[1-2][0-9]|3[0-1])$").test(e2);
42
+ }
43
+ function n(e2) {
44
+ var t2 = /\D/.exec(e2);
45
+ return t2 ? t2[0] : "";
46
+ }
47
+ function i(e2, t2, r2) {
48
+ void 0 === t2 && (t2 = ":"), void 0 === r2 && (r2 = false);
49
+ var i2 = new RegExp("^([0-1]|2(?=([0-3])|4" + t2 + "00))[0-9]" + t2 + "[0-5][0-9](" + t2 + "([0-5]|6(?=0))[0-9])?(.[0-9]{1,9})?$");
50
+ if (!r2 || !/[Z+\-]/.test(e2))
51
+ return i2.test(e2);
52
+ if (/Z$/.test(e2))
53
+ return i2.test(e2.replace("Z", ""));
54
+ var o2 = e2.includes("+"), a2 = e2.split(/[+-]/), u2 = a2[0], d2 = a2[1];
55
+ return i2.test(u2) && function(e3, t3, r3) {
56
+ return void 0 === r3 && (r3 = ":"), new RegExp(t3 ? "^(0(?!(2" + r3 + "4)|0" + r3 + "3)|1(?=([0-1]|2(?=" + r3 + "[04])|[34](?=" + r3 + "0))))([03469](?=" + r3 + "[03])|[17](?=" + r3 + "0)|2(?=" + r3 + "[04])|5(?=" + r3 + "[034])|8(?=" + r3 + "[04]))" + r3 + "([03](?=0)|4(?=5))[05]$" : "^(0(?=[^0])|1(?=[0-2]))([39](?=" + r3 + "[03])|[0-24-8](?=" + r3 + "00))" + r3 + "[03]0$").test(e3);
57
+ }(d2, o2, n(d2));
58
+ }
59
+ function o(e2) {
60
+ var t2 = e2.split("T"), o2 = t2[0], a2 = t2[1], u2 = r(o2, n(o2));
61
+ if (!a2)
62
+ return false;
63
+ var d2, s = (d2 = a2.match(/([^Z+\-\d])(?=\d+\1)/), Array.isArray(d2) ? d2[0] : "");
64
+ return u2 && i(a2, s, true);
65
+ }
66
+ function a(e2, t2) {
67
+ return void 0 === t2 && (t2 = "-"), new RegExp("^[0-9]{4}" + t2 + "(0(?=[^0])|1(?=[0-2]))[0-9]$").test(e2);
68
+ }
69
+ e.r(t), e.d(t, { isValidDate: () => r, isValidISODateString: () => o, isValidTime: () => i, isValidYearMonth: () => a });
70
+ var u = exports;
71
+ for (var d in t)
72
+ u[d] = t[d];
73
+ t.__esModule && Object.defineProperty(u, "__esModule", { value: true });
74
+ })();
75
+ }
76
+ });
77
+
20
78
  // src/index.ts
21
79
  var src_exports = {};
22
80
  __export(src_exports, {
@@ -29,6 +87,7 @@ __export(src_exports, {
29
87
  XRPCInvalidResponseError: () => XRPCInvalidResponseError,
30
88
  XRPCResponse: () => XRPCResponse,
31
89
  default: () => src_default,
90
+ defaultFetchHandler: () => defaultFetchHandler,
32
91
  errorResponseBody: () => errorResponseBody
33
92
  });
34
93
  module.exports = __toCommonJS(src_exports);
@@ -3186,6 +3245,11 @@ var lexArray = mod.object({
3186
3245
  minLength: mod.number().int().optional(),
3187
3246
  maxLength: mod.number().int().optional()
3188
3247
  });
3248
+ var lexPrimitiveArray = lexArray.merge(
3249
+ mod.object({
3250
+ items: lexPrimitive
3251
+ })
3252
+ );
3189
3253
  var lexToken = mod.object({
3190
3254
  type: mod.literal("token"),
3191
3255
  description: mod.string().optional()
@@ -3200,7 +3264,7 @@ var lexXrpcParameters = mod.object({
3200
3264
  type: mod.literal("params"),
3201
3265
  description: mod.string().optional(),
3202
3266
  required: mod.string().array().optional(),
3203
- properties: mod.record(lexPrimitive)
3267
+ properties: mod.record(mod.union([lexPrimitive, lexPrimitiveArray]))
3204
3268
  });
3205
3269
  var lexXrpcBody = mod.object({
3206
3270
  description: mod.string().optional(),
@@ -3296,6 +3360,7 @@ var LexiconDefNotFoundError = class extends Error {
3296
3360
  };
3297
3361
 
3298
3362
  // ../lexicon/src/validators/primitives.ts
3363
+ var import_iso_datestring_validator = __toESM(require_dist());
3299
3364
  function validate(lexicons, path, def, value) {
3300
3365
  switch (def.type) {
3301
3366
  case "boolean":
@@ -3483,8 +3548,7 @@ function datetime(lexicons, path, def, value) {
3483
3548
  }
3484
3549
  {
3485
3550
  try {
3486
- const date = new Date(Date.parse(value));
3487
- if (value !== date.toISOString()) {
3551
+ if (typeof value !== "string" || !(0, import_iso_datestring_validator.isValidISODateString)(value)) {
3488
3552
  throw new ValidationError(
3489
3553
  `${path} must be an iso8601 formatted datetime`
3490
3554
  );
@@ -3572,7 +3636,6 @@ function validate2(lexicons, path, def, value) {
3572
3636
  }
3573
3637
  }
3574
3638
  function array(lexicons, path, def, value) {
3575
- def = def;
3576
3639
  if (!Array.isArray(value)) {
3577
3640
  return {
3578
3641
  success: false,
@@ -3620,7 +3683,7 @@ function object(lexicons, path, def, value) {
3620
3683
  }
3621
3684
  if (Array.isArray(def.required)) {
3622
3685
  for (const key of def.required) {
3623
- if (!(key in value)) {
3686
+ if (typeof value[key] === "undefined") {
3624
3687
  return {
3625
3688
  success: false,
3626
3689
  error: new ValidationError(`${path} must have the property "${key}"`)
@@ -3724,13 +3787,12 @@ function toConcreteTypes(lexicons, def) {
3724
3787
 
3725
3788
  // ../lexicon/src/validators/xrpc.ts
3726
3789
  function params(lexicons, path, def, value) {
3727
- def = def;
3728
3790
  if (!value || typeof value !== "object") {
3729
3791
  value = {};
3730
3792
  }
3731
3793
  if (Array.isArray(def.required)) {
3732
3794
  for (const key of def.required) {
3733
- if (!(key in value)) {
3795
+ if (typeof value[key] === "undefined") {
3734
3796
  return {
3735
3797
  success: false,
3736
3798
  error: new ValidationError(`${path} must have the property "${key}"`)
@@ -3743,12 +3805,8 @@ function params(lexicons, path, def, value) {
3743
3805
  continue;
3744
3806
  }
3745
3807
  const paramDef = def.properties[key];
3746
- const res = validate(
3747
- lexicons,
3748
- key,
3749
- paramDef,
3750
- value[key]
3751
- );
3808
+ const val = value[key];
3809
+ const res = paramDef.type === "array" ? array(lexicons, key, paramDef, val) : validate(lexicons, key, paramDef, val);
3752
3810
  if (!res.success) {
3753
3811
  return res;
3754
3812
  }
@@ -3941,7 +3999,17 @@ function constructMethodCallUri(nsid, schema, serviceUri, params2) {
3941
3999
  throw new Error(`Invalid query parameter: ${key}`);
3942
4000
  }
3943
4001
  if (value !== void 0) {
3944
- uri.searchParams.set(key, encodeQueryParam(paramSchema.type, value));
4002
+ if (paramSchema.type === "array") {
4003
+ const vals = [];
4004
+ vals.concat(value).forEach((val) => {
4005
+ uri.searchParams.append(
4006
+ key,
4007
+ encodeQueryParam(paramSchema.items.type, val)
4008
+ );
4009
+ });
4010
+ } else {
4011
+ uri.searchParams.set(key, encodeQueryParam(paramSchema.type, value));
4012
+ }
3945
4013
  }
3946
4014
  }
3947
4015
  }
@@ -4119,11 +4187,13 @@ var ServiceClient = class {
4119
4187
  };
4120
4188
  async function defaultFetchHandler(httpUri, httpMethod, httpHeaders, httpReqBody) {
4121
4189
  try {
4122
- const res = await fetch(httpUri, {
4190
+ const reqInit = {
4123
4191
  method: httpMethod,
4124
4192
  headers: httpHeaders,
4125
- body: encodeMethodCallBody(httpHeaders, httpReqBody)
4126
- });
4193
+ body: encodeMethodCallBody(httpHeaders, httpReqBody),
4194
+ duplex: "half"
4195
+ };
4196
+ const res = await fetch(httpUri, reqInit);
4127
4197
  const resBody = await res.arrayBuffer();
4128
4198
  return {
4129
4199
  status: res.status,
@@ -4151,6 +4221,7 @@ var src_default = defaultInst;
4151
4221
  XRPCError,
4152
4222
  XRPCInvalidResponseError,
4153
4223
  XRPCResponse,
4224
+ defaultFetchHandler,
4154
4225
  errorResponseBody
4155
4226
  });
4156
4227
  //# sourceMappingURL=index.js.map