@loozo/angular-launch 0.0.5 → 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 +1068 -187
- 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 +55 -0
- package/channel.js +232 -0
- package/client.d.ts +63 -33
- package/connect.d.ts +201 -0
- package/connect.js +148 -0
- package/generation/index.d.ts +2 -123
- package/generation/index.js +3 -669
- 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 +8 -1
- package/index.js +3 -0
- package/json.d.ts +9 -0
- package/json.js +24 -0
- package/launch-queries.d.ts +16 -0
- package/launch-queries.js +3 -0
- package/navigation.d.ts +36 -0
- package/navigation.js +47 -0
- package/node/index.d.ts +60 -0
- package/node/index.js +178 -0
- package/node/upgrade-listener-slot.d.ts +1 -0
- package/node/upgrade-listener-slot.js +4 -0
- package/outcomes.d.ts +71 -0
- package/outcomes.js +146 -0
- package/package.json +21 -11
- package/redirect-translation.d.ts +53 -0
- package/redirect-translation.js +141 -0
- package/{generated.d.ts → remote-client.d.ts} +1 -1
- package/remote-client.js +179 -0
- 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 +62 -31
- package/server/backend.js +149 -55
- package/server/channel-messages.d.ts +54 -0
- package/server/channel-messages.js +84 -0
- package/server/channel-socket.d.ts +67 -0
- package/server/channel-socket.js +322 -0
- package/server/definitions.d.ts +270 -294
- package/server/definitions.js +35 -115
- package/server/dispatch.d.ts +121 -66
- package/server/dispatch.js +267 -87
- package/server/event-stream.d.ts +63 -0
- package/server/event-stream.js +173 -0
- package/server/generated.d.ts +3 -1
- package/server/generated.js +3 -1
- package/server/index.d.ts +6 -3
- package/server/index.js +3 -2
- package/server/outcomes.d.ts +64 -2
- package/server/outcomes.js +85 -4
- package/server/read-cookies.d.ts +19 -0
- package/server/read-cookies.js +39 -0
- package/server/response-effects.d.ts +105 -0
- package/server/response-effects.js +78 -0
- package/server/stages.d.ts +62 -0
- package/server/stages.js +51 -0
- package/server/upgrade.d.ts +57 -0
- package/server/upgrade.js +116 -0
- package/source-text.d.ts +1 -0
- package/source-text.js +56 -0
- package/stream.d.ts +34 -0
- package/stream.js +161 -0
- package/tanstack-query/README.md +113 -21
- package/tanstack-query/create-queries.d.ts +2 -0
- package/tanstack-query/{generated.js → create-queries.js} +13 -11
- 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 +50 -33
- 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/generated.js +0 -90
- package/generation/context-file.d.ts +0 -2
- package/generation/context-file.js +0 -3
- package/tanstack-query/generated.d.ts +0 -5
|
@@ -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";
|
package/generated.js
DELETED
|
@@ -1,90 +0,0 @@
|
|
|
1
|
-
import { inject } from "@angular/core";
|
|
2
|
-
import { HttpClient, HttpContext, HttpContextToken, HttpErrorResponse } from "@angular/common/http";
|
|
3
|
-
import { RpcError, recognizedError } from "./client.js";
|
|
4
|
-
import { serializeJson } from "./json.js";
|
|
5
|
-
export const RPC_CALL = new HttpContextToken(() => false);
|
|
6
|
-
const errorOwners = new WeakMap();
|
|
7
|
-
function call(http, group, method, input, { signal, transferCache = true } = {}) {
|
|
8
|
-
return new Promise((resolve, reject) => {
|
|
9
|
-
let subscription;
|
|
10
|
-
let settled = false;
|
|
11
|
-
function cleanup() {
|
|
12
|
-
signal?.removeEventListener("abort", abort);
|
|
13
|
-
subscription?.unsubscribe();
|
|
14
|
-
}
|
|
15
|
-
function abort() {
|
|
16
|
-
if (settled) {
|
|
17
|
-
return;
|
|
18
|
-
}
|
|
19
|
-
settled = true;
|
|
20
|
-
cleanup();
|
|
21
|
-
reject(new DOMException("RPC call aborted", "AbortError"));
|
|
22
|
-
}
|
|
23
|
-
if (signal?.aborted) {
|
|
24
|
-
abort();
|
|
25
|
-
return;
|
|
26
|
-
}
|
|
27
|
-
const envelope = JSON.parse(serializeJson({ input }));
|
|
28
|
-
signal?.addEventListener("abort", abort, { once: true });
|
|
29
|
-
if (method.kind === "mutation") {
|
|
30
|
-
transferCache = false;
|
|
31
|
-
}
|
|
32
|
-
subscription = http
|
|
33
|
-
.post(`/_rpc/${group}/${method.name}`, envelope, {
|
|
34
|
-
context: new HttpContext().set(RPC_CALL, true),
|
|
35
|
-
transferCache,
|
|
36
|
-
})
|
|
37
|
-
.subscribe({
|
|
38
|
-
next(value) {
|
|
39
|
-
settled = true;
|
|
40
|
-
cleanup();
|
|
41
|
-
resolve(value);
|
|
42
|
-
},
|
|
43
|
-
error(error) {
|
|
44
|
-
settled = true;
|
|
45
|
-
cleanup();
|
|
46
|
-
if (error instanceof HttpErrorResponse) {
|
|
47
|
-
const frame = recognizedError(error.error, error.status, method.errors);
|
|
48
|
-
if (frame) {
|
|
49
|
-
const converted = new RpcError(frame.code, error.status, frame.data, frame.message);
|
|
50
|
-
errorOwners.set(converted, { group, method: method.name });
|
|
51
|
-
reject(converted);
|
|
52
|
-
return;
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
reject(error);
|
|
56
|
-
},
|
|
57
|
-
});
|
|
58
|
-
if (settled) {
|
|
59
|
-
cleanup();
|
|
60
|
-
}
|
|
61
|
-
});
|
|
62
|
-
}
|
|
63
|
-
export function injectClient(groups) {
|
|
64
|
-
const http = inject(HttpClient);
|
|
65
|
-
const result = Object.create(null);
|
|
66
|
-
for (const [namespace, group] of Object.entries(groups)) {
|
|
67
|
-
const methods = Object.create(null);
|
|
68
|
-
for (const method of group.methods) {
|
|
69
|
-
let invoke;
|
|
70
|
-
if (method.input) {
|
|
71
|
-
invoke = (input, options) => call(http, namespace, method, input, options);
|
|
72
|
-
}
|
|
73
|
-
else {
|
|
74
|
-
invoke = (options) => call(http, namespace, method, null, options);
|
|
75
|
-
}
|
|
76
|
-
methods[method.name] = Object.assign(invoke, {
|
|
77
|
-
kind: method.kind,
|
|
78
|
-
isError: (error) => {
|
|
79
|
-
if (typeof error !== "object" || error === null) {
|
|
80
|
-
return false;
|
|
81
|
-
}
|
|
82
|
-
const owner = errorOwners.get(error);
|
|
83
|
-
return owner?.group === namespace && owner.method === method.name;
|
|
84
|
-
},
|
|
85
|
-
});
|
|
86
|
-
}
|
|
87
|
-
result[namespace] = methods;
|
|
88
|
-
}
|
|
89
|
-
return result;
|
|
90
|
-
}
|
|
@@ -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;
|