@loozo/angular-launch 0.0.4 → 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 +261 -89
- 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 +9 -6
- package/client.js +3 -2
- package/generation/context-file.d.ts +2 -0
- package/generation/context-file.js +3 -0
- package/generation/index.js +58 -33
- package/package.json +6 -2
- package/schemas/application.json +756 -0
- package/schemas/serve.json +153 -0
- package/server/backend.js +12 -1
- package/server/definitions.d.ts +329 -175
- package/server/definitions.js +82 -103
- package/server/dispatch.d.ts +42 -14
- package/server/dispatch.js +201 -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 +9 -13
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
|
|
@@ -153,8 +153,8 @@ export interface ContextDefinition<Value> {
|
|
|
153
153
|
* from this entry point and call it without type arguments; a context assigned to a
|
|
154
154
|
* variable and exported later, or `defineContext<MyContext>(...)`, fails generation.
|
|
155
155
|
* The factory runs once per live server request, for page renders
|
|
156
|
-
* and RPC requests alike, and its value is
|
|
157
|
-
*
|
|
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.
|
|
158
158
|
*
|
|
159
159
|
* @example
|
|
160
160
|
* ```ts
|
|
@@ -174,52 +174,6 @@ export interface ContextDefinition<Value> {
|
|
|
174
174
|
export declare function defineContext<Value>(create: (request: Request) => Value): ContextDefinition<Value>;
|
|
175
175
|
/** The awaited context type of a context created by {@link defineContext}. */
|
|
176
176
|
export type ContextOf<Definition> = Definition extends ContextDefinition<infer Value> ? Awaited<Value> : never;
|
|
177
|
-
/**
|
|
178
|
-
* A group guard, passed to `defineRpc` in `canActivate`.
|
|
179
|
-
*
|
|
180
|
-
* @remarks
|
|
181
|
-
* Group guards run after input validation and before the procedure's guards, for
|
|
182
|
-
* every procedure in the group. Returning `false` fails the call with `FORBIDDEN`;
|
|
183
|
-
* throwing fails it with `INTERNAL`. Guards aren't inherited through the route tree
|
|
184
|
-
* and don't narrow types.
|
|
185
|
-
*
|
|
186
|
-
* @example
|
|
187
|
-
* ```ts
|
|
188
|
-
* import type { Guard } from "@loozo/angular-launch/server";
|
|
189
|
-
*
|
|
190
|
-
* export const isSignedIn: Guard = ({ context }) => context.user !== null;
|
|
191
|
-
* ```
|
|
192
|
-
*/
|
|
193
|
-
export type Guard = (args: {
|
|
194
|
-
/** The context created for the request. */
|
|
195
|
-
readonly context: Context;
|
|
196
|
-
/**
|
|
197
|
-
* The live request the call runs in, bound to its request scope: the scope's copy of
|
|
198
|
-
* the RPC HTTP request, or, during server rendering, the page request given to Angular.
|
|
199
|
-
*/
|
|
200
|
-
readonly request: Request;
|
|
201
|
-
}) => boolean | Promise<boolean>;
|
|
202
|
-
/**
|
|
203
|
-
* A procedure guard, passed to `query` or `mutation` in `canActivate`.
|
|
204
|
-
*
|
|
205
|
-
* @remarks
|
|
206
|
-
* Procedure guards run after the group's guards and receive the validated input as
|
|
207
|
-
* well. Returning `false` fails the call with `FORBIDDEN`; throwing fails it with
|
|
208
|
-
* `INTERNAL`.
|
|
209
|
-
*
|
|
210
|
-
* @typeParam Input - The procedure's input schema, or `undefined` without input.
|
|
211
|
-
*/
|
|
212
|
-
export type ProcedureGuard<Input> = (args: {
|
|
213
|
-
/** The context created for the request. */
|
|
214
|
-
readonly context: Context;
|
|
215
|
-
/**
|
|
216
|
-
* The live request the call runs in, bound to its request scope: the scope's copy of
|
|
217
|
-
* the RPC HTTP request, or, during server rendering, the page request given to Angular.
|
|
218
|
-
*/
|
|
219
|
-
readonly request: Request;
|
|
220
|
-
/** The validated input: the schema's output, or `undefined` without an input schema. */
|
|
221
|
-
readonly input: OutputOf<Input>;
|
|
222
|
-
}) => boolean | Promise<boolean>;
|
|
223
177
|
type Digit = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9;
|
|
224
178
|
type StatusNumber<Value> = Value extends `${infer Status extends number}` ? Status : never;
|
|
225
179
|
/** An HTTP error status from 400 to 599, as an integer literal type. */
|
|
@@ -274,8 +228,211 @@ type ErrorContract<Definitions extends ErrorDefinitions> = {
|
|
|
274
228
|
type JsonResult<Value> = Value extends Promise<infer Result> ? Promise<JsonShape<Result>> : JsonShape<Value>;
|
|
275
229
|
/** Whether a procedure reads (`"query"`) or writes (`"mutation"`). */
|
|
276
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
|
+
}
|
|
277
295
|
/**
|
|
278
|
-
*
|
|
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.
|
|
279
436
|
*
|
|
280
437
|
* @remarks
|
|
281
438
|
* Procedures are values for `defineRpc`. Their types drive the generated client;
|
|
@@ -293,145 +450,135 @@ export interface Procedure<Input, Output, Kind extends ProcedureKind = "query",
|
|
|
293
450
|
readonly input: Input;
|
|
294
451
|
/** The declared errors, as written in the procedure's options. */
|
|
295
452
|
readonly errors: Errors;
|
|
296
|
-
/** The procedure's
|
|
297
|
-
readonly
|
|
453
|
+
/** The procedure's middleware, in the order it runs. Empty when none was given. */
|
|
454
|
+
readonly use: readonly AnyMiddleware[];
|
|
298
455
|
/**
|
|
299
456
|
* Runs the authored handler with the procedure's own error factories.
|
|
300
457
|
*
|
|
301
|
-
* @param args - The
|
|
302
|
-
*
|
|
458
|
+
* @param args - The call's arguments. Any `errors` passed here are ignored in favor of
|
|
459
|
+
* the procedure's own factories.
|
|
303
460
|
* @returns The handler's result.
|
|
304
461
|
*/
|
|
305
|
-
readonly handler: (args:
|
|
306
|
-
/** The context created for the request. */
|
|
307
|
-
readonly context: Context;
|
|
308
|
-
/** The validated input: the schema's output, or `undefined` without an input schema. */
|
|
309
|
-
readonly input: OutputOf<Input>;
|
|
310
|
-
/** Ignored; the authored handler receives the procedure's own factories. */
|
|
311
|
-
readonly errors: ErrorFactories<Errors>;
|
|
312
|
-
}) => Output | Promise<Output>;
|
|
462
|
+
readonly handler: (args: CallArgs<never, never>) => Output | Promise<Output>;
|
|
313
463
|
/** Internal record of errors created by this procedure's factories; not public API. */
|
|
314
464
|
readonly [declaredFailures]: WeakMap<object, DeclaredFailure>;
|
|
315
465
|
}
|
|
316
466
|
/**
|
|
317
|
-
* The options
|
|
467
|
+
* The options a group's `query` and `mutation` builders take.
|
|
318
468
|
*
|
|
319
469
|
* @remarks
|
|
320
470
|
* Write them inline in the call; generation reads them statically.
|
|
321
471
|
*
|
|
472
|
+
* @typeParam ContextValue - The context after the group's middleware.
|
|
322
473
|
* @typeParam Input - The input schema, or `undefined` without input.
|
|
474
|
+
* @typeParam Use - The procedure's middleware.
|
|
323
475
|
* @typeParam Output - The handler's return type.
|
|
324
476
|
* @typeParam Errors - The declared errors.
|
|
325
477
|
*/
|
|
326
|
-
export interface ProcedureOptions<Input, Output, Errors extends ErrorDefinitions> {
|
|
478
|
+
export interface ProcedureOptions<ContextValue, Input, Use extends readonly AnyMiddleware[], Output, Errors extends ErrorDefinitions> {
|
|
327
479
|
/** The schema the server validates the input with; omit it for a procedure without input. */
|
|
328
480
|
readonly input?: Input & InputContract<NoInfer<Input>>;
|
|
329
|
-
/** The declared errors, keyed by error code. */
|
|
330
|
-
readonly errors?: Errors & ErrorContract<NoInfer<Errors>>;
|
|
331
481
|
/**
|
|
332
|
-
*
|
|
333
|
-
*
|
|
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.
|
|
334
484
|
*/
|
|
335
|
-
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>>;
|
|
336
488
|
/**
|
|
337
489
|
* Runs the call and returns its strict JSON result.
|
|
338
490
|
*
|
|
339
|
-
* @param args - The
|
|
491
|
+
* @param args - The call's arguments and the error factories.
|
|
340
492
|
* @returns The call's result, or a promise of it.
|
|
341
493
|
*/
|
|
342
|
-
readonly handler: (args: {
|
|
343
|
-
/** The context created for the request. */
|
|
344
|
-
readonly context: Context;
|
|
345
|
-
/** The validated input: the schema's output, or `undefined` without an input schema. */
|
|
346
|
-
readonly input: OutputOf<Input>;
|
|
494
|
+
readonly handler: (args: CallArgs<ContextAfter<ContextValue, Use>, OutputOf<Input>> & {
|
|
347
495
|
/** One factory per declared error code, creating an error to `throw`. */
|
|
348
496
|
readonly errors: ErrorFactories<Errors>;
|
|
349
497
|
}) => Output & JsonResult<NoInfer<Output>>;
|
|
350
498
|
}
|
|
351
499
|
/**
|
|
352
|
-
*
|
|
353
|
-
*
|
|
354
|
-
* @remarks
|
|
355
|
-
* The handler receives `{ context, input, errors }`: the request's {@link Context},
|
|
356
|
-
* the validated input (`undefined` without an input schema) and the
|
|
357
|
-
* error factories for the declared errors. A call validates the input, runs the
|
|
358
|
-
* group's guards, then the procedure's guards, then the handler. Inputs and results
|
|
359
|
-
* must be strict JSON; a result that isn't fails the call with `INTERNAL`. Throwing an
|
|
360
|
-
* error created by `errors` returns that declared error; any other exception fails the
|
|
361
|
-
* call with `INTERNAL` and is logged on the server.
|
|
362
|
-
*
|
|
363
|
-
* An explicit `input` schema must have a concrete type, without `undefined`, `null`,
|
|
364
|
-
* `any` or `unknown` branches, or generation fails: `Schema<string> | undefined` is
|
|
365
|
-
* rejected. The schema's caller input type may still be a JSON union, as in
|
|
366
|
-
* `Schema<string | number, number>`.
|
|
367
|
-
*
|
|
368
|
-
* Query responses are stored in Angular's HTTP transfer cache during server rendering
|
|
369
|
-
* by default, so hydration reuses them.
|
|
370
|
-
*
|
|
371
|
-
* @example
|
|
372
|
-
* ```ts
|
|
373
|
-
* list: query({
|
|
374
|
-
* handler: ({ context }) => context.db.todos.listForUser(requireUser(context).id),
|
|
375
|
-
* }),
|
|
376
|
-
* ```
|
|
377
|
-
*
|
|
378
|
-
* @typeParam Input - The input schema; omit `input` for a procedure without input.
|
|
379
|
-
* @typeParam Output - The handler's return type.
|
|
380
|
-
* @typeParam Errors - The declared errors, written inline.
|
|
381
|
-
* @param options - The procedure's `input` schema, `canActivate` guards, inline
|
|
382
|
-
* `errors` declarations and `handler`.
|
|
383
|
-
* @returns The procedure, to use as a value in `defineRpc`.
|
|
384
|
-
* @throws TypeError when a declared error is invalid: its code isn't an identifier, is
|
|
385
|
-
* `__proto__` or a framework code, its status isn't an integer from 400 to 599, its
|
|
386
|
-
* message is empty, or its data isn't a Standard Schema V1.
|
|
387
|
-
*/
|
|
388
|
-
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>;
|
|
389
|
-
/**
|
|
390
|
-
* Defines a procedure that writes data.
|
|
500
|
+
* The `query` and `mutation` builders `defineRpc` passes to its callback.
|
|
391
501
|
*
|
|
392
502
|
* @remarks
|
|
393
|
-
*
|
|
394
|
-
* the validated input (`undefined` without an input schema) and the
|
|
395
|
-
* error factories for the declared errors. A call validates the input, runs the
|
|
396
|
-
* group's guards, then the procedure's guards, then the handler. Inputs and results
|
|
397
|
-
* must be strict JSON; a result that isn't fails the call with `INTERNAL`. Throwing an
|
|
398
|
-
* error created by `errors` returns that declared error; any other exception fails the
|
|
399
|
-
* call with `INTERNAL` and is logged on the server.
|
|
400
|
-
*
|
|
401
|
-
* An explicit `input` schema must have a concrete type, without `undefined`, `null`,
|
|
402
|
-
* `any` or `unknown` branches, or generation fails: `Schema<string> | undefined` is
|
|
403
|
-
* rejected. The schema's caller input type may still be a JSON union, as in
|
|
404
|
-
* `Schema<string | number, number>`.
|
|
405
|
-
*
|
|
406
|
-
* Mutations never use the HTTP transfer cache. During server rendering they fail with
|
|
407
|
-
* `FORBIDDEN` before validation, guards or the handler run.
|
|
408
|
-
*
|
|
409
|
-
* @example
|
|
410
|
-
* ```ts
|
|
411
|
-
* add: mutation({
|
|
412
|
-
* input: titleSchema,
|
|
413
|
-
* handler: ({ input, context }) =>
|
|
414
|
-
* context.db.todos.addForUser(requireUser(context).id, input.title),
|
|
415
|
-
* }),
|
|
416
|
-
* ```
|
|
503
|
+
* Function properties rather than methods, so destructuring them is safe.
|
|
417
504
|
*
|
|
418
|
-
* @typeParam
|
|
419
|
-
* @typeParam Output - The handler's return type.
|
|
420
|
-
* @typeParam Errors - The declared errors, written inline.
|
|
421
|
-
* @param options - The procedure's `input` schema, `canActivate` guards, inline
|
|
422
|
-
* `errors` declarations and `handler`.
|
|
423
|
-
* @returns The procedure, to use as a value in `defineRpc`.
|
|
424
|
-
* @throws TypeError when a declared error is invalid: its code isn't an identifier, is
|
|
425
|
-
* `__proto__` or a framework code, its status isn't an integer from 400 to 599, its
|
|
426
|
-
* message is empty, or its data isn't a Standard Schema V1.
|
|
505
|
+
* @typeParam ContextValue - The context after the group's middleware.
|
|
427
506
|
*/
|
|
428
|
-
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
|
+
}
|
|
429
576
|
/**
|
|
430
577
|
* The minimal shape `defineRpc` accepts for each procedure.
|
|
431
578
|
*
|
|
432
579
|
* @remarks
|
|
433
|
-
* Create procedures with
|
|
434
|
-
* other value.
|
|
580
|
+
* Create procedures with the `query` and `mutation` builders `defineRpc` passes to its
|
|
581
|
+
* callback; generation rejects any other value.
|
|
435
582
|
*/
|
|
436
583
|
export interface ProcedureDefinition {
|
|
437
584
|
/** Whether the procedure is a `"query"` or a `"mutation"`. */
|
|
@@ -448,8 +595,8 @@ export interface Group<Name extends string, Procedures extends Record<string, Pr
|
|
|
448
595
|
readonly name: Name;
|
|
449
596
|
/** The group's procedures, keyed by procedure name. */
|
|
450
597
|
readonly procedures: Procedures;
|
|
451
|
-
/** The group's
|
|
452
|
-
readonly
|
|
598
|
+
/** The group's middleware, in the order it runs. Empty when none was given. */
|
|
599
|
+
readonly use: readonly AnyMiddleware[];
|
|
453
600
|
}
|
|
454
601
|
/**
|
|
455
602
|
* Defines the RPC group for the route declared in the same directory.
|
|
@@ -458,46 +605,53 @@ export interface Group<Name extends string, Procedures extends Record<string, Pr
|
|
|
458
605
|
* Default-export the result from a file next to the route's `route.ts`; one group per
|
|
459
606
|
* route. That route must be one that gets generated files: not a redirect, and with a
|
|
460
607
|
* page or descendants with pages that aren't all redirects. Generation reads the call
|
|
461
|
-
* statically without running it: import `defineRpc
|
|
462
|
-
*
|
|
463
|
-
*
|
|
464
|
-
*
|
|
465
|
-
*
|
|
466
|
-
*
|
|
467
|
-
*
|
|
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.
|
|
468
620
|
*
|
|
469
621
|
* Exposure isn't authorization: anyone who can reach the server can call any
|
|
470
|
-
* procedure, so
|
|
622
|
+
* procedure, so protect every group and procedure that needs it.
|
|
471
623
|
*
|
|
472
624
|
* @example
|
|
473
625
|
* ```ts
|
|
474
|
-
* import { defineRpc
|
|
475
|
-
* import {
|
|
626
|
+
* import { defineRpc } from "@loozo/angular-launch/server";
|
|
627
|
+
* import { signedIn } from "../../auth.server";
|
|
476
628
|
*
|
|
477
629
|
* export default defineRpc(
|
|
478
630
|
* "todos",
|
|
479
|
-
* {
|
|
631
|
+
* ({ query }) => ({
|
|
480
632
|
* list: query({
|
|
481
|
-
* handler: ({ context }) => context.db.todos.listForUser(
|
|
633
|
+
* handler: ({ context }) => context.db.todos.listForUser(context.user.id),
|
|
482
634
|
* }),
|
|
483
|
-
* },
|
|
484
|
-
* {
|
|
635
|
+
* }),
|
|
636
|
+
* { use: [signedIn] },
|
|
485
637
|
* );
|
|
486
638
|
* ```
|
|
487
639
|
*
|
|
488
640
|
* @typeParam Name - The namespace literal.
|
|
641
|
+
* @typeParam Use - The group's middleware, written inline.
|
|
489
642
|
* @typeParam Procedures - The procedures, keyed by name.
|
|
490
643
|
* @param name - The namespace the generated client exposes the group under.
|
|
491
|
-
* @param
|
|
492
|
-
*
|
|
493
|
-
* 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.
|
|
494
647
|
* @returns The group, to default-export.
|
|
495
648
|
*/
|
|
496
|
-
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, }?: {
|
|
497
650
|
/**
|
|
498
|
-
*
|
|
499
|
-
* 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.
|
|
500
654
|
*/
|
|
501
|
-
readonly
|
|
655
|
+
readonly use?: Use & CheckChain<Context, unknown, NoInfer<Use>>;
|
|
502
656
|
}): Group<Name, Procedures>;
|
|
503
657
|
export {};
|