@atproto/xrpc 0.0.1 → 0.0.3

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.
File without changes
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, {
@@ -26,6 +84,7 @@ __export(src_exports, {
26
84
  ResponseTypeStrings: () => ResponseTypeStrings,
27
85
  ServiceClient: () => ServiceClient,
28
86
  XRPCError: () => XRPCError,
87
+ XRPCInvalidResponseError: () => XRPCInvalidResponseError,
29
88
  XRPCResponse: () => XRPCResponse,
30
89
  default: () => src_default,
31
90
  errorResponseBody: () => errorResponseBody
@@ -3024,6 +3083,18 @@ var XRPCError = class extends Error {
3024
3083
  }
3025
3084
  }
3026
3085
  };
3086
+ var XRPCInvalidResponseError = class extends XRPCError {
3087
+ constructor(lexiconNsid, validationError, responseBody) {
3088
+ super(
3089
+ 2 /* InvalidResponse */,
3090
+ ResponseTypeStrings[2 /* InvalidResponse */],
3091
+ `The server gave an invalid response and may be out of date.`
3092
+ );
3093
+ this.lexiconNsid = lexiconNsid;
3094
+ this.validationError = validationError;
3095
+ this.responseBody = responseBody;
3096
+ }
3097
+ };
3027
3098
 
3028
3099
  // ../nsid/src/index.ts
3029
3100
  var SEGMENT_RE = /^[a-zA-Z]([a-zA-Z0-9-])*$/;
@@ -3257,7 +3328,7 @@ var lexiconDoc = mod.object({
3257
3328
  }
3258
3329
  });
3259
3330
  function isObj(obj) {
3260
- return !!obj && typeof obj === "object";
3331
+ return obj !== null && typeof obj === "object";
3261
3332
  }
3262
3333
  function hasProp(data, prop) {
3263
3334
  return prop in data;
@@ -3283,6 +3354,7 @@ var LexiconDefNotFoundError = class extends Error {
3283
3354
  };
3284
3355
 
3285
3356
  // ../lexicon/src/validators/primitives.ts
3357
+ var import_iso_datestring_validator = __toESM(require_dist());
3286
3358
  function validate(lexicons, path, def, value) {
3287
3359
  switch (def.type) {
3288
3360
  case "boolean":
@@ -3470,8 +3542,7 @@ function datetime(lexicons, path, def, value) {
3470
3542
  }
3471
3543
  {
3472
3544
  try {
3473
- const date = new Date(Date.parse(value));
3474
- if (value !== date.toISOString()) {
3545
+ if (typeof value !== "string" || !(0, import_iso_datestring_validator.isValidISODateString)(value)) {
3475
3546
  throw new ValidationError(
3476
3547
  `${path} must be an iso8601 formatted datetime`
3477
3548
  );
@@ -3834,6 +3905,20 @@ var Lexicons = class {
3834
3905
  }
3835
3906
  return def;
3836
3907
  }
3908
+ validate(lexUri, value) {
3909
+ lexUri = toLexUri(lexUri);
3910
+ const def = this.getDefOrThrow(lexUri, ["record", "object"]);
3911
+ if (!isObj(value)) {
3912
+ throw new ValidationError(`Value must be an object`);
3913
+ }
3914
+ if (def.type === "record") {
3915
+ return object(this, "Record", def.record, value);
3916
+ } else if (def.type === "object") {
3917
+ return object(this, "Object", def, value);
3918
+ } else {
3919
+ throw new InvalidLexiconError("Definition must be a record or object");
3920
+ }
3921
+ }
3837
3922
  assertValidRecord(lexUri, value) {
3838
3923
  lexUri = toLexUri(lexUri);
3839
3924
  const def = this.getDefOrThrow(lexUri, ["record"]);
@@ -4071,6 +4156,15 @@ var ServiceClient = class {
4071
4156
  );
4072
4157
  const resCode = httpResponseCodeToEnum(res.status);
4073
4158
  if (resCode === 200 /* Success */) {
4159
+ try {
4160
+ this.baseClient.lex.assertValidXrpcOutput(methodNsid, res.body);
4161
+ } catch (e) {
4162
+ if (e instanceof ValidationError) {
4163
+ throw new XRPCInvalidResponseError(methodNsid, e, res.body);
4164
+ } else {
4165
+ throw e;
4166
+ }
4167
+ }
4074
4168
  return new XRPCResponse(res.body, res.headers);
4075
4169
  } else {
4076
4170
  if (res.body && isErrorResponseBody(res.body)) {
@@ -4113,6 +4207,7 @@ var src_default = defaultInst;
4113
4207
  ResponseTypeStrings,
4114
4208
  ServiceClient,
4115
4209
  XRPCError,
4210
+ XRPCInvalidResponseError,
4116
4211
  XRPCResponse,
4117
4212
  errorResponseBody
4118
4213
  });