@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
package/channel.d.ts
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import type { ChannelCallOptions, ErrorMetadata, RpcError } from "./client.js";
|
|
2
|
+
import type { TranslateRedirect } from "./redirect-translation.js";
|
|
3
|
+
/**
|
|
4
|
+
* An open channel: the values its handler yields, and `send` for messages to it.
|
|
5
|
+
*
|
|
6
|
+
* @remarks
|
|
7
|
+
* Returned by a generated channel callable, which opens the websocket at once. Iterate it
|
|
8
|
+
* once; a second iterator throws a `TypeError`. Iteration ends when the handler ends, and
|
|
9
|
+
* throws the procedure's `RpcError`, an `RpcRedirect` or an `RpcNotFound` when the call
|
|
10
|
+
* fails, an `AbortError` when the call's `signal` aborts, and a `TypeError` when the
|
|
11
|
+
* connection closes early, the server refused the handshake or sent a malformed frame.
|
|
12
|
+
* The signal counts until iteration ends: aborting after the last frame arrived drops the
|
|
13
|
+
* values not read yet and throws the `AbortError`. Leaving the loop early, as `break` does,
|
|
14
|
+
* closes the channel. Reads may overlap; they settle in order.
|
|
15
|
+
*
|
|
16
|
+
* Flow control is mutual: the server stops at 256 values this side hasn't read, and `send`
|
|
17
|
+
* waits once 32 messages are unread on the server. So read in a loop that runs alongside
|
|
18
|
+
* your sends; awaiting a batch of sends before reading can stall both sides for good.
|
|
19
|
+
*
|
|
20
|
+
* @typeParam Received - The values the handler yields.
|
|
21
|
+
* @typeParam Sent - The messages the channel's `message` schema accepts.
|
|
22
|
+
*/
|
|
23
|
+
export interface Channel<Received, Sent> extends AsyncIterable<Received> {
|
|
24
|
+
/**
|
|
25
|
+
* Sends one message to the handler.
|
|
26
|
+
*
|
|
27
|
+
* @remarks
|
|
28
|
+
* Messages wait here, in order, until the socket is open and the server has room for
|
|
29
|
+
* them.
|
|
30
|
+
*
|
|
31
|
+
* @param message - The message, as strict JSON.
|
|
32
|
+
* @returns A promise that resolves once the message was handed to the socket. It rejects
|
|
33
|
+
* with the channel's closing error when the channel closes first, and with a `TypeError`
|
|
34
|
+
* when the channel is already closed or `message` isn't strict JSON.
|
|
35
|
+
*/
|
|
36
|
+
send(message: Sent): Promise<void>;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Opens one channel call over the browser `WebSocket`.
|
|
40
|
+
*
|
|
41
|
+
* @remarks
|
|
42
|
+
* Internal. The socket's URL is `path` resolved against the page, with a `ws:` or `wss:`
|
|
43
|
+
* scheme. Not `HttpClient`, and no Angular pending task: an open channel never keeps the app
|
|
44
|
+
* unstable. See {@link Channel} for the caller-facing behavior. Values that arrive before
|
|
45
|
+
* they're read are kept; the server sends no more than its window.
|
|
46
|
+
*
|
|
47
|
+
* @param path - The procedure's `/_rpc/<namespace>/<procedure>?input=…` path.
|
|
48
|
+
* @param declared - The procedure's declared error metadata.
|
|
49
|
+
* @param own - Associates a converted error with its procedure for `isError`.
|
|
50
|
+
* @param options - The call options.
|
|
51
|
+
* @param opened - Called once the socket opens.
|
|
52
|
+
* @param refused - Fails the channel at once with this error, without a connection.
|
|
53
|
+
* @param translate - Puts a redirect's listed words in this build's language.
|
|
54
|
+
*/
|
|
55
|
+
export declare function openChannel(path: string, declared: readonly ErrorMetadata[], own: (error: RpcError<string, unknown>) => RpcError<string, unknown>, { signal }: ChannelCallOptions, opened?: () => void, refused?: Error, translate?: TranslateRedirect): Channel<unknown, unknown>;
|
package/channel.js
ADDED
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
import { parseJson, serializeJson } from "./json.js";
|
|
2
|
+
import { failureRejection } from "./stream.js";
|
|
3
|
+
/** How many values the server sends before this side must grant more. */
|
|
4
|
+
const valueWindow = 256;
|
|
5
|
+
/** How many messages this side sends before the server must grant more. */
|
|
6
|
+
const messageWindow = 32;
|
|
7
|
+
/**
|
|
8
|
+
* Opens one channel call over the browser `WebSocket`.
|
|
9
|
+
*
|
|
10
|
+
* @remarks
|
|
11
|
+
* Internal. The socket's URL is `path` resolved against the page, with a `ws:` or `wss:`
|
|
12
|
+
* scheme. Not `HttpClient`, and no Angular pending task: an open channel never keeps the app
|
|
13
|
+
* unstable. See {@link Channel} for the caller-facing behavior. Values that arrive before
|
|
14
|
+
* they're read are kept; the server sends no more than its window.
|
|
15
|
+
*
|
|
16
|
+
* @param path - The procedure's `/_rpc/<namespace>/<procedure>?input=…` path.
|
|
17
|
+
* @param declared - The procedure's declared error metadata.
|
|
18
|
+
* @param own - Associates a converted error with its procedure for `isError`.
|
|
19
|
+
* @param options - The call options.
|
|
20
|
+
* @param opened - Called once the socket opens.
|
|
21
|
+
* @param refused - Fails the channel at once with this error, without a connection.
|
|
22
|
+
* @param translate - Puts a redirect's listed words in this build's language.
|
|
23
|
+
*/
|
|
24
|
+
export function openChannel(path, declared, own, { signal }, opened = () => undefined, refused, translate) {
|
|
25
|
+
const values = [];
|
|
26
|
+
const sends = [];
|
|
27
|
+
let socket;
|
|
28
|
+
let sendCredit = messageWindow;
|
|
29
|
+
let readSinceGrant = 0;
|
|
30
|
+
// No more frames either way: the call ended, failed, was aborted or left.
|
|
31
|
+
let closed = false;
|
|
32
|
+
let failure;
|
|
33
|
+
// The consumer is done: it read the end, caught the failure or left the loop.
|
|
34
|
+
let settled = false;
|
|
35
|
+
let iterated = false;
|
|
36
|
+
let wake;
|
|
37
|
+
// Each read waits for the one before it, so concurrent `next()` calls settle in order.
|
|
38
|
+
let reading = Promise.resolve();
|
|
39
|
+
function finish(error) {
|
|
40
|
+
if (closed) {
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
closed = true;
|
|
44
|
+
failure = error;
|
|
45
|
+
const reason = error ?? new TypeError("Launch channel closed");
|
|
46
|
+
for (const pending of sends.splice(0)) {
|
|
47
|
+
pending.reject(reason);
|
|
48
|
+
}
|
|
49
|
+
// Closing again after the server's own close is harmless.
|
|
50
|
+
socket?.close(1000);
|
|
51
|
+
wake?.();
|
|
52
|
+
}
|
|
53
|
+
function settle() {
|
|
54
|
+
settled = true;
|
|
55
|
+
values.length = 0;
|
|
56
|
+
signal?.removeEventListener("abort", abort);
|
|
57
|
+
}
|
|
58
|
+
// Also after the last frame: values the consumer hasn't read yet aren't delivered.
|
|
59
|
+
function abort() {
|
|
60
|
+
if (settled) {
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
const reason = new DOMException("RPC channel aborted", "AbortError");
|
|
64
|
+
values.length = 0;
|
|
65
|
+
if (closed) {
|
|
66
|
+
failure = reason;
|
|
67
|
+
wake?.();
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
finish(reason);
|
|
71
|
+
}
|
|
72
|
+
function flush() {
|
|
73
|
+
while (socket?.readyState === 1 && !closed && sendCredit > 0 && sends.length > 0) {
|
|
74
|
+
const pending = sends.shift();
|
|
75
|
+
socket.send(pending.frame);
|
|
76
|
+
sendCredit -= 1;
|
|
77
|
+
pending.resolve();
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
function malformed() {
|
|
81
|
+
finish(new TypeError("Launch channel sent a malformed frame"));
|
|
82
|
+
}
|
|
83
|
+
function onMessage(event) {
|
|
84
|
+
if (closed) {
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
if (typeof event.data !== "string") {
|
|
88
|
+
malformed();
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
let frame;
|
|
92
|
+
try {
|
|
93
|
+
frame = parseJson(event.data);
|
|
94
|
+
}
|
|
95
|
+
catch {
|
|
96
|
+
malformed();
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
if (typeof frame !== "object" || frame === null || Array.isArray(frame)) {
|
|
100
|
+
malformed();
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
const single = Object.keys(frame).length === 1;
|
|
104
|
+
if (single && Object.hasOwn(frame, "value")) {
|
|
105
|
+
values.push(frame.value);
|
|
106
|
+
wake?.();
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
if (single && Object.hasOwn(frame, "ready")) {
|
|
110
|
+
const granted = frame.ready;
|
|
111
|
+
if (typeof granted !== "number" || !Number.isSafeInteger(granted) || granted <= 0) {
|
|
112
|
+
malformed();
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
sendCredit += granted;
|
|
116
|
+
flush();
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
if (single &&
|
|
120
|
+
Object.hasOwn(frame, "end") &&
|
|
121
|
+
frame.end === null) {
|
|
122
|
+
finish();
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
if (Object.hasOwn(frame, "status")) {
|
|
126
|
+
const { status, ...body } = frame;
|
|
127
|
+
let error;
|
|
128
|
+
if (typeof status === "number") {
|
|
129
|
+
error = failureRejection(body, status, declared, own, translate);
|
|
130
|
+
}
|
|
131
|
+
finish(error ?? new TypeError("Launch channel sent an unrecognized error"));
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
malformed();
|
|
135
|
+
}
|
|
136
|
+
function read() {
|
|
137
|
+
readSinceGrant += 1;
|
|
138
|
+
if (readSinceGrant >= valueWindow / 2 && !closed) {
|
|
139
|
+
socket.send(serializeJson({ ready: readSinceGrant }));
|
|
140
|
+
readSinceGrant = 0;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
if (refused) {
|
|
144
|
+
finish(refused);
|
|
145
|
+
}
|
|
146
|
+
else if (signal?.aborted) {
|
|
147
|
+
abort();
|
|
148
|
+
}
|
|
149
|
+
else {
|
|
150
|
+
try {
|
|
151
|
+
// Resolved against the page's base URL, with its http(s) scheme turned into ws(s).
|
|
152
|
+
socket = new WebSocket(path);
|
|
153
|
+
}
|
|
154
|
+
catch (error) {
|
|
155
|
+
finish(new TypeError("Launch channel couldn't connect", { cause: error }));
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
if (socket) {
|
|
159
|
+
const opening = socket;
|
|
160
|
+
signal?.addEventListener("abort", abort, { once: true });
|
|
161
|
+
opening.addEventListener("open", () => {
|
|
162
|
+
if (!closed) {
|
|
163
|
+
opened();
|
|
164
|
+
flush();
|
|
165
|
+
}
|
|
166
|
+
});
|
|
167
|
+
opening.addEventListener("message", onMessage);
|
|
168
|
+
// A refused handshake fires error, then close; a dropped connection only close.
|
|
169
|
+
opening.addEventListener("close", () => {
|
|
170
|
+
finish(new TypeError("Launch channel closed before it ended"));
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
return {
|
|
174
|
+
send(message) {
|
|
175
|
+
if (closed) {
|
|
176
|
+
return Promise.reject(new TypeError("Launch channel is closed"));
|
|
177
|
+
}
|
|
178
|
+
let frame;
|
|
179
|
+
try {
|
|
180
|
+
frame = serializeJson({ message });
|
|
181
|
+
}
|
|
182
|
+
catch (error) {
|
|
183
|
+
return Promise.reject(error);
|
|
184
|
+
}
|
|
185
|
+
return new Promise((resolve, reject) => {
|
|
186
|
+
sends.push({ frame, resolve, reject });
|
|
187
|
+
flush();
|
|
188
|
+
});
|
|
189
|
+
},
|
|
190
|
+
[Symbol.asyncIterator]() {
|
|
191
|
+
if (iterated) {
|
|
192
|
+
throw new TypeError("A Launch channel has one consumer: iterate it once");
|
|
193
|
+
}
|
|
194
|
+
iterated = true;
|
|
195
|
+
async function step() {
|
|
196
|
+
while (true) {
|
|
197
|
+
if (values.length > 0) {
|
|
198
|
+
const value = values.shift();
|
|
199
|
+
read();
|
|
200
|
+
return { done: false, value };
|
|
201
|
+
}
|
|
202
|
+
if (closed) {
|
|
203
|
+
if (settled) {
|
|
204
|
+
return { done: true, value: undefined };
|
|
205
|
+
}
|
|
206
|
+
settle();
|
|
207
|
+
if (failure) {
|
|
208
|
+
throw failure;
|
|
209
|
+
}
|
|
210
|
+
return { done: true, value: undefined };
|
|
211
|
+
}
|
|
212
|
+
await new Promise((resolve) => {
|
|
213
|
+
wake = resolve;
|
|
214
|
+
});
|
|
215
|
+
wake = undefined;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
return {
|
|
219
|
+
next() {
|
|
220
|
+
const result = reading.then(step);
|
|
221
|
+
reading = result.catch(() => undefined);
|
|
222
|
+
return result;
|
|
223
|
+
},
|
|
224
|
+
async return() {
|
|
225
|
+
settle();
|
|
226
|
+
finish();
|
|
227
|
+
return { done: true, value: undefined };
|
|
228
|
+
},
|
|
229
|
+
};
|
|
230
|
+
},
|
|
231
|
+
};
|
|
232
|
+
}
|
package/client.d.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import type { ErrorDefinitions,
|
|
1
|
+
import type { ErrorDefinitions, InputOf, OutputOf, Procedure, ProcedureKind, Schema } from "./server/definitions.js";
|
|
2
|
+
import type { Channel } from "./channel.js";
|
|
3
|
+
import type { ChannelConnector, ConnectInput, OptionalConnectInput, StreamConnector } from "./connect.js";
|
|
2
4
|
import { type InvalidInputData } from "./input-issues.js";
|
|
3
5
|
/** Per-call options for a generated query callable. */
|
|
4
6
|
export interface CallOptions {
|
|
@@ -33,12 +35,36 @@ export interface MutationCallOptions {
|
|
|
33
35
|
/** Only `false` is accepted: mutations never use the HTTP transfer cache. */
|
|
34
36
|
readonly transferCache?: false;
|
|
35
37
|
}
|
|
38
|
+
/** Per-call options for a generated stream callable. */
|
|
39
|
+
export interface StreamCallOptions {
|
|
40
|
+
/**
|
|
41
|
+
* Aborts the stream: its request is cancelled and iteration rejects with a
|
|
42
|
+
* `DOMException` named `AbortError`.
|
|
43
|
+
*
|
|
44
|
+
* @remarks
|
|
45
|
+
* Ending iteration early, such as with `break`, also closes the request. On the server,
|
|
46
|
+
* the stream's middleware and handler see their `signal` abort.
|
|
47
|
+
*/
|
|
48
|
+
readonly signal?: AbortSignal;
|
|
49
|
+
}
|
|
50
|
+
/** Per-call options for a generated channel callable. */
|
|
51
|
+
export interface ChannelCallOptions {
|
|
52
|
+
/**
|
|
53
|
+
* Aborts the channel: its socket closes, iteration rejects with a `DOMException` named
|
|
54
|
+
* `AbortError`, and messages waiting to be sent reject with it too.
|
|
55
|
+
*
|
|
56
|
+
* @remarks
|
|
57
|
+
* Ending iteration early, such as with `break`, also closes the socket. On the server, the
|
|
58
|
+
* channel's middleware and handler see their `signal` abort.
|
|
59
|
+
*/
|
|
60
|
+
readonly signal?: AbortSignal;
|
|
61
|
+
}
|
|
36
62
|
/**
|
|
37
63
|
* Browser-safe metadata for one declared error of a procedure.
|
|
38
64
|
*
|
|
39
65
|
* @remarks
|
|
40
66
|
* Internal; not exported from a public entry point. Generation writes these entries
|
|
41
|
-
* into each
|
|
67
|
+
* into each route copy's data so the client can recognize the procedure's declared
|
|
42
68
|
* error responses without the server's schemas.
|
|
43
69
|
*/
|
|
44
70
|
export interface ErrorMetadata {
|
|
@@ -188,36 +214,42 @@ type DeclaredError<Errors extends ErrorDefinitions> = {
|
|
|
188
214
|
* @remarks
|
|
189
215
|
* A union discriminated by `code`: one member per framework failure, with
|
|
190
216
|
* {@link InvalidInputData} for `INVALID_INPUT` and `null` data for the others, and one
|
|
191
|
-
* per declared error with its data schema's output type.
|
|
192
|
-
*
|
|
193
|
-
*
|
|
194
|
-
* @example
|
|
195
|
-
* ```ts
|
|
196
|
-
* import type { ClientRpcError } from "@loozo/angular-launch";
|
|
197
|
-
* import type todos from "./rpc";
|
|
198
|
-
*
|
|
199
|
-
* type CompleteError = ClientRpcError<typeof todos.procedures.complete>;
|
|
200
|
-
* ```
|
|
217
|
+
* per declared error with its data schema's output type. Narrow a caught error with the
|
|
218
|
+
* callable's `isError` to get it.
|
|
201
219
|
*
|
|
202
220
|
* @typeParam Value - A procedure created by `query` or `mutation`.
|
|
203
221
|
*/
|
|
204
|
-
export type ClientRpcError<Value> = Value extends Procedure<infer _Input, infer _Output, infer _Kind, infer Errors> ? FrameworkError | DeclaredError<Errors> : never;
|
|
205
|
-
type Options<Kind extends ProcedureKind> = Kind extends "mutation" ? MutationCallOptions : CallOptions;
|
|
206
|
-
type
|
|
222
|
+
export type ClientRpcError<Value> = Value extends Procedure<infer _Input, infer _Output, infer _Kind, infer Errors, infer _Message> ? FrameworkError | DeclaredError<Errors> : never;
|
|
223
|
+
type Options<Kind extends ProcedureKind> = Kind extends "mutation" ? MutationCallOptions : Kind extends "stream" ? StreamCallOptions : Kind extends "channel" ? ChannelCallOptions : CallOptions;
|
|
224
|
+
type Item<Output> = Output extends AsyncIterable<infer Value> ? Value : never;
|
|
225
|
+
type Sent<Message> = Message extends Schema ? InputOf<Message> : never;
|
|
226
|
+
type Result<Output, Kind extends ProcedureKind, Message> = Kind extends "stream" ? AsyncIterable<Item<Output>> : Kind extends "channel" ? Channel<Item<Output>, Sent<Message>> : Promise<Awaited<Output>>;
|
|
227
|
+
type Callable<Input, Output, Kind extends ProcedureKind, Message> = Input extends Schema<infer Caller, unknown> ? (input: Caller, options?: Options<Kind>) => Result<Output, Kind, Message> : (options?: Options<Kind>) => Result<Output, Kind, Message>;
|
|
228
|
+
type InputOption<Input> = Input extends Schema<infer Caller, unknown> ? ConnectInput<Caller> : OptionalConnectInput;
|
|
229
|
+
type Connectable<Input, Output, Kind extends ProcedureKind, Message> = Kind extends "stream" ? {
|
|
230
|
+
/** Follows the stream as signals; see {@link StreamConnector}. */
|
|
231
|
+
readonly connect: StreamConnector<InputOption<Input>, Item<Output>>;
|
|
232
|
+
} : Kind extends "channel" ? {
|
|
233
|
+
/** Runs the channel as signals; see {@link ChannelConnector}. */
|
|
234
|
+
readonly connect: ChannelConnector<InputOption<Input>, Item<Output>, Sent<Message>>;
|
|
235
|
+
} : {};
|
|
207
236
|
/**
|
|
208
237
|
* The generated client callable for a procedure.
|
|
209
238
|
*
|
|
210
239
|
* @remarks
|
|
211
|
-
* The type of `
|
|
240
|
+
* The type of `route.remote.<namespace>.<name>`. A procedure with an input schema is called as
|
|
212
241
|
* `(input, options?)`, where `input` is the schema's input type; one without input is
|
|
213
|
-
* called as `(options?)`.
|
|
214
|
-
*
|
|
215
|
-
*
|
|
242
|
+
* called as `(options?)`. A query or mutation call returns a promise of the handler's
|
|
243
|
+
* awaited result, a stream call an async iterable of its values, and a channel call an open
|
|
244
|
+
* {@link Channel}. Queries take {@link CallOptions}, mutations
|
|
245
|
+
* {@link MutationCallOptions}, streams {@link StreamCallOptions} and channels
|
|
246
|
+
* {@link ChannelCallOptions}. Streams and channels also have `connect()`, which follows them
|
|
247
|
+
* as signals.
|
|
216
248
|
*
|
|
217
|
-
* @typeParam Value - A procedure created by `query` or `
|
|
249
|
+
* @typeParam Value - A procedure created by `query`, `mutation`, `stream` or `channel`.
|
|
218
250
|
*/
|
|
219
|
-
export type ClientProcedure<Value> = Value extends Procedure<infer Input, infer Output, infer Kind, infer Errors> ? Callable<Input,
|
|
220
|
-
/** Whether the procedure is a `"query"` or a `"
|
|
251
|
+
export type ClientProcedure<Value> = Value extends Procedure<infer Input, infer Output, infer Kind, infer Errors, infer Message> ? Callable<Input, Output, Kind, Message> & Connectable<Input, Output, Kind, Message> & {
|
|
252
|
+
/** Whether the procedure is a `"query"`, a `"mutation"`, a `"stream"` or a `"channel"`. */
|
|
221
253
|
readonly kind: Kind;
|
|
222
254
|
/**
|
|
223
255
|
* Whether `error` is an {@link RpcError} returned by this exact procedure.
|
|
@@ -229,25 +261,23 @@ export type ClientProcedure<Value> = Value extends Procedure<infer Input, infer
|
|
|
229
261
|
isError(error: unknown): error is FrameworkError | DeclaredError<Errors>;
|
|
230
262
|
} : never;
|
|
231
263
|
/**
|
|
232
|
-
* The
|
|
264
|
+
* The client for one `.remote()` namespace: one callable per procedure.
|
|
233
265
|
*
|
|
234
266
|
* @remarks
|
|
235
|
-
*
|
|
236
|
-
*
|
|
237
|
-
*
|
|
238
|
-
* group with `import type` so server code stays out of the browser bundle.
|
|
267
|
+
* A route handle's `remote` holds one of these per namespace the route can see, its own and
|
|
268
|
+
* its ancestors'. Read `injectRoute().remote` and call the procedures later, outside an
|
|
269
|
+
* injection context if you like. A route copy exports the whole shape as `Remote`.
|
|
239
270
|
*
|
|
240
271
|
* @example
|
|
241
272
|
* ```ts
|
|
242
|
-
* import type {
|
|
243
|
-
* import type todos from "./rpc";
|
|
273
|
+
* import type { Remote } from "./route.generated";
|
|
244
274
|
*
|
|
245
|
-
* type Todos = Awaited<ReturnType<
|
|
275
|
+
* type Todos = Awaited<ReturnType<Remote["todos"]["list"]>>;
|
|
246
276
|
* ```
|
|
247
277
|
*
|
|
248
|
-
* @typeParam
|
|
278
|
+
* @typeParam Procedures - A namespace's procedures, keyed by name, as `.remote()` returns them.
|
|
249
279
|
*/
|
|
250
|
-
export type Client<
|
|
280
|
+
export type Client<Procedures> = {
|
|
251
281
|
readonly [Key in keyof Procedures]: ClientProcedure<Procedures[Key]>;
|
|
252
|
-
}
|
|
282
|
+
};
|
|
253
283
|
export {};
|
package/connect.d.ts
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
import { type Signal } from "@angular/core";
|
|
2
|
+
import type { Channel } from "./channel.js";
|
|
3
|
+
import type { RpcError } from "./client.js";
|
|
4
|
+
import type { RpcNotFound, RpcRedirect } from "./outcomes.js";
|
|
5
|
+
/**
|
|
6
|
+
* Where a connection is: `"idle"` without input or on the server, `"connecting"` until the
|
|
7
|
+
* server answers, `"open"` while it runs, `"ended"` once the handler ended, or `"failed"`.
|
|
8
|
+
*/
|
|
9
|
+
export type ConnectionStatus = "idle" | "connecting" | "open" | "ended" | "failed";
|
|
10
|
+
/**
|
|
11
|
+
* Why a connection failed: the procedure's `RpcError`, an `RpcRedirect` or an `RpcNotFound`
|
|
12
|
+
* for a failure the server sent, a `TypeError` for a lost or refused connection, or the
|
|
13
|
+
* `Error` `reduce` threw.
|
|
14
|
+
*
|
|
15
|
+
* @remarks
|
|
16
|
+
* Narrow an `RpcError` with the callable's `isError`. A `reduce` that throws something other
|
|
17
|
+
* than an `Error` fails the connection with an `Error` whose `cause` is what it threw.
|
|
18
|
+
*/
|
|
19
|
+
export type ConnectionError = RpcError<string, unknown> | RpcRedirect | RpcNotFound | TypeError | Error;
|
|
20
|
+
/**
|
|
21
|
+
* A stream's connection, created by its callable's `connect()`: its latest state as signals.
|
|
22
|
+
*
|
|
23
|
+
* @typeParam State - The state the values fold into.
|
|
24
|
+
*/
|
|
25
|
+
export interface StreamConnection<State> {
|
|
26
|
+
/**
|
|
27
|
+
* The latest state: `initial()` until the first value arrives, then the latest value or
|
|
28
|
+
* what `reduce` made of it. Kept through failures and reconnects until a new value
|
|
29
|
+
* replaces it.
|
|
30
|
+
*/
|
|
31
|
+
readonly value: Signal<State>;
|
|
32
|
+
/** Where the connection is. */
|
|
33
|
+
readonly status: Signal<ConnectionStatus>;
|
|
34
|
+
/** Why the connection failed, while `status` is `"failed"`; `undefined` otherwise. */
|
|
35
|
+
readonly error: Signal<ConnectionError | undefined>;
|
|
36
|
+
/**
|
|
37
|
+
* Opens a new call after the connection ended or failed, starting from the handler's first
|
|
38
|
+
* value. Does nothing in any other status.
|
|
39
|
+
*/
|
|
40
|
+
reconnect(): void;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* A channel's connection, created by its callable's `connect()`: a stream's connection, plus
|
|
44
|
+
* `send`.
|
|
45
|
+
*
|
|
46
|
+
* @typeParam State - The state the values fold into.
|
|
47
|
+
* @typeParam Sent - The messages the channel's `message` schema accepts.
|
|
48
|
+
*/
|
|
49
|
+
export interface Connection<State, Sent> extends StreamConnection<State> {
|
|
50
|
+
/**
|
|
51
|
+
* Sends one message to the handler.
|
|
52
|
+
*
|
|
53
|
+
* @param message - The message, as strict JSON.
|
|
54
|
+
* @returns A promise that resolves once the message was handed to the socket: messages
|
|
55
|
+
* wait while connecting and for the server's room while open. It rejects with the error
|
|
56
|
+
* `error()` then shows when the connection fails first, also when `reduce` throws; with an
|
|
57
|
+
* `AbortError` when Launch closes the connection first, for a new input, `reconnect()` or
|
|
58
|
+
* the injection context's destruction; and with a `TypeError` while idle, ended or failed.
|
|
59
|
+
*/
|
|
60
|
+
send(message: Sent): Promise<void>;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* The `input` option of a procedure with an input schema.
|
|
64
|
+
*
|
|
65
|
+
* @typeParam Input - The input schema's input type.
|
|
66
|
+
*/
|
|
67
|
+
export interface ConnectInput<Input> {
|
|
68
|
+
/**
|
|
69
|
+
* The call's input, read reactively: a change closes the connection and opens a new one,
|
|
70
|
+
* and `undefined` keeps it closed with status `"idle"`.
|
|
71
|
+
*/
|
|
72
|
+
readonly input: () => Input | undefined;
|
|
73
|
+
}
|
|
74
|
+
/** The `input` option of a procedure without input. */
|
|
75
|
+
export interface OptionalConnectInput {
|
|
76
|
+
/**
|
|
77
|
+
* Whether to connect, read reactively: `null` connects, `undefined` keeps the connection
|
|
78
|
+
* closed with status `"idle"`. Defaults to always connecting.
|
|
79
|
+
*/
|
|
80
|
+
readonly input?: () => null | undefined;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* The options of `connect()` without `reduce`: the state is the latest value.
|
|
84
|
+
*
|
|
85
|
+
* @typeParam Initial - What to show before the first value.
|
|
86
|
+
*/
|
|
87
|
+
export interface ConnectOptions<Initial> {
|
|
88
|
+
/**
|
|
89
|
+
* The state until the first value arrives, read reactively until then; also what the
|
|
90
|
+
* server renders.
|
|
91
|
+
*/
|
|
92
|
+
readonly initial: () => Initial;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* The options of `connect()` with `reduce`: each value folds into the state.
|
|
96
|
+
*
|
|
97
|
+
* @typeParam Value - The values the handler yields.
|
|
98
|
+
* @typeParam State - The state they fold into.
|
|
99
|
+
*/
|
|
100
|
+
export interface ReducingConnectOptions<Value, State> {
|
|
101
|
+
/**
|
|
102
|
+
* The state until the first value arrives, read reactively until then; also what the
|
|
103
|
+
* server renders, and what a new call's first value folds into.
|
|
104
|
+
*/
|
|
105
|
+
readonly initial: () => State;
|
|
106
|
+
/**
|
|
107
|
+
* Folds one value into the state. Runs untracked and must be pure; what it throws fails
|
|
108
|
+
* the connection.
|
|
109
|
+
*
|
|
110
|
+
* @param state - The current state.
|
|
111
|
+
* @param value - The value that arrived.
|
|
112
|
+
* @returns The new state.
|
|
113
|
+
*/
|
|
114
|
+
readonly reduce: (state: State, value: Value) => State;
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* A stream callable's `connect()`.
|
|
118
|
+
*
|
|
119
|
+
* @typeParam InputOption - The `input` option the stream takes.
|
|
120
|
+
* @typeParam Value - The values its handler yields.
|
|
121
|
+
*/
|
|
122
|
+
export interface StreamConnector<InputOption, Value> {
|
|
123
|
+
/**
|
|
124
|
+
* Follows the stream as signals, folding each value into the state with `reduce`.
|
|
125
|
+
*
|
|
126
|
+
* @remarks
|
|
127
|
+
* Call it in an injection context, such as a field initializer. It connects in the browser
|
|
128
|
+
* after the next render, never on the server, and never keeps the app from becoming
|
|
129
|
+
* stable. Destroying the injection context closes the connection.
|
|
130
|
+
*
|
|
131
|
+
* @param options - The input, the initial state and the fold.
|
|
132
|
+
* @returns The connection.
|
|
133
|
+
*/
|
|
134
|
+
<State>(options: InputOption & ReducingConnectOptions<Value, State>): StreamConnection<State>;
|
|
135
|
+
/**
|
|
136
|
+
* Follows the stream as signals: the state is the latest value.
|
|
137
|
+
*
|
|
138
|
+
* @remarks
|
|
139
|
+
* As the `reduce` form.
|
|
140
|
+
*
|
|
141
|
+
* @param options - The input and what to show before the first value.
|
|
142
|
+
* @returns The connection.
|
|
143
|
+
*/
|
|
144
|
+
<Initial>(options: InputOption & ConnectOptions<Initial>): StreamConnection<Value | Initial>;
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* A channel callable's `connect()`.
|
|
148
|
+
*
|
|
149
|
+
* @typeParam InputOption - The `input` option the channel takes.
|
|
150
|
+
* @typeParam Value - The values its handler yields.
|
|
151
|
+
* @typeParam Sent - The messages its `message` schema accepts.
|
|
152
|
+
*/
|
|
153
|
+
export interface ChannelConnector<InputOption, Value, Sent> {
|
|
154
|
+
/**
|
|
155
|
+
* Runs the channel as signals, folding each value into the state with `reduce`.
|
|
156
|
+
*
|
|
157
|
+
* @remarks
|
|
158
|
+
* Call it in an injection context, such as a field initializer. It connects in the browser
|
|
159
|
+
* after the next render, never on the server, and never keeps the app from becoming
|
|
160
|
+
* stable. Destroying the injection context closes the connection. It reads every value as
|
|
161
|
+
* it arrives, so sending never stalls it.
|
|
162
|
+
*
|
|
163
|
+
* @param options - The input, the initial state and the fold.
|
|
164
|
+
* @returns The connection.
|
|
165
|
+
*/
|
|
166
|
+
<State>(options: InputOption & ReducingConnectOptions<Value, State>): Connection<State, Sent>;
|
|
167
|
+
/**
|
|
168
|
+
* Runs the channel as signals: the state is the latest value.
|
|
169
|
+
*
|
|
170
|
+
* @remarks
|
|
171
|
+
* As the `reduce` form.
|
|
172
|
+
*
|
|
173
|
+
* @param options - The input and what to show before the first value.
|
|
174
|
+
* @returns The connection.
|
|
175
|
+
*/
|
|
176
|
+
<Initial>(options: InputOption & ConnectOptions<Initial>): Connection<Value | Initial, Sent>;
|
|
177
|
+
}
|
|
178
|
+
/** What `connect` opens one call with. Internal. */
|
|
179
|
+
export type Opener = (input: unknown, signal: AbortSignal, opened: () => void) => AsyncIterable<unknown> | Channel<unknown, unknown>;
|
|
180
|
+
interface RuntimeOptions {
|
|
181
|
+
readonly input?: () => unknown;
|
|
182
|
+
readonly initial: () => unknown;
|
|
183
|
+
readonly reduce?: (state: unknown, value: unknown) => unknown;
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* Follows a stream or channel as signals. Internal: generated callables expose it as
|
|
187
|
+
* `connect()`.
|
|
188
|
+
*
|
|
189
|
+
* @remarks
|
|
190
|
+
* Must run in an injection context. On the server it never connects: status stays `"idle"`
|
|
191
|
+
* and `value` is `initial()`. In the browser it connects after the next render, and again
|
|
192
|
+
* whenever `input` changes; each connection reads every value as it arrives. A connection
|
|
193
|
+
* closed for a new input or by the injection context's destruction doesn't change `status`
|
|
194
|
+
* or `error`.
|
|
195
|
+
*
|
|
196
|
+
* @param open - Opens one call.
|
|
197
|
+
* @param options - The `connect()` options.
|
|
198
|
+
* @returns The connection; `send` delegates to the open channel.
|
|
199
|
+
*/
|
|
200
|
+
export declare function connect(open: Opener, { input, initial, reduce }: RuntimeOptions): Connection<unknown, unknown>;
|
|
201
|
+
export {};
|