@invinite-org/data-api-client 5.0.0 → 5.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -280,6 +280,7 @@ src/apis/ReferenceApi.ts
280
280
  src/apis/TranscriptsApi.ts
281
281
  src/apis/WebhooksApi.ts
282
282
  src/apis/index.ts
283
+ src/errors.ts
283
284
  src/index.ts
284
285
  src/models/Announcement.ts
285
286
  src/models/AnnouncementCounterpartiesInner.ts
package/README.md CHANGED
@@ -102,21 +102,23 @@ const page = await response.value();
102
102
 
103
103
  On a non-2xx response the client throws `ResponseError`. The body is an RFC 9457
104
104
  `application/problem+json` document with a stable `code` (`not_covered`,
105
- `invalid_parameter`, `cursor_mismatch`, `rate_limited`, …) and a `request_id`. On `429`
106
- and `503`, wait for the `Retry-After` header before retrying.
105
+ `invalid_parameter`, `cursor_mismatch`, `rate_limited`, …), a `detail` (what happened), a
106
+ `remedy` (what to do next, one sentence) and a `request_id`. `toApiError` reads that body
107
+ once and returns an `InviniteApiError` with `status`, `code`, `detail`, `remedy`, `param`,
108
+ `errors`, `requestId`, `docsUrl`, `retryAfterMs` (the `Retry-After` header of a `429` /
109
+ `503`) and the whole `body`; it returns `undefined` for anything that is not a problem
110
+ answer (a network error, for example).
107
111
 
108
112
  ```ts
109
- import { ResponseError } from "@invinite-org/data-api-client";
113
+ import { toApiError } from "@invinite-org/data-api-client";
110
114
 
111
115
  try {
112
- await api.listFilings({ company: "NOPE" });
116
+ await api.listFilings({ company: "AAPL", limit: 500 });
113
117
  } catch (error) {
114
- if (error instanceof ResponseError) {
115
- const problem = await error.response.json();
116
- console.error(error.response.status, problem.code, problem.detail, problem.request_id);
117
- } else {
118
- throw error;
119
- }
118
+ const problem = await toApiError(error);
119
+ if (!problem) throw error;
120
+ console.error(problem.detail); // limit: must be <= 200.
121
+ console.error(problem.remedy); // Pass a limit of at most 200, or walk the pages with next_cursor.
120
122
  }
121
123
  ```
122
124
 
