@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.
Files changed (92) hide show
  1. package/README.md +1068 -187
  2. package/builders/application.js +32 -3
  3. package/builders/channel-forwarding.d.ts +15 -0
  4. package/builders/channel-forwarding.js +61 -0
  5. package/builders/options.d.ts +6 -2
  6. package/builders/options.js +16 -5
  7. package/builders/serve.js +198 -140
  8. package/channel.d.ts +55 -0
  9. package/channel.js +232 -0
  10. package/client.d.ts +63 -33
  11. package/connect.d.ts +201 -0
  12. package/connect.js +148 -0
  13. package/generation/index.d.ts +2 -123
  14. package/generation/index.js +3 -669
  15. package/generation/redirects.d.ts +14 -0
  16. package/generation/redirects.js +43 -0
  17. package/generation/run.d.ts +168 -0
  18. package/generation/run.js +659 -0
  19. package/index.d.ts +8 -1
  20. package/index.js +3 -0
  21. package/json.d.ts +9 -0
  22. package/json.js +24 -0
  23. package/launch-queries.d.ts +16 -0
  24. package/launch-queries.js +3 -0
  25. package/navigation.d.ts +36 -0
  26. package/navigation.js +47 -0
  27. package/node/index.d.ts +60 -0
  28. package/node/index.js +178 -0
  29. package/node/upgrade-listener-slot.d.ts +1 -0
  30. package/node/upgrade-listener-slot.js +4 -0
  31. package/outcomes.d.ts +71 -0
  32. package/outcomes.js +146 -0
  33. package/package.json +21 -11
  34. package/redirect-translation.d.ts +53 -0
  35. package/redirect-translation.js +141 -0
  36. package/{generated.d.ts → remote-client.d.ts} +1 -1
  37. package/remote-client.js +179 -0
  38. package/route-types.d.ts +130 -0
  39. package/route-types.js +1 -0
  40. package/route.d.ts +45 -0
  41. package/route.js +113 -0
  42. package/schemas/application.json +19 -3
  43. package/schemas/serve.json +3 -3
  44. package/server/application.d.ts +107 -0
  45. package/server/application.js +108 -0
  46. package/server/backend.d.ts +62 -31
  47. package/server/backend.js +149 -55
  48. package/server/channel-messages.d.ts +54 -0
  49. package/server/channel-messages.js +84 -0
  50. package/server/channel-socket.d.ts +67 -0
  51. package/server/channel-socket.js +322 -0
  52. package/server/definitions.d.ts +270 -294
  53. package/server/definitions.js +35 -115
  54. package/server/dispatch.d.ts +121 -66
  55. package/server/dispatch.js +267 -87
  56. package/server/event-stream.d.ts +63 -0
  57. package/server/event-stream.js +173 -0
  58. package/server/generated.d.ts +3 -1
  59. package/server/generated.js +3 -1
  60. package/server/index.d.ts +6 -3
  61. package/server/index.js +3 -2
  62. package/server/outcomes.d.ts +64 -2
  63. package/server/outcomes.js +85 -4
  64. package/server/read-cookies.d.ts +19 -0
  65. package/server/read-cookies.js +39 -0
  66. package/server/response-effects.d.ts +105 -0
  67. package/server/response-effects.js +78 -0
  68. package/server/stages.d.ts +62 -0
  69. package/server/stages.js +51 -0
  70. package/server/upgrade.d.ts +57 -0
  71. package/server/upgrade.js +116 -0
  72. package/source-text.d.ts +1 -0
  73. package/source-text.js +56 -0
  74. package/stream.d.ts +34 -0
  75. package/stream.js +161 -0
  76. package/tanstack-query/README.md +113 -21
  77. package/tanstack-query/create-queries.d.ts +2 -0
  78. package/tanstack-query/{generated.js → create-queries.js} +13 -11
  79. package/tanstack-query/hydration-window.d.ts +11 -0
  80. package/tanstack-query/hydration-window.js +9 -0
  81. package/tanstack-query/hydration.d.ts +4 -14
  82. package/tanstack-query/hydration.js +9 -12
  83. package/tanstack-query/index.d.ts +1 -1
  84. package/tanstack-query/queries.d.ts +50 -33
  85. package/testing/call-procedure.d.ts +154 -0
  86. package/testing/call-procedure.js +97 -0
  87. package/testing/index.d.ts +2 -0
  88. package/testing/index.js +1 -0
  89. package/generated.js +0 -90
  90. package/generation/context-file.d.ts +0 -2
  91. package/generation/context-file.js +0 -3
  92. 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
+ }
@@ -0,0 +1,2 @@
1
+ export { callProcedure } from "./call-procedure.js";
2
+ export type { CallProcedureOptions, ProcedureCall } from "./call-procedure.js";
@@ -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,2 +0,0 @@
1
- import type { GenerationSession } from "./index.js";
2
- export declare const contextFiles: WeakMap<GenerationSession, string>;
@@ -1,3 +0,0 @@
1
- // The context file each session's last pass discovered, for the serve builder's watcher. Internal:
2
- // the exports map doesn't reach this module, so it isn't public API.
3
- export const contextFiles = new WeakMap();
@@ -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;