@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,53 @@
|
|
|
1
|
+
import { InjectionToken, type EnvironmentProviders } from "@angular/core";
|
|
2
|
+
/**
|
|
3
|
+
* Puts the listed static path segments of a canonical redirect target, ordinals of
|
|
4
|
+
* {@link targetSegments}, in this build's language. Internal.
|
|
5
|
+
*/
|
|
6
|
+
export type TranslateRedirect = (to: `/${string}`, words: readonly number[]) => `/${string}`;
|
|
7
|
+
/**
|
|
8
|
+
* The installed {@link TranslateRedirect}, which the generated client applies when it
|
|
9
|
+
* recognizes a redirect frame with `words`. Internal.
|
|
10
|
+
*/
|
|
11
|
+
export declare const REDIRECT_TRANSLATOR: InjectionToken<TranslateRedirect>;
|
|
12
|
+
/** Every canonical static word of an app and its text in this build, in the same order. */
|
|
13
|
+
export interface RedirectWords {
|
|
14
|
+
readonly words: readonly string[];
|
|
15
|
+
readonly translations: readonly string[];
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* The path segments of a redirect target, in order, as `[start, end)` offsets into `to`.
|
|
19
|
+
*
|
|
20
|
+
* @remarks
|
|
21
|
+
* Internal. It recognizes only the paths `createUrl` writes: `/` or `/a/b` for a primary
|
|
22
|
+
* destination, and `/(outlet:a/b)` or `/a/(outlet:b)` for one in a named outlet, the anchor's
|
|
23
|
+
* segments before the group's; any `?` or `#` suffix follows. The outlet label is no segment.
|
|
24
|
+
* Matrix parameters, several groups, nested outlets and anything else aren't recognized.
|
|
25
|
+
*
|
|
26
|
+
* @param to - A root-relative redirect target.
|
|
27
|
+
* @returns The segment spans, or `undefined` for any other path.
|
|
28
|
+
*/
|
|
29
|
+
export declare function targetSegments(to: string): readonly (readonly [number, number])[] | undefined;
|
|
30
|
+
/**
|
|
31
|
+
* Swaps the path segments at `words` in `to` for their words' translation in `table`.
|
|
32
|
+
*
|
|
33
|
+
* @remarks
|
|
34
|
+
* Internal. `words` are ascending ordinals of {@link targetSegments}. Only those segments
|
|
35
|
+
* change: parameter and catchall values, outlet labels, the search and the fragment stay byte
|
|
36
|
+
* for byte, and a word missing from `table`, or one that doesn't decode, stays canonical.
|
|
37
|
+
*
|
|
38
|
+
* @throws TypeError when `to` isn't a path `createUrl` writes, which recognition already
|
|
39
|
+
* refused.
|
|
40
|
+
*/
|
|
41
|
+
export declare function swap(table: ReadonlyMap<string, string>, to: `/${string}`, words: readonly number[]): `/${string}`;
|
|
42
|
+
/**
|
|
43
|
+
* Provides the {@link REDIRECT_TRANSLATOR} of one build's generated word table.
|
|
44
|
+
*
|
|
45
|
+
* @remarks
|
|
46
|
+
* Internal: exported only from `@loozo/angular-launch/_redirects` for a translated app's
|
|
47
|
+
* generated `redirects.generated.ts`. `table` runs once, the first time a client needs it, so
|
|
48
|
+
* its `$localize` tags evaluate inside the localized application.
|
|
49
|
+
*
|
|
50
|
+
* @param table - Returns every canonical word and its translation in this build.
|
|
51
|
+
* @returns The providers.
|
|
52
|
+
*/
|
|
53
|
+
export declare function redirectTranslation(table: () => RedirectWords): EnvironmentProviders;
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import { InjectionToken, makeEnvironmentProviders } from "@angular/core";
|
|
2
|
+
/**
|
|
3
|
+
* The installed {@link TranslateRedirect}, which the generated client applies when it
|
|
4
|
+
* recognizes a redirect frame with `words`. Internal.
|
|
5
|
+
*/
|
|
6
|
+
export const REDIRECT_TRANSLATOR = new InjectionToken("Launch redirect translator");
|
|
7
|
+
// createUrl's encoding: component-encoded, with parentheses escaped too.
|
|
8
|
+
function encode(text) {
|
|
9
|
+
return encodeURIComponent(text).replaceAll("(", "%28").replaceAll(")", "%29");
|
|
10
|
+
}
|
|
11
|
+
// One path segment as createUrl encodes it, and an outlet group's label: the router's outlet
|
|
12
|
+
// name grammar, then ":". Sticky, so each match starts exactly at `lastIndex`.
|
|
13
|
+
const segmentText = /(?:[A-Za-z0-9\-_.!~*']|%[0-9A-Fa-f]{2})+/y;
|
|
14
|
+
const outletLabel = /[A-Za-z][A-Za-z0-9_-]*:/y;
|
|
15
|
+
// Reads `segment ("/" segment)*` from `at` into `spans`, stopping before an outlet group's
|
|
16
|
+
// "/(". Returns the offset after the last segment, or undefined when no segment starts there.
|
|
17
|
+
function readSegments(to, at, spans) {
|
|
18
|
+
for (;;) {
|
|
19
|
+
segmentText.lastIndex = at;
|
|
20
|
+
if (!segmentText.test(to)) {
|
|
21
|
+
return undefined;
|
|
22
|
+
}
|
|
23
|
+
const end = segmentText.lastIndex;
|
|
24
|
+
spans.push([at, end]);
|
|
25
|
+
if (to[end] !== "/" || to[end + 1] === "(") {
|
|
26
|
+
return end;
|
|
27
|
+
}
|
|
28
|
+
at = end + 1;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* The path segments of a redirect target, in order, as `[start, end)` offsets into `to`.
|
|
33
|
+
*
|
|
34
|
+
* @remarks
|
|
35
|
+
* Internal. It recognizes only the paths `createUrl` writes: `/` or `/a/b` for a primary
|
|
36
|
+
* destination, and `/(outlet:a/b)` or `/a/(outlet:b)` for one in a named outlet, the anchor's
|
|
37
|
+
* segments before the group's; any `?` or `#` suffix follows. The outlet label is no segment.
|
|
38
|
+
* Matrix parameters, several groups, nested outlets and anything else aren't recognized.
|
|
39
|
+
*
|
|
40
|
+
* @param to - A root-relative redirect target.
|
|
41
|
+
* @returns The segment spans, or `undefined` for any other path.
|
|
42
|
+
*/
|
|
43
|
+
export function targetSegments(to) {
|
|
44
|
+
// createUrl encodes "?" and "#" inside segments, so the first one ends the path.
|
|
45
|
+
const cut = to.search(/[?#]/);
|
|
46
|
+
const end = cut === -1 ? to.length : cut;
|
|
47
|
+
const spans = [];
|
|
48
|
+
let at = 1;
|
|
49
|
+
if (to[0] !== "/") {
|
|
50
|
+
return undefined;
|
|
51
|
+
}
|
|
52
|
+
if (at < end && to[at] !== "(") {
|
|
53
|
+
const after = readSegments(to, at, spans);
|
|
54
|
+
if (after === undefined) {
|
|
55
|
+
return undefined;
|
|
56
|
+
}
|
|
57
|
+
if (after === end) {
|
|
58
|
+
return spans;
|
|
59
|
+
}
|
|
60
|
+
// Only "/(" ends the anchor early: the group follows.
|
|
61
|
+
if (to[after] !== "/") {
|
|
62
|
+
return undefined;
|
|
63
|
+
}
|
|
64
|
+
at = after + 1;
|
|
65
|
+
}
|
|
66
|
+
if (at === end) {
|
|
67
|
+
return spans;
|
|
68
|
+
}
|
|
69
|
+
outletLabel.lastIndex = at + 1;
|
|
70
|
+
if (to[at] !== "(" || !outletLabel.test(to)) {
|
|
71
|
+
return undefined;
|
|
72
|
+
}
|
|
73
|
+
const close = readSegments(to, outletLabel.lastIndex, spans);
|
|
74
|
+
if (close === undefined || to[close] !== ")" || close + 1 !== end) {
|
|
75
|
+
return undefined;
|
|
76
|
+
}
|
|
77
|
+
return spans;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Swaps the path segments at `words` in `to` for their words' translation in `table`.
|
|
81
|
+
*
|
|
82
|
+
* @remarks
|
|
83
|
+
* Internal. `words` are ascending ordinals of {@link targetSegments}. Only those segments
|
|
84
|
+
* change: parameter and catchall values, outlet labels, the search and the fragment stay byte
|
|
85
|
+
* for byte, and a word missing from `table`, or one that doesn't decode, stays canonical.
|
|
86
|
+
*
|
|
87
|
+
* @throws TypeError when `to` isn't a path `createUrl` writes, which recognition already
|
|
88
|
+
* refused.
|
|
89
|
+
*/
|
|
90
|
+
export function swap(table, to, words) {
|
|
91
|
+
const segments = targetSegments(to);
|
|
92
|
+
if (segments === undefined) {
|
|
93
|
+
throw new TypeError(`Not a createUrl path: ${to}`);
|
|
94
|
+
}
|
|
95
|
+
let result = "";
|
|
96
|
+
let copied = 1;
|
|
97
|
+
for (const at of words) {
|
|
98
|
+
const [start, end] = segments[at];
|
|
99
|
+
let canonical;
|
|
100
|
+
try {
|
|
101
|
+
canonical = decodeURIComponent(to.slice(start, end));
|
|
102
|
+
}
|
|
103
|
+
catch {
|
|
104
|
+
continue;
|
|
105
|
+
}
|
|
106
|
+
const word = table.get(canonical);
|
|
107
|
+
if (word !== undefined) {
|
|
108
|
+
result += to.slice(copied, start) + encode(word);
|
|
109
|
+
copied = end;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
return `/${result}${to.slice(copied)}`;
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Provides the {@link REDIRECT_TRANSLATOR} of one build's generated word table.
|
|
116
|
+
*
|
|
117
|
+
* @remarks
|
|
118
|
+
* Internal: exported only from `@loozo/angular-launch/_redirects` for a translated app's
|
|
119
|
+
* generated `redirects.generated.ts`. `table` runs once, the first time a client needs it, so
|
|
120
|
+
* its `$localize` tags evaluate inside the localized application.
|
|
121
|
+
*
|
|
122
|
+
* @param table - Returns every canonical word and its translation in this build.
|
|
123
|
+
* @returns The providers.
|
|
124
|
+
*/
|
|
125
|
+
export function redirectTranslation(table) {
|
|
126
|
+
return makeEnvironmentProviders([
|
|
127
|
+
{
|
|
128
|
+
provide: REDIRECT_TRANSLATOR,
|
|
129
|
+
useFactory: () => {
|
|
130
|
+
let map;
|
|
131
|
+
return (to, words) => {
|
|
132
|
+
if (!map) {
|
|
133
|
+
const { words: canonical, translations } = table();
|
|
134
|
+
map = new Map(canonical.map((word, index) => [word, translations[index]]));
|
|
135
|
+
}
|
|
136
|
+
return swap(map, to, words);
|
|
137
|
+
};
|
|
138
|
+
},
|
|
139
|
+
},
|
|
140
|
+
]);
|
|
141
|
+
}
|
|
@@ -4,7 +4,7 @@ export declare const RPC_CALL: HttpContextToken<boolean>;
|
|
|
4
4
|
export interface ClientMethod {
|
|
5
5
|
readonly name: string;
|
|
6
6
|
readonly input: boolean;
|
|
7
|
-
readonly kind: "query" | "mutation";
|
|
7
|
+
readonly kind: "query" | "mutation" | "stream" | "channel";
|
|
8
8
|
readonly errors: readonly ErrorMetadata[];
|
|
9
9
|
}
|
|
10
10
|
export declare function injectClient(groups: Readonly<Record<string, {
|
package/remote-client.js
ADDED
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
import { isPlatformServer } from "@angular/common";
|
|
2
|
+
import { inject, PLATFORM_ID, REQUEST } from "@angular/core";
|
|
3
|
+
import { HttpClient, HttpContext, HttpContextToken, HttpErrorResponse } from "@angular/common/http";
|
|
4
|
+
import { openChannel } from "./channel.js";
|
|
5
|
+
import { RpcError, recognizedError, } from "./client.js";
|
|
6
|
+
import { connect } from "./connect.js";
|
|
7
|
+
import { serializeJson } from "./json.js";
|
|
8
|
+
import { recognizedOutcome } from "./outcomes.js";
|
|
9
|
+
import { REDIRECT_TRANSLATOR } from "./redirect-translation.js";
|
|
10
|
+
import { openStream } from "./stream.js";
|
|
11
|
+
export const RPC_CALL = new HttpContextToken(() => false);
|
|
12
|
+
const errorOwners = new WeakMap();
|
|
13
|
+
function call(http, group, method, input, translate, { signal, transferCache = true } = {}) {
|
|
14
|
+
return new Promise((resolve, reject) => {
|
|
15
|
+
let subscription;
|
|
16
|
+
let settled = false;
|
|
17
|
+
function cleanup() {
|
|
18
|
+
signal?.removeEventListener("abort", abort);
|
|
19
|
+
subscription?.unsubscribe();
|
|
20
|
+
}
|
|
21
|
+
function abort() {
|
|
22
|
+
if (settled) {
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
settled = true;
|
|
26
|
+
cleanup();
|
|
27
|
+
reject(new DOMException("RPC call aborted", "AbortError"));
|
|
28
|
+
}
|
|
29
|
+
if (signal?.aborted) {
|
|
30
|
+
abort();
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
const envelope = JSON.parse(serializeJson({ input }));
|
|
34
|
+
signal?.addEventListener("abort", abort, { once: true });
|
|
35
|
+
if (method.kind === "mutation") {
|
|
36
|
+
transferCache = false;
|
|
37
|
+
}
|
|
38
|
+
subscription = http
|
|
39
|
+
.post(`/_rpc/${group}/${method.name}`, envelope, {
|
|
40
|
+
context: new HttpContext().set(RPC_CALL, true),
|
|
41
|
+
transferCache,
|
|
42
|
+
})
|
|
43
|
+
.subscribe({
|
|
44
|
+
next(value) {
|
|
45
|
+
settled = true;
|
|
46
|
+
cleanup();
|
|
47
|
+
resolve(value);
|
|
48
|
+
},
|
|
49
|
+
error(error) {
|
|
50
|
+
settled = true;
|
|
51
|
+
cleanup();
|
|
52
|
+
if (error instanceof HttpErrorResponse) {
|
|
53
|
+
const frame = recognizedError(error.error, error.status, method.errors);
|
|
54
|
+
if (frame) {
|
|
55
|
+
const converted = new RpcError(frame.code, error.status, frame.data, frame.message);
|
|
56
|
+
errorOwners.set(converted, { group, method: method.name });
|
|
57
|
+
reject(converted);
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
const outcome = recognizedOutcome(error.error, error.status, translate);
|
|
61
|
+
if (outcome) {
|
|
62
|
+
reject(outcome);
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
reject(error);
|
|
67
|
+
},
|
|
68
|
+
});
|
|
69
|
+
if (settled) {
|
|
70
|
+
cleanup();
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
// Streams don't run during server rendering: iterating one fails at once with FORBIDDEN.
|
|
75
|
+
function refusedStream(error) {
|
|
76
|
+
return { next: () => Promise.reject(error) };
|
|
77
|
+
}
|
|
78
|
+
function owner(group, method) {
|
|
79
|
+
return (error) => {
|
|
80
|
+
errorOwners.set(error, { group, method: method.name });
|
|
81
|
+
return error;
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
function stream(server, translate, group, method, input, options = {}, opened) {
|
|
85
|
+
const own = owner(group, method);
|
|
86
|
+
// Each iteration is its own request, so iterating again reconnects.
|
|
87
|
+
return {
|
|
88
|
+
[Symbol.asyncIterator]: () => {
|
|
89
|
+
if (server) {
|
|
90
|
+
return refusedStream(own(new RpcError("FORBIDDEN", 403)));
|
|
91
|
+
}
|
|
92
|
+
return openStream(`/_rpc/${group}/${method.name}`, input, method.errors, own, options, opened, translate);
|
|
93
|
+
},
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
// Channels don't run during server rendering either: they fail at once with FORBIDDEN.
|
|
97
|
+
function channel(server, translate, group, method, input, options = {}, opened) {
|
|
98
|
+
const own = owner(group, method);
|
|
99
|
+
let path = `/_rpc/${group}/${method.name}`;
|
|
100
|
+
let refused;
|
|
101
|
+
if (server) {
|
|
102
|
+
refused = own(new RpcError("FORBIDDEN", 403));
|
|
103
|
+
}
|
|
104
|
+
else {
|
|
105
|
+
try {
|
|
106
|
+
path += `?input=${encodeURIComponent(serializeJson(input))}`;
|
|
107
|
+
}
|
|
108
|
+
catch (error) {
|
|
109
|
+
refused = error;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
return openChannel(path, method.errors, own, options, opened, refused, translate);
|
|
113
|
+
}
|
|
114
|
+
export function injectClient(groups) {
|
|
115
|
+
const http = inject(HttpClient);
|
|
116
|
+
let translate = inject(REDIRECT_TRANSLATOR, { optional: true }) ?? undefined;
|
|
117
|
+
let server = false;
|
|
118
|
+
// Only a redirect translation, streams and channels depend on the platform.
|
|
119
|
+
if (translate ||
|
|
120
|
+
Object.values(groups).some((group) => group.methods.some((method) => method.kind === "stream" || method.kind === "channel"))) {
|
|
121
|
+
server = isPlatformServer(inject(PLATFORM_ID));
|
|
122
|
+
}
|
|
123
|
+
// On the server, only a live request's rendering translates, as the rendering backend tells
|
|
124
|
+
// live from prerender. A prerendered page's calls run in this localized application, so
|
|
125
|
+
// their redirects are already in its language.
|
|
126
|
+
if (server && !(inject(REQUEST, { optional: true }) instanceof Request)) {
|
|
127
|
+
translate = undefined;
|
|
128
|
+
}
|
|
129
|
+
const result = Object.create(null);
|
|
130
|
+
for (const [namespace, group] of Object.entries(groups)) {
|
|
131
|
+
const methods = Object.create(null);
|
|
132
|
+
for (const method of group.methods) {
|
|
133
|
+
let invoke;
|
|
134
|
+
let open;
|
|
135
|
+
if (method.kind === "stream") {
|
|
136
|
+
open = (input, signal, opened) => stream(server, translate, namespace, method, input, { signal }, opened);
|
|
137
|
+
if (method.input) {
|
|
138
|
+
invoke = (input, options) => stream(server, translate, namespace, method, input, options);
|
|
139
|
+
}
|
|
140
|
+
else {
|
|
141
|
+
invoke = (options) => stream(server, translate, namespace, method, null, options);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
else if (method.kind === "channel") {
|
|
145
|
+
open = (input, signal, opened) => channel(server, translate, namespace, method, input, { signal }, opened);
|
|
146
|
+
if (method.input) {
|
|
147
|
+
invoke = (input, options) => channel(server, translate, namespace, method, input, options);
|
|
148
|
+
}
|
|
149
|
+
else {
|
|
150
|
+
invoke = (options) => channel(server, translate, namespace, method, null, options);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
else if (method.input) {
|
|
154
|
+
invoke = (input, options) => call(http, namespace, method, input, translate, options);
|
|
155
|
+
}
|
|
156
|
+
else {
|
|
157
|
+
invoke = (options) => call(http, namespace, method, null, translate, options);
|
|
158
|
+
}
|
|
159
|
+
if (open) {
|
|
160
|
+
const opener = open;
|
|
161
|
+
Object.assign(invoke, {
|
|
162
|
+
connect: (options) => connect(opener, options),
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
methods[method.name] = Object.assign(invoke, {
|
|
166
|
+
kind: method.kind,
|
|
167
|
+
isError: (error) => {
|
|
168
|
+
if (typeof error !== "object" || error === null) {
|
|
169
|
+
return false;
|
|
170
|
+
}
|
|
171
|
+
const owner = errorOwners.get(error);
|
|
172
|
+
return owner?.group === namespace && owner.method === method.name;
|
|
173
|
+
},
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
result[namespace] = methods;
|
|
177
|
+
}
|
|
178
|
+
return result;
|
|
179
|
+
}
|
package/route-types.d.ts
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import type { ContributionOf, ExtensionSpec, InputContribution, StepSignature } from "@loozo/angular-router/_core";
|
|
2
|
+
import type { Client } from "./client.js";
|
|
3
|
+
import type { MiddlewareDefinition, ProcedureBuilder, ProcedureDefinition, StageArgs } from "./server/definitions.js";
|
|
4
|
+
/** Type-only witness of Launch's route extension; absent at runtime. */
|
|
5
|
+
export declare const launch: unique symbol;
|
|
6
|
+
type Flat<Value> = {
|
|
7
|
+
[Key in keyof Value]: Value[Key];
|
|
8
|
+
} & {};
|
|
9
|
+
declare const noStage: unique symbol;
|
|
10
|
+
type NoStage = typeof noStage;
|
|
11
|
+
type StageValue<Own> = Own extends {
|
|
12
|
+
readonly context: (...args: never[]) => infer Value;
|
|
13
|
+
} ? Awaited<Value> : NoStage;
|
|
14
|
+
type Last<Chain, Context> = Chain extends readonly [infer Head, ...infer Rest] ? Last<Rest, StageValue<Head> extends NoStage ? Context : StageValue<Head>> : Context;
|
|
15
|
+
/**
|
|
16
|
+
* The context a route chain's `.context()` stages build: what its last stage returns, as
|
|
17
|
+
* it is. Each stage returns the route's whole context, so a stage that merges spreads the
|
|
18
|
+
* context it receives itself. `{}` without a stage.
|
|
19
|
+
*
|
|
20
|
+
* @typeParam Chain - A route's contribution chain.
|
|
21
|
+
*/
|
|
22
|
+
export type ChainContext<Chain> = Last<Chain, {}>;
|
|
23
|
+
type NamespacesOf<Own> = Own extends {
|
|
24
|
+
readonly remote: (...args: never[]) => infer Namespaces;
|
|
25
|
+
} ? Namespaces : {};
|
|
26
|
+
type Collect<Chain, Namespaces> = Chain extends readonly [infer Head, ...infer Rest] ? Collect<Rest, Namespaces & NamespacesOf<Head>> : Namespaces;
|
|
27
|
+
/**
|
|
28
|
+
* The `.remote()` namespaces a route chain declares, its own and its ancestors', keyed by
|
|
29
|
+
* namespace.
|
|
30
|
+
*
|
|
31
|
+
* @typeParam Chain - A route's contribution chain.
|
|
32
|
+
*/
|
|
33
|
+
export type ChainNamespaces<Chain> = Flat<Collect<Chain, {}>>;
|
|
34
|
+
type RemoteFor<Chain> = {
|
|
35
|
+
readonly [Namespace in keyof ChainNamespaces<Chain>]: Client<ChainNamespaces<Chain>[Namespace]>;
|
|
36
|
+
};
|
|
37
|
+
/**
|
|
38
|
+
* A route's client: one {@link Client} per namespace the route can see, its own and its
|
|
39
|
+
* ancestors'. A route copy exports it as `Remote`, and it's the type of `route.remote` and
|
|
40
|
+
* of `remote` in the route's kept callbacks.
|
|
41
|
+
*
|
|
42
|
+
* @typeParam Route - A route made by Launch's `defineRoute`, or its copy.
|
|
43
|
+
*/
|
|
44
|
+
export type RemoteOf<Route> = RemoteFor<ContributionOf<Route>>;
|
|
45
|
+
/**
|
|
46
|
+
* The context a route's procedures receive: what its own and its ancestors' `.context()`
|
|
47
|
+
* stages build. A route copy exports it as `Context`.
|
|
48
|
+
*
|
|
49
|
+
* @typeParam Route - A route made by Launch's `defineRoute`, or its copy.
|
|
50
|
+
*/
|
|
51
|
+
export type ContextOf<Route> = ChainContext<ContributionOf<Route>>;
|
|
52
|
+
type OwnEntry<Name extends string, Args> = Args extends readonly [infer Value] ? {
|
|
53
|
+
readonly [Key in Name]: Value;
|
|
54
|
+
} : {};
|
|
55
|
+
type UseEntry<Args> = {
|
|
56
|
+
readonly use: Args;
|
|
57
|
+
};
|
|
58
|
+
type InputMembers<Chain> = {
|
|
59
|
+
/** The route's client, its own and its ancestors' namespaces. */
|
|
60
|
+
readonly remote: RemoteFor<Chain>;
|
|
61
|
+
};
|
|
62
|
+
/** The signature of a route's `.context(stage)` step. */
|
|
63
|
+
export interface ContextStep extends StepSignature {
|
|
64
|
+
/** One stage, which receives the context of the stages above it. */
|
|
65
|
+
readonly parameters: [
|
|
66
|
+
stage: (args: StageArgs<ChainContext<this["chain"]>>) => object | PromiseLike<object>
|
|
67
|
+
];
|
|
68
|
+
/** The route's own record keeps the stage under `context`. */
|
|
69
|
+
readonly own: OwnEntry<"context", this["args"]>;
|
|
70
|
+
}
|
|
71
|
+
/** The signature of a route's `.use(...middleware)` step. */
|
|
72
|
+
export interface UseStep extends StepSignature {
|
|
73
|
+
/**
|
|
74
|
+
* Middleware that each accept the route's context: `middleware()` definitions, or bare
|
|
75
|
+
* functions, which receive it typed.
|
|
76
|
+
*/
|
|
77
|
+
readonly parameters: readonly (MiddlewareDefinition<NoInfer<ChainContext<this["chain"]>>> | MiddlewareDefinition<NoInfer<ChainContext<this["chain"]>>>["run"])[];
|
|
78
|
+
/** The route's own record keeps the middleware under `use`. */
|
|
79
|
+
readonly own: UseEntry<this["args"]>;
|
|
80
|
+
}
|
|
81
|
+
/** The signature of a route's `.remote(build)` step. */
|
|
82
|
+
export interface RemoteStep extends StepSignature {
|
|
83
|
+
/** A callback returning the route's namespaces, built with the route's context. */
|
|
84
|
+
readonly parameters: [
|
|
85
|
+
build: (procedures: ProcedureBuilder<ChainContext<this["chain"]>>) => Readonly<Record<string, Readonly<Record<string, ProcedureDefinition>>>>
|
|
86
|
+
];
|
|
87
|
+
/** The route's own record keeps the callback under `remote`. */
|
|
88
|
+
readonly own: OwnEntry<"remote", this["args"]>;
|
|
89
|
+
}
|
|
90
|
+
/** What Launch adds to a route's kept router callbacks: `remote`. */
|
|
91
|
+
export interface LaunchInput extends InputContribution {
|
|
92
|
+
/** The members kept callbacks receive besides the router's own. */
|
|
93
|
+
readonly input: InputMembers<this["chain"]>;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* One entry of Launch's step manifest.
|
|
97
|
+
*
|
|
98
|
+
* @typeParam Name - The step's method name.
|
|
99
|
+
* @typeParam Type - The step's typing.
|
|
100
|
+
* @typeParam Max - The most arguments it takes: one, or any number (`null`).
|
|
101
|
+
*/
|
|
102
|
+
export interface LaunchStep<Name extends string, Type extends StepSignature, Max extends 1 | null> {
|
|
103
|
+
/** The step's method name on the route chain. */
|
|
104
|
+
readonly name: Name;
|
|
105
|
+
/** The step's typing. */
|
|
106
|
+
readonly type: Type;
|
|
107
|
+
/** Always `true`: route copies strip the step, and only the server runs it. */
|
|
108
|
+
readonly serverOnly: true;
|
|
109
|
+
/** How many arguments the step takes. */
|
|
110
|
+
readonly arity: {
|
|
111
|
+
/** At least one. */
|
|
112
|
+
readonly min: 1;
|
|
113
|
+
/** One, or any number. */
|
|
114
|
+
readonly max: Max;
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
/** Launch's route extension: `.context()`, `.use()` and `.remote()`, all server-only. */
|
|
118
|
+
export interface LaunchSpec extends ExtensionSpec {
|
|
119
|
+
/** The steps, in the order a route may use them, each at most once. */
|
|
120
|
+
readonly steps: readonly [
|
|
121
|
+
LaunchStep<"context", ContextStep, 1>,
|
|
122
|
+
LaunchStep<"use", UseStep, null>,
|
|
123
|
+
LaunchStep<"remote", RemoteStep, 1>
|
|
124
|
+
];
|
|
125
|
+
/** What Launch adds to kept router callbacks. */
|
|
126
|
+
readonly input: LaunchInput;
|
|
127
|
+
/** The brand that ties a route's type to Launch's extension. */
|
|
128
|
+
readonly witness: typeof launch;
|
|
129
|
+
}
|
|
130
|
+
export {};
|
package/route-types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/route.d.ts
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { type RouteFactory } from "@loozo/angular-router/_core";
|
|
2
|
+
import type { LaunchSpec } from "./route-types.js";
|
|
3
|
+
/**
|
|
4
|
+
* Declares a route, like the router's `defineRoute`, with Launch's server steps offered
|
|
5
|
+
* first: `.context(stage)`, then `.use(...middleware)`, then `.remote(build)`, each at most
|
|
6
|
+
* once and in that order, before any router step.
|
|
7
|
+
*
|
|
8
|
+
* @remarks
|
|
9
|
+
* Import it from `@loozo/angular-launch` in every `route.ts` of a Launch app. Its steps are
|
|
10
|
+
* server-only: generation strips them from the route's browser copy, and the server reads
|
|
11
|
+
* them from `server.generated.ts`.
|
|
12
|
+
*
|
|
13
|
+
* - `.context(stage)` derives the route's context from its ancestors'. The stage receives
|
|
14
|
+
* `StageArgs` from `@loozo/angular-launch/server` and returns the route's whole context:
|
|
15
|
+
* spread the context above, `{ ...context, user }`, to keep its keys. It runs at most once
|
|
16
|
+
* per request scope, only when a call needs it.
|
|
17
|
+
* - `.use(...middleware)` runs around this route's own procedures, after the stages and
|
|
18
|
+
* before input validation. Each entry is a shared `middleware()` or a bare async function,
|
|
19
|
+
* which receives the route's context typed. A child route's procedures don't run it.
|
|
20
|
+
* - `.remote(({ query, mutation, stream, channel }) => ({ namespace: { … } }))` declares
|
|
21
|
+
* procedures in namespaces, each unique across the app and called at
|
|
22
|
+
* `/_rpc/<namespace>/<procedure>`. The route and its descendants see them as
|
|
23
|
+
* `injectRoute().remote.<namespace>` and as `remote` in their kept `resolve`, guards and
|
|
24
|
+
* `title`.
|
|
25
|
+
*
|
|
26
|
+
* Generation reads every step statically: write the chain as a direct default export, and
|
|
27
|
+
* each `.remote()` callback as an arrow function destructuring the builders it uses,
|
|
28
|
+
* returning a parenthesized object literal of namespaces whose procedures are direct
|
|
29
|
+
* builder calls with inline options.
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* ```ts
|
|
33
|
+
* import { defineRoute } from "@loozo/angular-launch";
|
|
34
|
+
* import parent from "../route";
|
|
35
|
+
*
|
|
36
|
+
* export default defineRoute("todos", { parent })
|
|
37
|
+
* .remote(({ query }) => ({
|
|
38
|
+
* todos: {
|
|
39
|
+
* list: query({ handler: ({ context }) => context.db.todos.listForUser(context.user.id) }),
|
|
40
|
+
* },
|
|
41
|
+
* }))
|
|
42
|
+
* .resolve({ todos: ({ remote }) => remote.todos.list() });
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
export declare const defineRoute: RouteFactory<LaunchSpec>;
|
package/route.js
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { runInInjectionContext } from "@angular/core";
|
|
2
|
+
import { createRouteFactory, } from "@loozo/angular-router/_core";
|
|
3
|
+
import { launchQueries } from "./launch-queries.js";
|
|
4
|
+
import { injectClient } from "./remote-client.js";
|
|
5
|
+
// A route copy's own record holds generation's data, `{ remote: { [namespace]: { methods } } }`,
|
|
6
|
+
// never the authored builder, which only the server's authored routes hold.
|
|
7
|
+
function namespacesOf(chain) {
|
|
8
|
+
const namespaces = Object.create(null);
|
|
9
|
+
for (const record of chain) {
|
|
10
|
+
const data = record.remote;
|
|
11
|
+
if (data === undefined) {
|
|
12
|
+
continue;
|
|
13
|
+
}
|
|
14
|
+
for (const [namespace, entry] of Object.entries(data)) {
|
|
15
|
+
namespaces[namespace] = entry;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
return namespaces;
|
|
19
|
+
}
|
|
20
|
+
// The queries and mutations, which have TanStack factories; streams and channels don't.
|
|
21
|
+
function queryMethodsOf(namespaces) {
|
|
22
|
+
const result = Object.create(null);
|
|
23
|
+
for (const [namespace, { methods }] of Object.entries(namespaces)) {
|
|
24
|
+
const entries = Object.create(null);
|
|
25
|
+
for (const { name, kind, input } of methods) {
|
|
26
|
+
if (kind === "query" || kind === "mutation") {
|
|
27
|
+
entries[name] = { kind, input };
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
result[namespace] = entries;
|
|
31
|
+
}
|
|
32
|
+
return result;
|
|
33
|
+
}
|
|
34
|
+
function input(chain) {
|
|
35
|
+
return { remote: injectClient(namespacesOf(chain)) };
|
|
36
|
+
}
|
|
37
|
+
// Lazy getters: a page may read `route.remote` first from an event or after an `await`, so
|
|
38
|
+
// each member is built on first read inside the handle creator's injection context.
|
|
39
|
+
function handle(chain, injector) {
|
|
40
|
+
let remote;
|
|
41
|
+
let queries;
|
|
42
|
+
function client() {
|
|
43
|
+
remote ??= runInInjectionContext(injector, () => injectClient(namespacesOf(chain)));
|
|
44
|
+
return remote;
|
|
45
|
+
}
|
|
46
|
+
return {
|
|
47
|
+
get remote() {
|
|
48
|
+
return client();
|
|
49
|
+
},
|
|
50
|
+
get queries() {
|
|
51
|
+
if (queries === undefined) {
|
|
52
|
+
const create = injector.get(launchQueries, null);
|
|
53
|
+
if (create === null) {
|
|
54
|
+
throw new Error("route.queries requires provideLaunchQueries() from @loozo/angular-launch/tanstack-query in the application providers");
|
|
55
|
+
}
|
|
56
|
+
const rpc = client();
|
|
57
|
+
queries = runInInjectionContext(injector, () => create(rpc, queryMethodsOf(namespacesOf(chain))));
|
|
58
|
+
}
|
|
59
|
+
return queries;
|
|
60
|
+
},
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Declares a route, like the router's `defineRoute`, with Launch's server steps offered
|
|
65
|
+
* first: `.context(stage)`, then `.use(...middleware)`, then `.remote(build)`, each at most
|
|
66
|
+
* once and in that order, before any router step.
|
|
67
|
+
*
|
|
68
|
+
* @remarks
|
|
69
|
+
* Import it from `@loozo/angular-launch` in every `route.ts` of a Launch app. Its steps are
|
|
70
|
+
* server-only: generation strips them from the route's browser copy, and the server reads
|
|
71
|
+
* them from `server.generated.ts`.
|
|
72
|
+
*
|
|
73
|
+
* - `.context(stage)` derives the route's context from its ancestors'. The stage receives
|
|
74
|
+
* `StageArgs` from `@loozo/angular-launch/server` and returns the route's whole context:
|
|
75
|
+
* spread the context above, `{ ...context, user }`, to keep its keys. It runs at most once
|
|
76
|
+
* per request scope, only when a call needs it.
|
|
77
|
+
* - `.use(...middleware)` runs around this route's own procedures, after the stages and
|
|
78
|
+
* before input validation. Each entry is a shared `middleware()` or a bare async function,
|
|
79
|
+
* which receives the route's context typed. A child route's procedures don't run it.
|
|
80
|
+
* - `.remote(({ query, mutation, stream, channel }) => ({ namespace: { … } }))` declares
|
|
81
|
+
* procedures in namespaces, each unique across the app and called at
|
|
82
|
+
* `/_rpc/<namespace>/<procedure>`. The route and its descendants see them as
|
|
83
|
+
* `injectRoute().remote.<namespace>` and as `remote` in their kept `resolve`, guards and
|
|
84
|
+
* `title`.
|
|
85
|
+
*
|
|
86
|
+
* Generation reads every step statically: write the chain as a direct default export, and
|
|
87
|
+
* each `.remote()` callback as an arrow function destructuring the builders it uses,
|
|
88
|
+
* returning a parenthesized object literal of namespaces whose procedures are direct
|
|
89
|
+
* builder calls with inline options.
|
|
90
|
+
*
|
|
91
|
+
* @example
|
|
92
|
+
* ```ts
|
|
93
|
+
* import { defineRoute } from "@loozo/angular-launch";
|
|
94
|
+
* import parent from "../route";
|
|
95
|
+
*
|
|
96
|
+
* export default defineRoute("todos", { parent })
|
|
97
|
+
* .remote(({ query }) => ({
|
|
98
|
+
* todos: {
|
|
99
|
+
* list: query({ handler: ({ context }) => context.db.todos.listForUser(context.user.id) }),
|
|
100
|
+
* },
|
|
101
|
+
* }))
|
|
102
|
+
* .resolve({ todos: ({ remote }) => remote.todos.list() });
|
|
103
|
+
* ```
|
|
104
|
+
*/
|
|
105
|
+
export const defineRoute = createRouteFactory({
|
|
106
|
+
steps: [
|
|
107
|
+
{ name: "context", serverOnly: true, arity: { min: 1, max: 1 } },
|
|
108
|
+
{ name: "use", serverOnly: true, arity: { min: 1, max: null } },
|
|
109
|
+
{ name: "remote", serverOnly: true, arity: { min: 1, max: 1 } },
|
|
110
|
+
],
|
|
111
|
+
input,
|
|
112
|
+
handle,
|
|
113
|
+
});
|