@@ -0,0 +1,39 @@
1
+ /** One parameter problem of a 400 (`errors[]`). */
2
+ export interface InviniteApiErrorIssue {
3
+ param: string | null;
4
+ code: string;
5
+ detail: string;
6
+ }
7
+ /** A problem answer of the Data API, read once from a `ResponseError`. */
8
+ export declare class InviniteApiError extends Error {
9
+ name: "InviniteApiError";
10
+ /** HTTP status of the answer. */
11
+ readonly status: number;
12
+ /** Stable lower_snake code (`invalid_parameter`, `not_found`, …). */
13
+ readonly code: string;
14
+ /** What happened, one sentence. */
15
+ readonly detail: string;
16
+ /** What to do next, one sentence (absent only on an answer that does not carry it). */
17
+ readonly remedy?: string;
18
+ /** The parameter the problem is about. */
19
+ readonly param?: string;
20
+ /** Every parameter problem of a 400. */
21
+ readonly errors?: InviniteApiErrorIssue[];
22
+ /** The `X-Request-Id` of the answer: quote it when reporting a problem. */
23
+ readonly requestId: string;
24
+ /** Documentation of the code. */
25
+ readonly docsUrl: string;
26
+ /** The `Retry-After` header in milliseconds (429 / 503). */
27
+ readonly retryAfterMs?: number;
28
+ /** The whole problem body. */
29
+ readonly body: Record<string, unknown>;
30
+ /** The HTTP response it was read from. */
31
+ readonly response: Response;
32
+ constructor(response: Response, body: Record<string, unknown>);
33
+ }
34
+ /**
35
+ * The problem behind an error a generated method threw: an `InviniteApiError` for a `ResponseError` whose body is a
36
+ * problem, `undefined` for anything else (a network error, a non-problem body, an already-read body). The body is
37
+ * read once per error; calling it again returns the same object.
38
+ */
39
+ export declare function toApiError(e: unknown): Promise<InviniteApiError | undefined>;
package/dist/errors.js ADDED
@@ -0,0 +1,98 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.InviniteApiError = void 0;
13
+ exports.toApiError = toApiError;
14
+ /* tslint:disable */
15
+ /* eslint-disable */
16
+ /**
17
+ * Typed problem errors: every 4xx / 5xx answer of the Data API is an RFC 9457 `application/problem+json` body
18
+ * `{ type, title, status, detail, code, param?, errors?, remedy, request_id, docs_url }`.
19
+ *
20
+ * Hand-written, not generated from the spec: `sdks/data/templates/typescript/errors.mustache` in
21
+ * outraday-org/invinite-data-api.
22
+ *
23
+ * ```ts
24
+ * try {
25
+ * await filings.listFilings({ company: "AAPL", limit: 500 });
26
+ * } catch (error) {
27
+ * const problem = await toApiError(error);
28
+ * if (!problem) throw error;
29
+ * console.error(problem.detail);
30
+ * console.error(problem.remedy);
31
+ * }
32
+ * ```
33
+ */
34
+ const runtime_1 = require("./runtime");
35
+ /** A problem answer of the Data API, read once from a `ResponseError`. */
36
+ class InviniteApiError extends Error {
37
+ constructor(response, body) {
38
+ var _a, _b, _c, _d, _e, _f, _g;
39
+ const text = (key) => (typeof body[key] === "string" ? body[key] : undefined);
40
+ const detail = (_b = (_a = text("detail")) !== null && _a !== void 0 ? _a : text("title")) !== null && _b !== void 0 ? _b : `HTTP ${response.status}`;
41
+ super(detail);
42
+ this.name = "InviniteApiError";
43
+ Object.setPrototypeOf(this, new.target.prototype);
44
+ this.status = typeof body.status === "number" ? body.status : response.status;
45
+ this.code = (_c = text("code")) !== null && _c !== void 0 ? _c : "";
46
+ this.detail = detail;
47
+ this.remedy = text("remedy");
48
+ this.param = text("param");
49
+ this.errors = Array.isArray(body.errors) ? body.errors : undefined;
50
+ this.requestId = (_e = (_d = text("request_id")) !== null && _d !== void 0 ? _d : response.headers.get("X-Request-Id")) !== null && _e !== void 0 ? _e : "";
51
+ this.docsUrl = (_f = text("docs_url")) !== null && _f !== void 0 ? _f : "";
52
+ const retryAfter = Number((_g = response.headers.get("Retry-After")) !== null && _g !== void 0 ? _g : Number.NaN);
53
+ this.retryAfterMs = Number.isFinite(retryAfter) && retryAfter >= 0 ? retryAfter * 1000 : undefined;
54
+ this.body = body;
55
+ this.response = response;
56
+ }
57
+ }
58
+ exports.InviniteApiError = InviniteApiError;
59
+ const read = new WeakMap();
60
+ function parse(error) {
61
+ return __awaiter(this, void 0, void 0, function* () {
62
+ const response = error.response;
63
+ if (!response || response.bodyUsed)
64
+ return undefined;
65
+ let body;
66
+ try {
67
+ // A clone: the caller can still read `error.response` itself.
68
+ body = JSON.parse(yield response.clone().text());
69
+ }
70
+ catch (_a) {
71
+ return undefined;
72
+ }
73
+ if (typeof body !== "object" || body === null || Array.isArray(body))
74
+ return undefined;
75
+ if (typeof body.code !== "string")
76
+ return undefined;
77
+ return new InviniteApiError(response, body);
78
+ });
79
+ }
80
+ /**
81
+ * The problem behind an error a generated method threw: an `InviniteApiError` for a `ResponseError` whose body is a
82
+ * problem, `undefined` for anything else (a network error, a non-problem body, an already-read body). The body is
83
+ * read once per error; calling it again returns the same object.
84
+ */
85
+ function toApiError(e) {
86
+ return __awaiter(this, void 0, void 0, function* () {
87
+ if (e instanceof InviniteApiError)
88
+ return e;
89
+ if (!(e instanceof runtime_1.ResponseError))
90
+ return undefined;
91
+ let pending = read.get(e);
92
+ if (!pending) {
93
+ pending = parse(e);
94
+ read.set(e, pending);
95
+ }
96
+ return pending;
97
+ });
98
+ }
@@ -0,0 +1,39 @@
1
+ /** One parameter problem of a 400 (`errors[]`). */
2
+ export interface InviniteApiErrorIssue {
3
+ param: string | null;
4
+ code: string;
5
+ detail: string;
6
+ }
7
+ /** A problem answer of the Data API, read once from a `ResponseError`. */
8
+ export declare class InviniteApiError extends Error {
9
+ name: "InviniteApiError";
10
+ /** HTTP status of the answer. */
11
+ readonly status: number;
12
+ /** Stable lower_snake code (`invalid_parameter`, `not_found`, …). */
13
+ readonly code: string;
14
+ /** What happened, one sentence. */
15
+ readonly detail: string;
16
+ /** What to do next, one sentence (absent only on an answer that does not carry it). */
17
+ readonly remedy?: string;
18
+ /** The parameter the problem is about. */
19
+ readonly param?: string;
20
+ /** Every parameter problem of a 400. */
21
+ readonly errors?: InviniteApiErrorIssue[];
22
+ /** The `X-Request-Id` of the answer: quote it when reporting a problem. */
23
+ readonly requestId: string;
24
+ /** Documentation of the code. */
25
+ readonly docsUrl: string;
26
+ /** The `Retry-After` header in milliseconds (429 / 503). */
27
+ readonly retryAfterMs?: number;
28
+ /** The whole problem body. */
29
+ readonly body: Record<string, unknown>;
30
+ /** The HTTP response it was read from. */
31
+ readonly response: Response;
32
+ constructor(response: Response, body: Record<string, unknown>);
33
+ }
34
+ /**
35
+ * The problem behind an error a generated method threw: an `InviniteApiError` for a `ResponseError` whose body is a
36
+ * problem, `undefined` for anything else (a network error, a non-problem body, an already-read body). The body is
37
+ * read once per error; calling it again returns the same object.
38
+ */
39
+ export declare function toApiError(e: unknown): Promise<InviniteApiError | undefined>;
@@ -0,0 +1,93 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ /* tslint:disable */
11
+ /* eslint-disable */
12
+ /**
13
+ * Typed problem errors: every 4xx / 5xx answer of the Data API is an RFC 9457 `application/problem+json` body
14
+ * `{ type, title, status, detail, code, param?, errors?, remedy, request_id, docs_url }`.
15
+ *
16
+ * Hand-written, not generated from the spec: `sdks/data/templates/typescript/errors.mustache` in
17
+ * outraday-org/invinite-data-api.
18
+ *
19
+ * ```ts
20
+ * try {
21
+ * await filings.listFilings({ company: "AAPL", limit: 500 });
22
+ * } catch (error) {
23
+ * const problem = await toApiError(error);
24
+ * if (!problem) throw error;
25
+ * console.error(problem.detail);
26
+ * console.error(problem.remedy);
27
+ * }
28
+ * ```
29
+ */
30
+ import { ResponseError } from './runtime';
31
+ /** A problem answer of the Data API, read once from a `ResponseError`. */
32
+ export class InviniteApiError extends Error {
33
+ constructor(response, body) {
34
+ var _a, _b, _c, _d, _e, _f, _g;
35
+ const text = (key) => (typeof body[key] === "string" ? body[key] : undefined);
36
+ const detail = (_b = (_a = text("detail")) !== null && _a !== void 0 ? _a : text("title")) !== null && _b !== void 0 ? _b : `HTTP ${response.status}`;
37
+ super(detail);
38
+ this.name = "InviniteApiError";
39
+ Object.setPrototypeOf(this, new.target.prototype);
40
+ this.status = typeof body.status === "number" ? body.status : response.status;
41
+ this.code = (_c = text("code")) !== null && _c !== void 0 ? _c : "";
42
+ this.detail = detail;
43
+ this.remedy = text("remedy");
44
+ this.param = text("param");
45
+ this.errors = Array.isArray(body.errors) ? body.errors : undefined;
46
+ this.requestId = (_e = (_d = text("request_id")) !== null && _d !== void 0 ? _d : response.headers.get("X-Request-Id")) !== null && _e !== void 0 ? _e : "";
47
+ this.docsUrl = (_f = text("docs_url")) !== null && _f !== void 0 ? _f : "";
48
+ const retryAfter = Number((_g = response.headers.get("Retry-After")) !== null && _g !== void 0 ? _g : Number.NaN);
49
+ this.retryAfterMs = Number.isFinite(retryAfter) && retryAfter >= 0 ? retryAfter * 1000 : undefined;
50
+ this.body = body;
51
+ this.response = response;
52
+ }
53
+ }
54
+ const read = new WeakMap();
55
+ function parse(error) {
56
+ return __awaiter(this, void 0, void 0, function* () {
57
+ const response = error.response;
58
+ if (!response || response.bodyUsed)
59
+ return undefined;
60
+ let body;
61
+ try {
62
+ // A clone: the caller can still read `error.response` itself.
63
+ body = JSON.parse(yield response.clone().text());
64
+ }
65
+ catch (_a) {
66
+ return undefined;
67
+ }
68
+ if (typeof body !== "object" || body === null || Array.isArray(body))
69
+ return undefined;
70
+ if (typeof body.code !== "string")
71
+ return undefined;
72
+ return new InviniteApiError(response, body);
73
+ });
74
+ }
75
+ /**
76
+ * The problem behind an error a generated method threw: an `InviniteApiError` for a `ResponseError` whose body is a
77
+ * problem, `undefined` for anything else (a network error, a non-problem body, an already-read body). The body is
78
+ * read once per error; calling it again returns the same object.
79
+ */
80
+ export function toApiError(e) {
81
+ return __awaiter(this, void 0, void 0, function* () {
82
+ if (e instanceof InviniteApiError)
83
+ return e;
84
+ if (!(e instanceof ResponseError))
85
+ return undefined;
86
+ let pending = read.get(e);
87
+ if (!pending) {
88
+ pending = parse(e);
89
+ read.set(e, pending);
90
+ }
91
+ return pending;
92
+ });
93
+ }
@@ -2,3 +2,4 @@ export * from './runtime';
2
2
  export * from './apis/index';
