@arbol-ai/maximus 0.0.0-stage → 7.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.
- package/LICENSE +21 -0
- package/README.md +51 -2
- package/config/oxfmt.d.mts +8 -0
- package/config/oxfmt.mjs +30 -0
- package/config/oxlint-plugin.mjs +41 -0
- package/config/oxlint.d.mts +8 -0
- package/config/oxlint.mjs +118 -0
- package/dist/analytics/analytics.d.ts +34 -0
- package/dist/analytics/analytics.js +62 -0
- package/dist/body.d.ts +2 -0
- package/dist/body.js +4 -0
- package/dist/chunks/body-BcMbreNd.js +106 -0
- package/dist/chunks/correlation-gaiEoMBr.js +45 -0
- package/dist/chunks/execution-qfZuMSIu.js +700 -0
- package/dist/chunks/json-Dwni3u4c.js +63 -0
- package/dist/chunks/record-D2H3cg8S.js +36 -0
- package/dist/chunks/schemas-q-51wBMQ.js +216 -0
- package/dist/chunks/src-Ph5KHCRP.js +1160 -0
- package/dist/chunks/telemetry-DvZgrzZn.js +220 -0
- package/dist/chunks/types-CGwAhnTE.d.ts +1630 -0
- package/dist/chunks/validation-DWxKk1Js.js +382 -0
- package/dist/client/client.d.ts +131 -0
- package/dist/client/client.js +325 -0
- package/dist/diagnostics.d.ts +41 -0
- package/dist/diagnostics.js +4 -0
- package/dist/errors.d.ts +2 -0
- package/dist/errors.js +128 -0
- package/dist/http-paths.d.ts +2 -0
- package/dist/http-paths.js +200 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +11 -0
- package/dist/internals.d.ts +2 -0
- package/dist/internals.js +5 -0
- package/dist/mcp/index.d.ts +12 -0
- package/dist/mcp/index.js +406 -0
- package/dist/postgres/index.d.ts +81 -0
- package/dist/postgres/index.js +140 -0
- package/dist/projection.d.ts +36 -0
- package/dist/projection.js +60 -0
- package/dist/schemas.d.ts +2 -0
- package/dist/schemas.js +4 -0
- package/dist/standard-failures.d.ts +91 -0
- package/dist/standard-failures.js +75 -0
- package/dist/tooling/cli.js +110 -0
- package/dist/tooling/testing.d.ts +7 -0
- package/dist/tooling/testing.js +21 -0
- package/dist/webhooks.d.ts +69 -0
- package/dist/webhooks.js +162 -0
- package/dist/workers/runtime.d.ts +112 -0
- package/dist/workers/runtime.js +228 -0
- package/package.json +157 -4
- package/skills/maximus/SKILL.md +89 -0
- package/skills/maximus/references/adding-an-operation.md +179 -0
- package/skills/maximus/references/building-a-service.md +158 -0
- package/skills/maximus/references/errors-and-results.md +77 -0
- package/skills/maximus/references/migration-from-v2.md +62 -0
- package/skills/maximus/references/migration-to-v5.md +37 -0
- package/skills/maximus/references/publishing-and-verification.md +39 -0
- package/skills/maximus/references/testing.md +73 -0
- package/skills/maximus/references/transports.md +119 -0
- package/templates/service/.agents/skills/project/SKILL.md.template +10 -0
- package/templates/service/.github/workflows/ci.yml.template +21 -0
- package/templates/service/.gitignore.template +8 -0
- package/templates/service/CLAUDE.md.template +3 -0
- package/templates/service/README.md.template +45 -0
- package/templates/service/cloudflare.config.ts.template +30 -0
- package/templates/service/oxfmt.config.ts.template +1 -0
- package/templates/service/oxlint.config.ts.template +1 -0
- package/templates/service/package.json.template +31 -0
- package/templates/service/src/app.ts.template +30 -0
- package/templates/service/src/index.ts.template +13 -0
- package/templates/service/tests/contract.test.ts.template +12 -0
- package/templates/service/tsconfig.json.template +16 -0
- package/templates/service/vite.config.ts.template +7 -0
- package/templates/service/vitest.config.ts.template +10 -0
|
@@ -0,0 +1,1630 @@
|
|
|
1
|
+
import { OptionalKind, Static, Static as Static$1, Static as Static$2, StaticDecode, StaticDecode as StaticDecode$1, TArray, TLiteral, TObject, TOptional, TProperties, TSchema, TSchema as TSchema$1, TSchema as TSchema$2, TString, TTransform, TUnion, TUnsafe, Type as Type$1, Type as Type$2, Type as t, Type as t$1 } from "@sinclair/typebox";
|
|
2
|
+
//#region src/attachment-schema.d.ts
|
|
3
|
+
/** A file an HTTP operation answers with: its bytes, media type, filename and exact length. */
|
|
4
|
+
interface Attachment {
|
|
5
|
+
/** The file's bytes, as a buffer or an unlocked stream. */
|
|
6
|
+
body: ReadableStream<Uint8Array> | Uint8Array;
|
|
7
|
+
/** The media type sent as `content-type`: `type/subtype`, at most 255 characters. */
|
|
8
|
+
contentType: string;
|
|
9
|
+
/** The name sent in `content-disposition`: 1 to 512 characters, without control characters or slashes. */
|
|
10
|
+
filename: string;
|
|
11
|
+
/** Exact body length, checked before headers and while streaming. */
|
|
12
|
+
byteLength: number;
|
|
13
|
+
}
|
|
14
|
+
/** The output of an operation that answers with a file (`http.response: 'attachment'`). */
|
|
15
|
+
declare const AttachmentOutputSchema: import("@sinclair/typebox").TUnsafe<Attachment>;
|
|
16
|
+
//#endregion
|
|
17
|
+
//#region src/internal/correlation.d.ts
|
|
18
|
+
/**
|
|
19
|
+
* Request correlation: request ids (a UUID, or a ray id from a trusted Cloudflare edge), W3C `traceparent`
|
|
20
|
+
* parsing, and the correlation that durable work persists.
|
|
21
|
+
*/
|
|
22
|
+
/** Only technical support identifiers. Never use these values as authority or dedupe keys. */
|
|
23
|
+
declare function isRequestId(value: unknown): value is string;
|
|
24
|
+
/**
|
|
25
|
+
* Public ingress mints an id by default. Only pass trustedEdgeRay from the
|
|
26
|
+
* Cloudflare edge or a separately authenticated internal caller; arbitrary
|
|
27
|
+
* X-Request-Id/CF-Ray supplied by an untrusted caller must not be adopted.
|
|
28
|
+
*/
|
|
29
|
+
declare function createRequestId(trustedEdgeRay?: string | null): string;
|
|
30
|
+
/** W3C v00 only. Parsing is not SDK span activation, sampling approval or authentication. */
|
|
31
|
+
interface TraceParent {
|
|
32
|
+
/** The trace id: 32 lowercase hexadecimal characters, never all zeros. */
|
|
33
|
+
traceId: string;
|
|
34
|
+
/** The caller's span id, the header's `parent-id`: 16 lowercase hexadecimal characters, never all zeros. */
|
|
35
|
+
parentSpanId: string;
|
|
36
|
+
/** Whether the header's trace flags set the sampled bit; the caller's hint, not a sampling decision. */
|
|
37
|
+
sampled: boolean;
|
|
38
|
+
}
|
|
39
|
+
/** Reads a W3C `traceparent` header of version 00, or nothing when it is malformed or all zeros. */
|
|
40
|
+
declare function parseTraceParent(value: string | null | undefined): TraceParent | undefined;
|
|
41
|
+
/**
|
|
42
|
+
* Durable correlation is persisted with domain work; it does not keep a request
|
|
43
|
+
* span open for hours. The owner starts a new attempt span and links it to origin.
|
|
44
|
+
* No principal, bearer, baggage, arguments or customer ids belong in this object.
|
|
45
|
+
*/
|
|
46
|
+
interface DurableCorrelation {
|
|
47
|
+
/** The id of the request that started the work: a UUID or a Cloudflare ray id. */
|
|
48
|
+
requestId: string;
|
|
49
|
+
/**
|
|
50
|
+
* The originating request's W3C `traceparent`, which a new attempt span links to; absent when there was
|
|
51
|
+
* none or it did not parse.
|
|
52
|
+
*/
|
|
53
|
+
originTraceParent?: string;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* The correlation to persist with durable work: a valid request id and, when it parses, the originating
|
|
57
|
+
* `traceparent`.
|
|
58
|
+
*/
|
|
59
|
+
declare function durableCorrelation(requestId: string, originTraceParent?: string): DurableCorrelation;
|
|
60
|
+
//#endregion
|
|
61
|
+
//#region src/errors.d.ts
|
|
62
|
+
/** The `data` of a failure whose code declares none: an empty object. */
|
|
63
|
+
interface FailureData {}
|
|
64
|
+
/**
|
|
65
|
+
* A failure as every transport carries it: a stable code, a message for people that never contains input
|
|
66
|
+
* values, whether repeating the request may succeed, and the data its code declares.
|
|
67
|
+
*/
|
|
68
|
+
interface Failure<Data = FailureData> {
|
|
69
|
+
/** The failure code, stable across releases, that clients branch on, such as `NOT_FOUND`. */
|
|
70
|
+
code: string;
|
|
71
|
+
/** What went wrong, for people; it never contains input values. */
|
|
72
|
+
message: string;
|
|
73
|
+
/** Whether repeating the same request may succeed. */
|
|
74
|
+
retryable: boolean;
|
|
75
|
+
/**
|
|
76
|
+
* The details the code declares, such as `issues` for `INVALID_INPUT`; an empty object when it declares
|
|
77
|
+
* none.
|
|
78
|
+
*/
|
|
79
|
+
data: Data;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* One failure of a catalog: its code, HTTP status, message, retryability, Retry-After seconds and,
|
|
83
|
+
* optionally, the strict schema of its data.
|
|
84
|
+
*/
|
|
85
|
+
interface ErrorDefinition {
|
|
86
|
+
/** The stable code clients branch on, such as `RATE_LIMITED`; unique within the catalog. */
|
|
87
|
+
code: string;
|
|
88
|
+
/** The HTTP status that answers the failure, from 400 to 599. */
|
|
89
|
+
status: number;
|
|
90
|
+
/** A fixed message for people; it never carries request data. */
|
|
91
|
+
message: string;
|
|
92
|
+
/** Whether the same request may succeed later; false when absent. */
|
|
93
|
+
retryable?: boolean;
|
|
94
|
+
/** The seconds a client waits before retrying, sent as `Retry-After`. */
|
|
95
|
+
retryAfter?: number;
|
|
96
|
+
/** Explicit, strict domain diagnostics; omitted definitions retain empty data. */
|
|
97
|
+
dataSchema?: TObject<TProperties>;
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Every failure, whatever its code: the base each catalog's failures narrow. The OpenAPI document
|
|
101
|
+
* names it `Failure`, so every service's errors share one type.
|
|
102
|
+
*/
|
|
103
|
+
declare const FailureSchema: TObject<{
|
|
104
|
+
code: import("@sinclair/typebox").TString;
|
|
105
|
+
message: import("@sinclair/typebox").TString;
|
|
106
|
+
retryable: import("@sinclair/typebox").TBoolean;
|
|
107
|
+
data: TObject<{}>;
|
|
108
|
+
}>;
|
|
109
|
+
/** The standard failures as a catalog defines them; INVALID_INPUT carries its field issues. */
|
|
110
|
+
declare const STANDARD: {
|
|
111
|
+
readonly unauthorized: {
|
|
112
|
+
readonly code: 'UNAUTHORIZED';
|
|
113
|
+
readonly status: 401;
|
|
114
|
+
readonly message: 'A valid access token is required.';
|
|
115
|
+
};
|
|
116
|
+
readonly forbidden: {
|
|
117
|
+
readonly code: 'FORBIDDEN';
|
|
118
|
+
readonly status: 403;
|
|
119
|
+
readonly message: 'The token does not authorize this operation.';
|
|
120
|
+
};
|
|
121
|
+
readonly notFound: {
|
|
122
|
+
readonly code: 'NOT_FOUND';
|
|
123
|
+
readonly status: 404;
|
|
124
|
+
readonly message: 'The requested operation does not exist.';
|
|
125
|
+
};
|
|
126
|
+
readonly methodNotAllowed: {
|
|
127
|
+
readonly code: 'HTTP_METHOD_NOT_ALLOWED';
|
|
128
|
+
readonly status: 405;
|
|
129
|
+
readonly message: 'The HTTP method is not supported for this resource.';
|
|
130
|
+
};
|
|
131
|
+
readonly tooLarge: {
|
|
132
|
+
readonly code: 'REQUEST_TOO_LARGE';
|
|
133
|
+
readonly status: 413;
|
|
134
|
+
readonly message: 'The request exceeds the allowed size.';
|
|
135
|
+
};
|
|
136
|
+
readonly unsupportedMedia: {
|
|
137
|
+
readonly code: 'UNSUPPORTED_MEDIA_TYPE';
|
|
138
|
+
readonly status: 415;
|
|
139
|
+
readonly message: 'The request content type is not supported.';
|
|
140
|
+
};
|
|
141
|
+
readonly rateLimited: {
|
|
142
|
+
readonly code: 'RATE_LIMITED';
|
|
143
|
+
readonly status: 429;
|
|
144
|
+
readonly message: 'The request rate limit was exceeded. Try again later.';
|
|
145
|
+
readonly retryable: true;
|
|
146
|
+
readonly retryAfter: 60;
|
|
147
|
+
};
|
|
148
|
+
readonly internal: {
|
|
149
|
+
readonly code: 'INTERNAL_ERROR';
|
|
150
|
+
readonly status: 500;
|
|
151
|
+
readonly message: 'The service could not complete the operation.';
|
|
152
|
+
};
|
|
153
|
+
readonly upstreamUnavailable: {
|
|
154
|
+
readonly code: 'UPSTREAM_UNAVAILABLE';
|
|
155
|
+
readonly status: 503;
|
|
156
|
+
readonly message: 'The upstream service could not complete the request.';
|
|
157
|
+
readonly retryable: true;
|
|
158
|
+
};
|
|
159
|
+
readonly upstreamTimeout: {
|
|
160
|
+
readonly code: 'UPSTREAM_TIMEOUT';
|
|
161
|
+
readonly status: 504;
|
|
162
|
+
readonly message: 'The upstream service did not respond before its deadline.';
|
|
163
|
+
readonly retryable: true;
|
|
164
|
+
};
|
|
165
|
+
readonly outcomeUnknown: {
|
|
166
|
+
readonly code: 'OUTCOME_UNKNOWN';
|
|
167
|
+
readonly status: 502;
|
|
168
|
+
readonly message: 'The operation may have been applied; check its result before repeating it.';
|
|
169
|
+
};
|
|
170
|
+
readonly invalidInput: {
|
|
171
|
+
readonly code: 'INVALID_INPUT';
|
|
172
|
+
readonly status: 400;
|
|
173
|
+
readonly message: 'The request does not match the published schema.';
|
|
174
|
+
readonly dataSchema: TObject<{
|
|
175
|
+
issues: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TArray<TObject<{
|
|
176
|
+
path: import("@sinclair/typebox").TString;
|
|
177
|
+
message: import("@sinclair/typebox").TString;
|
|
178
|
+
}>>>;
|
|
179
|
+
}>;
|
|
180
|
+
};
|
|
181
|
+
};
|
|
182
|
+
/** The schema of a definition's data: the one it declares, or the empty object. */
|
|
183
|
+
type DefinitionDataSchema<D extends ErrorDefinition> = D extends {
|
|
184
|
+
dataSchema: infer S extends TObject<TProperties>;
|
|
185
|
+
} ? S : TObject<Record<never, never>>;
|
|
186
|
+
/** What a failure's `create()` takes: the data its definition declares, optional when every field is. */
|
|
187
|
+
type CreateArguments<D extends ErrorDefinition> = D extends {
|
|
188
|
+
dataSchema: TObject<TProperties>;
|
|
189
|
+
} ? Record<never, never> extends Static<DefinitionDataSchema<D>> ? [data?: Static<DefinitionDataSchema<D>>] : [data: Static<DefinitionDataSchema<D>>] : [];
|
|
190
|
+
/** The strict schema of one catalog failure: its fixed code and message, its retryability and its data. */
|
|
191
|
+
type FailureSchemaOf<D extends ErrorDefinition> = TObject<{
|
|
192
|
+
code: TLiteral<D['code']>;
|
|
193
|
+
message: TLiteral<D['message']>;
|
|
194
|
+
retryable: TLiteral<boolean>;
|
|
195
|
+
data: DefinitionDataSchema<D>;
|
|
196
|
+
}>;
|
|
197
|
+
/**
|
|
198
|
+
* One failure of a catalog, as `catalog.<name>` holds it: its schema, `create()`, which builds the failure,
|
|
199
|
+
* and `is()`, which recognizes it.
|
|
200
|
+
*/
|
|
201
|
+
interface ErrorFactory<D extends ErrorDefinition> {
|
|
202
|
+
/** The failure's strict schema: its fixed code and message, its retryability and its data. */
|
|
203
|
+
schema: FailureSchemaOf<D>;
|
|
204
|
+
/** The failure, with the data its definition declares: required unless every field is optional. */
|
|
205
|
+
create: (...args: CreateArguments<D>) => Failure<Static<DefinitionDataSchema<D>>> & Pick<D, 'code' | 'message'>;
|
|
206
|
+
/** Whether a value is exactly this failure. */
|
|
207
|
+
is: (value: unknown) => value is Static<FailureSchemaOf<D>>;
|
|
208
|
+
}
|
|
209
|
+
/** A catalog's factories, one per definition name. */
|
|
210
|
+
type Factories<D extends Record<string, ErrorDefinition>> = { [K in keyof D]: ErrorFactory<D[K]>; };
|
|
211
|
+
/**
|
|
212
|
+
* What every catalog provides besides each failure's factory: the union schema of its failures, their
|
|
213
|
+
* definitions, and a failure's status, Retry-After and validation by code.
|
|
214
|
+
*/
|
|
215
|
+
interface ErrorCatalog {
|
|
216
|
+
/** The union of the catalog's failure schemas, one strict schema per definition. */
|
|
217
|
+
schema: TSchema;
|
|
218
|
+
/** Every definition of the catalog, standard and domain, by name. */
|
|
219
|
+
definitions: Readonly<Record<string, ErrorDefinition>>;
|
|
220
|
+
/** The HTTP status of a code; 500 for a code the catalog does not define. */
|
|
221
|
+
status: (code: string) => number;
|
|
222
|
+
/** The Retry-After seconds a code declares, if any. */
|
|
223
|
+
retryAfter: (code: string) => number | undefined;
|
|
224
|
+
/** The value as one of the catalog's failures; throws a TypeError when it is none of them. */
|
|
225
|
+
validate: (error: unknown) => Failure;
|
|
226
|
+
}
|
|
227
|
+
/** The union schema of a set of definitions: one strict failure schema per definition. */
|
|
228
|
+
type CatalogSchema<Definitions extends Record<string, ErrorDefinition>> = TUnion<{ [K in keyof Definitions]: FailureSchemaOf<Definitions[K]>; }[keyof Definitions][]>;
|
|
229
|
+
/** A catalog over a set of definitions: a factory per name, and the members of `ErrorCatalog`. */
|
|
230
|
+
type Catalog<Definitions extends Record<string, ErrorDefinition>> = Factories<Definitions> & {
|
|
231
|
+
/** The union of the catalog's failure schemas. */
|
|
232
|
+
schema: CatalogSchema<Definitions>;
|
|
233
|
+
/** Every definition of the catalog, by name. */
|
|
234
|
+
definitions: Definitions;
|
|
235
|
+
/** The HTTP status of a code; 500 for a code the catalog does not define. */
|
|
236
|
+
status: (code: string) => number;
|
|
237
|
+
/** The Retry-After seconds a code declares, if any. */
|
|
238
|
+
retryAfter: (code: string) => number | undefined;
|
|
239
|
+
/** The value as one of the catalog's failures; a TypeError when it is none of them. */
|
|
240
|
+
validate: (error: unknown) => Static<CatalogSchema<Definitions>>;
|
|
241
|
+
};
|
|
242
|
+
/**
|
|
243
|
+
* What `defineErrors` returns for an application's own definitions `D`: a factory for each failure,
|
|
244
|
+
* standard and domain, by name, and the members every `ErrorCatalog` has.
|
|
245
|
+
*/
|
|
246
|
+
type DefinedErrors<D extends Record<string, ErrorDefinition>> = Catalog<typeof STANDARD & D>;
|
|
247
|
+
/**
|
|
248
|
+
* The names a domain failure cannot take: the standard failures' (every catalog has them) and the
|
|
249
|
+
* catalog's own members (they would hide the failure's factory).
|
|
250
|
+
*/
|
|
251
|
+
type ReservedName = keyof typeof STANDARD | keyof ErrorCatalog;
|
|
252
|
+
/** The domain definitions as given, or, when they use a reserved name, a type that refuses each one. */
|
|
253
|
+
type WithoutReservedNames<D> = [Extract<keyof D, ReservedName>] extends [never] ? D : Readonly<Record<Extract<keyof D, ReservedName>, never>>;
|
|
254
|
+
/**
|
|
255
|
+
* An error catalog: the standard failures and the application's own, each with a fixed code, status, message
|
|
256
|
+
* and retry semantics. `catalog.name.create(data?)` builds a failure; the application validates every failure
|
|
257
|
+
* an operation returns against the catalog.
|
|
258
|
+
*/
|
|
259
|
+
declare function defineErrors<const D extends Record<string, ErrorDefinition>>(domain: WithoutReservedNames<D>): DefinedErrors<D>;
|
|
260
|
+
/**
|
|
261
|
+
* The catalog of the framework's own failures (`INVALID_INPUT`, `UNAUTHORIZED`, `FORBIDDEN`, `NOT_FOUND`, …).
|
|
262
|
+
*/
|
|
263
|
+
declare const standardErrors: DefinedErrors<{}>;
|
|
264
|
+
/** The schema of a Result: `{ ok: true, value } | { ok: false, error }`. */
|
|
265
|
+
declare function resultSchema<O extends TSchema, F extends TSchema>(value: O, error: F): TUnion<[TObject<{
|
|
266
|
+
ok: TLiteral<true>;
|
|
267
|
+
value: O;
|
|
268
|
+
}>, TObject<{
|
|
269
|
+
ok: TLiteral<false>;
|
|
270
|
+
error: F;
|
|
271
|
+
}>]>;
|
|
272
|
+
//#endregion
|
|
273
|
+
//#region src/result.d.ts
|
|
274
|
+
/** Explicit operation outcome. Transports keep their own status and protocol envelope. */
|
|
275
|
+
interface Success<T> {
|
|
276
|
+
/** Always `true`, which marks a success. */
|
|
277
|
+
ok: true;
|
|
278
|
+
/** What the success carries, such as an operation's output. */
|
|
279
|
+
value: T;
|
|
280
|
+
}
|
|
281
|
+
/** A failed outcome and its error. */
|
|
282
|
+
interface FailureResult<E> {
|
|
283
|
+
/** Always `false`, which marks a failure. */
|
|
284
|
+
ok: false;
|
|
285
|
+
/** What went wrong, usually a catalog failure from `catalog.someFailure.create()`. */
|
|
286
|
+
error: E;
|
|
287
|
+
}
|
|
288
|
+
/**
|
|
289
|
+
* What an operation returns: a success with its value or a failure with its error. Expected failures are
|
|
290
|
+
* results, never exceptions.
|
|
291
|
+
*/
|
|
292
|
+
type Result<T, E> = Success<T> | FailureResult<E>;
|
|
293
|
+
/** A success carrying `value`. */
|
|
294
|
+
declare function ok<T>(value: T): Success<T>;
|
|
295
|
+
/** A failure carrying `error`, usually `catalog.someFailure.create()`. */
|
|
296
|
+
declare function err<E>(error: E): FailureResult<E>;
|
|
297
|
+
/**
|
|
298
|
+
* Whether an untrusted answer (a Service Binding's, a stored one) is a Maximus Result: a success with
|
|
299
|
+
* its value, or a failure with a well-formed error. Anything else is not an answer at all.
|
|
300
|
+
*/
|
|
301
|
+
declare function isResult(value: unknown): value is Result<unknown, Failure>;
|
|
302
|
+
//#endregion
|
|
303
|
+
//#region src/binary.d.ts
|
|
304
|
+
/** The raw body of an operation declared with `http.body: 'bytes'`, read on demand. */
|
|
305
|
+
interface BinaryBody {
|
|
306
|
+
/** Read once within this operation's byte limit, after the owner admits the target resource. */
|
|
307
|
+
read: () => Promise<Result<Uint8Array<ArrayBuffer>, Failure>>;
|
|
308
|
+
}
|
|
309
|
+
//#endregion
|
|
310
|
+
//#region src/scopes.d.ts
|
|
311
|
+
/**
|
|
312
|
+
* Scope names and matching: the namespaced form every scope takes, and whether granted scopes admit a
|
|
313
|
+
* request.
|
|
314
|
+
*/
|
|
315
|
+
/** Whether a caller needs any one of several scopes (`any`, the default) or all of them. */
|
|
316
|
+
type ScopeMatch = 'any' | 'all';
|
|
317
|
+
/**
|
|
318
|
+
* Whether granted scopes admit a requirement: any one of the required scopes, or all of them with `all`.
|
|
319
|
+
* An empty requirement admits nobody.
|
|
320
|
+
*/
|
|
321
|
+
declare function matchesScopes(required: readonly string[], granted: readonly string[], match?: ScopeMatch): boolean;
|
|
322
|
+
//#endregion
|
|
323
|
+
//#region src/oauth.d.ts
|
|
324
|
+
/**
|
|
325
|
+
* OAuth protected-resource metadata (RFC 9728): the resources an application publishes and the document
|
|
326
|
+
* that describes each one.
|
|
327
|
+
*/
|
|
328
|
+
/** One OAuth resource published by an application, independent of its deployment hostname. */
|
|
329
|
+
interface OAuthResource<E = unknown> {
|
|
330
|
+
/** Exact audience clients request from the authorization server. */
|
|
331
|
+
audience: string | ((env: E) => string);
|
|
332
|
+
/** Verified issuer identifiers; this declaration does not change token verification. */
|
|
333
|
+
authorizationServers: readonly string[] | ((env: E) => readonly string[]);
|
|
334
|
+
/** OAuth grants supported by the owner, which may differ from internal operation scopes. */
|
|
335
|
+
scopes: readonly string[];
|
|
336
|
+
}
|
|
337
|
+
/** Serialize RFC 9728 metadata without publishing credentials or accepting request-derived authority. */
|
|
338
|
+
declare function protectedResourceMetadata<E>(resource: OAuthResource<E>, env: E): {
|
|
339
|
+
resource: string;
|
|
340
|
+
authorization_servers: string[];
|
|
341
|
+
bearer_methods_supported: string[];
|
|
342
|
+
scopes_supported: string[];
|
|
343
|
+
};
|
|
344
|
+
//#endregion
|
|
345
|
+
//#region src/tokens.d.ts
|
|
346
|
+
/**
|
|
347
|
+
* Who calls, as every operation receives it: the actor, its kind, its verified organization and the request
|
|
348
|
+
* id.
|
|
349
|
+
*/
|
|
350
|
+
declare const PrincipalSchema: import("@sinclair/typebox").TObject<{
|
|
351
|
+
actorId: import("@sinclair/typebox").TString;
|
|
352
|
+
actorKind: import("@sinclair/typebox").TUnion<[import("@sinclair/typebox").TLiteral<"user">, import("@sinclair/typebox").TLiteral<"service">]>;
|
|
353
|
+
organizationId: import("@sinclair/typebox").TString;
|
|
354
|
+
requestId: import("@sinclair/typebox").TString;
|
|
355
|
+
}>;
|
|
356
|
+
/** Who calls: the actor, its kind, its organization and the request id. */
|
|
357
|
+
type Principal = Static<typeof PrincipalSchema>;
|
|
358
|
+
/**
|
|
359
|
+
* The OAuth client a token was issued to: its id and its class, `human`, `machine` or `dcr` (a dynamically
|
|
360
|
+
* registered client).
|
|
361
|
+
*/
|
|
362
|
+
declare const TokenClientSchema: import("@sinclair/typebox").TObject<{
|
|
363
|
+
id: import("@sinclair/typebox").TString;
|
|
364
|
+
class: import("@sinclair/typebox").TUnion<[import("@sinclair/typebox").TLiteral<"human">, import("@sinclair/typebox").TLiteral<"machine">, import("@sinclair/typebox").TLiteral<"dcr">]>;
|
|
365
|
+
}>;
|
|
366
|
+
/** The OAuth client a token was issued to. */
|
|
367
|
+
type TokenClient = Static<typeof TokenClientSchema>;
|
|
368
|
+
/**
|
|
369
|
+
* What a verified token grants: the principal without a request id, its scopes and, for an OAuth token, its
|
|
370
|
+
* client. A custom `tokens.resolve` returns this shape.
|
|
371
|
+
*/
|
|
372
|
+
declare const TokenPrincipalSchema: import("@sinclair/typebox").TObject<{
|
|
373
|
+
actorId: import("@sinclair/typebox").TString;
|
|
374
|
+
actorKind: import("@sinclair/typebox").TUnion<[import("@sinclair/typebox").TLiteral<"user">, import("@sinclair/typebox").TLiteral<"service">]>;
|
|
375
|
+
organizationId: import("@sinclair/typebox").TString;
|
|
376
|
+
scopes: import("@sinclair/typebox").TArray<import("@sinclair/typebox").TString>;
|
|
377
|
+
/** Custom resolvers must classify DCR from verified application policy, never request headers. */
|
|
378
|
+
client: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TObject<{
|
|
379
|
+
id: import("@sinclair/typebox").TString;
|
|
380
|
+
class: import("@sinclair/typebox").TUnion<[import("@sinclair/typebox").TLiteral<"human">, import("@sinclair/typebox").TLiteral<"machine">, import("@sinclair/typebox").TLiteral<"dcr">]>;
|
|
381
|
+
}>>;
|
|
382
|
+
}>;
|
|
383
|
+
/** What a verified token grants. */
|
|
384
|
+
type TokenPrincipal = Static<typeof TokenPrincipalSchema>;
|
|
385
|
+
/**
|
|
386
|
+
* How callers obtain and present the access token, as published in OpenAPI
|
|
387
|
+
* and OAuth discovery.
|
|
388
|
+
*
|
|
389
|
+
* - `oauth` (default): an OAuth 2.0 access token from the issuer. OpenAPI
|
|
390
|
+
* publishes the authorization-code and client-credentials flows, and
|
|
391
|
+
* `/.well-known/oauth-protected-resource` names the issuer.
|
|
392
|
+
* - `bearer`: a token the application issues and verifies itself through
|
|
393
|
+
* `resolve` (a signed visitor session, for example). OpenAPI publishes a
|
|
394
|
+
* plain HTTP bearer scheme with `description` saying how to obtain one, no
|
|
395
|
+
* OAuth discovery is served, and a 401 does not point at any. It requires
|
|
396
|
+
* `resolve` and cannot serve the MCP transport, whose clients discover
|
|
397
|
+
* authorization through OAuth.
|
|
398
|
+
*/
|
|
399
|
+
type TokenScheme = {
|
|
400
|
+
type: 'oauth';
|
|
401
|
+
} | {
|
|
402
|
+
type: 'bearer';
|
|
403
|
+
description: string;
|
|
404
|
+
} | {
|
|
405
|
+
type: 'oauth+bearer';
|
|
406
|
+
description: string;
|
|
407
|
+
};
|
|
408
|
+
/** Request-local authority resolution; callbacks must never retain this context between requests. */
|
|
409
|
+
interface TokenResolutionContext<E = unknown> {
|
|
410
|
+
/** The request's environment bindings, as scoped by `aroundOperation` when it wraps the operation. */
|
|
411
|
+
readonly env: E;
|
|
412
|
+
/** The incoming request being admitted. */
|
|
413
|
+
readonly request: Request;
|
|
414
|
+
/** Where the request arrived: `mcp` at a transport's path, such as `/mcp`, else `http`. */
|
|
415
|
+
readonly surface: 'http' | 'mcp';
|
|
416
|
+
/** Declared application scopes, compiled once; these are capabilities to map, never caller grants. */
|
|
417
|
+
readonly applicationScopes: readonly string[];
|
|
418
|
+
/**
|
|
419
|
+
* Keeps work running after the response, such as the Worker's `ExecutionContext`; absent when the handler
|
|
420
|
+
* was called without one.
|
|
421
|
+
*/
|
|
422
|
+
readonly background?: BackgroundContext;
|
|
423
|
+
}
|
|
424
|
+
/** The JWS algorithms an issuer may sign access tokens with: asymmetric ones, which a JWKS can verify. */
|
|
425
|
+
type TokenAlgorithm = 'RS256' | 'RS384' | 'RS512' | 'PS256' | 'PS384' | 'PS512' | 'ES256' | 'ES384' | 'ES512' | 'EdDSA';
|
|
426
|
+
/**
|
|
427
|
+
* Where an authorization server's access tokens put what the principal needs, and the subject its machines
|
|
428
|
+
* carry, when they differ from the defaults. A claim is named as it is, or by a dotted path into an object
|
|
429
|
+
* claim such as `tenant.id`; a claim whose own name has dots, such as `https://example.com/org_id`, is read
|
|
430
|
+
* as it is named.
|
|
431
|
+
*/
|
|
432
|
+
interface TokenClaims {
|
|
433
|
+
/** The claim that holds the caller's organization id; `org_id` by default. */
|
|
434
|
+
organization?: string;
|
|
435
|
+
/**
|
|
436
|
+
* The claim that holds the granted scopes, a space-separated string or a list; `scope` by default (`scp`
|
|
437
|
+
* for Okta, `permissions` for WorkOS session tokens).
|
|
438
|
+
*/
|
|
439
|
+
scopes?: string;
|
|
440
|
+
/**
|
|
441
|
+
* The claim that holds the OAuth client id; by default `azp`, else `client_id`, which must agree when both
|
|
442
|
+
* are present (`cid` for Okta).
|
|
443
|
+
*/
|
|
444
|
+
client?: string;
|
|
445
|
+
/**
|
|
446
|
+
* The claim that lists what a person may do, a space-separated string or a list; `permissions` by default,
|
|
447
|
+
* as Auth0 issues it with RBAC. A person holds only the granted scopes it also lists, so a token without it
|
|
448
|
+
* grants a person none. `false` trusts the granted scopes alone: set it only for an authorization server
|
|
449
|
+
* that grants each person no more than they may hold, such as Okta with access policies by group, never
|
|
450
|
+
* for one that grants whatever scope a client requests.
|
|
451
|
+
*/
|
|
452
|
+
permissions?: string | false;
|
|
453
|
+
/**
|
|
454
|
+
* The subject of a machine's token, given its client id: `{client}@clients` by default, as Auth0 issues
|
|
455
|
+
* them, or `{client}`, the client id itself, as RFC 9068 recommends and Okta and WorkOS issue them. Set
|
|
456
|
+
* `{client}` only when the authorization server assigns every client id, so none can equal a person's
|
|
457
|
+
* subject (RFC 9068, section 5).
|
|
458
|
+
*/
|
|
459
|
+
machineSubject?: '{client}@clients' | '{client}';
|
|
460
|
+
}
|
|
461
|
+
/** A token `cf dev` accepts from localhost, so a service runs without an authorization server. */
|
|
462
|
+
interface TokenDevelopment {
|
|
463
|
+
/**
|
|
464
|
+
* The env binding that holds the token, such as `DEVELOPMENT_TOKEN` set in `.dev.vars` and declared in
|
|
465
|
+
* `cloudflare.config.ts` for the development mode alone; at least 32 characters, or it never matches.
|
|
466
|
+
*/
|
|
467
|
+
binding: string;
|
|
468
|
+
/** The organization the developer acts in; `local-development` by default. */
|
|
469
|
+
organizationId?: string;
|
|
470
|
+
}
|
|
471
|
+
/**
|
|
472
|
+
* How the application verifies bearer tokens: the audiences they must carry, the OAuth issuer and its JWKS,
|
|
473
|
+
* or the application's own `resolve`, the scheme OpenAPI publishes and the OAuth discovery it serves.
|
|
474
|
+
*/
|
|
475
|
+
interface TokenPolicy<E = unknown> {
|
|
476
|
+
/**
|
|
477
|
+
* The audience access tokens must carry, or a list of audiences any one of which a token may carry
|
|
478
|
+
* (the service's own and a gateway's in front of it, for example). The first is the canonical
|
|
479
|
+
* resource that OAuth discovery names.
|
|
480
|
+
*/
|
|
481
|
+
audience: string | readonly string[];
|
|
482
|
+
/**
|
|
483
|
+
* The OAuth authorization server, an absolute HTTPS URL; the default verification requires it as `iss`.
|
|
484
|
+
* Required unless `scheme.type` is `bearer`.
|
|
485
|
+
*/
|
|
486
|
+
issuer?: string;
|
|
487
|
+
/** The issuer's JWKS URL, resolved against `issuer` and HTTPS only; defaults to `.well-known/jwks.json`. */
|
|
488
|
+
jwksUri?: string;
|
|
489
|
+
/**
|
|
490
|
+
* The authorization endpoint OpenAPI publishes for the authorization-code flow, resolved against `issuer`
|
|
491
|
+
* and HTTPS only; `authorize` by default, as Auth0 serves it. The server's metadata names it as
|
|
492
|
+
* `authorization_endpoint`.
|
|
493
|
+
*/
|
|
494
|
+
authorizationEndpoint?: string;
|
|
495
|
+
/**
|
|
496
|
+
* The token endpoint OpenAPI publishes for both flows, resolved against `issuer` and HTTPS only;
|
|
497
|
+
* `oauth/token` by default, as Auth0 serves it. The server's metadata names it as `token_endpoint`.
|
|
498
|
+
*/
|
|
499
|
+
tokenEndpoint?: string;
|
|
500
|
+
/** The algorithms the issuer signs access tokens with; `['RS256']` by default. */
|
|
501
|
+
algorithms?: readonly TokenAlgorithm[];
|
|
502
|
+
/**
|
|
503
|
+
* Where the access tokens put the organization, the scopes, the client and a person's permissions, and the
|
|
504
|
+
* subject a machine's carries; the standard claims and Auth0's conventions by default.
|
|
505
|
+
*/
|
|
506
|
+
claims?: TokenClaims;
|
|
507
|
+
/**
|
|
508
|
+
* A token accepted from localhost as a developer holding every scope the application declares. A request
|
|
509
|
+
* to any other host never matches, and neither does a token shorter than 32 characters. Never set the
|
|
510
|
+
* token on a deployed Worker.
|
|
511
|
+
*/
|
|
512
|
+
development?: TokenDevelopment;
|
|
513
|
+
/** Native Results express expected credential refusals; a raw verified identity remains supported. */
|
|
514
|
+
resolve?: (token: string, context?: TokenResolutionContext<E>) => Promise<TokenPrincipal | Result<TokenPrincipal, Failure>>;
|
|
515
|
+
/** Absolute discovery URL when a mounted API and its OAuth resource have different origins or paths. */
|
|
516
|
+
resourceMetadataUrl?: string | null | ((context: TokenResolutionContext<E>) => string | null);
|
|
517
|
+
/** Absolute RFC 9728 paths for distinct resources; replaces inferred discovery when configured. */
|
|
518
|
+
resources?: Readonly<Record<string, OAuthResource<E>>>;
|
|
519
|
+
/**
|
|
520
|
+
* How clients obtain tokens, as OpenAPI and discovery publish it: `oauth` (the default), `bearer` for
|
|
521
|
+
* tokens the application issues, or `oauth+bearer` for both.
|
|
522
|
+
*/
|
|
523
|
+
scheme?: TokenScheme;
|
|
524
|
+
}
|
|
525
|
+
/**
|
|
526
|
+
* The principal a verified access token grants, read where `mapping` says. A subject of the shape
|
|
527
|
+
* `mapping.machineSubject` names, `<client id>@clients` by default, is a machine; anyone else is a person,
|
|
528
|
+
* who holds only the granted scopes their permissions claim also lists. Malformed or inconsistent claims
|
|
529
|
+
* are refused.
|
|
530
|
+
*/
|
|
531
|
+
declare function projectToken(claims: unknown, mapping?: TokenClaims): Result<TokenPrincipal, Failure>;
|
|
532
|
+
/** A token is accepted when its `aud` names any one of the policy's audiences. */
|
|
533
|
+
declare function createTokenVerifier<E>(policy: TokenPolicy<E>): (token: string) => Promise<import("jose").JWTPayload>;
|
|
534
|
+
/**
|
|
535
|
+
* Admits a request by its bearer token: verifies it (OAuth by JWKS, or the policy's own `resolve`) and checks
|
|
536
|
+
* that it carries the required scope. Refusals are `UNAUTHORIZED` or `FORBIDDEN` failures, never exceptions.
|
|
537
|
+
*/
|
|
538
|
+
declare function createTokenAuthorization<E>(policy: TokenPolicy<E>, given?: (token: string) => Promise<unknown>): (request: Request, scope: string | readonly string[], context?: TokenResolutionContext<E>, scopeMatch?: ScopeMatch) => Promise<Result<TokenPrincipal, Failure>>;
|
|
539
|
+
//#endregion
|
|
540
|
+
//#region src/telemetry.d.ts
|
|
541
|
+
/** Where background work goes: the runtime's `waitUntil`, such as a Worker's ExecutionContext. */
|
|
542
|
+
interface BackgroundContext {
|
|
543
|
+
/**
|
|
544
|
+
* Keeps `promise` running after the response, as a Worker's `ctx.waitUntil` does. Best effort: never
|
|
545
|
+
* durable.
|
|
546
|
+
*/
|
|
547
|
+
waitUntil: (promise: Promise<unknown>) => void;
|
|
548
|
+
}
|
|
549
|
+
/**
|
|
550
|
+
* What observers learn about one operation call: the service, operation, principal and surface, when and for
|
|
551
|
+
* how long, the outcome and its failure code, and the active trace id. Never input, output or credentials.
|
|
552
|
+
*/
|
|
553
|
+
interface ExecutionObservation {
|
|
554
|
+
/** The application's `name`, its service identifier. */
|
|
555
|
+
service: string;
|
|
556
|
+
/** The operation's name, its key among the application's operations. */
|
|
557
|
+
operation: string;
|
|
558
|
+
/** The caller the operation ran as: its actor, actor kind, organization and request id. */
|
|
559
|
+
principal: Principal;
|
|
560
|
+
/**
|
|
561
|
+
* Where the call arrived: `http` at an operation route, `mcp` through a transport (MCP tool calls), `rpc`
|
|
562
|
+
* over Worker RPC.
|
|
563
|
+
*/
|
|
564
|
+
surface: 'http' | 'mcp' | 'rpc';
|
|
565
|
+
/** When the call was recorded, just after it finished, in Unix milliseconds. */
|
|
566
|
+
occurredAt: number;
|
|
567
|
+
/** Wall-clock time the operation's execution took, in whole microseconds; not CPU time. */
|
|
568
|
+
durationMicros: number;
|
|
569
|
+
/** `success` when the operation answered its output, `failure` when it answered a failure. */
|
|
570
|
+
outcome: 'success' | 'failure';
|
|
571
|
+
/** The failure's code, such as `INVALID_INPUT`, when `outcome` is `failure`; null on success. */
|
|
572
|
+
errorCode: string | null;
|
|
573
|
+
/**
|
|
574
|
+
* The active exporter trace id that `telemetry.traceId` returned, 32 lowercase hexadecimal characters; null
|
|
575
|
+
* when it returned none or an invalid one.
|
|
576
|
+
*/
|
|
577
|
+
traceId: string | null;
|
|
578
|
+
}
|
|
579
|
+
/**
|
|
580
|
+
* What observers learn about a request refused before any operation ran: the operation and principal when
|
|
581
|
+
* known, the surface, its timing and the failure code.
|
|
582
|
+
*/
|
|
583
|
+
interface AdmissionObservation {
|
|
584
|
+
/** The application's `name`, its service identifier. */
|
|
585
|
+
service: string;
|
|
586
|
+
/**
|
|
587
|
+
* The name of the operation the refusal concerns; null when there is none, such as for an unknown path or a
|
|
588
|
+
* whole transport request.
|
|
589
|
+
*/
|
|
590
|
+
operation: string | null;
|
|
591
|
+
/**
|
|
592
|
+
* The verified caller when the refusal came after token verification, such as a rate limit; null otherwise.
|
|
593
|
+
*/
|
|
594
|
+
principal: Principal | null;
|
|
595
|
+
/** The refused request's id, echoed in its `x-request-id` header. */
|
|
596
|
+
requestId: string;
|
|
597
|
+
/**
|
|
598
|
+
* The active exporter trace id that `telemetry.traceId` returned, 32 lowercase hexadecimal characters; null
|
|
599
|
+
* when it returned none or an invalid one.
|
|
600
|
+
*/
|
|
601
|
+
traceId: string | null;
|
|
602
|
+
/** Where the request arrived: `mcp` at a transport's path, such as `/mcp`, else `http`. */
|
|
603
|
+
surface: 'http' | 'mcp';
|
|
604
|
+
/** When the refusal was recorded, in Unix milliseconds. */
|
|
605
|
+
occurredAt: number;
|
|
606
|
+
/** Wall-clock time from the start of admission to the refusal, in whole microseconds. */
|
|
607
|
+
durationMicros: number;
|
|
608
|
+
/** The refusal's failure code, such as `UNAUTHORIZED`, `NOT_FOUND` or `RATE_LIMITED`. */
|
|
609
|
+
errorCode: string;
|
|
610
|
+
}
|
|
611
|
+
/**
|
|
612
|
+
* Receives observations as background work: operation calls, refused admissions and unexpected errors. An
|
|
613
|
+
* observer that fails affects neither the response nor another observer.
|
|
614
|
+
*/
|
|
615
|
+
interface Observer<E> {
|
|
616
|
+
/**
|
|
617
|
+
* Receives the observation of every operation call, success or failure, on every surface, with that call's
|
|
618
|
+
* env.
|
|
619
|
+
*/
|
|
620
|
+
execution?: (event: ExecutionObservation, env: E) => Promise<unknown>;
|
|
621
|
+
/**
|
|
622
|
+
* Receives the observation of each refusal before execution, such as a bad token or a rate limit, with the
|
|
623
|
+
* request's env.
|
|
624
|
+
*/
|
|
625
|
+
admission?: (event: AdmissionObservation, env: E) => Promise<unknown>;
|
|
626
|
+
/**
|
|
627
|
+
* Called synchronously with each unexpected error as thrown, its surface (such as `http` or `operation`)
|
|
628
|
+
* and the request id when known; record only a safe description.
|
|
629
|
+
*/
|
|
630
|
+
error?: (error: unknown, surface: string, requestId?: string) => void;
|
|
631
|
+
}
|
|
632
|
+
/**
|
|
633
|
+
* Whether diagnostics and observers run (for each env, when a function), the observers, and how to read the
|
|
634
|
+
* active trace id.
|
|
635
|
+
*/
|
|
636
|
+
interface TelemetryConfig<E> {
|
|
637
|
+
/**
|
|
638
|
+
* Whether Maximus writes its log events, calls observers and annotates spans, or a function that decides
|
|
639
|
+
* per env; on by default.
|
|
640
|
+
*/
|
|
641
|
+
enabled?: boolean | ((env: E) => boolean);
|
|
642
|
+
/**
|
|
643
|
+
* The observers that receive execution and admission observations and unexpected errors, each isolated from
|
|
644
|
+
* the others.
|
|
645
|
+
*/
|
|
646
|
+
observers?: readonly Observer<E>[];
|
|
647
|
+
/** Read an actual active exporter trace, never a client-supplied header or generated correlation ID. */
|
|
648
|
+
traceId?: () => string | null;
|
|
649
|
+
}
|
|
650
|
+
/**
|
|
651
|
+
* Whether telemetry is on for this env: it is unless `enabled` is false, or a function that returns false or
|
|
652
|
+
* has no env to read.
|
|
653
|
+
*/
|
|
654
|
+
declare function telemetryEnabled<E>(config: Pick<TelemetryConfig<E>, 'enabled'> | undefined, env: E | undefined): boolean;
|
|
655
|
+
/**
|
|
656
|
+
* Runs `callback` and everything it calls, synchronously or not, with diagnostics on or off and these
|
|
657
|
+
* observers.
|
|
658
|
+
*/
|
|
659
|
+
declare function withDiagnostics<T>(enabled: boolean, callback: () => T, observers?: readonly Observer<never>[]): T;
|
|
660
|
+
/** Whether the current call runs with diagnostics on; they are on outside any `withDiagnostics`. */
|
|
661
|
+
declare function diagnosticsEnabled(): boolean;
|
|
662
|
+
/**
|
|
663
|
+
* Logs an unexpected error by its safe description only (`maximus.unexpected`: error and cause names, issue
|
|
664
|
+
* count, stack locations; never a message) and tells the observers.
|
|
665
|
+
*/
|
|
666
|
+
declare function reportUnexpected(error: unknown, surface?: string, requestId?: string): void;
|
|
667
|
+
/** Hands the observation of one operation call to the observers, as background work. */
|
|
668
|
+
declare function recordExecution<E>(manifest: {
|
|
669
|
+
name: string;
|
|
670
|
+
telemetry?: TelemetryConfig<E>;
|
|
671
|
+
}, env: E, background: BackgroundContext | undefined, invocation: {
|
|
672
|
+
principal: Principal;
|
|
673
|
+
operation: string;
|
|
674
|
+
surface: ExecutionObservation['surface'];
|
|
675
|
+
started: number;
|
|
676
|
+
result: Result<unknown, Failure>;
|
|
677
|
+
}): void;
|
|
678
|
+
//#endregion
|
|
679
|
+
//#region src/manifest.d.ts
|
|
680
|
+
/** Transport-verified authority, separate from the unchanged ordinary RPC principal. */
|
|
681
|
+
interface InvocationAuthority {
|
|
682
|
+
/** The transport whose admission verified the token, such as `http` or `mcp`. */
|
|
683
|
+
readonly surface: 'http' | 'mcp';
|
|
684
|
+
/** The scopes the verified token grants, which a handler checks its `conditionalScopes` against. */
|
|
685
|
+
readonly scopes: readonly string[];
|
|
686
|
+
/** The OAuth client the verified token was issued to: its id and its class, `human`, `machine` or `dcr`. */
|
|
687
|
+
readonly client: Readonly<TokenClient>;
|
|
688
|
+
/**
|
|
689
|
+
* The bearer token the call was admitted with, verified: on an HTTP or MCP call to the service's origin,
|
|
690
|
+
* including one a gateway forwards. It is the caller's OAuth access token, unless
|
|
691
|
+
* `tokens.resolve` admitted another credential (a workload key under `oauth+bearer`) or it is the
|
|
692
|
+
* development token; absent under the application's own `bearer` scheme. For an owner that calls another
|
|
693
|
+
* service as the same caller, which verifies it again. Never log, store or return it.
|
|
694
|
+
*/
|
|
695
|
+
readonly token?: string;
|
|
696
|
+
}
|
|
697
|
+
/**
|
|
698
|
+
* What preparation and middleware start from for one call: the env, the verified principal and authority, the
|
|
699
|
+
* decoded input, the operation's name, a declared byte body and the runtime's background work.
|
|
700
|
+
*/
|
|
701
|
+
interface Invocation<Env> {
|
|
702
|
+
/** The Worker's bindings; inside `http.aroundOperation`, the environment that hook passed to `run`. */
|
|
703
|
+
env: Env;
|
|
704
|
+
/** Available only to explicitly declared HTTP byte-body operations. */
|
|
705
|
+
readonly body?: BinaryBody;
|
|
706
|
+
/**
|
|
707
|
+
* The verified caller: its actor, actor kind, organization and request id. Scope every tenant query by its
|
|
708
|
+
* `organizationId`.
|
|
709
|
+
*/
|
|
710
|
+
principal: Principal;
|
|
711
|
+
/**
|
|
712
|
+
* The verified token's surface, scopes and client, on HTTP and MCP calls whose token names a client; absent
|
|
713
|
+
* over ordinary RPC.
|
|
714
|
+
*/
|
|
715
|
+
readonly authority?: InvocationAuthority;
|
|
716
|
+
/** Already decoded operation input; preparation must not re-validate or log it. */
|
|
717
|
+
readonly input?: unknown;
|
|
718
|
+
/** Builder-owned operation identifier, never taken from caller input. */
|
|
719
|
+
readonly operation?: string;
|
|
720
|
+
/** Bounded best-effort work attached to the invoking runtime. */
|
|
721
|
+
readonly background?: BackgroundContext;
|
|
722
|
+
}
|
|
723
|
+
/** One scope, or several of which any one admits the caller. */
|
|
724
|
+
type OperationScope = string | readonly [string, ...string[]];
|
|
725
|
+
/** Whether an operation belongs in public contracts (OpenAPI, typed clients, discovery metadata). */
|
|
726
|
+
declare function isPublicOperation(declaration: {
|
|
727
|
+
visibility?: 'public' | 'internal';
|
|
728
|
+
}): boolean;
|
|
729
|
+
/** The scopes of an operation as a list; a caller needs any one of them. */
|
|
730
|
+
declare function operationScopes(declaration: {
|
|
731
|
+
scope: OperationScope;
|
|
732
|
+
}): readonly string[];
|
|
733
|
+
/** Shared by HTTP admission, MCP discovery and calls, and execution. */
|
|
734
|
+
declare function operationMatchesScopes(declaration: {
|
|
735
|
+
scope: OperationScope;
|
|
736
|
+
scopeMatch?: ScopeMatch;
|
|
737
|
+
}, granted: readonly string[]): boolean;
|
|
738
|
+
/**
|
|
739
|
+
* Where and to whom an operation is reachable: its HTTP route, MCP tool and RPC method, each declared
|
|
740
|
+
* explicitly, its visibility and the actors it admits.
|
|
741
|
+
*/
|
|
742
|
+
interface Exposure {
|
|
743
|
+
/** Eligible principal kinds; omitted only for legacy contracts. */
|
|
744
|
+
actorKinds?: readonly Principal['actorKind'][];
|
|
745
|
+
/** Versioned route; required string input fields may occupy complete {parameter} segments. */
|
|
746
|
+
http?: {
|
|
747
|
+
method: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
|
|
748
|
+
path: string;
|
|
749
|
+
body?: 'bytes';
|
|
750
|
+
/** Input metadata fields sourced from explicit HTTP headers, decoded by the input schema. */
|
|
751
|
+
headers?: Readonly<Record<string, string>>;
|
|
752
|
+
response?: 'attachment';
|
|
753
|
+
successStatus?: 200 | 201 | 202;
|
|
754
|
+
};
|
|
755
|
+
/**
|
|
756
|
+
* `internal`: an operation for the service's own staff. It stays routed and
|
|
757
|
+
* scope-checked like any other, but it is absent from the public OpenAPI
|
|
758
|
+
* document, from the protected-resource `scopes_supported` and from the
|
|
759
|
+
* typed client; MCP lists it only to tokens that carry its scope. Hiding is
|
|
760
|
+
* not authorization: the scope is what admits the caller.
|
|
761
|
+
*/
|
|
762
|
+
visibility?: 'public' | 'internal';
|
|
763
|
+
/**
|
|
764
|
+
* The MCP tool: its unique name, whether repeating a call is safe (`idempotentHint`) and whether it reaches
|
|
765
|
+
* outside the service (`openWorldHint`, default `true`).
|
|
766
|
+
*/
|
|
767
|
+
mcp?: {
|
|
768
|
+
name: string;
|
|
769
|
+
idempotent: boolean;
|
|
770
|
+
openWorld?: boolean;
|
|
771
|
+
};
|
|
772
|
+
/**
|
|
773
|
+
* The native RPC method: `true` admits every caller in the application's `rpc.callers`, `{ callers }` only
|
|
774
|
+
* the listed ones among them.
|
|
775
|
+
*/
|
|
776
|
+
rpc?: true | {
|
|
777
|
+
callers: readonly string[];
|
|
778
|
+
};
|
|
779
|
+
}
|
|
780
|
+
/**
|
|
781
|
+
* An operation as an application holds it: its scope, words, effect, schemas, limits, exposure and handler.
|
|
782
|
+
*/
|
|
783
|
+
interface OperationShape extends Exposure {
|
|
784
|
+
/**
|
|
785
|
+
* The scope a caller needs. Several scopes mean any one of them admits the
|
|
786
|
+
* caller (`['notes:notes:read', 'notes:notes:write']`: a write grant
|
|
787
|
+
* also reads). Published as alternative OpenAPI security requirements.
|
|
788
|
+
*/
|
|
789
|
+
scope: OperationScope;
|
|
790
|
+
/** Arrays retain any-of semantics unless the owner explicitly declares all-of. */
|
|
791
|
+
scopeMatch?: ScopeMatch;
|
|
792
|
+
/** Owner handlers select conditional scopes for the actual resource/fields. Absence of authority fails closed. */
|
|
793
|
+
conditionalScopes?: readonly string[];
|
|
794
|
+
/**
|
|
795
|
+
* The domain failure codes the operation may answer, besides the standard ones every operation may.
|
|
796
|
+
* Declared, they are all its documents list, and any other domain failure it returns becomes
|
|
797
|
+
* INTERNAL_ERROR and is reported. Omitted, every failure of the catalog is listed and allowed.
|
|
798
|
+
*/
|
|
799
|
+
failures?: readonly string[];
|
|
800
|
+
/** A short human name: the OpenAPI `summary` and the MCP tool's title. */
|
|
801
|
+
title: string;
|
|
802
|
+
/**
|
|
803
|
+
* What the operation does, for people and agents: the OpenAPI `description` and the MCP tool's description.
|
|
804
|
+
*/
|
|
805
|
+
description: string;
|
|
806
|
+
/** The OpenAPI tag; defaults to the operation's name. */
|
|
807
|
+
group?: string;
|
|
808
|
+
/**
|
|
809
|
+
* Whether the operation reads, writes or deletes: only a read may use `GET`, and the MCP read-only and
|
|
810
|
+
* destructive hints follow it.
|
|
811
|
+
*/
|
|
812
|
+
effect: 'read' | 'write' | 'delete';
|
|
813
|
+
/**
|
|
814
|
+
* The schema each call's input is decoded against: a strict object, or a union of strict objects except on
|
|
815
|
+
* `GET`.
|
|
816
|
+
*/
|
|
817
|
+
input: TSchema;
|
|
818
|
+
/**
|
|
819
|
+
* The schema every successful value is checked against: a strict object, a union of strict objects or
|
|
820
|
+
* `AttachmentOutputSchema`.
|
|
821
|
+
*/
|
|
822
|
+
output: TSchema;
|
|
823
|
+
/**
|
|
824
|
+
* Largest JSON request body this operation accepts, in bytes. Defaults to
|
|
825
|
+
* the application's `maxBodyBytes` (32 KiB). Raise it only for operations
|
|
826
|
+
* whose input is legitimately large (a CSV import, a document), never
|
|
827
|
+
* application-wide; the ceiling is 16 MiB. Not allowed on GET operations,
|
|
828
|
+
* whose input is the query string.
|
|
829
|
+
*/
|
|
830
|
+
maxBodyBytes?: number;
|
|
831
|
+
/** Exact attachment/JSON output byte bound, up to 16 MiB; otherwise inherits the application bound. */
|
|
832
|
+
maxOutputBytes?: number;
|
|
833
|
+
/** The handler: it receives the prepared context and the decoded input and returns a `Result`. */
|
|
834
|
+
execute: (context: never, input: never) => unknown;
|
|
835
|
+
/** Captured by the fluent builder; each mounted group owns its inferred preparation and middleware. */
|
|
836
|
+
pipeline?: {
|
|
837
|
+
context: (invocation: never) => Promise<Result<unknown, Failure>>;
|
|
838
|
+
middleware: readonly OperationMiddleware<never>[];
|
|
839
|
+
};
|
|
840
|
+
}
|
|
841
|
+
/**
|
|
842
|
+
* Declares one operation on its own, keeping its literal types. The builder's `.operation()` is the usual way
|
|
843
|
+
* to declare one. As there, `execute` receives the decoded input, `StaticDecode<typeof input>`.
|
|
844
|
+
*/
|
|
845
|
+
declare function operation<C, I extends TSchema, O extends TSchema, const X extends Exposure>(definition: X & {
|
|
846
|
+
scope: OperationScope;
|
|
847
|
+
scopeMatch?: ScopeMatch;
|
|
848
|
+
conditionalScopes?: readonly string[];
|
|
849
|
+
title: string;
|
|
850
|
+
description: string;
|
|
851
|
+
group?: string;
|
|
852
|
+
effect: OperationShape['effect'];
|
|
853
|
+
input: I;
|
|
854
|
+
output: O;
|
|
855
|
+
execute: (context: C, input: StaticDecode<I>) => Promise<Result<Static<O>, Failure>>;
|
|
856
|
+
}): X & {
|
|
857
|
+
scope: OperationScope;
|
|
858
|
+
scopeMatch?: ScopeMatch;
|
|
859
|
+
conditionalScopes?: readonly string[];
|
|
860
|
+
title: string;
|
|
861
|
+
description: string;
|
|
862
|
+
group?: string;
|
|
863
|
+
effect: OperationShape['effect'];
|
|
864
|
+
input: I;
|
|
865
|
+
output: O;
|
|
866
|
+
execute: (context: C, input: StaticDecode<I>) => Promise<Result<Static<O>, Failure>>;
|
|
867
|
+
};
|
|
868
|
+
/** The intersection of a union's members: the operations of every composed group. */
|
|
869
|
+
type Intersection<U> = (U extends unknown ? (value: U) => void : never) extends ((value: infer I) => void) ? I : never;
|
|
870
|
+
/** Compose resource modules without silently overwriting a capability. */
|
|
871
|
+
declare function composeOperations<const G extends readonly Record<string, OperationShape>[]>(...groups: G): Intersection<G[number]>;
|
|
872
|
+
/**
|
|
873
|
+
* Code that wraps an operation after admission and preparation: it receives the context and `next`, which it
|
|
874
|
+
* may call once, and returns a Result.
|
|
875
|
+
*/
|
|
876
|
+
type OperationMiddleware<C> = (context: C, next: () => Promise<Result<unknown, Failure>>) => Promise<Result<unknown, Failure>>;
|
|
877
|
+
/** A Markdown usage guide, served over MCP as a resource and a reusable prompt. */
|
|
878
|
+
interface McpGuide {
|
|
879
|
+
/**
|
|
880
|
+
* The guide's unique name, `^[a-z][a-z0-9-]{0,63}$`: its prompt's name and the end of its resource URI,
|
|
881
|
+
* `<origin>/guides/<name>`.
|
|
882
|
+
*/
|
|
883
|
+
name: string;
|
|
884
|
+
/** The guide's human title, shown on its resource and its prompt. */
|
|
885
|
+
title: string;
|
|
886
|
+
/** The guide's Markdown: the resource's content and the prompt's message. */
|
|
887
|
+
text: string;
|
|
888
|
+
}
|
|
889
|
+
/**
|
|
890
|
+
* An Agent Skill served through the MCP Skills extension (`io.modelcontextprotocol/skills`) as
|
|
891
|
+
* `skill://<name>/SKILL.md`, whose frontmatter is generated from `name` and `description`. Skills are
|
|
892
|
+
* static guidance shared by every caller: never put tenant data in them, and they grant no tool or scope.
|
|
893
|
+
*/
|
|
894
|
+
interface McpSkill {
|
|
895
|
+
/** Lowercase letters and digits joined by single hyphens, at most 64 characters; also the skill directory. */
|
|
896
|
+
name: string;
|
|
897
|
+
/** One line of at most 1024 characters: what the workflow achieves and when to use it. */
|
|
898
|
+
description: string;
|
|
899
|
+
/** The Markdown instructions that follow the frontmatter. */
|
|
900
|
+
text: string;
|
|
901
|
+
}
|
|
902
|
+
/** The MCP server's own description: instructions for clients, guides, skills and an optional skill tool. */
|
|
903
|
+
interface McpConfiguration {
|
|
904
|
+
/** Server guidance returned when a client connects. */
|
|
905
|
+
instructions?: string;
|
|
906
|
+
/** Markdown usage guides, each served as a resource and as a prompt. */
|
|
907
|
+
guides?: readonly McpGuide[];
|
|
908
|
+
/**
|
|
909
|
+
* Agent Skills served through the MCP Skills extension as `skill://<name>/SKILL.md`, the same for every
|
|
910
|
+
* caller.
|
|
911
|
+
*/
|
|
912
|
+
skills?: readonly McpSkill[];
|
|
913
|
+
/**
|
|
914
|
+
* Also serve the skills through one read-only tool with this name, for clients that import only tools.
|
|
915
|
+
* Omit it to keep skills out of the tool catalog.
|
|
916
|
+
*/
|
|
917
|
+
skillTool?: string;
|
|
918
|
+
}
|
|
919
|
+
/**
|
|
920
|
+
* A built application: its identity, token policy, error catalog, operations and the configuration every
|
|
921
|
+
* transport reads.
|
|
922
|
+
*/
|
|
923
|
+
interface AppManifest<Env, C, Ops extends Record<string, OperationShape>> {
|
|
924
|
+
/** The service's identifier: the MCP server name and the `service` that `/health` and telemetry report. */
|
|
925
|
+
name: string;
|
|
926
|
+
/** The prefix of every scope, `<namespace>:<resource>:<action>`; it matches `^[a-z][a-z0-9-]{1,63}$`. */
|
|
927
|
+
namespace: string;
|
|
928
|
+
/** The service's human name: OpenAPI `info.title`, the MCP server title and the OAuth `resource_name`. */
|
|
929
|
+
title: string;
|
|
930
|
+
/**
|
|
931
|
+
* The service's version: OpenAPI `info.version`, the MCP server version and the `version` that `/health`
|
|
932
|
+
* reports.
|
|
933
|
+
*/
|
|
934
|
+
version: string;
|
|
935
|
+
/** The service's public HTTPS base URL, which OpenAPI, MCP resources and OAuth discovery advertise. */
|
|
936
|
+
origin: string;
|
|
937
|
+
/**
|
|
938
|
+
* How bearer tokens are verified and documented: their audiences, the OAuth issuer and JWKS, or the
|
|
939
|
+
* application's own `resolve`.
|
|
940
|
+
*/
|
|
941
|
+
tokens: TokenPolicy<Env>;
|
|
942
|
+
/** The catalog `defineErrors()` returns; every failure a call answers is validated against it. */
|
|
943
|
+
errors: ErrorCatalog;
|
|
944
|
+
/**
|
|
945
|
+
* The middleware `.use()` added, in the order it runs; each wraps a call after admission and context
|
|
946
|
+
* preparation.
|
|
947
|
+
*/
|
|
948
|
+
middleware?: readonly OperationMiddleware<C>[];
|
|
949
|
+
/**
|
|
950
|
+
* Prepares a call's context from its invocation with what each `.derive()` adds; a failure it returns
|
|
951
|
+
* answers the call.
|
|
952
|
+
*/
|
|
953
|
+
context: (invocation: Invocation<Env>) => Promise<Result<C, Failure>>;
|
|
954
|
+
/**
|
|
955
|
+
* The application's operations by name; each name is also its OpenAPI `operationId` and native RPC method.
|
|
956
|
+
*/
|
|
957
|
+
operations: Ops;
|
|
958
|
+
/**
|
|
959
|
+
* The Workers allowed to call native RPC methods, by the `caller` their Service Binding props name; `[]`
|
|
960
|
+
* for none.
|
|
961
|
+
*/
|
|
962
|
+
rpc: {
|
|
963
|
+
callers: readonly string[];
|
|
964
|
+
};
|
|
965
|
+
/**
|
|
966
|
+
* Whether diagnostics and observers run, the observers that receive execution and admission events, and
|
|
967
|
+
* where the trace id comes from.
|
|
968
|
+
*/
|
|
969
|
+
telemetry?: TelemetryConfig<Env>;
|
|
970
|
+
/**
|
|
971
|
+
* A Workers rate limiting binding each HTTP and MCP call must pass, per organization and actor;
|
|
972
|
+
* `retryAfter` sets a refusal's `Retry-After` seconds, 60 by default.
|
|
973
|
+
*/
|
|
974
|
+
rateLimit?: {
|
|
975
|
+
binding: keyof Env;
|
|
976
|
+
retryAfter?: number;
|
|
977
|
+
};
|
|
978
|
+
/**
|
|
979
|
+
* The request body limit, in bytes, of every operation that sets none: 32 KiB by default, at most 16 MiB.
|
|
980
|
+
*/
|
|
981
|
+
maxBodyBytes?: number;
|
|
982
|
+
/** The output limit, in bytes, of every operation that sets none: 256 KiB by default, at most 16 MiB. */
|
|
983
|
+
maxOutputBytes?: number;
|
|
984
|
+
/** The MCP server's instructions, guides and skills; setting it requires the MCP transport. */
|
|
985
|
+
mcp?: McpConfiguration;
|
|
986
|
+
}
|
|
987
|
+
//#endregion
|
|
988
|
+
//#region src/builder.d.ts
|
|
989
|
+
/**
|
|
990
|
+
* What `maximus()` takes: the application without its operations, context and middleware, which the builder
|
|
991
|
+
* adds.
|
|
992
|
+
*/
|
|
993
|
+
type ApplicationConfiguration<E> = Omit<AppManifest<E, Invocation<E>, Record<never, never>>, 'context' | 'operations' | 'middleware'>;
|
|
994
|
+
/**
|
|
995
|
+
* Refuses, in the type, a module that declares an operation the builder already has: `.mount()` would throw
|
|
996
|
+
* on it. The property names the operations the two declare.
|
|
997
|
+
*/
|
|
998
|
+
type DistinctOperations<Ops, M> = [Extract<keyof M, keyof Ops>] extends [never] ? unknown : {
|
|
999
|
+
readonly 'Operations declared twice': Extract<keyof M, keyof Ops>;
|
|
1000
|
+
};
|
|
1001
|
+
/** An operation's definition as `.operation()` takes it: its scope, exposure, words, schemas and limits. */
|
|
1002
|
+
type Definition<I extends TSchema, O extends TSchema, X extends Exposure> = X & Omit<OperationShape, keyof Exposure | 'input' | 'output' | 'execute' | 'pipeline'> & {
|
|
1003
|
+
input: I;
|
|
1004
|
+
output: O;
|
|
1005
|
+
};
|
|
1006
|
+
/** Refuses a definition field no operation has, such as a misspelled one, which would otherwise do nothing. */
|
|
1007
|
+
type KnownFields<X> = Readonly<Record<Exclude<keyof X, keyof OperationShape>, never>>;
|
|
1008
|
+
/** Immutable builders let independent resource modules share inferred context without overwriting routes. */
|
|
1009
|
+
declare class ApplicationBuilder<E, C, Ops extends Record<string, OperationShape>> {
|
|
1010
|
+
/**
|
|
1011
|
+
* The application declared so far: its configuration, context preparation, middleware and operations, which
|
|
1012
|
+
* `.build()` checks.
|
|
1013
|
+
*/
|
|
1014
|
+
readonly definition: AppManifest<E, C, Ops>;
|
|
1015
|
+
private readonly root;
|
|
1016
|
+
constructor(definition: AppManifest<E, C, Ops>, root?: object);
|
|
1017
|
+
/**
|
|
1018
|
+
* Declares an operation: a camelCase name no other operation uses, its definition (scope, words, effect,
|
|
1019
|
+
* schemas and exposure) and its handler, which receives the prepared context and the decoded input and
|
|
1020
|
+
* returns a Result.
|
|
1021
|
+
*/
|
|
1022
|
+
operation<const N extends string, I extends TSchema, O extends TSchema, const X extends Exposure>(name: N extends keyof Ops ? never : N, definition: Definition<I, O, X> & KnownFields<X>, execute: (context: C & (X extends {
|
|
1023
|
+
http: {
|
|
1024
|
+
body: 'bytes';
|
|
1025
|
+
};
|
|
1026
|
+
} ? {
|
|
1027
|
+
body: BinaryBody;
|
|
1028
|
+
} : object), input: StaticDecode<I>) => Promise<Result<Static<O>, Failure>>): ApplicationBuilder<E, C, Ops & Record<N, Definition<I, O, X> & {
|
|
1029
|
+
execute: typeof execute;
|
|
1030
|
+
}>>;
|
|
1031
|
+
/**
|
|
1032
|
+
* Adds what `resolve` returns to the context of every operation declared after it. It runs after admission
|
|
1033
|
+
* and before the middleware; a failure it returns answers the call. Derive before declaring operations.
|
|
1034
|
+
*/
|
|
1035
|
+
derive<D extends object>(resolve: (context: C) => Promise<Result<D, Failure>>): ApplicationBuilder<E, C & D, Ops>;
|
|
1036
|
+
/**
|
|
1037
|
+
* Wraps every operation declared after it, once the caller is admitted and the context prepared. The
|
|
1038
|
+
* middleware calls `next` at most once.
|
|
1039
|
+
*/
|
|
1040
|
+
use(middleware: OperationMiddleware<C>): ApplicationBuilder<E, C, Ops>;
|
|
1041
|
+
/**
|
|
1042
|
+
* Adds the operations of a resource module built from the same application; each keeps the context and
|
|
1043
|
+
* middleware it was declared with.
|
|
1044
|
+
*/
|
|
1045
|
+
mount<MC, M extends Record<string, OperationShape>>(module: ApplicationBuilder<E, MC, M> & DistinctOperations<Ops, M>): ApplicationBuilder<E, C, Ops & M>;
|
|
1046
|
+
/**
|
|
1047
|
+
* Checks every rule the application must keep and returns it. A refusal is a TypeError that names the rule
|
|
1048
|
+
* and the operation that breaks it.
|
|
1049
|
+
*/
|
|
1050
|
+
build(): AppManifest<E, C, Ops>;
|
|
1051
|
+
}
|
|
1052
|
+
/**
|
|
1053
|
+
* Starts an application: its name, scope namespace, origin, token policy, error catalog and RPC callers.
|
|
1054
|
+
* Chain `.derive()` and `.use()` to prepare each call, `.operation()` to declare operations, `.mount()` to
|
|
1055
|
+
* compose resource modules that share this base, then `.build()`.
|
|
1056
|
+
*/
|
|
1057
|
+
declare function maximus<E>(configuration: ApplicationConfiguration<E>): ApplicationBuilder<E, Invocation<E>, Record<never, never>>;
|
|
1058
|
+
//#endregion
|
|
1059
|
+
//#region src/validation.d.ts
|
|
1060
|
+
/**
|
|
1061
|
+
* Checks a value against a schema without decoding it: the value when it matches, else an `INVALID_INPUT`
|
|
1062
|
+
* failure without issues. Use `decodeInput` for input with defaults and codecs.
|
|
1063
|
+
*/
|
|
1064
|
+
declare function parseInput<Schema extends TSchema>(schema: Schema, input: unknown): {
|
|
1065
|
+
ok: true;
|
|
1066
|
+
value: (Schema & {
|
|
1067
|
+
params: [];
|
|
1068
|
+
})["static"];
|
|
1069
|
+
error?: undefined;
|
|
1070
|
+
} | {
|
|
1071
|
+
value?: undefined;
|
|
1072
|
+
ok: false;
|
|
1073
|
+
error: {
|
|
1074
|
+
code: 'INVALID_INPUT';
|
|
1075
|
+
message: string;
|
|
1076
|
+
retryable: boolean;
|
|
1077
|
+
data: {};
|
|
1078
|
+
};
|
|
1079
|
+
};
|
|
1080
|
+
/** Checks a value against a schema and returns it typed, or throws a TypeError that names no value. */
|
|
1081
|
+
declare function parseSchema<Schema extends TSchema>(schema: Schema, input: unknown): Static<Schema>;
|
|
1082
|
+
/** Admit a real wire schema once, materialize declared defaults, and decode native TypeBox transforms.
|
|
1083
|
+
* Output validation deliberately remains check-only: response data must already be canonical. */
|
|
1084
|
+
declare function decodeSchema<Schema extends TSchema>(schema: Schema, input: unknown): StaticDecode<Schema>;
|
|
1085
|
+
/** Field issues a refusal carries: enough to correct the input, never an input value. */
|
|
1086
|
+
interface InputIssue {
|
|
1087
|
+
/** RFC 6901 JSON Pointer to the refused field; empty for the whole input. */
|
|
1088
|
+
path: string;
|
|
1089
|
+
/** Why the field was refused, for people, at most 256 characters; it never contains the input value. */
|
|
1090
|
+
message: string;
|
|
1091
|
+
}
|
|
1092
|
+
/**
|
|
1093
|
+
* The transport maps schema codec failures to its declared INVALID_INPUT contract, with the first
|
|
1094
|
+
* field issues. Schema messages name the expectation, not the value; owner refinements must too.
|
|
1095
|
+
*/
|
|
1096
|
+
declare function decodeInput<Schema extends TSchema>(schema: Schema, input: unknown): {
|
|
1097
|
+
error?: undefined;
|
|
1098
|
+
ok: true;
|
|
1099
|
+
value: import("@sinclair/typebox").StaticDecodeIsAny<Schema> extends true ? unknown : (import("@sinclair/typebox").TDecodeType<Schema> & {
|
|
1100
|
+
params: [];
|
|
1101
|
+
})["static"];
|
|
1102
|
+
} | {
|
|
1103
|
+
value?: undefined;
|
|
1104
|
+
ok: false;
|
|
1105
|
+
error: {
|
|
1106
|
+
code: 'INVALID_INPUT';
|
|
1107
|
+
message: string;
|
|
1108
|
+
retryable: boolean;
|
|
1109
|
+
data: {
|
|
1110
|
+
issues: InputIssue[];
|
|
1111
|
+
};
|
|
1112
|
+
};
|
|
1113
|
+
};
|
|
1114
|
+
//#endregion
|
|
1115
|
+
//#region src/input-schemas.d.ts
|
|
1116
|
+
/** Type-only: the key under which a codec's schema type carries its wire type. No schema object has it. */
|
|
1117
|
+
declare const WIRE: unique symbol;
|
|
1118
|
+
/**
|
|
1119
|
+
* What a codec's schema admits on the wire, before it decodes, carried in its type only: `SchemaInput` reads
|
|
1120
|
+
* it, so callers send what the source schema checks rather than what the handler receives.
|
|
1121
|
+
*/
|
|
1122
|
+
interface WireType<W> {
|
|
1123
|
+
/**
|
|
1124
|
+
* The wire type `SchemaInput` reads: what the codec admits before it decodes. Type-only; no schema object
|
|
1125
|
+
* has it.
|
|
1126
|
+
*/
|
|
1127
|
+
readonly [WIRE]?: W;
|
|
1128
|
+
}
|
|
1129
|
+
/** Defaults are optional on input and materialized before domain execution. */
|
|
1130
|
+
type InputKeys<P extends TProperties> = { [K in keyof P]: P[K] extends {
|
|
1131
|
+
default: unknown;
|
|
1132
|
+
} | TOptional<TSchema> ? K : never; }[keyof P];
|
|
1133
|
+
/**
|
|
1134
|
+
* The type a schema accepts on the wire: a field that is optional or has a default may be omitted, and a
|
|
1135
|
+
* field a codec declares (`normalizeSchema`, `transformSchema`, `pipeSchema`) is sent as its source.
|
|
1136
|
+
*/
|
|
1137
|
+
type SchemaInput<S extends TSchema, Depth extends unknown[] = []> = Depth['length'] extends 6 ? Static<S> : typeof WIRE extends keyof S ? S extends WireType<infer W> ? W : never : S extends TObject<infer P> ? { [K in Exclude<keyof P, InputKeys<P>>]: SchemaInput<P[K]>; } & { [K in InputKeys<P>]?: SchemaInput<P[K]>; } : S extends TArray<infer I> ? SchemaInput<I>[] : S extends TUnion<infer U> ? SchemaInput<U[number]> : Static<S>;
|
|
1138
|
+
/**
|
|
1139
|
+
* One refused field: a code, a message that names the expectation and never the value, and the path to the
|
|
1140
|
+
* field.
|
|
1141
|
+
*/
|
|
1142
|
+
interface SchemaIssue {
|
|
1143
|
+
/** The kind of issue, such as `invalid_input` for a schema mismatch or `custom` for a refinement. */
|
|
1144
|
+
code: string;
|
|
1145
|
+
/** Why the field was refused, for people; it names the expectation, never the value. */
|
|
1146
|
+
message: string;
|
|
1147
|
+
/** The keys and array indexes that lead to the refused field; empty when the whole value is refused. */
|
|
1148
|
+
path: (string | number)[];
|
|
1149
|
+
}
|
|
1150
|
+
/** Every issue of a refused value, as `readSchema` throws it and `checkSchema` returns it. */
|
|
1151
|
+
declare class SchemaValidationError extends TypeError {
|
|
1152
|
+
/** Every issue of the refused value; the error's message is the first one's. */
|
|
1153
|
+
readonly issues: SchemaIssue[];
|
|
1154
|
+
constructor(issues: SchemaIssue[]);
|
|
1155
|
+
}
|
|
1156
|
+
/** A union of literals: `literalUnion(['open', 'done'])`. */
|
|
1157
|
+
declare const literalUnion: <const V extends readonly (string | number | boolean)[]>(values: V) => TUnion<import("@sinclair/typebox").TLiteral<V[number]>[]>;
|
|
1158
|
+
/**
|
|
1159
|
+
* The schema with a default that decoding applies when the field is absent, so the field is optional on the
|
|
1160
|
+
* wire.
|
|
1161
|
+
*/
|
|
1162
|
+
declare const defaultSchema: <S extends TSchema, const D>(schema: S, value: D) => S & {
|
|
1163
|
+
default: D;
|
|
1164
|
+
};
|
|
1165
|
+
/**
|
|
1166
|
+
* A strict object that, read with `stripUnknown` (as `readSchema` does), drops unknown keys instead of
|
|
1167
|
+
* refusing them. Wrap an input that uses it in `inputSchema()`.
|
|
1168
|
+
*/
|
|
1169
|
+
declare const stripObject: <P extends TProperties>(properties: P) => TObject<P>;
|
|
1170
|
+
/** The object, refusing unknown keys even where a read strips them. */
|
|
1171
|
+
declare const strictObject: <S extends TSchema>(schema: S) => S;
|
|
1172
|
+
/** The object, admitting and keeping unknown keys. */
|
|
1173
|
+
declare const looseObject: <S extends TSchema>(schema: S) => S;
|
|
1174
|
+
/** The object with more properties, keeping how it treats unknown keys. */
|
|
1175
|
+
declare const extendObject: <P extends TProperties, Q extends TProperties>(schema: TObject<P>, properties: Q) => TObject<P & Q>;
|
|
1176
|
+
/** Owner normalization keeps persisted/document input behavior separate from wire validation. */
|
|
1177
|
+
declare const normalizeSchema: <S extends TSchema, O>(schema: S, normalize: (value: Static<S>) => O) => TUnsafe<O> & WireType<SchemaInput<S>> & (S extends TOptional<TSchema> ? {
|
|
1178
|
+
[OptionalKind]: 'Optional';
|
|
1179
|
+
} : Record<never, never>) & (S extends {
|
|
1180
|
+
default: infer D;
|
|
1181
|
+
} ? {
|
|
1182
|
+
default: D;
|
|
1183
|
+
} : Record<never, never>);
|
|
1184
|
+
/** Apply a domain transform only after validating its source value. */
|
|
1185
|
+
declare const transformSchema: <S extends TSchema, O>(schema: S, transform: (value: Static<S>) => O) => TUnsafe<O> & WireType<SchemaInput<S>> & (S extends TOptional<TSchema> ? {
|
|
1186
|
+
[OptionalKind]: 'Optional';
|
|
1187
|
+
} : Record<never, never>) & (S extends {
|
|
1188
|
+
default: infer D;
|
|
1189
|
+
} ? {
|
|
1190
|
+
default: D;
|
|
1191
|
+
} : Record<never, never>);
|
|
1192
|
+
/**
|
|
1193
|
+
* Admits a value as `source`, then checks and decodes the result as `target`: a wire shape read into its
|
|
1194
|
+
* canonical one. Runs inside `inputSchema()`.
|
|
1195
|
+
*/
|
|
1196
|
+
declare const pipeSchema: <S extends TSchema, O extends TSchema>(source: S, target: O) => O & WireType<SchemaInput<S>>;
|
|
1197
|
+
/**
|
|
1198
|
+
* Runs `inspect` on a value the schema admits; every `addIssue` refuses the value with that issue. Runs
|
|
1199
|
+
* inside `inputSchema()`.
|
|
1200
|
+
*/
|
|
1201
|
+
declare const inspectSchema: <S extends TSchema>(schema: S, inspect: (value: Static<S>, context: {
|
|
1202
|
+
addIssue: (issue: SchemaIssue) => void;
|
|
1203
|
+
}) => void) => S;
|
|
1204
|
+
/**
|
|
1205
|
+
* Refuses a value the schema admits when `predicate` rejects it, with `detail` as the issue's message and
|
|
1206
|
+
* path. Runs inside `inputSchema()`.
|
|
1207
|
+
*/
|
|
1208
|
+
declare const refineSchema: <S extends TSchema>(schema: S, predicate: (value: Static<S>) => unknown, detail?: string | {
|
|
1209
|
+
message?: string;
|
|
1210
|
+
error?: string;
|
|
1211
|
+
path?: (string | number)[];
|
|
1212
|
+
}) => S;
|
|
1213
|
+
/** A URL whose protocol, and optionally hostname, match the given patterns. Runs inside `inputSchema()`. */
|
|
1214
|
+
declare const urlSchema: (options: {
|
|
1215
|
+
protocol: RegExp;
|
|
1216
|
+
hostname?: RegExp;
|
|
1217
|
+
maxLength?: number;
|
|
1218
|
+
}) => import("@sinclair/typebox").TString;
|
|
1219
|
+
/**
|
|
1220
|
+
* Compiles a schema's codecs (defaults, normalizers, transforms, inspections, refinements, pipes and
|
|
1221
|
+
* stripping) into one input schema: the wire accepts `SchemaInput<S>`, and the handler receives the
|
|
1222
|
+
* decoded, canonical `StaticDecode<S>`, which is `Static<S>` unless `S` holds a TypeBox transform. An
|
|
1223
|
+
* operation whose input uses a codec must be wrapped in it.
|
|
1224
|
+
*/
|
|
1225
|
+
declare function inputSchema<S extends TSchema>(schema: S, options?: {
|
|
1226
|
+
stripUnknown?: boolean;
|
|
1227
|
+
}): TTransform<TUnsafe<SchemaInput<S>>, StaticDecode<S>>;
|
|
1228
|
+
/** The same, reading another wire shape `wire` and decoding it into `schema`'s canonical type. */
|
|
1229
|
+
declare function inputSchema<S extends TSchema, W extends TSchema>(schema: S, wire: W): TTransform<W, StaticDecode<S>>;
|
|
1230
|
+
//#endregion
|
|
1231
|
+
//#region src/schema-reader.d.ts
|
|
1232
|
+
/**
|
|
1233
|
+
* What `checkSchema` answers: the decoded data, `StaticDecode<S>` (which is `Static<S>` unless `S` holds a
|
|
1234
|
+
* TypeBox transform), or the error that lists every issue.
|
|
1235
|
+
*/
|
|
1236
|
+
type Checked<S extends TSchema> = {
|
|
1237
|
+
success: true;
|
|
1238
|
+
data: StaticDecode<S>;
|
|
1239
|
+
error?: never;
|
|
1240
|
+
} | {
|
|
1241
|
+
success: false;
|
|
1242
|
+
error: SchemaValidationError;
|
|
1243
|
+
data?: never;
|
|
1244
|
+
};
|
|
1245
|
+
/**
|
|
1246
|
+
* Reads untrusted data — a stored record, a provider or model answer — through its schema's owner
|
|
1247
|
+
* semantics: defaults applied, normalizers run before their checks, unknown keys of a strip object
|
|
1248
|
+
* dropped. A refusal carries every issue with its path; nothing is coerced.
|
|
1249
|
+
*/
|
|
1250
|
+
declare function checkSchema<S extends TSchema>(schema: S, input: unknown): Checked<S>;
|
|
1251
|
+
/** `checkSchema`, throwing its SchemaValidationError on refusal. */
|
|
1252
|
+
declare function readSchema<S extends TSchema>(schema: S, input: unknown): StaticDecode<S>;
|
|
1253
|
+
//#endregion
|
|
1254
|
+
//#region src/pagination.d.ts
|
|
1255
|
+
/**
|
|
1256
|
+
* How to reach a list's neighboring pages: whether they exist and their opaque cursors, named `PageInfo`.
|
|
1257
|
+
*/
|
|
1258
|
+
declare const PageInfoSchema: TObject<{
|
|
1259
|
+
hasNextPage: import("@sinclair/typebox").TBoolean;
|
|
1260
|
+
hasPreviousPage: import("@sinclair/typebox").TBoolean;
|
|
1261
|
+
nextCursor: import("@sinclair/typebox").TUnion<[import("@sinclair/typebox").TString, import("@sinclair/typebox").TNull]>;
|
|
1262
|
+
previousCursor: import("@sinclair/typebox").TUnion<[import("@sinclair/typebox").TString, import("@sinclair/typebox").TNull]>;
|
|
1263
|
+
}>;
|
|
1264
|
+
/** How to reach a list's neighboring pages. */
|
|
1265
|
+
type PageInfo = Static<typeof PageInfoSchema>;
|
|
1266
|
+
/**
|
|
1267
|
+
* A list's `limit`: an integer from 1 to `maximum` (100), `default` (25) when the caller sends none.
|
|
1268
|
+
* A list whose items are costly lowers the maximum: `{ ...PageQueryFields, limit: pageLimit({ maximum: 20 }) }`.
|
|
1269
|
+
*/
|
|
1270
|
+
declare function pageLimit(options?: {
|
|
1271
|
+
maximum?: number;
|
|
1272
|
+
default?: number;
|
|
1273
|
+
}): import("@sinclair/typebox").TInteger & {
|
|
1274
|
+
default: number;
|
|
1275
|
+
};
|
|
1276
|
+
/** The query fields of a list, spread into its input beside its own filters. */
|
|
1277
|
+
declare const PageQueryFields: {
|
|
1278
|
+
cursor: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TString>;
|
|
1279
|
+
direction: import("@sinclair/typebox").TUnion<[TLiteral<"forward">, TLiteral<"backward">]> & {
|
|
1280
|
+
default: "forward";
|
|
1281
|
+
};
|
|
1282
|
+
limit: import("@sinclair/typebox").TInteger & {
|
|
1283
|
+
default: number;
|
|
1284
|
+
};
|
|
1285
|
+
};
|
|
1286
|
+
/** The list query a handler receives, its defaults applied. */
|
|
1287
|
+
type PageQuery = Static<TObject<typeof PageQueryFields>>;
|
|
1288
|
+
/** The TypeBox type of a `Page` of `Item`. */
|
|
1289
|
+
type TPage<Item extends TSchema> = TObject<{
|
|
1290
|
+
data: TArray<Item>;
|
|
1291
|
+
object: TLiteral<'list'>;
|
|
1292
|
+
pageInfo: typeof PageInfoSchema;
|
|
1293
|
+
}>;
|
|
1294
|
+
/** One page of a list, as a handler returns it. */
|
|
1295
|
+
interface Page<Item> {
|
|
1296
|
+
/** The items of this page, in the list's stable order. */
|
|
1297
|
+
data: Item[];
|
|
1298
|
+
/** Always `list`, the discriminator of a list response. */
|
|
1299
|
+
object: 'list';
|
|
1300
|
+
/** Whether neighboring pages exist, and the opaque cursors that reach them. */
|
|
1301
|
+
pageInfo: PageInfo;
|
|
1302
|
+
}
|
|
1303
|
+
/**
|
|
1304
|
+
* The output of a list of `item`, named by `$id` (`AppointmentPage`). Give each list its own name and
|
|
1305
|
+
* use it for that list only: two different shapes under one `$id` become two components, the second
|
|
1306
|
+
* one suffixed `_2`.
|
|
1307
|
+
*/
|
|
1308
|
+
declare function Page<Item extends TSchema>(item: Item, options: {
|
|
1309
|
+
$id: string;
|
|
1310
|
+
}): TPage<Item>;
|
|
1311
|
+
//#endregion
|
|
1312
|
+
//#region src/common-schemas.d.ts
|
|
1313
|
+
/** An email address of at most 254 characters, named `EmailAddress`. */
|
|
1314
|
+
declare const EmailAddressSchema: TString;
|
|
1315
|
+
/** A phone number in E.164 format, named `PhoneNumber`. */
|
|
1316
|
+
declare const PhoneNumberSchema: TString;
|
|
1317
|
+
/** An HTTPS URL of at most 2048 characters, named `HttpsUrl`. */
|
|
1318
|
+
declare const HttpsUrlSchema: TString;
|
|
1319
|
+
/** A `#rrggbb` color, named `HexColor`. */
|
|
1320
|
+
declare const HexColorSchema: TString;
|
|
1321
|
+
/** What every `delete` answers. */
|
|
1322
|
+
declare const DeletedObjectSchema: import("@sinclair/typebox").TObject<{
|
|
1323
|
+
id: TString;
|
|
1324
|
+
deleted: import("@sinclair/typebox").TLiteral<true>;
|
|
1325
|
+
}>;
|
|
1326
|
+
/** What a `delete` answers: the id and `deleted: true`. */
|
|
1327
|
+
type DeletedObject = Static<typeof DeletedObjectSchema>;
|
|
1328
|
+
/** A link that grants access by itself until it expires, such as a file download. */
|
|
1329
|
+
declare const SignedUrlSchema: import("@sinclair/typebox").TObject<{
|
|
1330
|
+
url: TString;
|
|
1331
|
+
expiresAt: TString;
|
|
1332
|
+
}>;
|
|
1333
|
+
/** A URL that grants access by itself, and when it expires. */
|
|
1334
|
+
type SignedUrl = Static<typeof SignedUrlSchema>;
|
|
1335
|
+
/** Another resource, by its id and the name people know it by. */
|
|
1336
|
+
declare const NamedReferenceSchema: import("@sinclair/typebox").TObject<{
|
|
1337
|
+
id: TString;
|
|
1338
|
+
name: TString;
|
|
1339
|
+
}>;
|
|
1340
|
+
/** Another resource, by id and name. */
|
|
1341
|
+
type NamedReference = Static<typeof NamedReferenceSchema>;
|
|
1342
|
+
/** A postal address, shaped as FHIR `Address`. */
|
|
1343
|
+
declare const PostalAddressSchema: import("@sinclair/typebox").TObject<{
|
|
1344
|
+
city: import("@sinclair/typebox").TOptional<TString>;
|
|
1345
|
+
countryCode: import("@sinclair/typebox").TOptional<TString>;
|
|
1346
|
+
line: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TArray<TString>>;
|
|
1347
|
+
postalCode: import("@sinclair/typebox").TOptional<TString>;
|
|
1348
|
+
state: import("@sinclair/typebox").TOptional<TString>;
|
|
1349
|
+
use: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TUnion<[import("@sinclair/typebox").TLiteral<"home">, import("@sinclair/typebox").TLiteral<"work">]>>;
|
|
1350
|
+
}>;
|
|
1351
|
+
/** A postal address, shaped as FHIR `Address`. */
|
|
1352
|
+
type PostalAddress = Static<typeof PostalAddressSchema>;
|
|
1353
|
+
//#endregion
|
|
1354
|
+
//#region src/schemas.d.ts
|
|
1355
|
+
/** The input of an operation that takes none: an empty strict object. */
|
|
1356
|
+
declare const EmptyInputSchema: import("@sinclair/typebox").TObject<{}>;
|
|
1357
|
+
/**
|
|
1358
|
+
* An organization id as identity providers issue them (`org_…`, a UUID, a slug): 1 to 128 letters, digits,
|
|
1359
|
+
* `_` and `-`, starting with a letter or digit, named `OrganizationId`. Nothing in it can change the meaning
|
|
1360
|
+
* of a storage key, a path or a log line.
|
|
1361
|
+
*/
|
|
1362
|
+
declare const OrganizationIdSchema: import("@sinclair/typebox").TString;
|
|
1363
|
+
/** A lowercase RFC 9562 UUID of version 1 to 8, named `Uuid`. */
|
|
1364
|
+
declare const UuidSchema: import("@sinclair/typebox").TString;
|
|
1365
|
+
//#endregion
|
|
1366
|
+
//#region src/http.d.ts
|
|
1367
|
+
/**
|
|
1368
|
+
* What a transport receives for one admitted request: the request and its id, the verified caller, the
|
|
1369
|
+
* application, the operations it may serve, the OpenAPI document, and `invoke`, which runs an operation
|
|
1370
|
+
* through the shared execution path.
|
|
1371
|
+
*/
|
|
1372
|
+
interface TransportContext {
|
|
1373
|
+
/**
|
|
1374
|
+
* The admitted request, rebuilt around the body admission read within the transport's limit, so the
|
|
1375
|
+
* transport can read it again.
|
|
1376
|
+
*/
|
|
1377
|
+
request: Request;
|
|
1378
|
+
/** The request id echoed in `x-request-id`; use it to correlate diagnostics. */
|
|
1379
|
+
requestId: string;
|
|
1380
|
+
/**
|
|
1381
|
+
* The verified caller: its actor, actor kind, organization, granted scopes and, when known, its OAuth
|
|
1382
|
+
* client.
|
|
1383
|
+
*/
|
|
1384
|
+
authenticated: TokenPrincipal;
|
|
1385
|
+
/** The application the transport serves: its name, title, version, public origin and MCP configuration. */
|
|
1386
|
+
application: {
|
|
1387
|
+
name: string;
|
|
1388
|
+
title: string;
|
|
1389
|
+
version: string;
|
|
1390
|
+
origin: string;
|
|
1391
|
+
mcp?: McpConfiguration;
|
|
1392
|
+
};
|
|
1393
|
+
/**
|
|
1394
|
+
* The operations the transport may run, as `[name, operation]` pairs: every operation the application
|
|
1395
|
+
* declares. `invoke` refuses any other.
|
|
1396
|
+
*/
|
|
1397
|
+
operations: readonly [string, OperationShape][];
|
|
1398
|
+
/** Returns the application's OpenAPI document, the one its `/openapi.json` serves. */
|
|
1399
|
+
contract: () => Record<string, unknown>;
|
|
1400
|
+
/** The application's OpenAPI document as the JSON or YAML text `/openapi.json` or `/openapi.yaml` serves. */
|
|
1401
|
+
contractText: (format: 'json' | 'yaml') => string;
|
|
1402
|
+
/** The public URL of one of the application's own paths, such as `/openapi.json`, below its base path. */
|
|
1403
|
+
url: (path: string) => string;
|
|
1404
|
+
/**
|
|
1405
|
+
* Runs one of `operations` as the verified caller and answers its Result; another operation fails with
|
|
1406
|
+
* `NOT_FOUND`, and each later call is rate limited as its own request.
|
|
1407
|
+
*/
|
|
1408
|
+
invoke: (name: string, operation: OperationShape, input: unknown) => Promise<Result<unknown, Failure>>;
|
|
1409
|
+
/**
|
|
1410
|
+
* Counts one more request this HTTP request carries, such as a JSON-RPC request of a batch, against the
|
|
1411
|
+
* caller's rate limit as a request of its own: false when the limit refuses it. Admission counted the
|
|
1412
|
+
* first, and `invoke` counts each of its calls itself.
|
|
1413
|
+
*/
|
|
1414
|
+
admitRequest: () => Promise<boolean>;
|
|
1415
|
+
/** Bytes of the request body the transport received. */
|
|
1416
|
+
bodyBytes: number;
|
|
1417
|
+
/** The body limit HTTP applies to an operation; a transport holds each invocation to it. */
|
|
1418
|
+
bodyLimit: (operation: OperationShape) => number;
|
|
1419
|
+
}
|
|
1420
|
+
/**
|
|
1421
|
+
* A protocol served at one path beside the operations, such as MCP: the scope a caller needs, the body it
|
|
1422
|
+
* admits and its handler.
|
|
1423
|
+
*/
|
|
1424
|
+
interface HttpTransport {
|
|
1425
|
+
/** Identifies protocol independently of its URL mount. */
|
|
1426
|
+
protocol?: 'mcp';
|
|
1427
|
+
/**
|
|
1428
|
+
* The exact path the transport answers, such as `/mcp`; it stays absolute when the application has a base
|
|
1429
|
+
* path.
|
|
1430
|
+
*/
|
|
1431
|
+
path: string;
|
|
1432
|
+
/**
|
|
1433
|
+
* The scope a caller needs to use the transport, given the application's namespace; MCP's is
|
|
1434
|
+
* `<namespace>:mcp:use`.
|
|
1435
|
+
*/
|
|
1436
|
+
scope: (namespace: string) => string;
|
|
1437
|
+
/**
|
|
1438
|
+
* The largest body the transport admits, given the operations the caller may run (those its scopes and
|
|
1439
|
+
* actor kind admit) and the application's limit. Omit it to keep the application's limit; a transport that
|
|
1440
|
+
* raises it must enforce `bodyLimit` per invocation.
|
|
1441
|
+
*/
|
|
1442
|
+
maxBodyBytes?: (operations: readonly [string, OperationShape][], fallback: number) => number;
|
|
1443
|
+
/** Answers one request at `path`, after its token, rate limit and body have been admitted. */
|
|
1444
|
+
handle: (context: TransportContext) => Promise<Response>;
|
|
1445
|
+
}
|
|
1446
|
+
/** What a plain route receives besides the request. */
|
|
1447
|
+
interface RouteContext {
|
|
1448
|
+
/** The request id echoed in `x-request-id`; use it to correlate diagnostics. */
|
|
1449
|
+
requestId: string;
|
|
1450
|
+
/** Use the application's declared error catalog and this request's correlation for a raw protocol refusal. */
|
|
1451
|
+
fail: (failure: Failure, headers?: ResponseInit['headers']) => Response;
|
|
1452
|
+
/** Keep work running after the response (Workers `ctx.waitUntil`). Best effort: never durable. */
|
|
1453
|
+
waitUntil: (promise: Promise<unknown>) => void;
|
|
1454
|
+
}
|
|
1455
|
+
/**
|
|
1456
|
+
* A plain HTTP route outside token admission, for callers that cannot present a
|
|
1457
|
+
* bearer token: provider webhooks (signed bodies), signed links, OAuth
|
|
1458
|
+
* callbacks. The handler owns authentication completely: it must verify a
|
|
1459
|
+
* signature or token before it treats the request as data. Plain routes share
|
|
1460
|
+
* the handler's diagnostics, request id and security headers; they never
|
|
1461
|
+
* receive a principal and are absent from OpenAPI.
|
|
1462
|
+
*/
|
|
1463
|
+
type HttpRoute<E> = (request: Request, env: E, context: RouteContext) => Promise<Response>;
|
|
1464
|
+
/**
|
|
1465
|
+
* How the HTTP handler composes the application with the rest of a Worker: exact origins, transports, plain
|
|
1466
|
+
* routes and mounts, a base path, a fallback and request hooks.
|
|
1467
|
+
*/
|
|
1468
|
+
interface HttpOptions<E = unknown> {
|
|
1469
|
+
/** Exact deployment origins; refusal precedes metadata, hooks and token admission. */
|
|
1470
|
+
origins?: readonly string[];
|
|
1471
|
+
/**
|
|
1472
|
+
* Test seam for the default OAuth verification: returns a token's claims in place of the JWT check; claim
|
|
1473
|
+
* mapping and scope checks still apply. Never deploy it.
|
|
1474
|
+
*/
|
|
1475
|
+
verify?: (token: string) => Promise<unknown>;
|
|
1476
|
+
/**
|
|
1477
|
+
* Called with the operation name and request id before each operation runs, while telemetry is on;
|
|
1478
|
+
* `createWorker` uses it to set span attributes.
|
|
1479
|
+
*/
|
|
1480
|
+
annotate?: (operation: string, requestId: string) => void;
|
|
1481
|
+
/**
|
|
1482
|
+
* Protocol transports served beside the operations, such as `mcp()`; one per path, and at most one MCP
|
|
1483
|
+
* transport.
|
|
1484
|
+
*/
|
|
1485
|
+
transports?: readonly HttpTransport[];
|
|
1486
|
+
/**
|
|
1487
|
+
* Plain routes by exact path. Paths under `/v<n>/` and every discovery,
|
|
1488
|
+
* operation or transport path are reserved. A route may sit beneath a
|
|
1489
|
+
* transport's path (a second MCP server at `/mcp/agent-runtime`): the
|
|
1490
|
+
* transport answers only its exact path.
|
|
1491
|
+
*/
|
|
1492
|
+
routes?: Readonly<Record<string, HttpRoute<E>>>;
|
|
1493
|
+
/** Mount native operations and discovery below this path; transports and plain routes remain absolute. */
|
|
1494
|
+
basePath?: string;
|
|
1495
|
+
/** Absolute public liveness path; defaults to the mounted `/health`. No dependency probes run here. */
|
|
1496
|
+
healthPath?: string;
|
|
1497
|
+
/** Prefix protocols own the prefix itself and every slash-delimited descendant. Nested mounts use the longest prefix. */
|
|
1498
|
+
mounts?: Readonly<Record<string, HttpRoute<E>>>;
|
|
1499
|
+
/** Request-local product admission, before any route or native token admission. */
|
|
1500
|
+
before?: (request: Request, env: E, context: RouteContext) => Response | null | Promise<Response | null>;
|
|
1501
|
+
/** Handles only paths outside all native, transport and mounted protocol namespaces. */
|
|
1502
|
+
fallback?: HttpRoute<E>;
|
|
1503
|
+
/** Scope a matched HTTP operation's environment. The continuation may run only once. */
|
|
1504
|
+
aroundOperation?: (request: Request, env: E, operation: Readonly<{
|
|
1505
|
+
name: string;
|
|
1506
|
+
effect: OperationShape['effect'];
|
|
1507
|
+
}>, run: (scopedEnv: E) => Promise<Response>) => Promise<Response>;
|
|
1508
|
+
}
|
|
1509
|
+
/**
|
|
1510
|
+
* Serves an application over HTTP as one function, `(request, env, background?) => Promise<Response>`: its
|
|
1511
|
+
* operations at their routes, the OpenAPI document as JSON and YAML, OAuth discovery, `/health`, transports
|
|
1512
|
+
* such as MCP, plain routes and mounts. Construction refuses a path two of them would claim.
|
|
1513
|
+
*/
|
|
1514
|
+
declare function createHttpHandler<E, C, O extends Record<string, OperationShape>>(manifest: AppManifest<E, C, O>, hooks?: HttpOptions<E>): (request: Request, env: E, background?: BackgroundContext) => Promise<Response>;
|
|
1515
|
+
//#endregion
|
|
1516
|
+
//#region src/http-paths.d.ts
|
|
1517
|
+
/**
|
|
1518
|
+
* `@arbol-ai/maximus/http-paths`: versioned route templates, path parameter decoding, and the router that
|
|
1519
|
+
* matches a request to exactly one operation.
|
|
1520
|
+
*/
|
|
1521
|
+
/** Whether a byte operation may read one of its input fields from this header. */
|
|
1522
|
+
declare function isInputHeaderName(header: string): boolean;
|
|
1523
|
+
/** Parse declared templates, never caller paths. Parameters occupy one complete path segment. */
|
|
1524
|
+
declare function operationPathParameters(path: string): string[];
|
|
1525
|
+
/** A decoded item key stays in one segment; dot segments and controls cannot alter URL ownership. */
|
|
1526
|
+
declare function decodePathParameter(segment: string): string | null;
|
|
1527
|
+
/** Item keys are nonempty strings that cannot change URL path ownership when encoded. */
|
|
1528
|
+
declare function isPathParameterValue(value: unknown): value is string;
|
|
1529
|
+
/** Owner metadata is retained unchanged; schema admission remains the manifest's responsibility. */
|
|
1530
|
+
interface HttpRouteDefinition {
|
|
1531
|
+
/**
|
|
1532
|
+
* The method and versioned path template to route, such as `GET /v1/notes/{noteId}`; a definition without
|
|
1533
|
+
* one is not routed.
|
|
1534
|
+
*/
|
|
1535
|
+
http?: {
|
|
1536
|
+
method: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
|
|
1537
|
+
path: string;
|
|
1538
|
+
};
|
|
1539
|
+
}
|
|
1540
|
+
/** A matched route: its definition and its decoded path parameters. */
|
|
1541
|
+
interface HttpRouteMatch<D> {
|
|
1542
|
+
/** The matched route's definition, as it was passed to the resolver. */
|
|
1543
|
+
definition: D;
|
|
1544
|
+
/** Each path parameter's decoded value, keyed by its name in the template. */
|
|
1545
|
+
parameters: Readonly<Record<string, string>>;
|
|
1546
|
+
}
|
|
1547
|
+
/**
|
|
1548
|
+
* Exact routes use a Map. Template branches compile into deterministic states at construction:
|
|
1549
|
+
* one transition per segment, including overlapping fixed prefixes, with no per-request route scan.
|
|
1550
|
+
*/
|
|
1551
|
+
declare function createHttpRouteResolver<D extends HttpRouteDefinition>(definitions: readonly D[]): {
|
|
1552
|
+
match: (method: string, path: string) => HttpRouteMatch<D> | null;
|
|
1553
|
+
/** Used only at construction to prevent a transport from bypassing item-route authorization. */
|
|
1554
|
+
ownsPath(path: string): boolean;
|
|
1555
|
+
};
|
|
1556
|
+
//#endregion
|
|
1557
|
+
//#region src/internal/body.d.ts
|
|
1558
|
+
/** Closed decoding vocabulary used by bounded transport readers. */
|
|
1559
|
+
declare const HTTP_BODY_FAILURES: readonly ['too_large', 'invalid_length', 'invalid_json', 'invalid_utf8', 'unexpected_content_type', 'aborted', 'read_failed'];
|
|
1560
|
+
/** Why a body could not be read. */
|
|
1561
|
+
type HttpBodyFailureReason = (typeof HTTP_BODY_FAILURES)[number];
|
|
1562
|
+
/** A body that could not be read, with its reason. */
|
|
1563
|
+
interface HttpBodyFailure extends Failure<{
|
|
1564
|
+
reason: HttpBodyFailureReason;
|
|
1565
|
+
}> {
|
|
1566
|
+
/** The one code every body read failure carries; `data.reason` says why the body could not be read. */
|
|
1567
|
+
code: 'HTTP_BODY_INVALID';
|
|
1568
|
+
}
|
|
1569
|
+
/** How much of a body to read, and until when. */
|
|
1570
|
+
interface BodyReadOptions {
|
|
1571
|
+
/** The most body bytes to read, a positive safe integer; a longer body fails as `too_large`. */
|
|
1572
|
+
maxBytes: number;
|
|
1573
|
+
/** Pass the same deadline to fetch and the reader for outbound requests. */
|
|
1574
|
+
signal?: AbortSignal;
|
|
1575
|
+
}
|
|
1576
|
+
/** What a body is read from: a request or a response. */
|
|
1577
|
+
type BodySource = Pick<Request, 'body' | 'headers'>;
|
|
1578
|
+
/**
|
|
1579
|
+
* Read at most the owner's byte limit, including chunked bodies. Returns original
|
|
1580
|
+
* bytes for signature verification. Expected decoding failures are plain results.
|
|
1581
|
+
* Rejected streams are cancelled without awaiting a remote acknowledgement.
|
|
1582
|
+
*/
|
|
1583
|
+
declare function readBodyBytes(source: BodySource, options: BodyReadOptions): Promise<Result<Uint8Array<ArrayBuffer>, HttpBodyFailure>>;
|
|
1584
|
+
/** Decode bounded UTF-8; signed text adapters can retain vendor replacement semantics. */
|
|
1585
|
+
declare function readBodyText(source: BodySource, options: BodyReadOptions & {
|
|
1586
|
+
fatalUtf8?: boolean;
|
|
1587
|
+
}): Promise<Result<string, HttpBodyFailure>>;
|
|
1588
|
+
//#endregion
|
|
1589
|
+
//#region src/internal/json.d.ts
|
|
1590
|
+
/** `application/json`, or a structured `+json` type such as `application/problem+json`. */
|
|
1591
|
+
declare function isJsonMediaType(value: string | null): boolean;
|
|
1592
|
+
/** Decode bounded JSON into unknown for the owner's input/output validator. */
|
|
1593
|
+
declare function readJsonBody(source: Pick<Request, 'body' | 'headers'>, options: {
|
|
1594
|
+
maxBytes: number;
|
|
1595
|
+
signal?: AbortSignal;
|
|
1596
|
+
allowJsonSuffix?: boolean;
|
|
1597
|
+
}): Promise<Result<unknown, HttpBodyFailure>>;
|
|
1598
|
+
/** Pass the same deadline to fetch; empty 204/205 responses have no JSON body. */
|
|
1599
|
+
declare function readJsonResponse(response: Response, options: {
|
|
1600
|
+
maxBytes: number;
|
|
1601
|
+
signal: AbortSignal;
|
|
1602
|
+
}): Promise<Result<unknown, HttpBodyFailure>>;
|
|
1603
|
+
/** Bounded Retry-After hint only. A hint never makes repeating a mutation safe. */
|
|
1604
|
+
declare function retryAfterSeconds(value: string | null, nowMs: number, maximum?: number): number | undefined;
|
|
1605
|
+
//#endregion
|
|
1606
|
+
//#region src/openapi.d.ts
|
|
1607
|
+
/**
|
|
1608
|
+
* The document the origin serves at `/openapi.json`: every public operation that declares an HTTP route,
|
|
1609
|
+
* never an internal one. Each operation also carries its scopes, effect, limits in force, MCP tool hints
|
|
1610
|
+
* and native RPC as `x-*` extensions.
|
|
1611
|
+
*
|
|
1612
|
+
* Every schema reference in it is a local `#/components/schemas/<name>` pointer: the `$id`-named
|
|
1613
|
+
* schemas of its paths and components (TypeBox Recursive/Ref) become components, as `bundleSchemas`
|
|
1614
|
+
* describes.
|
|
1615
|
+
*/
|
|
1616
|
+
declare function createOpenApi<E, C, O extends Record<string, OperationShape>>(manifest: AppManifest<E, C, O>): Record<string, unknown>;
|
|
1617
|
+
//#endregion
|
|
1618
|
+
//#region src/execution.d.ts
|
|
1619
|
+
/**
|
|
1620
|
+
* The execution path every transport shares: checks that the caller may run the operation, decodes the input,
|
|
1621
|
+
* prepares the context, runs the middleware and the handler, then checks the result against the declared
|
|
1622
|
+
* output and failures. An unexpected error becomes `INTERNAL_ERROR` and is reported, never thrown.
|
|
1623
|
+
*/
|
|
1624
|
+
declare function executeOperation<E, C, O extends Record<string, OperationShape>>(manifest: AppManifest<E, C, O>, operation: OperationShape, env: E, principal: Principal, input: unknown, authority?: InvocationAuthority, background?: BackgroundContext, body?: BinaryBody): Promise<Result<unknown, Failure>>;
|
|
1625
|
+
//#endregion
|
|
1626
|
+
//#region src/define-app.d.ts
|
|
1627
|
+
/** Checks the application, then hands it back unchanged. */
|
|
1628
|
+
declare function defineApp<Env, C>(): <const Ops extends Record<string, OperationShape>>(manifest: AppManifest<Env, C, Ops>) => AppManifest<Env, C, Ops>;
|
|
1629
|
+
//#endregion
|
|
1630
|
+
export { PageQuery as $, telemetryEnabled as $t, EmptyInputSchema as A, FailureData as An, AppManifest as At, EmailAddressSchema as B, parseTraceParent as Bn, composeOperations as Bt, operationPathParameters as C, isResult as Cn, decodeInput as Ct, RouteContext as D, ErrorDefinition as Dn, ApplicationBuilder as Dt, HttpTransport as E, ErrorCatalog as En, parseSchema as Et, Type$2 as F, DurableCorrelation as Fn, McpGuide as Ft, PhoneNumberSchema as G, AdmissionObservation as Gt, HttpsUrlSchema as H, AttachmentOutputSchema as Hn, operation as Ht, UuidSchema as I, TraceParent as In, McpSkill as It, SignedUrl as J, Observer as Jt, PostalAddress as K, BackgroundContext as Kt, t$1 as L, createRequestId as Ln, OperationMiddleware as Lt, Static$2 as M, defineErrors as Mn, Invocation as Mt, StaticDecode$1 as N, resultSchema as Nn, InvocationAuthority as Nt, TransportContext as O, ErrorFactory as On, ApplicationConfiguration as Ot, TSchema$2 as P, standardErrors as Pn, McpConfiguration as Pt, PageInfoSchema as Q, reportUnexpected as Qt, DeletedObject as R, durableCorrelation as Rn, OperationScope as Rt, isPathParameterValue as S, err as Sn, InputIssue as St, HttpRoute as T, DefinedErrors as Tn, parseInput as Tt, NamedReference as U, operationMatchesScopes as Ut, HexColorSchema as V, Attachment as Vn, isPublicOperation as Vt, NamedReferenceSchema as W, operationScopes as Wt, Page as X, diagnosticsEnabled as Xt, SignedUrlSchema as Y, TelemetryConfig as Yt, PageInfo as Z, recordExecution as Zt, HttpRouteDefinition as _, matchesScopes as _n, refineSchema as _t, Type$1 as a, TokenClient as an, readSchema as at, decodePathParameter as b, Result as bn, transformSchema as bt, isJsonMediaType as c, TokenPrincipal as cn, SchemaValidationError as ct, retryAfterSeconds as d, createTokenAuthorization as dn, inputSchema as dt, withDiagnostics as en, PageQueryFields as et, BodyReadOptions as f, createTokenVerifier as fn, inspectSchema as ft, readBodyText as g, ScopeMatch as gn, pipeSchema as gt, readBodyBytes as h, protectedResourceMetadata as hn, normalizeSchema as ht, TSchema$1 as i, TokenClaims as in, checkSchema as it, OrganizationIdSchema as j, FailureSchema as jn, Exposure as jt, createHttpHandler as k, Failure as kn, maximus as kt, readJsonBody as l, TokenResolutionContext as ln, defaultSchema as lt, HttpBodyFailureReason as m, OAuthResource as mn, looseObject as mt, executeOperation as n, PrincipalSchema as nn, pageLimit as nt, t as o, TokenDevelopment as on, SchemaInput as ot, HttpBodyFailure as p, projectToken as pn, literalUnion as pt, PostalAddressSchema as q, ExecutionObservation as qt, Static$1 as r, TokenAlgorithm as rn, Checked as rt, createOpenApi as s, TokenPolicy as sn, SchemaIssue as st, defineApp as t, Principal as tn, TPage as tt, readJsonResponse as u, TokenScheme as un, extendObject as ut, HttpRouteMatch as v, BinaryBody as vn, strictObject as vt, HttpOptions as w, ok as wn, decodeSchema as wt, isInputHeaderName as x, Success as xn, urlSchema as xt, createHttpRouteResolver as y, FailureResult as yn, stripObject as yt, DeletedObjectSchema as z, isRequestId as zn, OperationShape as zt };
|