@loozo/angular-launch 0.0.6 → 0.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/README.md +715 -255
- package/builders/application.js +32 -3
- package/builders/channel-forwarding.d.ts +15 -0
- package/builders/channel-forwarding.js +61 -0
- package/builders/options.d.ts +6 -2
- package/builders/options.js +16 -5
- package/builders/serve.js +198 -140
- package/channel.d.ts +3 -1
- package/channel.js +3 -2
- package/client.d.ts +14 -24
- package/generation/index.d.ts +2 -123
- package/generation/index.js +3 -685
- package/generation/redirects.d.ts +14 -0
- package/generation/redirects.js +43 -0
- package/generation/run.d.ts +168 -0
- package/generation/run.js +659 -0
- package/index.d.ts +2 -0
- package/index.js +1 -0
- package/launch-queries.d.ts +16 -0
- package/launch-queries.js +3 -0
- package/navigation.d.ts +3 -3
- package/navigation.js +1 -1
- package/node/index.d.ts +3 -0
- package/node/index.js +6 -3
- package/node/upgrade-listener-slot.d.ts +1 -0
- package/node/upgrade-listener-slot.js +4 -0
- package/outcomes.d.ts +23 -7
- package/outcomes.js +58 -12
- package/package.json +15 -11
- package/redirect-translation.d.ts +53 -0
- package/redirect-translation.js +141 -0
- package/{generated.js → remote-client.js} +27 -21
- package/route-types.d.ts +130 -0
- package/route-types.js +1 -0
- package/route.d.ts +45 -0
- package/route.js +113 -0
- package/schemas/application.json +19 -3
- package/schemas/serve.json +3 -3
- package/server/application.d.ts +107 -0
- package/server/application.js +108 -0
- package/server/backend.d.ts +25 -13
- package/server/backend.js +39 -24
- package/server/channel-messages.d.ts +1 -1
- package/server/channel-messages.js +1 -1
- package/server/channel-socket.d.ts +1 -1
- package/server/definitions.d.ts +146 -298
- package/server/definitions.js +29 -117
- package/server/dispatch.d.ts +67 -63
- package/server/dispatch.js +121 -61
- package/server/event-stream.d.ts +2 -2
- package/server/event-stream.js +1 -1
- package/server/generated.d.ts +2 -1
- package/server/generated.js +2 -1
- package/server/index.d.ts +3 -2
- package/server/index.js +2 -1
- package/server/outcomes.d.ts +26 -13
- package/server/outcomes.js +43 -16
- package/server/read-cookies.d.ts +19 -0
- package/server/read-cookies.js +39 -0
- package/server/response-effects.d.ts +2 -2
- package/server/stages.d.ts +62 -0
- package/server/stages.js +51 -0
- package/server/upgrade.d.ts +11 -12
- package/server/upgrade.js +13 -14
- package/source-text.d.ts +1 -0
- package/source-text.js +56 -0
- package/stream.d.ts +6 -3
- package/stream.js +8 -6
- package/tanstack-query/README.md +15 -15
- package/tanstack-query/create-queries.d.ts +2 -0
- package/tanstack-query/{generated.js → create-queries.js} +8 -9
- package/tanstack-query/hydration-window.d.ts +11 -0
- package/tanstack-query/hydration-window.js +9 -0
- package/tanstack-query/hydration.d.ts +4 -14
- package/tanstack-query/hydration.js +9 -12
- package/tanstack-query/index.d.ts +1 -1
- package/tanstack-query/queries.d.ts +42 -27
- package/testing/call-procedure.d.ts +154 -0
- package/testing/call-procedure.js +97 -0
- package/testing/index.d.ts +2 -0
- package/testing/index.js +1 -0
- package/generation/context-file.d.ts +0 -2
- package/generation/context-file.js +0 -3
- package/tanstack-query/generated.d.ts +0 -5
- /package/{generated.d.ts → remote-client.d.ts} +0 -0
|
@@ -1,21 +1,17 @@
|
|
|
1
1
|
import { isPlatformBrowser } from "@angular/common";
|
|
2
|
-
import { APP_BOOTSTRAP_LISTENER, ApplicationRef, inject,
|
|
2
|
+
import { APP_BOOTSTRAP_LISTENER, ApplicationRef, inject, makeEnvironmentProviders, PLATFORM_ID, } from "@angular/core";
|
|
3
|
+
import { launchQueries } from "../launch-queries.js";
|
|
4
|
+
import { createQueries } from "./create-queries.js";
|
|
5
|
+
import { hydrationWindow } from "./hydration-window.js";
|
|
3
6
|
/**
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
* @remarks
|
|
8
|
-
* Internal; provided by {@link provideLaunchQueries} and read by generated query options.
|
|
9
|
-
*/
|
|
10
|
-
export const hydrationWindow = new InjectionToken("Launch hydration window");
|
|
11
|
-
/**
|
|
12
|
-
* Provides what generated `injectQueries()` functions need: the app's hydration window.
|
|
7
|
+
* Provides what route handles' `queries` need: the TanStack factories and the app's
|
|
8
|
+
* hydration window.
|
|
13
9
|
*
|
|
14
10
|
* @remarks
|
|
15
11
|
* Add it to the application's providers next to TanStack's `provideTanStackQuery(...)`,
|
|
16
|
-
* not to a route or component. `
|
|
12
|
+
* not to a route or component. Reading `route.queries` throws without it.
|
|
17
13
|
*
|
|
18
|
-
* In the browser,
|
|
14
|
+
* In the browser, query options don't refetch on mount while the app hydrates,
|
|
19
15
|
* from bootstrap until the app is first stable, so data dehydrated during server rendering
|
|
20
16
|
* is used as is even with `staleTime: 0`. After that, `refetchOnMount` follows your
|
|
21
17
|
* `QueryClient` defaults. This is the same window Angular's HTTP transfer cache uses.
|
|
@@ -43,6 +39,7 @@ export const hydrationWindow = new InjectionToken("Launch hydration window");
|
|
|
43
39
|
*/
|
|
44
40
|
export function provideLaunchQueries() {
|
|
45
41
|
return makeEnvironmentProviders([
|
|
42
|
+
{ provide: launchQueries, useValue: createQueries },
|
|
46
43
|
{
|
|
47
44
|
provide: hydrationWindow,
|
|
48
45
|
useFactory: () => ({ open: isPlatformBrowser(inject(PLATFORM_ID)) }),
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
export { provideLaunchQueries } from "./hydration.js";
|
|
2
|
-
export type {
|
|
2
|
+
export type { NamespaceQueries, QueriesOf } from "./queries.js";
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import type { CreateMutationOptions, DataTag, QueryKeyWithDataTag, SkipToken, UndefinedInitialDataOptions } from "@tanstack/angular-query";
|
|
2
|
-
import type {
|
|
3
|
-
type
|
|
2
|
+
import type { ContributionOf } from "@loozo/angular-router/_core";
|
|
3
|
+
import type { ChainNamespaces } from "../route-types.js";
|
|
4
|
+
import type { Procedure, Schema } from "../server/definitions.js";
|
|
5
|
+
type NamespaceKey<Namespace> = readonly ["launch", Namespace];
|
|
4
6
|
type ProcedureKey<Namespace, Name> = readonly ["launch", Namespace, Name];
|
|
5
7
|
type InputKey<Namespace, Name, Input> = readonly ["launch", Namespace, Name, Input];
|
|
6
8
|
type QueryOverrides<Output, Data, Key extends readonly unknown[]> = Omit<UndefinedInitialDataOptions<Output, unknown, Data, Key>, "queryKey" | "queryFn">;
|
|
@@ -81,46 +83,59 @@ type MutationProcedure<Namespace, Name, Input, Output> = {
|
|
|
81
83
|
type Variables<Input> = Input extends Schema<infer Caller, unknown> ? Caller : void;
|
|
82
84
|
type ProcedureQueries<Namespace, Name, Value> = Value extends Procedure<infer Input, infer Output, infer Kind, infer _Errors, infer _Message> ? Kind extends "query" ? QueryProcedure<Namespace, Name, Input, Awaited<Output>> : MutationProcedure<Namespace, Name, Input, Awaited<Output>> : never;
|
|
83
85
|
/**
|
|
84
|
-
* The TanStack Query factories for
|
|
85
|
-
* procedure, plus the
|
|
86
|
+
* The TanStack Query factories for one `.remote()` namespace: query or mutation options and
|
|
87
|
+
* keys per procedure, plus the namespace's key.
|
|
86
88
|
*
|
|
87
89
|
* @remarks
|
|
88
|
-
*
|
|
89
|
-
*
|
|
90
|
-
*
|
|
91
|
-
*
|
|
92
|
-
*
|
|
93
|
-
*
|
|
94
|
-
* typed `unknown`: narrow them with the callable's `isError`.
|
|
90
|
+
* Keys are `["launch", namespace]` for the namespace, `["launch", namespace, procedure]`
|
|
91
|
+
* for a procedure and `["launch", namespace, procedure, input]` for one input, so
|
|
92
|
+
* invalidating a shorter key also invalidates the longer ones under it. Queries call the
|
|
93
|
+
* procedure with the query's `signal` and `transferCache: false`, since TanStack transfers
|
|
94
|
+
* the data itself. Errors are typed `unknown`: narrow them with the callable's `isError`.
|
|
95
|
+
* Streams and channels have no factories.
|
|
95
96
|
*
|
|
96
97
|
* Query options default `retry` to never for an `RpcError`, `RpcRedirect` or
|
|
97
98
|
* `RpcNotFound` and to your `QueryClient` default, or three retries, for anything else,
|
|
98
99
|
* and `refetchOnMount` to `false` while the app hydrates and to your `QueryClient`
|
|
99
100
|
* default, or `true`, after. Overrides replace either default.
|
|
100
101
|
*
|
|
101
|
-
* @
|
|
102
|
-
*
|
|
103
|
-
* import { injectQueries } from "./rpc.generated";
|
|
104
|
-
*
|
|
105
|
-
* const queries = injectQueries();
|
|
106
|
-
*
|
|
107
|
-
* injectQuery(() => queries.todos.list.queryOptions());
|
|
108
|
-
* injectMutation(() => queries.todos.add.mutationOptions());
|
|
109
|
-
* client.invalidateQueries({ queryKey: queries.todos.$key() });
|
|
110
|
-
* ```
|
|
111
|
-
*
|
|
112
|
-
* @typeParam Value - A group created by `defineRpc`.
|
|
102
|
+
* @typeParam Namespace - The namespace's name.
|
|
103
|
+
* @typeParam Procedures - Its procedures, keyed by name, as `.remote()` returns them.
|
|
113
104
|
*/
|
|
114
|
-
export type
|
|
105
|
+
export type NamespaceQueries<Namespace, Procedures> = {
|
|
115
106
|
/**
|
|
116
|
-
* The key of every query and mutation of the
|
|
107
|
+
* The key of every query and mutation of the namespace.
|
|
117
108
|
*
|
|
118
109
|
* @returns `["launch", namespace]`.
|
|
119
110
|
*/
|
|
120
|
-
$key():
|
|
111
|
+
$key(): NamespaceKey<Namespace>;
|
|
121
112
|
} & {
|
|
122
113
|
readonly [Name in keyof Procedures as Procedures[Name] extends {
|
|
123
114
|
readonly kind: "stream" | "channel";
|
|
124
115
|
} ? never : Name]: ProcedureQueries<Namespace, Name, Procedures[Name]>;
|
|
125
|
-
}
|
|
116
|
+
};
|
|
117
|
+
/**
|
|
118
|
+
* A route's TanStack Query factories: one {@link NamespaceQueries} per namespace the route
|
|
119
|
+
* can see, its own and its ancestors'.
|
|
120
|
+
*
|
|
121
|
+
* @remarks
|
|
122
|
+
* The type of `route.queries` and of the `Queries` a route copy exports, which generation
|
|
123
|
+
* writes when its `tanstackQuery` option is on.
|
|
124
|
+
*
|
|
125
|
+
* @example
|
|
126
|
+
* ```ts
|
|
127
|
+
* readonly route = injectRoute();
|
|
128
|
+
* readonly todos = injectQuery(() => this.route.queries.todos.list.queryOptions());
|
|
129
|
+
* readonly add = injectMutation(() => this.route.queries.todos.add.mutationOptions());
|
|
130
|
+
*
|
|
131
|
+
* refresh() {
|
|
132
|
+
* return this.client.invalidateQueries({ queryKey: this.route.queries.todos.$key() });
|
|
133
|
+
* }
|
|
134
|
+
* ```
|
|
135
|
+
*
|
|
136
|
+
* @typeParam Route - A route made by Launch's `defineRoute`, or its copy.
|
|
137
|
+
*/
|
|
138
|
+
export type QueriesOf<Route> = {
|
|
139
|
+
readonly [Namespace in keyof ChainNamespaces<ContributionOf<Route>>]: NamespaceQueries<Namespace, ChainNamespaces<ContributionOf<Route>>[Namespace]>;
|
|
140
|
+
};
|
|
126
141
|
export {};
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import type { AnyRoute, ContributionOf } from "@loozo/angular-router/_core";
|
|
2
|
+
import type { InvalidInputData } from "../input-issues.js";
|
|
3
|
+
import type { ChainContext } from "../route-types.js";
|
|
4
|
+
import type { ErrorDefinitions, OutputOf, Procedure, Schema } from "../server/definitions.js";
|
|
5
|
+
type OwnNamespaces<Route> = ContributionOf<Route> extends readonly [...unknown[], infer Own] ? Own extends {
|
|
6
|
+
readonly remote: (...args: never[]) => infer Namespaces;
|
|
7
|
+
} ? Namespaces : {} : {};
|
|
8
|
+
type CallableName<Procedures> = {
|
|
9
|
+
[Name in keyof Procedures & string]: Procedures[Name] extends {
|
|
10
|
+
readonly kind: "query" | "mutation";
|
|
11
|
+
} ? Name : never;
|
|
12
|
+
}[keyof Procedures & string];
|
|
13
|
+
type ProcedureAt<Route, Namespace, Name> = Namespace extends keyof OwnNamespaces<Route> ? Name extends keyof OwnNamespaces<Route>[Namespace] ? OwnNamespaces<Route>[Namespace][Name] : never : never;
|
|
14
|
+
type RootContext<Route> = ContributionOf<Route> extends readonly [infer Root, ...unknown[]] ? ChainContext<readonly [Root]> : never;
|
|
15
|
+
type InputOption<Value> = Value extends Procedure<infer Input, infer _Output, infer _Kind, infer _Errors, infer _Message> ? Input extends Schema<infer Caller, unknown> ? {
|
|
16
|
+
/** The input as a caller sends it: the input schema's input type. */
|
|
17
|
+
readonly input: Caller;
|
|
18
|
+
} : {
|
|
19
|
+
/** A procedure without an input schema takes no input. */
|
|
20
|
+
readonly input?: undefined;
|
|
21
|
+
} : never;
|
|
22
|
+
/**
|
|
23
|
+
* The options {@link callProcedure} takes.
|
|
24
|
+
*
|
|
25
|
+
* @typeParam Route - The route that declares the procedure's namespace.
|
|
26
|
+
* @typeParam Value - The procedure, as the route's `.remote()` declares it.
|
|
27
|
+
*/
|
|
28
|
+
export type CallProcedureOptions<Route, Value> = {
|
|
29
|
+
/**
|
|
30
|
+
* The context the root route's `.context()` stage would return, which the stages below it
|
|
31
|
+
* receive; `{}` when the root route has no stage. The root stage itself doesn't run, so
|
|
32
|
+
* what it would provide, such as the user or a database client, can be a fake.
|
|
33
|
+
*/
|
|
34
|
+
readonly context: RootContext<Route>;
|
|
35
|
+
/**
|
|
36
|
+
* The request stages, middleware and the handler receive as `request`, and whose signal
|
|
37
|
+
* they receive as `signal`. Defaults to a `POST` to
|
|
38
|
+
* `http://localhost/_rpc/<namespace>/<procedure>`.
|
|
39
|
+
*/
|
|
40
|
+
readonly request?: Request;
|
|
41
|
+
} & InputOption<Value>;
|
|
42
|
+
type FrameworkError = {
|
|
43
|
+
/** The input failed the input schema, or was sent to a procedure without one. */
|
|
44
|
+
readonly code: "INVALID_INPUT";
|
|
45
|
+
/** Launch's message for the code. */
|
|
46
|
+
readonly message: string;
|
|
47
|
+
/** The input schema's issues; none for input sent to a procedure without one. */
|
|
48
|
+
readonly data: InvalidInputData;
|
|
49
|
+
} | {
|
|
50
|
+
/** A stage, middleware or the handler threw `forbidden()`, or the call failed. */
|
|
51
|
+
readonly code: "FORBIDDEN" | "INTERNAL";
|
|
52
|
+
/** Launch's message for the code. */
|
|
53
|
+
readonly message: string;
|
|
54
|
+
/** Always `null`. */
|
|
55
|
+
readonly data: null;
|
|
56
|
+
};
|
|
57
|
+
type DeclaredError<Errors extends ErrorDefinitions> = {
|
|
58
|
+
[Code in keyof Errors & string]: {
|
|
59
|
+
/** The declared error's code. */
|
|
60
|
+
readonly code: Code;
|
|
61
|
+
/** The declared message. */
|
|
62
|
+
readonly message: string;
|
|
63
|
+
/** The error's data, as its data schema produced it. */
|
|
64
|
+
readonly data: OutputOf<Errors[Code]["data"]>;
|
|
65
|
+
};
|
|
66
|
+
}[keyof Errors & string];
|
|
67
|
+
/**
|
|
68
|
+
* How a {@link callProcedure} call settled, as the browser would receive it, with the
|
|
69
|
+
* cookies and headers it set.
|
|
70
|
+
*
|
|
71
|
+
* @remarks
|
|
72
|
+
* Narrow with `"result" in call`, `"error" in call`, `"redirect" in call` or
|
|
73
|
+
* `"notFound" in call`, then an error by its `code`. Results and error data are what the
|
|
74
|
+
* wire carries: strict JSON, parsed back.
|
|
75
|
+
*
|
|
76
|
+
* @typeParam Value - The procedure, as the route's `.remote()` declares it.
|
|
77
|
+
*/
|
|
78
|
+
export type ProcedureCall<Value> = Value extends Procedure<infer _Input, infer Output, infer _Kind, infer Errors, infer _Message> ? ({
|
|
79
|
+
/** Always 200: the handler returned. */
|
|
80
|
+
readonly status: 200;
|
|
81
|
+
/** The handler's result. */
|
|
82
|
+
readonly result: Awaited<Output>;
|
|
83
|
+
} | {
|
|
84
|
+
/** The error's status: the declared one, or Launch's for its own codes. */
|
|
85
|
+
readonly status: number;
|
|
86
|
+
/** A framework failure or one of the procedure's declared errors. */
|
|
87
|
+
readonly error: FrameworkError | DeclaredError<Errors>;
|
|
88
|
+
} | {
|
|
89
|
+
/** Always 303. */
|
|
90
|
+
readonly status: 303;
|
|
91
|
+
/**
|
|
92
|
+
* A stage, middleware or the handler threw `redirect()`. Never with `words`: the
|
|
93
|
+
* test's binding isn't translated.
|
|
94
|
+
*/
|
|
95
|
+
readonly redirect: {
|
|
96
|
+
/** The root-relative URL `createUrl` built for the destination. */
|
|
97
|
+
readonly to: `/${string}`;
|
|
98
|
+
};
|
|
99
|
+
} | {
|
|
100
|
+
/** Always 404. */
|
|
101
|
+
readonly status: 404;
|
|
102
|
+
/** A stage, middleware or the handler threw `notFound()`. Always `null`. */
|
|
103
|
+
readonly notFound: null;
|
|
104
|
+
}) & {
|
|
105
|
+
/** The `Set-Cookie` values the call set, in order. */
|
|
106
|
+
readonly cookies: readonly string[];
|
|
107
|
+
/** The headers the call set, without Launch's own response headers. */
|
|
108
|
+
readonly headers: Headers;
|
|
109
|
+
} : never;
|
|
110
|
+
/**
|
|
111
|
+
* Calls a query or mutation of a route's own namespace in a test, from the context its root
|
|
112
|
+
* route's stage would return.
|
|
113
|
+
*
|
|
114
|
+
* @remarks
|
|
115
|
+
* The call runs as an RPC over HTTP from that point: the `.context()` stages below the root
|
|
116
|
+
* route, in a fresh request scope; the declaring route's `.use()` middleware; input
|
|
117
|
+
* validation; the procedure's middleware; then the handler. The root route's own stage
|
|
118
|
+
* never runs: `options.context` stands in for what it returns, so a test can pass fakes for
|
|
119
|
+
* what that stage provides. The root route is the first route of the chain that uses a
|
|
120
|
+
* Launch step.
|
|
121
|
+
*
|
|
122
|
+
* The call isn't an HTTP request: there's no host, origin, method or content-type check,
|
|
123
|
+
* no `createRequestScope` and no server rendering. Test those through the generated
|
|
124
|
+
* `handleRequest`.
|
|
125
|
+
*
|
|
126
|
+
* Every outcome settles to a value: a result, an error, a redirect or a not-found. An
|
|
127
|
+
* unexpected failure settles to `INTERNAL`, logged with `console.error` as the server logs
|
|
128
|
+
* it. Streams and channels can't be called.
|
|
129
|
+
*
|
|
130
|
+
* @example
|
|
131
|
+
* ```ts
|
|
132
|
+
* import { callProcedure } from "@loozo/angular-launch/testing";
|
|
133
|
+
* import todos from "./route";
|
|
134
|
+
*
|
|
135
|
+
* const call = await callProcedure(todos, "todos", "complete", {
|
|
136
|
+
* context: { todos: fakeTodos, user: { id: "alice" } },
|
|
137
|
+
* input: { id: "2" },
|
|
138
|
+
* });
|
|
139
|
+
*
|
|
140
|
+
* expect(call).toMatchObject({ status: 404, error: { code: "NOT_FOUND" } });
|
|
141
|
+
* ```
|
|
142
|
+
*
|
|
143
|
+
* @param route - The authored route (its `route.ts` default export) whose own `.remote()`
|
|
144
|
+
* declares `namespace`. Call an ancestor's namespace through the ancestor's route.
|
|
145
|
+
* @param namespace - One of the route's own namespaces.
|
|
146
|
+
* @param procedure - A query or mutation of the namespace.
|
|
147
|
+
* @param options - The root context, the input and the request.
|
|
148
|
+
* @returns How the call settled, with the cookies and headers it set.
|
|
149
|
+
* @throws TypeError for a route Launch's `defineRoute` didn't make, a namespace the route
|
|
150
|
+
* doesn't declare, an unknown procedure, a stream or a channel. Rejects when
|
|
151
|
+
* `options.request`'s body was already read.
|
|
152
|
+
*/
|
|
153
|
+
export declare function callProcedure<Route extends AnyRoute, Namespace extends keyof OwnNamespaces<Route> & string, Name extends CallableName<OwnNamespaces<Route>[Namespace]>>(route: Route, namespace: Namespace, procedure: Name, options: CallProcedureOptions<Route, ProcedureAt<Route, Namespace, Name>>): Promise<ProcedureCall<ProcedureAt<Route, Namespace, Name>>>;
|
|
154
|
+
export {};
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { bindNamespaces, launchChain } from "../server/application.js";
|
|
2
|
+
import { createRequestScope, runProcedure } from "../server/dispatch.js";
|
|
3
|
+
import { createResponseEffects } from "../server/response-effects.js";
|
|
4
|
+
/**
|
|
5
|
+
* Calls a query or mutation of a route's own namespace in a test, from the context its root
|
|
6
|
+
* route's stage would return.
|
|
7
|
+
*
|
|
8
|
+
* @remarks
|
|
9
|
+
* The call runs as an RPC over HTTP from that point: the `.context()` stages below the root
|
|
10
|
+
* route, in a fresh request scope; the declaring route's `.use()` middleware; input
|
|
11
|
+
* validation; the procedure's middleware; then the handler. The root route's own stage
|
|
12
|
+
* never runs: `options.context` stands in for what it returns, so a test can pass fakes for
|
|
13
|
+
* what that stage provides. The root route is the first route of the chain that uses a
|
|
14
|
+
* Launch step.
|
|
15
|
+
*
|
|
16
|
+
* The call isn't an HTTP request: there's no host, origin, method or content-type check,
|
|
17
|
+
* no `createRequestScope` and no server rendering. Test those through the generated
|
|
18
|
+
* `handleRequest`.
|
|
19
|
+
*
|
|
20
|
+
* Every outcome settles to a value: a result, an error, a redirect or a not-found. An
|
|
21
|
+
* unexpected failure settles to `INTERNAL`, logged with `console.error` as the server logs
|
|
22
|
+
* it. Streams and channels can't be called.
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```ts
|
|
26
|
+
* import { callProcedure } from "@loozo/angular-launch/testing";
|
|
27
|
+
* import todos from "./route";
|
|
28
|
+
*
|
|
29
|
+
* const call = await callProcedure(todos, "todos", "complete", {
|
|
30
|
+
* context: { todos: fakeTodos, user: { id: "alice" } },
|
|
31
|
+
* input: { id: "2" },
|
|
32
|
+
* });
|
|
33
|
+
*
|
|
34
|
+
* expect(call).toMatchObject({ status: 404, error: { code: "NOT_FOUND" } });
|
|
35
|
+
* ```
|
|
36
|
+
*
|
|
37
|
+
* @param route - The authored route (its `route.ts` default export) whose own `.remote()`
|
|
38
|
+
* declares `namespace`. Call an ancestor's namespace through the ancestor's route.
|
|
39
|
+
* @param namespace - One of the route's own namespaces.
|
|
40
|
+
* @param procedure - A query or mutation of the namespace.
|
|
41
|
+
* @param options - The root context, the input and the request.
|
|
42
|
+
* @returns How the call settled, with the cookies and headers it set.
|
|
43
|
+
* @throws TypeError for a route Launch's `defineRoute` didn't make, a namespace the route
|
|
44
|
+
* doesn't declare, an unknown procedure, a stream or a channel. Rejects when
|
|
45
|
+
* `options.request`'s body was already read.
|
|
46
|
+
*/
|
|
47
|
+
export async function callProcedure(route, namespace, procedure, options) {
|
|
48
|
+
const chain = launchChain(route);
|
|
49
|
+
if (chain === undefined || chain.length === 0) {
|
|
50
|
+
throw new TypeError("callProcedure takes a route made by Launch's defineRoute");
|
|
51
|
+
}
|
|
52
|
+
const own = chain[chain.length - 1];
|
|
53
|
+
if (typeof own.remote !== "function") {
|
|
54
|
+
throw new TypeError(`The route declares no namespace; ${namespace} isn't its own`);
|
|
55
|
+
}
|
|
56
|
+
const { context, input = null, request = new Request(`http://localhost/_rpc/${namespace}/${procedure}`, { method: "POST" }), } = options;
|
|
57
|
+
// The supplied context stands in for the root route's stage; the stages below it run.
|
|
58
|
+
const stages = [{ run: () => context, route: "the context passed to callProcedure" }];
|
|
59
|
+
for (const record of chain.slice(1)) {
|
|
60
|
+
if (typeof record.context === "function") {
|
|
61
|
+
stages.push({ run: record.context, route: "a stage below the root route" });
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
const namespaces = bindNamespaces(own, stages);
|
|
65
|
+
if (!Object.hasOwn(namespaces, namespace)) {
|
|
66
|
+
throw new TypeError(`The route doesn't declare the namespace ${namespace}; call an ancestor's namespace through its route`);
|
|
67
|
+
}
|
|
68
|
+
const procedures = namespaces[namespace].procedures;
|
|
69
|
+
if (!Object.hasOwn(procedures, procedure)) {
|
|
70
|
+
throw new TypeError(`The namespace ${namespace} has no procedure ${procedure}`);
|
|
71
|
+
}
|
|
72
|
+
const { kind } = procedures[procedure];
|
|
73
|
+
if (kind !== "query" && kind !== "mutation") {
|
|
74
|
+
throw new TypeError(`callProcedure runs queries and mutations, not a ${kind}`);
|
|
75
|
+
}
|
|
76
|
+
const application = { id: "launch-call-procedure", namespaces };
|
|
77
|
+
const scope = createRequestScope(application, request);
|
|
78
|
+
const effects = createResponseEffects();
|
|
79
|
+
const reply = await runProcedure(`/_rpc/${namespace}/${procedure}`, { input }, scope, application, scope.request, "http", scope.request.signal, effects);
|
|
80
|
+
effects.commit();
|
|
81
|
+
let settled;
|
|
82
|
+
// Every other status carries a frame Launch built: an error, a redirect or a not-found.
|
|
83
|
+
if (reply.status === 200) {
|
|
84
|
+
settled = { result: reply.body };
|
|
85
|
+
}
|
|
86
|
+
else {
|
|
87
|
+
settled = reply.body;
|
|
88
|
+
}
|
|
89
|
+
const call = {
|
|
90
|
+
status: reply.status,
|
|
91
|
+
...settled,
|
|
92
|
+
cookies: [...effects.cookies],
|
|
93
|
+
// A copy, as the server's response takes: later writes don't reach a settled call.
|
|
94
|
+
headers: new Headers(effects.headers),
|
|
95
|
+
};
|
|
96
|
+
return call;
|
|
97
|
+
}
|
package/testing/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { callProcedure } from "./call-procedure.js";
|
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
export interface QueriesMethod {
|
|
2
|
-
readonly kind: "query" | "mutation";
|
|
3
|
-
readonly input: boolean;
|
|
4
|
-
}
|
|
5
|
-
export declare function createQueries(rpc: Readonly<Record<string, Readonly<Record<string, unknown>>>>, groups: Readonly<Record<string, Readonly<Record<string, QueriesMethod>>>>): unknown;
|
|
File without changes
|