3
3
  export * from './models/index';
4
4
  export * from './pagination';
5
+ export * from './errors';
package/dist/esm/index.js CHANGED
@@ -4,3 +4,4 @@ export * from './runtime';
4
4
  export * from './apis/index';
5
5
  export * from './models/index';
6
6
  export * from './pagination';
7
+ export * from './errors';
@@ -58,6 +58,12 @@ export interface Problem {
58
58
  * @memberof Problem
59
59
  */
60
60
  errors?: Array<ProblemErrorsInner>;
61
+ /**
62
+ * What to do next: one imperative sentence ending with a period, surface neutral (no CLI command, no HTTP status number). A per-code default when the operation gives none
63
+ * @type {string}
64
+ * @memberof Problem
65
+ */
66
+ remedy: string;
61
67
  /**
62
68
  * The `X-Request-Id` of this response
63
69
  * @type {string}
@@ -45,6 +45,8 @@ export function instanceOfProblem(value) {
45
45
  return false;
46
46
  if (!('code' in value) || value['code'] === undefined)
47
47
  return false;
48
+ if (!('remedy' in value) || value['remedy'] === undefined)
49
+ return false;
48
50
  if (!('request_id' in value) || value['request_id'] === undefined)
49
51
  return false;
50
52
  if (!('docs_url' in value) || value['docs_url'] === undefined)
@@ -66,6 +68,7 @@ export function ProblemFromJSONTyped(json, ignoreDiscriminator) {
66
68
  'code': json['code'],
67
69
  'param': json['param'] == null ? undefined : json['param'],
68
70
  'errors': json['errors'] == null ? undefined : (json['errors'].map(ProblemErrorsInnerFromJSON)),
71
+ 'remedy': json['remedy'],
69
72
  'request_id': json['request_id'],
70
73
  'docs_url': json['docs_url'],
71
74
  };
@@ -85,6 +88,7 @@ export function ProblemToJSONTyped(value, ignoreDiscriminator = false) {
85
88
  'code': value['code'],
86
89
  'param': value['param'],
87
90
  'errors': value['errors'] == null ? undefined : (value['errors'].map(ProblemErrorsInnerToJSON)),
91
+ 'remedy': value['remedy'],
88
92
  'request_id': value['request_id'],
89
93
  'docs_url': value['docs_url'],
90
94
  };
package/dist/index.d.ts CHANGED
@@ -2,3 +2,4 @@ export * from './runtime';
2
2
  export * from './apis/index';
3
3
  export * from './models/index';
4
4
  export * from './pagination';
5
+ export * from './errors';
package/dist/index.js CHANGED
@@ -20,3 +20,4 @@ __exportStar(require("./runtime"), exports);
20
20
  __exportStar(require("./apis/index"), exports);
21
21
  __exportStar(require("./models/index"), exports);
22
22
  __exportStar(require("./pagination"), exports);
23
+ __exportStar(require("./errors"), exports);
@@ -58,6 +58,12 @@ export interface Problem {
58
58
  * @memberof Problem
59
59
  */
60
60
  errors?: Array<ProblemErrorsInner>;
61
+ /**
62
+ * What to do next: one imperative sentence ending with a period, surface neutral (no CLI command, no HTTP status number). A per-code default when the operation gives none
63
+ * @type {string}
64
+ * @memberof Problem
65
+ */
66
+ remedy: string;
61
67
  /**
62
68
  * The `X-Request-Id` of this response
63
69
  * @type {string}
@@ -53,6 +53,8 @@ function instanceOfProblem(value) {
53
53
  return false;
54
54
  if (!('code' in value) || value['code'] === undefined)
55
55
  return false;
56
+ if (!('remedy' in value) || value['remedy'] === undefined)
57
+ return false;
56
58
  if (!('request_id' in value) || value['request_id'] === undefined)
57
59
  return false;
58
60
  if (!('docs_url' in value) || value['docs_url'] === undefined)
@@ -74,6 +76,7 @@ function ProblemFromJSONTyped(json, ignoreDiscriminator) {
74
76
  'code': json['code'],
75
77
  'param': json['param'] == null ? undefined : json['param'],
76
78
  'errors': json['errors'] == null ? undefined : (json['errors'].map(ProblemErrorsInner_1.ProblemErrorsInnerFromJSON)),
79
+ 'remedy': json['remedy'],
77
80
  'request_id': json['request_id'],
78
81
  'docs_url': json['docs_url'],
79
82
  };
@@ -93,6 +96,7 @@ function ProblemToJSONTyped(value, ignoreDiscriminator = false) {
93
96
  'code': value['code'],
94
97
  'param': value['param'],
95
98
  'errors': value['errors'] == null ? undefined : (value['errors'].map(ProblemErrorsInner_1.ProblemErrorsInnerToJSON)),
99
+ 'remedy': value['remedy'],
96
100
  'request_id': value['request_id'],
97
101
  'docs_url': value['docs_url'],
98
102
  };
package/docs/Problem.md CHANGED
@@ -14,6 +14,7 @@ Name | Type
14
14
  `code` | string
15
15
  `param` | string
16
16
  `errors` | [Array&lt;ProblemErrorsInner&gt;](ProblemErrorsInner.md)
17
+ `remedy` | string
17
18
  `request_id` | string
18
19
  `docs_url` | string
19
20
 
@@ -31,6 +32,7 @@ const example = {
31
32
  "code": invalid_parameter,
32
33
  "param": limit,
33
34
  "errors": null,
35
+ "remedy": Pass a limit of at most 200, or walk the pages with next_cursor.,
34
36
  "request_id": req_01JCX3V7Q9W2N8M4K6T0B5R1YZ,
35
37
  "docs_url": https://data.invinite.com/docs/errors#invalid_parameter,
36
38
  } satisfies Problem
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@invinite-org/data-api-client",
3
- "version": "5.0.0",
3
+ "version": "5.1.0",
4
4
  "description": "Typed TypeScript/JavaScript client for the Invinite Data API",
5
5
  "author": "OpenAPI-Generator",
6
6
  "repository": {
package/src/errors.ts ADDED
@@ -0,0 +1,123 @@
1
+ /* tslint:disable */
2
+ /* eslint-disable */
3
+ /**
4
+ * Typed problem errors: every 4xx / 5xx answer of the Data API is an RFC 9457 `application/problem+json` body
5
+ * `{ type, title, status, detail, code, param?, errors?, remedy, request_id, docs_url }`.
6
+ *
7
+ * Hand-written, not generated from the spec: `sdks/data/templates/typescript/errors.mustache` in
8
+ * outraday-org/invinite-data-api.
9
+ *
10
+ * ```ts
11
+ * try {
12
+ * await filings.listFilings({ company: "AAPL", limit: 500 });
13
+ * } catch (error) {
14
+ * const problem = await toApiError(error);
15
+ * if (!problem) throw error;
16
+ * console.error(problem.detail);
17
+ * console.error(problem.remedy);
18
+ * }
19
+ * ```
20
+ */
21
+ import { ResponseError } from './runtime';
22
+
23
+ /** One parameter problem of a 400 (`errors[]`). */
24
+ export interface InviniteApiErrorIssue {
25
+ param: string | null;
26
+ code: string;
27
+ detail: string;
28
+ }
29
+
30
+ /** A problem answer of the Data API, read once from a `ResponseError`. */
31
+ export class InviniteApiError extends Error {
32
+ override name: "InviniteApiError" = "InviniteApiError";
33
+
34
+ /** HTTP status of the answer. */
35
+ readonly status: number;
36
+ /** Stable lower_snake code (`invalid_parameter`, `not_found`, …). */
37
+ readonly code: string;
38
+ /** What happened, one sentence. */
39
+ readonly detail: string;
40
+ /** What to do next, one sentence (absent only on an answer that does not carry it). */
41
+ readonly remedy?: string;
42
+ /** The parameter the problem is about. */
43
+ readonly param?: string;
44
+ /** Every parameter problem of a 400. */
45
+ readonly errors?: InviniteApiErrorIssue[];
46
+ /** The `X-Request-Id` of the answer: quote it when reporting a problem. */
47
+ readonly requestId: string;
48
+ /** Documentation of the code. */
49
+ readonly docsUrl: string;
50
+ /** The `Retry-After` header in milliseconds (429 / 503). */
51
+ readonly retryAfterMs?: number;
52
+ /** The whole problem body. */
53
+ readonly body: Record<string, unknown>;
54
+ /** The HTTP response it was read from. */
55
+ readonly response: Response;
56
+
57
+ constructor(response: Response, body: Record<string, unknown>) {
58
+ const text = (key: string): string | undefined => (typeof body[key] === "string" ? (body[key] as string) : undefined);
59
+ const detail = text("detail") ?? text("title") ?? `HTTP ${response.status}`;
60
+
61
+ super(detail);
62
+
63
+ Object.setPrototypeOf(this, new.target.prototype);
64
+
65
+ this.status = typeof body.status === "number" ? body.status : response.status;
66
+ this.code = text("code") ?? "";
67
+ this.detail = detail;
68
+ this.remedy = text("remedy");
69
+ this.param = text("param");
70
+ this.errors = Array.isArray(body.errors) ? (body.errors as InviniteApiErrorIssue[]) : undefined;
71
+ this.requestId = text("request_id") ?? response.headers.get("X-Request-Id") ?? "";
72
+ this.docsUrl = text("docs_url") ?? "";
73
+
74
+ const retryAfter = Number(response.headers.get("Retry-After") ?? Number.NaN);
75
+
76
+ this.retryAfterMs = Number.isFinite(retryAfter) && retryAfter >= 0 ? retryAfter * 1000 : undefined;
77
+ this.body = body;
78
+ this.response = response;
79
+ }
80
+ }
81
+
82
+ const read = new WeakMap<object, Promise<InviniteApiError | undefined>>();
83
+
84
+ async function parse(error: ResponseError): Promise<InviniteApiError | undefined> {
85
+ const response = error.response;
86
+
87
+ if (!response || response.bodyUsed) return undefined;
88
+
89
+ let body: unknown;
90
+
91
+ try {
92
+ // A clone: the caller can still read `error.response` itself.
93
+ body = JSON.parse(await response.clone().text());
94
+ } catch {
95
+ return undefined;
96
+ }
97
+
98
+ if (typeof body !== "object" || body === null || Array.isArray(body)) return undefined;
99
+
100
+ if (typeof (body as Record<string, unknown>).code !== "string") return undefined;
101
+
102
+ return new InviniteApiError(response, body as Record<string, unknown>);
103
+ }
104
+
105
+ /**
106
+ * The problem behind an error a generated method threw: an `InviniteApiError` for a `ResponseError` whose body is a
107
+ * problem, `undefined` for anything else (a network error, a non-problem body, an already-read body). The body is
108
+ * read once per error; calling it again returns the same object.
109
+ */
110
+ export async function toApiError(e: unknown): Promise<InviniteApiError | undefined> {
111
+ if (e instanceof InviniteApiError) return e;
112
+
113
+ if (!(e instanceof ResponseError)) return undefined;
114
+
115
+ let pending = read.get(e);
116
+
117
+ if (!pending) {
118
+ pending = parse(e);
119
+ read.set(e, pending);
120
+ }
121
+
122
+ return pending;
123
+ }
package/src/index.ts CHANGED
@@ -4,3 +4,4 @@ export * from './runtime';
4
4
  export * from './apis/index';
5
5
  export * from './models/index';
6
6
  export * from './pagination';
7
+ export * from './errors';
@@ -69,6 +69,12 @@ export interface Problem {
69
69
  * @memberof Problem
70
70
  */
71
71
  errors?: Array<ProblemErrorsInner>;
72
+ /**
73
+ * What to do next: one imperative sentence ending with a period, surface neutral (no CLI command, no HTTP status number). A per-code default when the operation gives none
74
+ * @type {string}
75
+ * @memberof Problem
76
+ */
77
+ remedy: string;
72
78
  /**
73
79
  * The `X-Request-Id` of this response
74
80
  * @type {string}
@@ -115,6 +121,7 @@ export function instanceOfProblem(value: object): value is Problem {
115
121
  if (!('status' in value) || value['status'] === undefined) return false;
116
122
  if (!('detail' in value) || value['detail'] === undefined) return false;
117
123
  if (!('code' in value) || value['code'] === undefined) return false;
124
+ if (!('remedy' in value) || value['remedy'] === undefined) return false;
118
125
  if (!('request_id' in value) || value['request_id'] === undefined) return false;
119
126
  if (!('docs_url' in value) || value['docs_url'] === undefined) return false;
120
127
  return true;
@@ -137,6 +144,7 @@ export function ProblemFromJSONTyped(json: any, ignoreDiscriminator: boolean): P
137
144
  'code': json['code'],
138
145
  'param': json['param'] == null ? undefined : json['param'],
139
146
  'errors': json['errors'] == null ? undefined : ((json['errors'] as Array<any>).map(ProblemErrorsInnerFromJSON)),
147
+ 'remedy': json['remedy'],
140
148
  'request_id': json['request_id'],
141
149
  'docs_url': json['docs_url'],
142
150
  };
@@ -160,6 +168,7 @@ export function ProblemToJSONTyped(value?: Problem | null, ignoreDiscriminator:
160
168
  'code': value['code'],
161
169
  'param': value['param'],
162
170
  'errors': value['errors'] == null ? undefined : ((value['errors'] as Array<any>).map(ProblemErrorsInnerToJSON)),
171
+ 'remedy': value['remedy'],
163
172
  'request_id': value['request_id'],
164
173
  'docs_url': value['docs_url'],
165
174
  };