@loozo/angular-launch 0.0.3 → 0.0.5
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/README.md +290 -98
- package/builders/application.d.ts +4 -0
- package/builders/application.js +71 -0
- package/builders/options.d.ts +10 -0
- package/builders/options.js +64 -0
- package/builders/serve.d.ts +8 -0
- package/builders/serve.js +248 -0
- package/builders.json +15 -0
- package/client.d.ts +42 -21
- package/client.js +34 -16
- package/generation/context-file.d.ts +2 -0
- package/generation/context-file.js +3 -0
- package/generation/index.d.ts +8 -1
- package/generation/index.js +75 -38
- package/index.d.ts +1 -0
- package/input-issues.d.ts +56 -0
- package/input-issues.js +78 -0
- package/package.json +21 -3
- package/schemas/application.json +756 -0
- package/schemas/serve.json +153 -0
- package/server/backend.d.ts +3 -3
- package/server/backend.js +19 -8
- package/server/definitions.d.ts +347 -177
- package/server/definitions.js +82 -103
- package/server/dispatch.d.ts +45 -14
- package/server/dispatch.js +212 -73
- package/server/index.d.ts +4 -3
- package/server/index.js +2 -1
- package/server/outcomes.d.ts +26 -0
- package/server/outcomes.js +36 -0
- package/tanstack-query/README.md +164 -0
- package/tanstack-query/generated.d.ts +5 -0
- package/tanstack-query/generated.js +127 -0
- package/tanstack-query/hydration.d.ts +46 -0
- package/tanstack-query/hydration.js +70 -0
- package/tanstack-query/index.d.ts +2 -0
- package/tanstack-query/index.js +1 -0
- package/tanstack-query/queries.d.ts +124 -0
- package/tanstack-query/queries.js +1 -0
package/server/definitions.d.ts
CHANGED
|
@@ -16,7 +16,7 @@ export interface Register {
|
|
|
16
16
|
* The application's context type, as registered by generation.
|
|
17
17
|
*
|
|
18
18
|
* @remarks
|
|
19
|
-
*
|
|
19
|
+
* Middleware and handlers start from a value of this type as `context`. It's `never` until
|
|
20
20
|
* generation has registered the context type, so include the generated files in your
|
|
21
21
|
* tsconfig.
|
|
22
22
|
*/
|
|
@@ -30,8 +30,8 @@ export type Context = Register extends {
|
|
|
30
30
|
* @remarks
|
|
31
31
|
* Launch uses schemas for procedure inputs and declared error data. Validation may be
|
|
32
32
|
* asynchronous and may transform the value. For a procedure input, callers pass the
|
|
33
|
-
* input type and the procedure's
|
|
34
|
-
*
|
|
33
|
+
* input type and the procedure's middleware and handler receive the output type; group
|
|
34
|
+
* middleware receives no input, and the client's success value is the handler's result,
|
|
35
35
|
* not a schema output. For declared error data, the client receives the output type.
|
|
36
36
|
*
|
|
37
37
|
* @example
|
|
@@ -87,10 +87,18 @@ export interface Schema<Input = unknown, Output = Input> {
|
|
|
87
87
|
/** Absent on success. */
|
|
88
88
|
readonly issues?: undefined;
|
|
89
89
|
} | {
|
|
90
|
-
/**
|
|
90
|
+
/**
|
|
91
|
+
* The problems found, which mark a failure. For a procedure input, each issue's
|
|
92
|
+
* message and path are sent to the client as `INVALID_INPUT` data.
|
|
93
|
+
*/
|
|
91
94
|
readonly issues: readonly {
|
|
92
95
|
/** A description of the problem. */
|
|
93
96
|
readonly message: string;
|
|
97
|
+
/** Where the problem is: property keys, or `{ key }` segments. */
|
|
98
|
+
readonly path?: readonly (PropertyKey | {
|
|
99
|
+
/** The segment's property key. */
|
|
100
|
+
readonly key: PropertyKey;
|
|
101
|
+
})[] | undefined;
|
|
94
102
|
}[];
|
|
95
103
|
} | Promise<{
|
|
96
104
|
/** The validated, possibly transformed value. */
|
|
@@ -98,10 +106,18 @@ export interface Schema<Input = unknown, Output = Input> {
|
|
|
98
106
|
/** Absent on success. */
|
|
99
107
|
readonly issues?: undefined;
|
|
100
108
|
} | {
|
|
101
|
-
/**
|
|
109
|
+
/**
|
|
110
|
+
* The problems found, which mark a failure. For a procedure input, each issue's
|
|
111
|
+
* message and path are sent to the client as `INVALID_INPUT` data.
|
|
112
|
+
*/
|
|
102
113
|
readonly issues: readonly {
|
|
103
114
|
/** A description of the problem. */
|
|
104
115
|
readonly message: string;
|
|
116
|
+
/** Where the problem is: property keys, or `{ key }` segments. */
|
|
117
|
+
readonly path?: readonly (PropertyKey | {
|
|
118
|
+
/** The segment's property key. */
|
|
119
|
+
readonly key: PropertyKey;
|
|
120
|
+
})[] | undefined;
|
|
105
121
|
}[];
|
|
106
122
|
}>;
|
|
107
123
|
};
|
|
@@ -137,8 +153,8 @@ export interface ContextDefinition<Value> {
|
|
|
137
153
|
* from this entry point and call it without type arguments; a context assigned to a
|
|
138
154
|
* variable and exported later, or `defineContext<MyContext>(...)`, fails generation.
|
|
139
155
|
* The factory runs once per live server request, for page renders
|
|
140
|
-
* and RPC requests alike, and its value is
|
|
141
|
-
*
|
|
156
|
+
* and RPC requests alike, and its value is the `context` middleware and handlers start
|
|
157
|
+
* from. Keep per-request state in the context rather than in module variables.
|
|
142
158
|
*
|
|
143
159
|
* @example
|
|
144
160
|
* ```ts
|
|
@@ -158,52 +174,6 @@ export interface ContextDefinition<Value> {
|
|
|
158
174
|
export declare function defineContext<Value>(create: (request: Request) => Value): ContextDefinition<Value>;
|
|
159
175
|
/** The awaited context type of a context created by {@link defineContext}. */
|
|
160
176
|
export type ContextOf<Definition> = Definition extends ContextDefinition<infer Value> ? Awaited<Value> : never;
|
|
161
|
-
/**
|
|
162
|
-
* A group guard, passed to `defineRpc` in `canActivate`.
|
|
163
|
-
*
|
|
164
|
-
* @remarks
|
|
165
|
-
* Group guards run after input validation and before the procedure's guards, for
|
|
166
|
-
* every procedure in the group. Returning `false` fails the call with `FORBIDDEN`;
|
|
167
|
-
* throwing fails it with `INTERNAL`. Guards aren't inherited through the route tree
|
|
168
|
-
* and don't narrow types.
|
|
169
|
-
*
|
|
170
|
-
* @example
|
|
171
|
-
* ```ts
|
|
172
|
-
* import type { Guard } from "@loozo/angular-launch/server";
|
|
173
|
-
*
|
|
174
|
-
* export const isSignedIn: Guard = ({ context }) => context.user !== null;
|
|
175
|
-
* ```
|
|
176
|
-
*/
|
|
177
|
-
export type Guard = (args: {
|
|
178
|
-
/** The context created for the request. */
|
|
179
|
-
readonly context: Context;
|
|
180
|
-
/**
|
|
181
|
-
* The live request the call runs in, bound to its request scope: the scope's copy of
|
|
182
|
-
* the RPC HTTP request, or, during server rendering, the page request given to Angular.
|
|
183
|
-
*/
|
|
184
|
-
readonly request: Request;
|
|
185
|
-
}) => boolean | Promise<boolean>;
|
|
186
|
-
/**
|
|
187
|
-
* A procedure guard, passed to `query` or `mutation` in `canActivate`.
|
|
188
|
-
*
|
|
189
|
-
* @remarks
|
|
190
|
-
* Procedure guards run after the group's guards and receive the validated input as
|
|
191
|
-
* well. Returning `false` fails the call with `FORBIDDEN`; throwing fails it with
|
|
192
|
-
* `INTERNAL`.
|
|
193
|
-
*
|
|
194
|
-
* @typeParam Input - The procedure's input schema, or `undefined` without input.
|
|
195
|
-
*/
|
|
196
|
-
export type ProcedureGuard<Input> = (args: {
|
|
197
|
-
/** The context created for the request. */
|
|
198
|
-
readonly context: Context;
|
|
199
|
-
/**
|
|
200
|
-
* The live request the call runs in, bound to its request scope: the scope's copy of
|
|
201
|
-
* the RPC HTTP request, or, during server rendering, the page request given to Angular.
|
|
202
|
-
*/
|
|
203
|
-
readonly request: Request;
|
|
204
|
-
/** The validated input: the schema's output, or `undefined` without an input schema. */
|
|
205
|
-
readonly input: OutputOf<Input>;
|
|
206
|
-
}) => boolean | Promise<boolean>;
|
|
207
177
|
type Digit = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9;
|
|
208
178
|
type StatusNumber<Value> = Value extends `${infer Status extends number}` ? Status : never;
|
|
209
179
|
/** An HTTP error status from 400 to 599, as an integer literal type. */
|
|
@@ -258,8 +228,211 @@ type ErrorContract<Definitions extends ErrorDefinitions> = {
|
|
|
258
228
|
type JsonResult<Value> = Value extends Promise<infer Result> ? Promise<JsonShape<Result>> : JsonShape<Value>;
|
|
259
229
|
/** Whether a procedure reads (`"query"`) or writes (`"mutation"`). */
|
|
260
230
|
export type ProcedureKind = "query" | "mutation";
|
|
231
|
+
declare const middlewareResult: unique symbol;
|
|
232
|
+
type OptionalKeys<Value> = {
|
|
233
|
+
[Key in keyof Value]-?: {} extends Pick<Value, Key> ? Key : never;
|
|
234
|
+
}[keyof Value];
|
|
235
|
+
type IsUnion<Value, Whole = Value> = Value extends unknown ? [Whole] extends [Value] ? false : true : never;
|
|
236
|
+
type AdditionRule<Value> = true extends IsUnion<Value> ? never : {
|
|
237
|
+
readonly [Key in OptionalKeys<Value>]-?: never;
|
|
238
|
+
};
|
|
239
|
+
/**
|
|
240
|
+
* What a middleware's `next()` resolves to, and what the middleware must return.
|
|
241
|
+
*
|
|
242
|
+
* @remarks
|
|
243
|
+
* Opaque: it proves the rest of the call ran, and its type carries what the middleware
|
|
244
|
+
* added to the context. There's no other way to create one.
|
|
245
|
+
*
|
|
246
|
+
* Results that add different keys don't combine, so a middleware whose `next(...)` calls
|
|
247
|
+
* add different keys is a type error rather than a result that hides some of them.
|
|
248
|
+
* Results with the same keys combine only when one's values already include the
|
|
249
|
+
* other's, so a value that differs per branch belongs in one `next()` call.
|
|
250
|
+
*
|
|
251
|
+
* @typeParam Added - The values the middleware passed to `next()`.
|
|
252
|
+
* @typeParam Keys - The keys of `Added`. Leave it to its default.
|
|
253
|
+
*/
|
|
254
|
+
export interface MiddlewareResult<Added, Keys = keyof Added> {
|
|
255
|
+
/** Type-only brand; absent at runtime. */
|
|
256
|
+
readonly [middlewareResult]: {
|
|
257
|
+
/** The added keys, in both directions so that results only combine with the same keys. */
|
|
258
|
+
readonly keys: (key: Keys) => Keys;
|
|
259
|
+
/** The added values. */
|
|
260
|
+
readonly values: Added;
|
|
261
|
+
};
|
|
262
|
+
}
|
|
263
|
+
/**
|
|
264
|
+
* What middleware and handlers receive for one call.
|
|
265
|
+
*
|
|
266
|
+
* @typeParam ContextValue - The context at this point of the call.
|
|
267
|
+
* @typeParam Input - The validated input, or `unknown` for group middleware.
|
|
268
|
+
*/
|
|
269
|
+
export interface CallArgs<ContextValue, Input> {
|
|
270
|
+
/**
|
|
271
|
+
* The context created for the request, with what earlier middleware added to it.
|
|
272
|
+
*
|
|
273
|
+
* @remarks
|
|
274
|
+
* Read-only at the top level: a middleware adds or replaces values through `next()`,
|
|
275
|
+
* so later middleware and the handler see their types. Nested objects stay writable.
|
|
276
|
+
*/
|
|
277
|
+
readonly context: Readonly<ContextValue>;
|
|
278
|
+
/**
|
|
279
|
+
* The validated input: the schema's output, or `undefined` without an input schema.
|
|
280
|
+
* Group middleware runs before validation and receives `undefined`.
|
|
281
|
+
*/
|
|
282
|
+
readonly input: Input;
|
|
283
|
+
/**
|
|
284
|
+
* The live request the call runs in, bound to its request scope: the scope's copy of
|
|
285
|
+
* the RPC HTTP request, or, during server rendering, the page request given to Angular.
|
|
286
|
+
*/
|
|
287
|
+
readonly request: Request;
|
|
288
|
+
/**
|
|
289
|
+
* Aborts when the caller goes away: over HTTP when the incoming request's signal
|
|
290
|
+
* aborts, and during server rendering when the page request aborts or `HttpClient`
|
|
291
|
+
* unsubscribes before the call settled. Advisory: pass it on to work that accepts one.
|
|
292
|
+
*/
|
|
293
|
+
readonly signal: AbortSignal;
|
|
294
|
+
}
|
|
261
295
|
/**
|
|
262
|
-
*
|
|
296
|
+
* What a middleware receives: the call's arguments and `next`.
|
|
297
|
+
*
|
|
298
|
+
* @remarks
|
|
299
|
+
* Annotate a middleware's argument with this type to declare what it needs: the context
|
|
300
|
+
* values earlier middleware must have added, and the input the procedure's schema must
|
|
301
|
+
* produce. Without an annotation a middleware needs the registered {@link Context} and
|
|
302
|
+
* `unknown` input, so it fits anywhere.
|
|
303
|
+
*
|
|
304
|
+
* @example
|
|
305
|
+
* ```ts
|
|
306
|
+
* export const ownTodo = middleware(
|
|
307
|
+
* async ({ context, input, next }: MiddlewareArgs<{ user: User; db: Db }, { id: string }>) => {
|
|
308
|
+
* const todo = await context.db.todos.findForUser(context.user.id, input.id);
|
|
309
|
+
*
|
|
310
|
+
* if (todo === null) {
|
|
311
|
+
* throw forbidden();
|
|
312
|
+
* }
|
|
313
|
+
*
|
|
314
|
+
* return next({ todo });
|
|
315
|
+
* },
|
|
316
|
+
* );
|
|
317
|
+
* ```
|
|
318
|
+
*
|
|
319
|
+
* @typeParam Needs - The context the middleware needs. Defaults to {@link Context}.
|
|
320
|
+
* @typeParam Input - The input the middleware needs. Defaults to `unknown`.
|
|
321
|
+
*/
|
|
322
|
+
export interface MiddlewareArgs<Needs = Context, Input = unknown> extends CallArgs<Needs, Input> {
|
|
323
|
+
/**
|
|
324
|
+
* Continues the call, optionally with values merged into the context.
|
|
325
|
+
*
|
|
326
|
+
* @remarks
|
|
327
|
+
* A function property rather than a method, so destructuring it is safe.
|
|
328
|
+
*/
|
|
329
|
+
readonly next: {
|
|
330
|
+
/**
|
|
331
|
+
* Continues the call with the context unchanged.
|
|
332
|
+
*
|
|
333
|
+
* @returns The result to return from the middleware. It rejects with whatever the
|
|
334
|
+
* rest of the call threw, including declared errors and `forbidden()`.
|
|
335
|
+
*/
|
|
336
|
+
(): Promise<MiddlewareResult<{}>>;
|
|
337
|
+
/**
|
|
338
|
+
* Continues the call with `added` merged into the context by spread, so later
|
|
339
|
+
* middleware and the handler see its values and types.
|
|
340
|
+
*
|
|
341
|
+
* @remarks
|
|
342
|
+
* `added` must be a single object type whose properties are all required: a missing
|
|
343
|
+
* optional value would leave the earlier one in place, which the context's type can't
|
|
344
|
+
* follow, and a union could hide such a key in one of its members. Write a value that
|
|
345
|
+
* may be missing as `next({ user: maybeUser })` with a type such as `User | null`, and
|
|
346
|
+
* alternatives as a union value: `next({ session: a | b })`.
|
|
347
|
+
*
|
|
348
|
+
* @param added - The values to add or replace.
|
|
349
|
+
* @returns The result to return from the middleware. It rejects with whatever the
|
|
350
|
+
* rest of the call threw, including declared errors and `forbidden()`.
|
|
351
|
+
*/
|
|
352
|
+
<Added extends object>(added: Added & AdditionRule<Added>): Promise<MiddlewareResult<Added>>;
|
|
353
|
+
};
|
|
354
|
+
}
|
|
355
|
+
/**
|
|
356
|
+
* A middleware created by {@link middleware}.
|
|
357
|
+
*
|
|
358
|
+
* @typeParam Needs - The context it needs.
|
|
359
|
+
* @typeParam Added - The values it adds to the context.
|
|
360
|
+
* @typeParam Input - The input it needs.
|
|
361
|
+
*/
|
|
362
|
+
export interface MiddlewareDefinition<Needs, Added, Input> {
|
|
363
|
+
/** Runs the middleware for one call. */
|
|
364
|
+
readonly run: (args: MiddlewareArgs<Needs, Input>) => Promise<MiddlewareResult<Added>>;
|
|
365
|
+
}
|
|
366
|
+
type AnyMiddleware = {
|
|
367
|
+
readonly run: (args: never) => Promise<{
|
|
368
|
+
readonly [middlewareResult]: {
|
|
369
|
+
readonly keys: (key: never) => PropertyKey;
|
|
370
|
+
readonly values: object;
|
|
371
|
+
};
|
|
372
|
+
}>;
|
|
373
|
+
};
|
|
374
|
+
/**
|
|
375
|
+
* Creates a middleware: code that runs around a procedure call, to refuse it, add typed
|
|
376
|
+
* values to its context, or wrap the rest of it.
|
|
377
|
+
*
|
|
378
|
+
* @remarks
|
|
379
|
+
* List middleware in `defineRpc`'s `use` to run it for every procedure in the group, or
|
|
380
|
+
* in a procedure's `use` to run it for that procedure. A call runs the group's
|
|
381
|
+
* middleware with no input, then validates the input, then runs the procedure's
|
|
382
|
+
* middleware with the validated input, then the handler. Middleware runs for every
|
|
383
|
+
* call, also during server rendering, where `request` is the page request.
|
|
384
|
+
*
|
|
385
|
+
* `run` must call `next()` exactly once and return its result: calling it twice or
|
|
386
|
+
* returning anything else fails the call with `INTERNAL`. `next(added)` merges `added`
|
|
387
|
+
* into the context by spread, so the context must be a plain object. The context is
|
|
388
|
+
* read-only at the top level: change it through `next()`. Every `next(...)` in a
|
|
389
|
+
* middleware adds the same keys, and a value that differs per branch goes in one
|
|
390
|
+
* `next()` call with a union-typed value, such as
|
|
391
|
+
* `next({ user: preview ? null : context.user })`: separate `next({ user: null })` and
|
|
392
|
+
* `next({ user: context.user })` calls don't infer `User | null` when `context.user` is
|
|
393
|
+
* a `User`. `next()` rejects when anything after it throws, so `await next()` in a `try`
|
|
394
|
+
* wraps the rest of the call.
|
|
395
|
+
*
|
|
396
|
+
* Refuse a call with `throw forbidden()`, which fails it with `FORBIDDEN`. Any other
|
|
397
|
+
* exception fails it with `INTERNAL` and is logged on the server. Declared errors belong
|
|
398
|
+
* to handlers.
|
|
399
|
+
*
|
|
400
|
+
* @example
|
|
401
|
+
* ```ts
|
|
402
|
+
* import { forbidden, middleware } from "@loozo/angular-launch/server";
|
|
403
|
+
*
|
|
404
|
+
* export const signedIn = middleware(async ({ context, next }) => {
|
|
405
|
+
* if (context.user === null) {
|
|
406
|
+
* throw forbidden();
|
|
407
|
+
* }
|
|
408
|
+
*
|
|
409
|
+
* return next({ user: context.user });
|
|
410
|
+
* });
|
|
411
|
+
* ```
|
|
412
|
+
*
|
|
413
|
+
* @typeParam Needs - The context it needs, from an annotated argument. Defaults to
|
|
414
|
+
* {@link Context}.
|
|
415
|
+
* @typeParam Added - The values it adds, inferred from what it passes to `next()`.
|
|
416
|
+
* @typeParam Input - The input it needs, from an annotated argument. Defaults to
|
|
417
|
+
* `unknown`.
|
|
418
|
+
* @param run - Runs the middleware for one call.
|
|
419
|
+
* @returns The middleware, to list in a `use` array.
|
|
420
|
+
*/
|
|
421
|
+
export declare function middleware<Needs = Context, Added extends object = {}, Input = unknown>(run: (args: MiddlewareArgs<Needs, Input>) => Promise<MiddlewareResult<Added>>): MiddlewareDefinition<Needs, Added, Input>;
|
|
422
|
+
type KeySpellings<Key> = Key extends number ? Key | `${Key}` : Key extends `${infer Numeric extends number}` ? number extends Numeric ? Key : `${Numeric}` extends Key ? Key | Numeric : Key : Key;
|
|
423
|
+
type Merge<Base, Added> = [keyof Added] extends [never] ? Base : Base extends unknown ? Omit<Base, KeySpellings<keyof Added>> & Added : never;
|
|
424
|
+
type Flat<Value> = {
|
|
425
|
+
[Key in keyof Value]: Value[Key];
|
|
426
|
+
} & {};
|
|
427
|
+
type ContextAfter<Base, Use> = Use extends readonly [infer First, ...infer Rest] ? First extends MiddlewareDefinition<infer _Needs, infer Added, infer _Input> ? ContextAfter<Flat<Merge<Base, Added>>, Rest> : Base : Base;
|
|
428
|
+
type CheckChain<Base, Input, Use> = Use extends readonly [infer First, ...infer Rest] ? First extends MiddlewareDefinition<infer Needs, infer Added, infer NeedsInput> ? [
|
|
429
|
+
[
|
|
430
|
+
Base
|
|
431
|
+
] extends [Needs] ? [Input] extends [NeedsInput] ? true extends IsUnion<Added> ? never : [OptionalKeys<Added>] extends [never] ? First : never : never : never,
|
|
432
|
+
...CheckChain<Flat<Merge<Base, Added>>, Input, Rest>
|
|
433
|
+
] : [never] : [];
|
|
434
|
+
/**
|
|
435
|
+
* A procedure created by a group's `query` or `mutation` builder.
|
|
263
436
|
*
|
|
264
437
|
* @remarks
|
|
265
438
|
* Procedures are values for `defineRpc`. Their types drive the generated client;
|
|
@@ -277,145 +450,135 @@ export interface Procedure<Input, Output, Kind extends ProcedureKind = "query",
|
|
|
277
450
|
readonly input: Input;
|
|
278
451
|
/** The declared errors, as written in the procedure's options. */
|
|
279
452
|
readonly errors: Errors;
|
|
280
|
-
/** The procedure's
|
|
281
|
-
readonly
|
|
453
|
+
/** The procedure's middleware, in the order it runs. Empty when none was given. */
|
|
454
|
+
readonly use: readonly AnyMiddleware[];
|
|
282
455
|
/**
|
|
283
456
|
* Runs the authored handler with the procedure's own error factories.
|
|
284
457
|
*
|
|
285
|
-
* @param args - The
|
|
286
|
-
*
|
|
458
|
+
* @param args - The call's arguments. Any `errors` passed here are ignored in favor of
|
|
459
|
+
* the procedure's own factories.
|
|
287
460
|
* @returns The handler's result.
|
|
288
461
|
*/
|
|
289
|
-
readonly handler: (args:
|
|
290
|
-
/** The context created for the request. */
|
|
291
|
-
readonly context: Context;
|
|
292
|
-
/** The validated input: the schema's output, or `undefined` without an input schema. */
|
|
293
|
-
readonly input: OutputOf<Input>;
|
|
294
|
-
/** Ignored; the authored handler receives the procedure's own factories. */
|
|
295
|
-
readonly errors: ErrorFactories<Errors>;
|
|
296
|
-
}) => Output | Promise<Output>;
|
|
462
|
+
readonly handler: (args: CallArgs<never, never>) => Output | Promise<Output>;
|
|
297
463
|
/** Internal record of errors created by this procedure's factories; not public API. */
|
|
298
464
|
readonly [declaredFailures]: WeakMap<object, DeclaredFailure>;
|
|
299
465
|
}
|
|
300
466
|
/**
|
|
301
|
-
* The options
|
|
467
|
+
* The options a group's `query` and `mutation` builders take.
|
|
302
468
|
*
|
|
303
469
|
* @remarks
|
|
304
470
|
* Write them inline in the call; generation reads them statically.
|
|
305
471
|
*
|
|
472
|
+
* @typeParam ContextValue - The context after the group's middleware.
|
|
306
473
|
* @typeParam Input - The input schema, or `undefined` without input.
|
|
474
|
+
* @typeParam Use - The procedure's middleware.
|
|
307
475
|
* @typeParam Output - The handler's return type.
|
|
308
476
|
* @typeParam Errors - The declared errors.
|
|
309
477
|
*/
|
|
310
|
-
export interface ProcedureOptions<Input, Output, Errors extends ErrorDefinitions> {
|
|
478
|
+
export interface ProcedureOptions<ContextValue, Input, Use extends readonly AnyMiddleware[], Output, Errors extends ErrorDefinitions> {
|
|
311
479
|
/** The schema the server validates the input with; omit it for a procedure without input. */
|
|
312
480
|
readonly input?: Input & InputContract<NoInfer<Input>>;
|
|
313
|
-
/** The declared errors, keyed by error code. */
|
|
314
|
-
readonly errors?: Errors & ErrorContract<NoInfer<Errors>>;
|
|
315
481
|
/**
|
|
316
|
-
*
|
|
317
|
-
*
|
|
482
|
+
* Middleware that runs in order after the group's middleware and input validation,
|
|
483
|
+
* with the validated input. Each entry must accept the context and input before it.
|
|
318
484
|
*/
|
|
319
|
-
readonly
|
|
485
|
+
readonly use?: Use & CheckChain<ContextValue, OutputOf<Input>, NoInfer<Use>>;
|
|
486
|
+
/** The declared errors, keyed by error code. */
|
|
487
|
+
readonly errors?: Errors & ErrorContract<NoInfer<Errors>>;
|
|
320
488
|
/**
|
|
321
489
|
* Runs the call and returns its strict JSON result.
|
|
322
490
|
*
|
|
323
|
-
* @param args - The
|
|
491
|
+
* @param args - The call's arguments and the error factories.
|
|
324
492
|
* @returns The call's result, or a promise of it.
|
|
325
493
|
*/
|
|
326
|
-
readonly handler: (args: {
|
|
327
|
-
/** The context created for the request. */
|
|
328
|
-
readonly context: Context;
|
|
329
|
-
/** The validated input: the schema's output, or `undefined` without an input schema. */
|
|
330
|
-
readonly input: OutputOf<Input>;
|
|
494
|
+
readonly handler: (args: CallArgs<ContextAfter<ContextValue, Use>, OutputOf<Input>> & {
|
|
331
495
|
/** One factory per declared error code, creating an error to `throw`. */
|
|
332
496
|
readonly errors: ErrorFactories<Errors>;
|
|
333
497
|
}) => Output & JsonResult<NoInfer<Output>>;
|
|
334
498
|
}
|
|
335
499
|
/**
|
|
336
|
-
*
|
|
337
|
-
*
|
|
338
|
-
* @remarks
|
|
339
|
-
* The handler receives `{ context, input, errors }`: the request's {@link Context},
|
|
340
|
-
* the validated input (`undefined` without an input schema) and the
|
|
341
|
-
* error factories for the declared errors. A call validates the input, runs the
|
|
342
|
-
* group's guards, then the procedure's guards, then the handler. Inputs and results
|
|
343
|
-
* must be strict JSON; a result that isn't fails the call with `INTERNAL`. Throwing an
|
|
344
|
-
* error created by `errors` returns that declared error; any other exception fails the
|
|
345
|
-
* call with `INTERNAL` and is logged on the server.
|
|
346
|
-
*
|
|
347
|
-
* An explicit `input` schema must have a concrete type, without `undefined`, `null`,
|
|
348
|
-
* `any` or `unknown` branches, or generation fails: `Schema<string> | undefined` is
|
|
349
|
-
* rejected. The schema's caller input type may still be a JSON union, as in
|
|
350
|
-
* `Schema<string | number, number>`.
|
|
351
|
-
*
|
|
352
|
-
* Query responses are stored in Angular's HTTP transfer cache during server rendering
|
|
353
|
-
* by default, so hydration reuses them.
|
|
354
|
-
*
|
|
355
|
-
* @example
|
|
356
|
-
* ```ts
|
|
357
|
-
* list: query({
|
|
358
|
-
* handler: ({ context }) => context.db.todos.listForUser(requireUser(context).id),
|
|
359
|
-
* }),
|
|
360
|
-
* ```
|
|
361
|
-
*
|
|
362
|
-
* @typeParam Input - The input schema; omit `input` for a procedure without input.
|
|
363
|
-
* @typeParam Output - The handler's return type.
|
|
364
|
-
* @typeParam Errors - The declared errors, written inline.
|
|
365
|
-
* @param options - The procedure's `input` schema, `canActivate` guards, inline
|
|
366
|
-
* `errors` declarations and `handler`.
|
|
367
|
-
* @returns The procedure, to use as a value in `defineRpc`.
|
|
368
|
-
* @throws TypeError when a declared error is invalid: its code isn't an identifier, is
|
|
369
|
-
* `__proto__` or a framework code, its status isn't an integer from 400 to 599, its
|
|
370
|
-
* message is empty, or its data isn't a Standard Schema V1.
|
|
371
|
-
*/
|
|
372
|
-
export declare function query<Input extends Schema | undefined = undefined, Output = unknown, const Errors extends ErrorDefinitions = {}>(options: ProcedureOptions<Input, Output, Errors>): Procedure<Input, Output, "query", Errors>;
|
|
373
|
-
/**
|
|
374
|
-
* Defines a procedure that writes data.
|
|
500
|
+
* The `query` and `mutation` builders `defineRpc` passes to its callback.
|
|
375
501
|
*
|
|
376
502
|
* @remarks
|
|
377
|
-
*
|
|
378
|
-
* the validated input (`undefined` without an input schema) and the
|
|
379
|
-
* error factories for the declared errors. A call validates the input, runs the
|
|
380
|
-
* group's guards, then the procedure's guards, then the handler. Inputs and results
|
|
381
|
-
* must be strict JSON; a result that isn't fails the call with `INTERNAL`. Throwing an
|
|
382
|
-
* error created by `errors` returns that declared error; any other exception fails the
|
|
383
|
-
* call with `INTERNAL` and is logged on the server.
|
|
384
|
-
*
|
|
385
|
-
* An explicit `input` schema must have a concrete type, without `undefined`, `null`,
|
|
386
|
-
* `any` or `unknown` branches, or generation fails: `Schema<string> | undefined` is
|
|
387
|
-
* rejected. The schema's caller input type may still be a JSON union, as in
|
|
388
|
-
* `Schema<string | number, number>`.
|
|
389
|
-
*
|
|
390
|
-
* Mutations never use the HTTP transfer cache. During server rendering they fail with
|
|
391
|
-
* `FORBIDDEN` before validation, guards or the handler run.
|
|
392
|
-
*
|
|
393
|
-
* @example
|
|
394
|
-
* ```ts
|
|
395
|
-
* add: mutation({
|
|
396
|
-
* input: titleSchema,
|
|
397
|
-
* handler: ({ input, context }) =>
|
|
398
|
-
* context.db.todos.addForUser(requireUser(context).id, input.title),
|
|
399
|
-
* }),
|
|
400
|
-
* ```
|
|
503
|
+
* Function properties rather than methods, so destructuring them is safe.
|
|
401
504
|
*
|
|
402
|
-
* @typeParam
|
|
403
|
-
* @typeParam Output - The handler's return type.
|
|
404
|
-
* @typeParam Errors - The declared errors, written inline.
|
|
405
|
-
* @param options - The procedure's `input` schema, `canActivate` guards, inline
|
|
406
|
-
* `errors` declarations and `handler`.
|
|
407
|
-
* @returns The procedure, to use as a value in `defineRpc`.
|
|
408
|
-
* @throws TypeError when a declared error is invalid: its code isn't an identifier, is
|
|
409
|
-
* `__proto__` or a framework code, its status isn't an integer from 400 to 599, its
|
|
410
|
-
* message is empty, or its data isn't a Standard Schema V1.
|
|
505
|
+
* @typeParam ContextValue - The context after the group's middleware.
|
|
411
506
|
*/
|
|
412
|
-
export
|
|
507
|
+
export interface ProcedureBuilder<ContextValue> {
|
|
508
|
+
/**
|
|
509
|
+
* Defines a procedure that reads data.
|
|
510
|
+
*
|
|
511
|
+
* @remarks
|
|
512
|
+
* The handler receives `{ context, input, errors, request, signal }`: the context
|
|
513
|
+
* after the group's and the procedure's middleware, the validated input (`undefined`
|
|
514
|
+
* without an input schema), the error factories for the declared errors, the request
|
|
515
|
+
* the call runs in and its abort signal. Inputs and results must be strict JSON; a
|
|
516
|
+
* result that isn't fails the call with `INTERNAL`. Throwing an error created by
|
|
517
|
+
* `errors` returns that declared error, and throwing `forbidden()` fails the call with
|
|
518
|
+
* `FORBIDDEN`; any other exception fails the call with `INTERNAL` and is logged on the
|
|
519
|
+
* server.
|
|
520
|
+
*
|
|
521
|
+
* An explicit `input` schema must have a concrete type, without `undefined`, `null`,
|
|
522
|
+
* `any` or `unknown` branches, or generation fails: `Schema<string> | undefined` is
|
|
523
|
+
* rejected. The schema's caller input type may still be a JSON union, as in
|
|
524
|
+
* `Schema<string | number, number>`.
|
|
525
|
+
*
|
|
526
|
+
* Query responses are stored in Angular's HTTP transfer cache during server rendering
|
|
527
|
+
* by default, so hydration reuses them.
|
|
528
|
+
*
|
|
529
|
+
* @example
|
|
530
|
+
* ```ts
|
|
531
|
+
* list: query({
|
|
532
|
+
* handler: ({ context, signal }) => context.db.todos.listForUser(context.user.id, { signal }),
|
|
533
|
+
* }),
|
|
534
|
+
* ```
|
|
535
|
+
*
|
|
536
|
+
* @typeParam Input - The input schema; omit `input` for a procedure without input.
|
|
537
|
+
* @typeParam Use - The procedure's middleware, written inline.
|
|
538
|
+
* @typeParam Output - The handler's return type.
|
|
539
|
+
* @typeParam Errors - The declared errors, written inline.
|
|
540
|
+
* @param options - The procedure's `input` schema, `use` middleware, inline `errors`
|
|
541
|
+
* declarations and `handler`.
|
|
542
|
+
* @returns The procedure, to use as a value in the callback's result.
|
|
543
|
+
* @throws TypeError when a declared error is invalid: its code isn't an identifier, is
|
|
544
|
+
* `__proto__` or a framework code, its status isn't an integer from 400 to 599, its
|
|
545
|
+
* message is empty, or its data isn't a Standard Schema V1.
|
|
546
|
+
*/
|
|
547
|
+
readonly query: <Input extends Schema | undefined = undefined, const Use extends readonly AnyMiddleware[] = [], Output = unknown, const Errors extends ErrorDefinitions = {}>(options: ProcedureOptions<ContextValue, Input, Use, Output, Errors>) => Procedure<Input, Output, "query", Errors>;
|
|
548
|
+
/**
|
|
549
|
+
* Defines a procedure that writes data.
|
|
550
|
+
*
|
|
551
|
+
* @remarks
|
|
552
|
+
* Takes the same options as `query`, and its handler receives the same arguments.
|
|
553
|
+
*
|
|
554
|
+
* Mutations never use the HTTP transfer cache. During server rendering they fail with
|
|
555
|
+
* `FORBIDDEN` before any middleware, validation or the handler runs.
|
|
556
|
+
*
|
|
557
|
+
* @example
|
|
558
|
+
* ```ts
|
|
559
|
+
* add: mutation({
|
|
560
|
+
* input: titleSchema,
|
|
561
|
+
* handler: ({ input, context }) => context.db.todos.addForUser(context.user.id, input.title),
|
|
562
|
+
* }),
|
|
563
|
+
* ```
|
|
564
|
+
*
|
|
565
|
+
* @typeParam Input - The input schema; omit `input` for a procedure without input.
|
|
566
|
+
* @typeParam Use - The procedure's middleware, written inline.
|
|
567
|
+
* @typeParam Output - The handler's return type.
|
|
568
|
+
* @typeParam Errors - The declared errors, written inline.
|
|
569
|
+
* @param options - The procedure's `input` schema, `use` middleware, inline `errors`
|
|
570
|
+
* declarations and `handler`.
|
|
571
|
+
* @returns The procedure, to use as a value in the callback's result.
|
|
572
|
+
* @throws TypeError when a declared error is invalid, as for `query`.
|
|
573
|
+
*/
|
|
574
|
+
readonly mutation: <Input extends Schema | undefined = undefined, const Use extends readonly AnyMiddleware[] = [], Output = unknown, const Errors extends ErrorDefinitions = {}>(options: ProcedureOptions<ContextValue, Input, Use, Output, Errors>) => Procedure<Input, Output, "mutation", Errors>;
|
|
575
|
+
}
|
|
413
576
|
/**
|
|
414
577
|
* The minimal shape `defineRpc` accepts for each procedure.
|
|
415
578
|
*
|
|
416
579
|
* @remarks
|
|
417
|
-
* Create procedures with
|
|
418
|
-
* other value.
|
|
580
|
+
* Create procedures with the `query` and `mutation` builders `defineRpc` passes to its
|
|
581
|
+
* callback; generation rejects any other value.
|
|
419
582
|
*/
|
|
420
583
|
export interface ProcedureDefinition {
|
|
421
584
|
/** Whether the procedure is a `"query"` or a `"mutation"`. */
|
|
@@ -432,8 +595,8 @@ export interface Group<Name extends string, Procedures extends Record<string, Pr
|
|
|
432
595
|
readonly name: Name;
|
|
433
596
|
/** The group's procedures, keyed by procedure name. */
|
|
434
597
|
readonly procedures: Procedures;
|
|
435
|
-
/** The group's
|
|
436
|
-
readonly
|
|
598
|
+
/** The group's middleware, in the order it runs. Empty when none was given. */
|
|
599
|
+
readonly use: readonly AnyMiddleware[];
|
|
437
600
|
}
|
|
438
601
|
/**
|
|
439
602
|
* Defines the RPC group for the route declared in the same directory.
|
|
@@ -442,46 +605,53 @@ export interface Group<Name extends string, Procedures extends Record<string, Pr
|
|
|
442
605
|
* Default-export the result from a file next to the route's `route.ts`; one group per
|
|
443
606
|
* route. That route must be one that gets generated files: not a redirect, and with a
|
|
444
607
|
* page or descendants with pages that aren't all redirects. Generation reads the call
|
|
445
|
-
* statically without running it: import `defineRpc
|
|
446
|
-
*
|
|
447
|
-
*
|
|
448
|
-
*
|
|
449
|
-
*
|
|
450
|
-
*
|
|
451
|
-
*
|
|
608
|
+
* statically without running it: import `defineRpc` by name from this entry point and
|
|
609
|
+
* call it directly without type arguments, with a literal identifier other than
|
|
610
|
+
* `__proto__` for the namespace; pass an arrow function whose parameter destructures
|
|
611
|
+
* `query` and/or `mutation` without renaming and whose body is a parenthesized object
|
|
612
|
+
* literal of procedures, each a direct `query(...)` or `mutation(...)` call with inline
|
|
613
|
+
* options; and write the group options inline. Each route's generated `injectRpc()`
|
|
614
|
+
* exposes its own group and its ancestors' groups under their namespaces. Each namespace
|
|
615
|
+
* must be unique across the app: calls go to `/_rpc/<namespace>/<procedure>`.
|
|
616
|
+
*
|
|
617
|
+
* The group's `use` middleware runs for every procedure in the group, before input
|
|
618
|
+
* validation, and the procedures' handlers and middleware see what it adds to the
|
|
619
|
+
* context. It isn't inherited through the route tree.
|
|
452
620
|
*
|
|
453
621
|
* Exposure isn't authorization: anyone who can reach the server can call any
|
|
454
|
-
* procedure, so
|
|
622
|
+
* procedure, so protect every group and procedure that needs it.
|
|
455
623
|
*
|
|
456
624
|
* @example
|
|
457
625
|
* ```ts
|
|
458
|
-
* import { defineRpc
|
|
459
|
-
* import {
|
|
626
|
+
* import { defineRpc } from "@loozo/angular-launch/server";
|
|
627
|
+
* import { signedIn } from "../../auth.server";
|
|
460
628
|
*
|
|
461
629
|
* export default defineRpc(
|
|
462
630
|
* "todos",
|
|
463
|
-
* {
|
|
631
|
+
* ({ query }) => ({
|
|
464
632
|
* list: query({
|
|
465
|
-
* handler: ({ context }) => context.db.todos.listForUser(
|
|
633
|
+
* handler: ({ context }) => context.db.todos.listForUser(context.user.id),
|
|
466
634
|
* }),
|
|
467
|
-
* },
|
|
468
|
-
* {
|
|
635
|
+
* }),
|
|
636
|
+
* { use: [signedIn] },
|
|
469
637
|
* );
|
|
470
638
|
* ```
|
|
471
639
|
*
|
|
472
640
|
* @typeParam Name - The namespace literal.
|
|
641
|
+
* @typeParam Use - The group's middleware, written inline.
|
|
473
642
|
* @typeParam Procedures - The procedures, keyed by name.
|
|
474
643
|
* @param name - The namespace the generated client exposes the group under.
|
|
475
|
-
* @param
|
|
476
|
-
*
|
|
477
|
-
* group.
|
|
644
|
+
* @param build - Returns the procedures, keyed by name, built with the `query` and
|
|
645
|
+
* `mutation` builders it receives.
|
|
646
|
+
* @param options - `use`: middleware that runs for every procedure in the group.
|
|
478
647
|
* @returns The group, to default-export.
|
|
479
648
|
*/
|
|
480
|
-
export declare function defineRpc<Name extends string, Procedures extends Record<string, ProcedureDefinition
|
|
649
|
+
export declare function defineRpc<Name extends string, const Use extends readonly AnyMiddleware[] = [], Procedures extends Record<string, ProcedureDefinition> = {}>(name: Name, build: (builder: ProcedureBuilder<ContextAfter<Context, Use>>) => Procedures, { use, }?: {
|
|
481
650
|
/**
|
|
482
|
-
*
|
|
483
|
-
* procedure's own
|
|
651
|
+
* Middleware that runs in order for every call to any procedure in the group, before
|
|
652
|
+
* input validation and the procedure's own middleware. It receives no input. Each
|
|
653
|
+
* entry must accept the context before it.
|
|
484
654
|
*/
|
|
485
|
-
readonly
|
|
655
|
+
readonly use?: Use & CheckChain<Context, unknown, NoInfer<Use>>;
|
|
486
656
|
}): Group<Name, Procedures>;
|
|
487
657
|
export {};
|