@openmeter/client 1.0.0-beta.229 → 1.0.0-beta.231
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/LICENSE +202 -0
- package/README.md +364 -66
- package/dist/core.d.ts +0 -1
- package/dist/core.js +8 -1
- package/dist/funcs/addons.d.ts +49 -1
- package/dist/funcs/addons.js +161 -29
- package/dist/funcs/apps.d.ts +14 -1
- package/dist/funcs/apps.js +52 -9
- package/dist/funcs/billing.d.ts +46 -1
- package/dist/funcs/billing.js +126 -17
- package/dist/funcs/currencies.d.ts +30 -1
- package/dist/funcs/currencies.js +113 -23
- package/dist/funcs/customers.d.ts +167 -2
- package/dist/funcs/customers.js +580 -90
- package/dist/funcs/defaults.d.ts +10 -1
- package/dist/funcs/defaults.js +35 -5
- package/dist/funcs/entitlements.d.ts +5 -1
- package/dist/funcs/entitlements.js +25 -6
- package/dist/funcs/events.d.ts +14 -1
- package/dist/funcs/events.js +41 -9
- package/dist/funcs/features.d.ts +42 -1
- package/dist/funcs/features.js +142 -28
- package/dist/funcs/governance.d.ts +14 -1
- package/dist/funcs/governance.js +42 -10
- package/dist/funcs/index.d.ts +1 -1
- package/dist/funcs/index.js +2 -1
- package/dist/funcs/invoices.d.ts +51 -0
- package/dist/funcs/invoices.js +125 -0
- package/dist/funcs/llmCost.d.ts +36 -1
- package/dist/funcs/llmCost.js +114 -24
- package/dist/funcs/meters.d.ts +55 -2
- package/dist/funcs/meters.js +172 -28
- package/dist/funcs/planAddons.d.ts +35 -1
- package/dist/funcs/planAddons.js +139 -29
- package/dist/funcs/plans.d.ts +49 -1
- package/dist/funcs/plans.js +163 -25
- package/dist/funcs/subscriptions.d.ts +61 -2
- package/dist/funcs/subscriptions.js +238 -40
- package/dist/funcs/tax.d.ts +25 -1
- package/dist/funcs/tax.js +104 -22
- package/dist/index.d.ts +6 -4
- package/dist/index.js +4 -3
- package/dist/lib/config.d.ts +9 -1
- package/dist/lib/config.js +1 -1
- package/dist/lib/encodings.d.ts +1 -2
- package/dist/lib/encodings.js +6 -5
- package/dist/lib/paginate.d.ts +56 -0
- package/dist/lib/paginate.js +60 -0
- package/dist/lib/request.d.ts +0 -1
- package/dist/lib/request.js +1 -1
- package/dist/lib/to-error.d.ts +0 -1
- package/dist/lib/to-error.js +1 -1
- package/dist/lib/types.d.ts +0 -1
- package/dist/lib/types.js +1 -1
- package/dist/lib/version.d.ts +1 -0
- package/dist/lib/version.js +5 -0
- package/dist/lib/wire.d.ts +20 -0
- package/dist/lib/wire.js +401 -0
- package/dist/models/errors.d.ts +13 -2
- package/dist/models/errors.js +31 -4
- package/dist/models/operations/addons.d.ts +17 -6
- package/dist/models/operations/addons.js +1 -1
- package/dist/models/operations/apps.d.ts +4 -6
- package/dist/models/operations/apps.js +1 -1
- package/dist/models/operations/billing.d.ts +5 -5
- package/dist/models/operations/billing.js +1 -1
- package/dist/models/operations/currencies.d.ts +27 -10
- package/dist/models/operations/currencies.js +1 -1
- package/dist/models/operations/customers.d.ts +87 -33
- package/dist/models/operations/customers.js +1 -1
- package/dist/models/operations/defaults.d.ts +2 -2
- package/dist/models/operations/defaults.js +1 -1
- package/dist/models/operations/entitlements.d.ts +0 -1
- package/dist/models/operations/entitlements.js +1 -1
- package/dist/models/operations/events.d.ts +20 -5
- package/dist/models/operations/events.js +1 -1
- package/dist/models/operations/features.d.ts +24 -9
- package/dist/models/operations/features.js +1 -1
- package/dist/models/operations/governance.d.ts +3 -3
- package/dist/models/operations/governance.js +1 -1
- package/dist/models/operations/invoices.d.ts +45 -0
- package/dist/models/operations/invoices.js +2 -0
- package/dist/models/operations/llmCost.d.ts +16 -5
- package/dist/models/operations/llmCost.js +1 -1
- package/dist/models/operations/meters.d.ts +29 -9
- package/dist/models/operations/meters.js +1 -1
- package/dist/models/operations/planAddons.d.ts +7 -7
- package/dist/models/operations/planAddons.js +1 -1
- package/dist/models/operations/plans.d.ts +14 -6
- package/dist/models/operations/plans.js +1 -1
- package/dist/models/operations/subscriptions.d.ts +36 -11
- package/dist/models/operations/subscriptions.js +1 -1
- package/dist/models/operations/tax.d.ts +6 -6
- package/dist/models/operations/tax.js +1 -1
- package/dist/models/schemas.d.ts +27484 -3125
- package/dist/models/schemas.js +6247 -495
- package/dist/models/types.d.ts +4548 -1398
- package/dist/models/types.js +1 -1
- package/dist/sdk/addons.d.ts +60 -1
- package/dist/sdk/addons.js +63 -1
- package/dist/sdk/apps.d.ts +25 -1
- package/dist/sdk/apps.js +28 -1
- package/dist/sdk/billing.d.ts +57 -1
- package/dist/sdk/billing.js +60 -1
- package/dist/sdk/customers.d.ts +213 -2
- package/dist/sdk/customers.js +228 -2
- package/dist/sdk/defaults.d.ts +10 -1
- package/dist/sdk/defaults.js +11 -1
- package/dist/sdk/entitlements.d.ts +5 -1
- package/dist/sdk/entitlements.js +6 -1
- package/dist/sdk/events.d.ts +25 -1
- package/dist/sdk/events.js +28 -1
- package/dist/sdk/features.d.ts +53 -1
- package/dist/sdk/features.js +56 -1
- package/dist/sdk/internal.d.ts +180 -0
- package/dist/sdk/internal.js +224 -0
- package/dist/sdk/llmCost.d.ts +57 -1
- package/dist/sdk/llmCost.js +62 -1
- package/dist/sdk/meters.d.ts +66 -2
- package/dist/sdk/meters.js +71 -2
- package/dist/sdk/planAddons.d.ts +46 -1
- package/dist/sdk/planAddons.js +49 -1
- package/dist/sdk/plans.d.ts +60 -1
- package/dist/sdk/plans.js +63 -1
- package/dist/sdk/sdk.d.ts +9 -7
- package/dist/sdk/sdk.js +11 -10
- package/dist/sdk/subscriptions.d.ts +71 -1
- package/dist/sdk/subscriptions.js +76 -1
- package/dist/sdk/tax.d.ts +34 -1
- package/dist/sdk/tax.js +37 -1
- package/package.json +31 -6
- package/dist/sdk/currencies.d.ts +0 -12
- package/dist/sdk/currencies.js +0 -21
- package/dist/sdk/governance.d.ts +0 -9
- package/dist/sdk/governance.js +0 -12
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { type RequestOptions, type Result } from './types.js';
|
|
2
|
+
export declare class PaginationLimitExceededError extends Error {
|
|
3
|
+
constructor(limit: number);
|
|
4
|
+
}
|
|
5
|
+
interface PageNumberEnvelope<TItem> {
|
|
6
|
+
data: TItem[];
|
|
7
|
+
meta: {
|
|
8
|
+
page: {
|
|
9
|
+
number: number;
|
|
10
|
+
size: number;
|
|
11
|
+
total: number;
|
|
12
|
+
};
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
interface PageNumberRequest {
|
|
16
|
+
page?: {
|
|
17
|
+
number?: number;
|
|
18
|
+
size?: number;
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Iterates every item of a page-number-paginated list operation, advancing
|
|
23
|
+
* `page.number` from wherever the caller's own request starts. Every other
|
|
24
|
+
* field on `request` — filters, sort, page.size — is sent unchanged on every
|
|
25
|
+
* page fetch; only the page number advances. Stops on a page shorter than
|
|
26
|
+
* the server's own reported `size` (including an empty page) or once the
|
|
27
|
+
* running item count reaches the server's reported `total`, whichever comes
|
|
28
|
+
* first — so a final page that exactly fills `total` does not trigger one
|
|
29
|
+
* more, empty request.
|
|
30
|
+
*/
|
|
31
|
+
export declare function paginatePages<TItem, TReq extends PageNumberRequest>(fetchPage: (req: TReq, options?: RequestOptions) => Promise<Result<PageNumberEnvelope<TItem>>>, request: TReq, options?: RequestOptions): AsyncGenerator<TItem, void, undefined>;
|
|
32
|
+
interface CursorEnvelope<TItem> {
|
|
33
|
+
data: TItem[];
|
|
34
|
+
meta: {
|
|
35
|
+
page: {
|
|
36
|
+
next?: string;
|
|
37
|
+
};
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
interface CursorRequest {
|
|
41
|
+
page?: {
|
|
42
|
+
after?: string;
|
|
43
|
+
before?: string;
|
|
44
|
+
size?: number;
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Iterates every item of a cursor-paginated list operation, following
|
|
49
|
+
* `meta.page.next` until the server stops returning one. Despite carrying a
|
|
50
|
+
* `format: uri` annotation in the spec, `next` is the opaque cursor token the
|
|
51
|
+
* API expects back verbatim in `page.after` on the following request — not a
|
|
52
|
+
* URI to fetch directly. Every other field on `request` is sent unchanged on
|
|
53
|
+
* every page fetch; only `page.after` advances.
|
|
54
|
+
*/
|
|
55
|
+
export declare function paginateCursor<TItem, TReq extends CursorRequest>(fetchPage: (req: TReq, options?: RequestOptions) => Promise<Result<CursorEnvelope<TItem>>>, request: TReq, options?: RequestOptions): AsyncGenerator<TItem, void, undefined>;
|
|
56
|
+
export {};
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
// Code generated by @openmeter/typespec-typescript. DO NOT EDIT.
|
|
2
|
+
import { unwrap } from './types.js';
|
|
3
|
+
// A misbehaving server — inconsistent `meta.page.total`/`size`, or a
|
|
4
|
+
// `meta.page.next` cursor that never resolves to an empty page — would
|
|
5
|
+
// otherwise make these generators loop forever. 10,000 pages is far beyond
|
|
6
|
+
// any real OpenMeter list, so hitting it means the server's pagination
|
|
7
|
+
// contract broke, not that the caller has a legitimately large list.
|
|
8
|
+
const MAX_PAGINATION_PAGES = 10_000;
|
|
9
|
+
export class PaginationLimitExceededError extends Error {
|
|
10
|
+
constructor(limit) {
|
|
11
|
+
super(`pagination exceeded the maximum of ${limit} pages; the server may be returning inconsistent pagination metadata`);
|
|
12
|
+
this.name = 'PaginationLimitExceededError';
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Iterates every item of a page-number-paginated list operation, advancing
|
|
17
|
+
* `page.number` from wherever the caller's own request starts. Every other
|
|
18
|
+
* field on `request` — filters, sort, page.size — is sent unchanged on every
|
|
19
|
+
* page fetch; only the page number advances. Stops on a page shorter than
|
|
20
|
+
* the server's own reported `size` (including an empty page) or once the
|
|
21
|
+
* running item count reaches the server's reported `total`, whichever comes
|
|
22
|
+
* first — so a final page that exactly fills `total` does not trigger one
|
|
23
|
+
* more, empty request.
|
|
24
|
+
*/
|
|
25
|
+
export async function* paginatePages(fetchPage, request, options) {
|
|
26
|
+
let req = request;
|
|
27
|
+
let seen = 0;
|
|
28
|
+
for (let page = 0; page < MAX_PAGINATION_PAGES; page++) {
|
|
29
|
+
const res = unwrap(await fetchPage(req, options));
|
|
30
|
+
yield* res.data;
|
|
31
|
+
seen += res.data.length;
|
|
32
|
+
const { number, size, total } = res.meta.page;
|
|
33
|
+
if (res.data.length === 0 || res.data.length < size || seen >= total) {
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
req = { ...req, page: { ...req.page, number: number + 1 } };
|
|
37
|
+
}
|
|
38
|
+
throw new PaginationLimitExceededError(MAX_PAGINATION_PAGES);
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Iterates every item of a cursor-paginated list operation, following
|
|
42
|
+
* `meta.page.next` until the server stops returning one. Despite carrying a
|
|
43
|
+
* `format: uri` annotation in the spec, `next` is the opaque cursor token the
|
|
44
|
+
* API expects back verbatim in `page.after` on the following request — not a
|
|
45
|
+
* URI to fetch directly. Every other field on `request` is sent unchanged on
|
|
46
|
+
* every page fetch; only `page.after` advances.
|
|
47
|
+
*/
|
|
48
|
+
export async function* paginateCursor(fetchPage, request, options) {
|
|
49
|
+
let req = request;
|
|
50
|
+
for (let page = 0; page < MAX_PAGINATION_PAGES; page++) {
|
|
51
|
+
const res = unwrap(await fetchPage(req, options));
|
|
52
|
+
yield* res.data;
|
|
53
|
+
const next = res.meta.page.next;
|
|
54
|
+
if (!next) {
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
req = { ...req, page: { ...req.page, after: next } };
|
|
58
|
+
}
|
|
59
|
+
throw new PaginationLimitExceededError(MAX_PAGINATION_PAGES);
|
|
60
|
+
}
|
package/dist/lib/request.d.ts
CHANGED
package/dist/lib/request.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
// Code generated by @openmeter/typespec-typescript. DO NOT EDIT.
|
|
1
2
|
import { ok, err } from './types.js';
|
|
2
3
|
import { toError } from './to-error.js';
|
|
3
4
|
export async function request(fn) {
|
|
@@ -8,4 +9,3 @@ export async function request(fn) {
|
|
|
8
9
|
return err(await toError(e));
|
|
9
10
|
}
|
|
10
11
|
}
|
|
11
|
-
//# sourceMappingURL=request.js.map
|
package/dist/lib/to-error.d.ts
CHANGED
package/dist/lib/to-error.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
// Code generated by @openmeter/typespec-typescript. DO NOT EDIT.
|
|
1
2
|
import { HTTPError as KyHTTPError } from 'ky';
|
|
2
3
|
import * as schemas from '../models/schemas.js';
|
|
3
4
|
import { HTTPError } from '../models/errors.js';
|
|
@@ -13,4 +14,3 @@ export async function toError(e) {
|
|
|
13
14
|
}
|
|
14
15
|
return new Error(String(e));
|
|
15
16
|
}
|
|
16
|
-
//# sourceMappingURL=to-error.js.map
|
package/dist/lib/types.d.ts
CHANGED
package/dist/lib/types.js
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const SDK_VERSION = "v1.0.0-beta.231";
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
// Code generated by @openmeter/typespec-typescript. DO NOT EDIT.
|
|
2
|
+
// The committed value is a dev placeholder. The publish flow
|
|
3
|
+
// (`make -C api/spec publish-aip-sdk`) stamps the real release version here
|
|
4
|
+
// before `pnpm publish`, after `pnpm version` updates package.json.
|
|
5
|
+
export const SDK_VERSION = 'v1.0.0-beta.231';
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { output, ZodType } from 'zod';
|
|
2
|
+
export declare function toCamelCase(name: string): string;
|
|
3
|
+
export declare function toSnakeCase(name: string): string;
|
|
4
|
+
export declare class UnsafeIntegerError extends Error {
|
|
5
|
+
constructor(value: bigint);
|
|
6
|
+
}
|
|
7
|
+
export type DateString = string & Record<never, never>;
|
|
8
|
+
export type AcceptDateStrings<T> = T extends Date ? Date | DateString : T extends (infer E)[] ? AcceptDateStrings<E>[] : T extends object ? {
|
|
9
|
+
[K in keyof T]: AcceptDateStrings<T[K]>;
|
|
10
|
+
} : T;
|
|
11
|
+
export declare class DepthLimitExceededError extends Error {
|
|
12
|
+
constructor();
|
|
13
|
+
}
|
|
14
|
+
export declare function toWire<T>(data: T, schema: ZodType): T;
|
|
15
|
+
export declare function fromWire<S extends ZodType>(data: unknown, schema: S): output<S>;
|
|
16
|
+
export declare class ValidationError extends Error {
|
|
17
|
+
readonly issues: unknown;
|
|
18
|
+
constructor(message: string, issues: unknown);
|
|
19
|
+
}
|
|
20
|
+
export declare function assertValid(schema: ZodType, data: unknown): void;
|
package/dist/lib/wire.js
ADDED
|
@@ -0,0 +1,401 @@
|
|
|
1
|
+
// Code generated by @openmeter/typespec-typescript. DO NOT EDIT.
|
|
2
|
+
export function toCamelCase(name) {
|
|
3
|
+
return name.replace(/_([a-z0-9])/g, (_m, c) => c.toUpperCase());
|
|
4
|
+
}
|
|
5
|
+
export function toSnakeCase(name) {
|
|
6
|
+
return name.replace(/([A-Z])/g, (_m, c) => `_${c.toLowerCase()}`);
|
|
7
|
+
}
|
|
8
|
+
function def(schema) {
|
|
9
|
+
return schema?.def;
|
|
10
|
+
}
|
|
11
|
+
// Unwrap optional/nullable/default to the schema that describes the value's
|
|
12
|
+
// shape. These wrappers never change keys, so the walker looks through them
|
|
13
|
+
// before classifying a node.
|
|
14
|
+
function unwrap(schema) {
|
|
15
|
+
let current = schema;
|
|
16
|
+
for (let i = 0; i < 100 && current; i++) {
|
|
17
|
+
const d = def(current);
|
|
18
|
+
if (d &&
|
|
19
|
+
(d.type === 'optional' ||
|
|
20
|
+
d.type === 'nullable' ||
|
|
21
|
+
d.type === 'default') &&
|
|
22
|
+
d.innerType) {
|
|
23
|
+
current = d.innerType;
|
|
24
|
+
continue;
|
|
25
|
+
}
|
|
26
|
+
return current;
|
|
27
|
+
}
|
|
28
|
+
/* v8 ignore next -- loop returns inside; reached only past the cycle guard */
|
|
29
|
+
return current;
|
|
30
|
+
}
|
|
31
|
+
// The default to write for a field the caller omitted, present only when the
|
|
32
|
+
// field is required-with-default in the spec — emitted as `.default(x)` with no
|
|
33
|
+
// `.optional()` in the wrapper chain (TypeSpec `field: T = x`, not
|
|
34
|
+
// `field?: T = x`). Such fields must be present on the wire (e.g. a CloudEvents
|
|
35
|
+
// envelope's `specversion`, which the server's parser rejects when absent), yet
|
|
36
|
+
// the generated request type allows omitting them so callers get the documented
|
|
37
|
+
// default for free — toWire is the layer that has to reconcile the two.
|
|
38
|
+
// Spec-optional fields (`.optional().default(x)`) return undefined and stay off
|
|
39
|
+
// the wire: their default is the server's to apply, and materializing them
|
|
40
|
+
// client-side would silently overwrite server state on update requests.
|
|
41
|
+
function requiredDefault(schema) {
|
|
42
|
+
let current = schema;
|
|
43
|
+
let found;
|
|
44
|
+
for (let i = 0; i < 100 && current; i++) {
|
|
45
|
+
const d = def(current);
|
|
46
|
+
/* v8 ignore next 3 -- every zod schema carries a def; guards the type only */
|
|
47
|
+
if (!d) {
|
|
48
|
+
break;
|
|
49
|
+
}
|
|
50
|
+
if (d.type === 'optional') {
|
|
51
|
+
return undefined;
|
|
52
|
+
}
|
|
53
|
+
if (d.type === 'default') {
|
|
54
|
+
found ??= { value: d.defaultValue };
|
|
55
|
+
}
|
|
56
|
+
else if (d.type !== 'nullable') {
|
|
57
|
+
break;
|
|
58
|
+
}
|
|
59
|
+
current = d.innerType;
|
|
60
|
+
}
|
|
61
|
+
return found;
|
|
62
|
+
}
|
|
63
|
+
function shapeOf(schema) {
|
|
64
|
+
return schema?.shape;
|
|
65
|
+
}
|
|
66
|
+
// The element schema for array data: the schema's own element when it is an
|
|
67
|
+
// array, or the array variant's element when it is a union of T and T[]
|
|
68
|
+
// (the single-or-batch body shape).
|
|
69
|
+
function arrayElement(schema) {
|
|
70
|
+
const d = def(schema);
|
|
71
|
+
if (d?.type === 'array') {
|
|
72
|
+
return d.element;
|
|
73
|
+
}
|
|
74
|
+
if (d?.type === 'union') {
|
|
75
|
+
for (const option of d.options ?? []) {
|
|
76
|
+
const od = def(unwrap(option));
|
|
77
|
+
if (od?.type === 'array') {
|
|
78
|
+
return od.element;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
return undefined;
|
|
83
|
+
}
|
|
84
|
+
// Whether a record value schema needs walking: it carries renamable fields
|
|
85
|
+
// (object/record/array/union of such) or date-typed values that must map
|
|
86
|
+
// between `Date` and the RFC 3339 wire string. Other scalars, literals,
|
|
87
|
+
// unknown, and any are left untouched, so user data (labels, dimensions,
|
|
88
|
+
// event payloads) is never rewritten.
|
|
89
|
+
function needsWalk(schema) {
|
|
90
|
+
const s = unwrap(schema);
|
|
91
|
+
const d = def(s);
|
|
92
|
+
/* v8 ignore next 3 -- a record always has a value schema; defensive only */
|
|
93
|
+
if (!d) {
|
|
94
|
+
return false;
|
|
95
|
+
}
|
|
96
|
+
if (d.type === 'object' ||
|
|
97
|
+
d.type === 'record' ||
|
|
98
|
+
d.type === 'date' ||
|
|
99
|
+
d.type === 'bigint') {
|
|
100
|
+
return true;
|
|
101
|
+
}
|
|
102
|
+
if (d.type === 'array') {
|
|
103
|
+
return needsWalk(d.element);
|
|
104
|
+
}
|
|
105
|
+
if (d.type === 'union') {
|
|
106
|
+
return (d.options ?? []).some(needsWalk);
|
|
107
|
+
}
|
|
108
|
+
return false;
|
|
109
|
+
}
|
|
110
|
+
// Thrown by toWire when a bigint value (an int64/uint64 field) is outside
|
|
111
|
+
// JSON's exactly-representable integer range. The wire carries int64 as a JSON
|
|
112
|
+
// number (the server decodes it into a Go int64), so a value beyond 2^53-1
|
|
113
|
+
// cannot be sent without silent precision corruption — a typed, immediate
|
|
114
|
+
// failure is the only honest option. request() catches it like any Error and
|
|
115
|
+
// surfaces it as Result.error.
|
|
116
|
+
export class UnsafeIntegerError extends Error {
|
|
117
|
+
constructor(value) {
|
|
118
|
+
super(`bigint value ${value} exceeds JSON's safe integer range and cannot be sent without precision loss`);
|
|
119
|
+
this.name = 'UnsafeIntegerError';
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
const _literalsSurviveWidening = true;
|
|
123
|
+
void _literalsSurviveWidening;
|
|
124
|
+
// A handful of schemas are genuinely self-referential (e.g. the `and`/`or`
|
|
125
|
+
// legs of a filter tree), so nesting depth is bounded only by the DATA the
|
|
126
|
+
// server sends, not by the schema. Without a limit, a crafted or
|
|
127
|
+
// accidentally-deep response recurses until the JS engine throws a raw
|
|
128
|
+
// `RangeError: Maximum call stack size exceeded` — still caught by request()
|
|
129
|
+
// and surfaced as Result.error, but as an opaque native error instead of a
|
|
130
|
+
// typed one. 500 levels is far beyond any real filter/record/array nesting
|
|
131
|
+
// in the API today; it exists to fail predictably, not to constrain valid data.
|
|
132
|
+
const MAX_WALK_DEPTH = 500;
|
|
133
|
+
export class DepthLimitExceededError extends Error {
|
|
134
|
+
constructor() {
|
|
135
|
+
super(`wire mapping exceeded maximum nesting depth (${MAX_WALK_DEPTH})`);
|
|
136
|
+
this.name = 'DepthLimitExceededError';
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
function walk(data, schema, dir, depth = 0) {
|
|
140
|
+
if (data === null || data === undefined) {
|
|
141
|
+
return data;
|
|
142
|
+
}
|
|
143
|
+
// A Date can only ever mean its wire serialization, wherever it sits — a
|
|
144
|
+
// typed date field, a record value, or an unknown-schema position. Wire→
|
|
145
|
+
// public data never contains Date instances (it comes from JSON.parse), so
|
|
146
|
+
// this only rewrites public→wire. The same holds for bigint: JSON.parse
|
|
147
|
+
// never produces one, and public→wire it must become a JSON number wherever
|
|
148
|
+
// it sits.
|
|
149
|
+
if (data instanceof Date) {
|
|
150
|
+
return dir.mapDate(data);
|
|
151
|
+
}
|
|
152
|
+
if (typeof data === 'bigint') {
|
|
153
|
+
return dir.mapBigInt(data);
|
|
154
|
+
}
|
|
155
|
+
if (depth > MAX_WALK_DEPTH) {
|
|
156
|
+
throw new DepthLimitExceededError();
|
|
157
|
+
}
|
|
158
|
+
const s = unwrap(schema);
|
|
159
|
+
const d = def(s);
|
|
160
|
+
// A date-typed node maps between the public `Date` and the RFC 3339 wire
|
|
161
|
+
// string (fromWire revives the string; a string handed to toWire by an
|
|
162
|
+
// untyped caller passes through as-is).
|
|
163
|
+
if (d?.type === 'date') {
|
|
164
|
+
return dir.mapDate(data);
|
|
165
|
+
}
|
|
166
|
+
// A bigint-typed node revives the wire's JSON number into the public
|
|
167
|
+
// `bigint` (public→wire bigints were already mapped by the value check
|
|
168
|
+
// above, so only fromWire reaches a number here).
|
|
169
|
+
if (d?.type === 'bigint') {
|
|
170
|
+
return dir.mapBigInt(data);
|
|
171
|
+
}
|
|
172
|
+
if (Array.isArray(data)) {
|
|
173
|
+
// The schema may be the array itself or a union with an array variant
|
|
174
|
+
// (e.g. a single-or-batch body `T | T[]`); resolve the element schema from
|
|
175
|
+
// whichever applies so array items are still walked with their shape.
|
|
176
|
+
const element = arrayElement(s);
|
|
177
|
+
return data.map((item) => walk(item, element, dir, depth + 1));
|
|
178
|
+
}
|
|
179
|
+
if (typeof data !== 'object') {
|
|
180
|
+
// A wire datetime can sit behind a union (`DateTime | null`,
|
|
181
|
+
// enum-or-DateTime): revive the string only when the union's date variant
|
|
182
|
+
// is its sole plausible owner, so enum literals and plain-string variants
|
|
183
|
+
// pass through untouched.
|
|
184
|
+
if (typeof data === 'string' &&
|
|
185
|
+
d?.type === 'union' &&
|
|
186
|
+
unionDateClaims(s, data)) {
|
|
187
|
+
return dir.mapDate(data);
|
|
188
|
+
}
|
|
189
|
+
return data;
|
|
190
|
+
}
|
|
191
|
+
const record = data;
|
|
192
|
+
if (d?.type === 'record') {
|
|
193
|
+
// Record keys are user data (label/dimension names) — preserved verbatim.
|
|
194
|
+
// Only the value is walked, and only when it needs mapping. A null
|
|
195
|
+
// prototype avoids the `__proto__` key silently reassigning `out`'s own
|
|
196
|
+
// prototype instead of becoming a visible entry (user data may contain
|
|
197
|
+
// any key, including reserved object-literal property names). The
|
|
198
|
+
// prototype is restored once every key is a plain own property, so the
|
|
199
|
+
// returned object still behaves normally for consumers (instanceof,
|
|
200
|
+
// template literals) — `Object.prototype` itself was never touched.
|
|
201
|
+
const valueSchema = needsWalk(d.valueType) ? d.valueType : undefined;
|
|
202
|
+
const out = Object.create(null);
|
|
203
|
+
for (const [key, value] of Object.entries(record)) {
|
|
204
|
+
out[key] = valueSchema ? walk(value, valueSchema, dir, depth + 1) : value;
|
|
205
|
+
}
|
|
206
|
+
Object.setPrototypeOf(out, Object.prototype);
|
|
207
|
+
return out;
|
|
208
|
+
}
|
|
209
|
+
if (d?.type === 'union') {
|
|
210
|
+
const variant = selectVariant(record, s, dir);
|
|
211
|
+
if (!variant) {
|
|
212
|
+
// No confident match: leave keys untransformed rather than guess.
|
|
213
|
+
return data;
|
|
214
|
+
}
|
|
215
|
+
return walk(data, variant, dir, depth + 1);
|
|
216
|
+
}
|
|
217
|
+
if (d?.type === 'object') {
|
|
218
|
+
const shape = shapeOf(s) ?? {};
|
|
219
|
+
// A null prototype avoids two failure modes from data-controlled keys
|
|
220
|
+
// like `__proto__`/`constructor`: (1) `fieldFor` below reading an
|
|
221
|
+
// inherited Object.prototype member instead of correctly treating the
|
|
222
|
+
// key as schema-undeclared, and (2) the assignment at the end of this
|
|
223
|
+
// loop reassigning `out`'s own prototype instead of adding a visible key.
|
|
224
|
+
const out = Object.create(null);
|
|
225
|
+
for (const [key, value] of Object.entries(record)) {
|
|
226
|
+
const fieldSchema = fieldFor(shape, key);
|
|
227
|
+
// Keys the schema does not declare are dropped, so the result matches the
|
|
228
|
+
// typed shape exactly (a server-added field has no place in the type).
|
|
229
|
+
if (fieldSchema === undefined) {
|
|
230
|
+
continue;
|
|
231
|
+
}
|
|
232
|
+
out[dir.rename(key)] = walk(value, fieldSchema, dir, depth + 1);
|
|
233
|
+
}
|
|
234
|
+
if (dir.applyDefaults) {
|
|
235
|
+
// Shape keys are generated camelCase identifiers (never data-controlled),
|
|
236
|
+
// so direct indexing into `record` is safe here. Runs after the data loop
|
|
237
|
+
// so an explicit `key: undefined` entry is also replaced by the default.
|
|
238
|
+
for (const [key, fieldSchema] of Object.entries(shape)) {
|
|
239
|
+
if (record[key] !== undefined) {
|
|
240
|
+
continue;
|
|
241
|
+
}
|
|
242
|
+
const dflt = requiredDefault(fieldSchema);
|
|
243
|
+
if (dflt !== undefined) {
|
|
244
|
+
out[dir.rename(key)] = walk(dflt.value, fieldSchema, dir, depth + 1);
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
Object.setPrototypeOf(out, Object.prototype);
|
|
249
|
+
return out;
|
|
250
|
+
}
|
|
251
|
+
// Scalar or unknown schema: pass through untransformed.
|
|
252
|
+
return data;
|
|
253
|
+
}
|
|
254
|
+
// Resolve a data key to its field schema. The schema is camelCase-keyed; a
|
|
255
|
+
// wire→public data key is snake, so it is camelized to index the shape.
|
|
256
|
+
// Own-property checks (not `shape[key]`) so a data-controlled key like
|
|
257
|
+
// `__proto__` or `constructor` cannot resolve to an inherited
|
|
258
|
+
// Object.prototype member and be mistaken for a declared schema field.
|
|
259
|
+
function fieldFor(shape, dataKey) {
|
|
260
|
+
if (Object.hasOwn(shape, dataKey)) {
|
|
261
|
+
return shape[dataKey];
|
|
262
|
+
}
|
|
263
|
+
const camelKey = toCamelCase(dataKey);
|
|
264
|
+
return Object.hasOwn(shape, camelKey) ? shape[camelKey] : undefined;
|
|
265
|
+
}
|
|
266
|
+
function selectVariant(data, schema, dir) {
|
|
267
|
+
const d = def(schema);
|
|
268
|
+
const options = d?.options ?? [];
|
|
269
|
+
if (d?.discriminator && schema) {
|
|
270
|
+
// O(1) dispatch on the discriminator literal. The data key is the wire-name in
|
|
271
|
+
// fromWire (snake) and the public name in toWire (camel); the variant map is
|
|
272
|
+
// keyed by the literal value, which is identical in both directions.
|
|
273
|
+
const dataKey = dir.discriminatorKey(d.discriminator);
|
|
274
|
+
return variantsByDiscriminator(schema, d).get(data[dataKey]);
|
|
275
|
+
}
|
|
276
|
+
// Non-discriminated union: the codegen gate guarantees at most one object
|
|
277
|
+
// variant (it fails the build for a mapped union with two or more), so the single
|
|
278
|
+
// object-shaped option is unambiguous. Other variants (scalars, arrays) reach the
|
|
279
|
+
// walk through their own data-kind branches, not here.
|
|
280
|
+
return options.find((option) => def(unwrap(option))?.type === 'object');
|
|
281
|
+
}
|
|
282
|
+
// Memoized literal→variant map for a discriminated union, built once per schema.
|
|
283
|
+
const variantMapCache = new WeakMap();
|
|
284
|
+
function variantsByDiscriminator(schema, d) {
|
|
285
|
+
const cached = variantMapCache.get(schema);
|
|
286
|
+
if (cached) {
|
|
287
|
+
return cached;
|
|
288
|
+
}
|
|
289
|
+
const map = new Map();
|
|
290
|
+
for (const option of d.options ?? []) {
|
|
291
|
+
const shape = shapeOf(unwrap(option));
|
|
292
|
+
const literal = literalValue(shape?.[d.discriminator]);
|
|
293
|
+
if (literal !== undefined) {
|
|
294
|
+
map.set(literal, option);
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
variantMapCache.set(schema, map);
|
|
298
|
+
return map;
|
|
299
|
+
}
|
|
300
|
+
function literalValue(schema) {
|
|
301
|
+
const s = unwrap(schema);
|
|
302
|
+
if (def(s)?.type === 'literal') {
|
|
303
|
+
return s.value;
|
|
304
|
+
}
|
|
305
|
+
/* v8 ignore next -- a discriminated-union variant's discriminator is a literal */
|
|
306
|
+
return undefined;
|
|
307
|
+
}
|
|
308
|
+
// Whether a union's date variant is the sole plausible owner of a string
|
|
309
|
+
// value: the union carries a date option, no string-capable sibling (a plain
|
|
310
|
+
// string variant, an enum containing the value, an equal string literal)
|
|
311
|
+
// claims it, and the value actually parses as a date. `DateTime | null`
|
|
312
|
+
// revives its RFC 3339 string; `'immediate' | DateTime` keeps the enum
|
|
313
|
+
// literal a string. Fail-open: an unclaimed string stays a string.
|
|
314
|
+
function unionDateClaims(schema, value) {
|
|
315
|
+
let hasDate = false;
|
|
316
|
+
for (const option of def(schema)?.options ?? []) {
|
|
317
|
+
const od = def(unwrap(option));
|
|
318
|
+
if (od?.type === 'date') {
|
|
319
|
+
hasDate = true;
|
|
320
|
+
}
|
|
321
|
+
else if (od?.type === 'string') {
|
|
322
|
+
return false;
|
|
323
|
+
}
|
|
324
|
+
else if (od?.type === 'enum' &&
|
|
325
|
+
Object.values(od.entries ?? {}).includes(value)) {
|
|
326
|
+
return false;
|
|
327
|
+
}
|
|
328
|
+
else if (od?.type === 'literal' && literalValue(option) === value) {
|
|
329
|
+
return false;
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
return hasDate && !Number.isNaN(Date.parse(value));
|
|
333
|
+
}
|
|
334
|
+
const toWireDirection = {
|
|
335
|
+
rename: toSnakeCase,
|
|
336
|
+
discriminatorKey: (camelKey) => camelKey,
|
|
337
|
+
mapDate: (value) => (value instanceof Date ? value.toISOString() : value),
|
|
338
|
+
mapBigInt: (value) => {
|
|
339
|
+
if (typeof value !== 'bigint') {
|
|
340
|
+
return value;
|
|
341
|
+
}
|
|
342
|
+
if (value > BigInt(Number.MAX_SAFE_INTEGER) ||
|
|
343
|
+
value < -BigInt(Number.MAX_SAFE_INTEGER)) {
|
|
344
|
+
throw new UnsafeIntegerError(value);
|
|
345
|
+
}
|
|
346
|
+
return Number(value);
|
|
347
|
+
},
|
|
348
|
+
applyDefaults: true,
|
|
349
|
+
};
|
|
350
|
+
const fromWireDirection = {
|
|
351
|
+
rename: toCamelCase,
|
|
352
|
+
discriminatorKey: (camelKey) => toSnakeCase(camelKey),
|
|
353
|
+
mapDate: (value) => (typeof value === 'string' ? new Date(value) : value),
|
|
354
|
+
mapBigInt: (value) => typeof value === 'number' && Number.isInteger(value)
|
|
355
|
+
? BigInt(value)
|
|
356
|
+
: value,
|
|
357
|
+
applyDefaults: false,
|
|
358
|
+
};
|
|
359
|
+
// Rewrite a request body or query object from the camelCase public shape to the
|
|
360
|
+
// snake_case wire shape, driven by its schema. Record keys (label/dimension names)
|
|
361
|
+
// are preserved; `Date` values serialize to RFC 3339 strings; `bigint` values
|
|
362
|
+
// (int64 fields) become JSON numbers; omitted required-with-default fields are
|
|
363
|
+
// filled with their declared default (see requiredDefault). The return is typed
|
|
364
|
+
// as the input `T` so call sites stay cast-free (the runtime object has snake keys
|
|
365
|
+
// and wire-encoded dates, but the value is write-only — it flows straight into
|
|
366
|
+
// `json:`/`toURLSearchParams`, both of which accept any object).
|
|
367
|
+
export function toWire(data, schema) {
|
|
368
|
+
return walk(data, schema, toWireDirection);
|
|
369
|
+
}
|
|
370
|
+
// Rewrite a response body from the snake_case wire shape to the camelCase public
|
|
371
|
+
// shape: renames keys and revives RFC 3339 strings into `Date`s at date-typed
|
|
372
|
+
// nodes — never applies defaults or any other coercion. The result is the
|
|
373
|
+
// schema's output shape: `walk` produces exactly the schema's known fields in
|
|
374
|
+
// camelCase, so the inferred `output<S>` type describes the runtime value (the
|
|
375
|
+
// same wire-trust boundary as a plain `.json<T>()`, with no `.parse()`).
|
|
376
|
+
export function fromWire(data, schema) {
|
|
377
|
+
return walk(data, schema, fromWireDirection);
|
|
378
|
+
}
|
|
379
|
+
// Thrown by assertValid when the optional `validate` client option is on and data
|
|
380
|
+
// fails its schema. request() catches it like any Error and surfaces it as
|
|
381
|
+
// Result.error.
|
|
382
|
+
export class ValidationError extends Error {
|
|
383
|
+
issues;
|
|
384
|
+
constructor(message, issues) {
|
|
385
|
+
super(message);
|
|
386
|
+
this.issues = issues;
|
|
387
|
+
this.name = 'ValidationError';
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
// Opt-in schema check used by the funcs (when the validate option is on) against
|
|
391
|
+
// the snake_case wire payload: the request body after toWire, the raw response
|
|
392
|
+
// before fromWire, each against its generated `…Wire` schema. It is a GATE, not a
|
|
393
|
+
// transform — the safeParse output (coercions/defaults) is discarded, so validation
|
|
394
|
+
// never mutates the payload or return value. Off by default; the SDK does not
|
|
395
|
+
// validate by default (additive server fields must not break clients).
|
|
396
|
+
export function assertValid(schema, data) {
|
|
397
|
+
const result = schema.safeParse(data);
|
|
398
|
+
if (!result.success) {
|
|
399
|
+
throw new ValidationError('schema validation failed', result.error.issues);
|
|
400
|
+
}
|
|
401
|
+
}
|
package/dist/models/errors.d.ts
CHANGED
|
@@ -5,12 +5,23 @@ export declare class HTTPError extends Error {
|
|
|
5
5
|
title: string;
|
|
6
6
|
status: number;
|
|
7
7
|
url: string;
|
|
8
|
+
retryAfter: number | undefined;
|
|
8
9
|
protected __raw?: Record<string, unknown> | undefined;
|
|
9
|
-
constructor(message: string, type: string, title: string, status: number, url: string, __raw?: Record<string, unknown> | undefined);
|
|
10
|
+
constructor(message: string, type: string, title: string, status: number, url: string, retryAfter: number | undefined, __raw?: Record<string, unknown> | undefined);
|
|
10
11
|
static fromResponse(resp: {
|
|
11
12
|
response: Response;
|
|
12
13
|
error?: z.infer<typeof schemas.baseError>;
|
|
13
14
|
}): HTTPError;
|
|
15
|
+
/**
|
|
16
|
+
* The `invalid_parameters` problem-detail extension member, present on Bad
|
|
17
|
+
* Request (400) responses. Typed from the schema, not runtime-validated
|
|
18
|
+
* (like the rest of the SDK, this trusts the server rather than rejecting
|
|
19
|
+
* additive fields) — `undefined` when the response didn't carry one.
|
|
20
|
+
*/
|
|
21
|
+
get invalidParameters(): z.infer<typeof schemas.invalidParameters> | undefined;
|
|
22
|
+
/**
|
|
23
|
+
* Escape hatch for problem-detail extension members without a typed
|
|
24
|
+
* accessor above.
|
|
25
|
+
*/
|
|
14
26
|
getField(key: string): unknown;
|
|
15
27
|
}
|
|
16
|
-
//# sourceMappingURL=errors.d.ts.map
|