@loozo/angular-launch 0.0.0-stage → 0.0.2
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/LICENSE +21 -0
- package/README.md +546 -2
- package/client.d.ts +232 -0
- package/client.js +169 -0
- package/generated.d.ts +13 -0
- package/generated.js +90 -0
- package/generation/index.d.ts +116 -0
- package/generation/index.js +634 -0
- package/index.d.ts +2 -0
- package/index.js +1 -0
- package/json.d.ts +13 -0
- package/json.js +48 -0
- package/package.json +48 -4
- package/server/backend.d.ts +58 -0
- package/server/backend.js +166 -0
- package/server/definitions.d.ts +486 -0
- package/server/definitions.js +170 -0
- package/server/dispatch.d.ts +140 -0
- package/server/dispatch.js +249 -0
- package/server/errors.d.ts +21 -0
- package/server/errors.js +32 -0
- package/server/generated.d.ts +2 -0
- package/server/generated.js +3 -0
- package/server/index.d.ts +6 -0
- package/server/index.js +2 -0
package/client.d.ts
ADDED
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
import type { ErrorDefinitions, Group, OutputOf, Procedure, ProcedureKind, Schema } from "./server/definitions.js";
|
|
2
|
+
/** Per-call options for a generated query callable. */
|
|
3
|
+
export interface CallOptions {
|
|
4
|
+
/**
|
|
5
|
+
* Aborts the call: the HTTP request is cancelled and the promise rejects with a
|
|
6
|
+
* `DOMException` named `AbortError`.
|
|
7
|
+
*
|
|
8
|
+
* @remarks
|
|
9
|
+
* An already aborted signal rejects without sending a request. Aborting doesn't
|
|
10
|
+
* cancel server work that has already started.
|
|
11
|
+
*/
|
|
12
|
+
readonly signal?: AbortSignal;
|
|
13
|
+
/**
|
|
14
|
+
* Whether Angular's HTTP transfer cache may store this call's response during server
|
|
15
|
+
* rendering and reuse it during hydration. Defaults to `true`.
|
|
16
|
+
*/
|
|
17
|
+
readonly transferCache?: boolean;
|
|
18
|
+
}
|
|
19
|
+
/** Per-call options for a generated mutation callable. */
|
|
20
|
+
export interface MutationCallOptions {
|
|
21
|
+
/**
|
|
22
|
+
* Aborts the call: the HTTP request is cancelled and the promise rejects with a
|
|
23
|
+
* `DOMException` named `AbortError`.
|
|
24
|
+
*
|
|
25
|
+
* @remarks
|
|
26
|
+
* Aborting doesn't cancel server work that has already started, so the write may
|
|
27
|
+
* still complete.
|
|
28
|
+
*/
|
|
29
|
+
readonly signal?: AbortSignal;
|
|
30
|
+
/** Only `false` is accepted: mutations never use the HTTP transfer cache. */
|
|
31
|
+
readonly transferCache?: false;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Browser-safe metadata for one declared error of a procedure.
|
|
35
|
+
*
|
|
36
|
+
* @remarks
|
|
37
|
+
* Internal; not exported from a public entry point. Generation writes these entries
|
|
38
|
+
* into each `rpc.generated.ts` so the client can recognize the procedure's declared
|
|
39
|
+
* error responses without the server's schemas.
|
|
40
|
+
*/
|
|
41
|
+
export interface ErrorMetadata {
|
|
42
|
+
/** The declared error code, such as `"NOT_FOUND"`. */
|
|
43
|
+
readonly code: string;
|
|
44
|
+
/** The HTTP status the error is sent with, from 400 to 599. */
|
|
45
|
+
readonly status: number;
|
|
46
|
+
/** The declared public message sent with every instance of the error. */
|
|
47
|
+
readonly message: string;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* The four error codes Launch itself returns, with their HTTP status and public message.
|
|
51
|
+
*
|
|
52
|
+
* @remarks
|
|
53
|
+
* - `INVALID_INPUT` (400): the request or its input failed validation.
|
|
54
|
+
* - `FORBIDDEN` (403): a guard returned `false`, or a mutation was called during
|
|
55
|
+
* server rendering.
|
|
56
|
+
* - `RPC_NOT_FOUND` (404): no procedure exists at the requested path.
|
|
57
|
+
* - `INTERNAL` (500): anything else. Details go to the server log, never to the client.
|
|
58
|
+
*
|
|
59
|
+
* These codes are reserved: procedures can't declare errors with them.
|
|
60
|
+
*/
|
|
61
|
+
export declare const frameworkFailures: {
|
|
62
|
+
readonly INVALID_INPUT: {
|
|
63
|
+
readonly status: 400;
|
|
64
|
+
readonly message: "Invalid RPC input";
|
|
65
|
+
};
|
|
66
|
+
readonly FORBIDDEN: {
|
|
67
|
+
readonly status: 403;
|
|
68
|
+
readonly message: "RPC forbidden";
|
|
69
|
+
};
|
|
70
|
+
readonly RPC_NOT_FOUND: {
|
|
71
|
+
readonly status: 404;
|
|
72
|
+
readonly message: "RPC not found";
|
|
73
|
+
};
|
|
74
|
+
readonly INTERNAL: {
|
|
75
|
+
readonly status: 500;
|
|
76
|
+
readonly message: "Internal server error";
|
|
77
|
+
};
|
|
78
|
+
};
|
|
79
|
+
/** One of the error codes Launch itself returns: a key of {@link frameworkFailures}. */
|
|
80
|
+
export type FrameworkCode = keyof typeof frameworkFailures;
|
|
81
|
+
/**
|
|
82
|
+
* An error response returned by a procedure: either a Launch framework failure or one
|
|
83
|
+
* of the procedure's declared errors.
|
|
84
|
+
*
|
|
85
|
+
* @remarks
|
|
86
|
+
* Generated callables reject with an `RpcError` only for a response that exactly
|
|
87
|
+
* matches a framework failure or one of that procedure's declared errors. Network
|
|
88
|
+
* failures, aborts and unrecognized responses reject with the original error instead.
|
|
89
|
+
* Use the callable's `isError` to narrow a caught value: it recognizes only errors
|
|
90
|
+
* returned by that exact procedure, not `RpcError` instances created elsewhere.
|
|
91
|
+
*
|
|
92
|
+
* @typeParam Code - The error code.
|
|
93
|
+
* @typeParam Data - The error's data: `null` for framework failures, the data schema's
|
|
94
|
+
* output for declared errors.
|
|
95
|
+
*/
|
|
96
|
+
export declare class RpcError<Code extends string = FrameworkCode, Data = null> extends Error {
|
|
97
|
+
/** The framework or declared error code. */
|
|
98
|
+
readonly code: Code;
|
|
99
|
+
/** The HTTP status of the response. */
|
|
100
|
+
readonly status: number;
|
|
101
|
+
/** The error's data: `null` for framework failures, validated data for declared errors. */
|
|
102
|
+
readonly data: Data;
|
|
103
|
+
/**
|
|
104
|
+
* Creates an error with the given code, status and data.
|
|
105
|
+
*
|
|
106
|
+
* @param code - The error code.
|
|
107
|
+
* @param status - The HTTP status of the response.
|
|
108
|
+
* @param data - The error's data. Defaults to `null`.
|
|
109
|
+
* @param message - The public message. Defaults to the framework message when `code`
|
|
110
|
+
* is a {@link FrameworkCode}.
|
|
111
|
+
* @throws TypeError when `message` is omitted and `code` isn't a framework code.
|
|
112
|
+
*/
|
|
113
|
+
constructor(
|
|
114
|
+
/** The framework or declared error code. */
|
|
115
|
+
code: Code,
|
|
116
|
+
/** The HTTP status of the response. */
|
|
117
|
+
status: number,
|
|
118
|
+
/** The error's data: `null` for framework failures, validated data for declared errors. */
|
|
119
|
+
data?: Data, message?: string);
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Recognizes a response body that is exactly a Launch framework failure.
|
|
123
|
+
*
|
|
124
|
+
* @remarks
|
|
125
|
+
* Internal; not exported from a public entry point. Used by the generated client to
|
|
126
|
+
* convert responses into {@link RpcError}s. The body must be strict JSON shaped
|
|
127
|
+
* exactly as `{ error: { code, message, data: null } }`, with the framework code's own
|
|
128
|
+
* status and message.
|
|
129
|
+
*
|
|
130
|
+
* @param value - The parsed response body.
|
|
131
|
+
* @param status - The response's HTTP status.
|
|
132
|
+
* @returns The framework code, or `undefined` when the body doesn't match exactly.
|
|
133
|
+
*/
|
|
134
|
+
export declare function recognizedFailure(value: unknown, status: number): FrameworkCode | undefined;
|
|
135
|
+
/**
|
|
136
|
+
* Recognizes a response body that is exactly a framework failure or one of the given
|
|
137
|
+
* declared errors.
|
|
138
|
+
*
|
|
139
|
+
* @remarks
|
|
140
|
+
* Internal; not exported from a public entry point. Used by the generated client to
|
|
141
|
+
* convert responses into {@link RpcError}s. A declared error matches only when its
|
|
142
|
+
* code, status and message all equal one entry of `declared`. Entries using framework
|
|
143
|
+
* codes are ignored.
|
|
144
|
+
*
|
|
145
|
+
* @param value - The parsed response body.
|
|
146
|
+
* @param status - The response's HTTP status.
|
|
147
|
+
* @param declared - The procedure's declared error metadata.
|
|
148
|
+
* @returns The recognized code, message and data, or `undefined` when the body doesn't
|
|
149
|
+
* match exactly.
|
|
150
|
+
*/
|
|
151
|
+
export declare function recognizedError(value: unknown, status: number, declared: readonly ErrorMetadata[]): {
|
|
152
|
+
/** The framework or declared error code. */
|
|
153
|
+
code: string;
|
|
154
|
+
/** The error's message. */
|
|
155
|
+
message: string;
|
|
156
|
+
/** The declared error's data as received, or `null` for a framework failure. */
|
|
157
|
+
data: unknown;
|
|
158
|
+
} | undefined;
|
|
159
|
+
type FrameworkError = {
|
|
160
|
+
[Code in FrameworkCode]: RpcError<Code, null>;
|
|
161
|
+
}[FrameworkCode];
|
|
162
|
+
type DeclaredError<Errors extends ErrorDefinitions> = {
|
|
163
|
+
[Code in keyof Errors & string]: RpcError<Code, OutputOf<Errors[Code]["data"]>>;
|
|
164
|
+
}[keyof Errors & string];
|
|
165
|
+
/**
|
|
166
|
+
* The errors a generated callable for a procedure can reject with as {@link RpcError}s.
|
|
167
|
+
*
|
|
168
|
+
* @remarks
|
|
169
|
+
* A union discriminated by `code`: one member per framework failure with `null` data,
|
|
170
|
+
* and one per declared error with its data schema's output type. Import the group
|
|
171
|
+
* with `import type` so server code stays out of the browser bundle.
|
|
172
|
+
*
|
|
173
|
+
* @example
|
|
174
|
+
* ```ts
|
|
175
|
+
* import type { ClientRpcError } from "@loozo/angular-launch";
|
|
176
|
+
* import type todos from "./rpc";
|
|
177
|
+
*
|
|
178
|
+
* type CompleteError = ClientRpcError<typeof todos.procedures.complete>;
|
|
179
|
+
* ```
|
|
180
|
+
*
|
|
181
|
+
* @typeParam Value - A procedure created by `query` or `mutation`.
|
|
182
|
+
*/
|
|
183
|
+
export type ClientRpcError<Value> = Value extends Procedure<infer _Input, infer _Output, infer _Kind, infer Errors> ? FrameworkError | DeclaredError<Errors> : never;
|
|
184
|
+
type Options<Kind extends ProcedureKind> = Kind extends "mutation" ? MutationCallOptions : CallOptions;
|
|
185
|
+
type Callable<Input, Output, Kind extends ProcedureKind> = Input extends Schema<infer Caller, unknown> ? (input: Caller, options?: Options<Kind>) => Promise<Output> : (options?: Options<Kind>) => Promise<Output>;
|
|
186
|
+
/**
|
|
187
|
+
* The generated client callable for a procedure.
|
|
188
|
+
*
|
|
189
|
+
* @remarks
|
|
190
|
+
* The type of `Client<typeof group>["name"]`. A procedure with an input schema is called as
|
|
191
|
+
* `(input, options?)`, where `input` is the schema's input type; one without input is
|
|
192
|
+
* called as `(options?)`. Every call
|
|
193
|
+
* returns a promise of the handler's awaited result. Queries take {@link CallOptions}
|
|
194
|
+
* and mutations take {@link MutationCallOptions}.
|
|
195
|
+
*
|
|
196
|
+
* @typeParam Value - A procedure created by `query` or `mutation`.
|
|
197
|
+
*/
|
|
198
|
+
export type ClientProcedure<Value> = Value extends Procedure<infer Input, infer Output, infer Kind, infer Errors> ? Callable<Input, Awaited<Output>, Kind> & {
|
|
199
|
+
/** Whether the procedure is a `"query"` or a `"mutation"`. */
|
|
200
|
+
readonly kind: Kind;
|
|
201
|
+
/**
|
|
202
|
+
* Whether `error` is an {@link RpcError} returned by this exact procedure.
|
|
203
|
+
*
|
|
204
|
+
* @param error - A value caught from a call.
|
|
205
|
+
* @returns `true` only for errors this procedure's calls rejected with, narrowed to
|
|
206
|
+
* its framework failures and declared errors.
|
|
207
|
+
*/
|
|
208
|
+
isError(error: unknown): error is FrameworkError | DeclaredError<Errors>;
|
|
209
|
+
} : never;
|
|
210
|
+
/**
|
|
211
|
+
* The generated client for an RPC group: one callable per procedure.
|
|
212
|
+
*
|
|
213
|
+
* @remarks
|
|
214
|
+
* Generated `injectRpc()` functions return an object of these, keyed by namespace.
|
|
215
|
+
* Call `injectRpc()` in an Angular injection context where `HttpClient` is provided;
|
|
216
|
+
* the clients it returns can be called later, outside an injection context. Import the
|
|
217
|
+
* group with `import type` so server code stays out of the browser bundle.
|
|
218
|
+
*
|
|
219
|
+
* @example
|
|
220
|
+
* ```ts
|
|
221
|
+
* import type { Client } from "@loozo/angular-launch";
|
|
222
|
+
* import type todos from "./rpc";
|
|
223
|
+
*
|
|
224
|
+
* type Todos = Awaited<ReturnType<Client<typeof todos>["list"]>>;
|
|
225
|
+
* ```
|
|
226
|
+
*
|
|
227
|
+
* @typeParam Value - A group created by `defineRpc`.
|
|
228
|
+
*/
|
|
229
|
+
export type Client<Value> = Value extends Group<string, infer Procedures> ? {
|
|
230
|
+
readonly [Key in keyof Procedures]: ClientProcedure<Procedures[Key]>;
|
|
231
|
+
} : never;
|
|
232
|
+
export {};
|
package/client.js
ADDED
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import { serializeJson } from "./json.js";
|
|
2
|
+
/**
|
|
3
|
+
* The four error codes Launch itself returns, with their HTTP status and public message.
|
|
4
|
+
*
|
|
5
|
+
* @remarks
|
|
6
|
+
* - `INVALID_INPUT` (400): the request or its input failed validation.
|
|
7
|
+
* - `FORBIDDEN` (403): a guard returned `false`, or a mutation was called during
|
|
8
|
+
* server rendering.
|
|
9
|
+
* - `RPC_NOT_FOUND` (404): no procedure exists at the requested path.
|
|
10
|
+
* - `INTERNAL` (500): anything else. Details go to the server log, never to the client.
|
|
11
|
+
*
|
|
12
|
+
* These codes are reserved: procedures can't declare errors with them.
|
|
13
|
+
*/
|
|
14
|
+
export const frameworkFailures = {
|
|
15
|
+
INVALID_INPUT: { status: 400, message: "Invalid RPC input" },
|
|
16
|
+
FORBIDDEN: { status: 403, message: "RPC forbidden" },
|
|
17
|
+
RPC_NOT_FOUND: { status: 404, message: "RPC not found" },
|
|
18
|
+
INTERNAL: { status: 500, message: "Internal server error" },
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* An error response returned by a procedure: either a Launch framework failure or one
|
|
22
|
+
* of the procedure's declared errors.
|
|
23
|
+
*
|
|
24
|
+
* @remarks
|
|
25
|
+
* Generated callables reject with an `RpcError` only for a response that exactly
|
|
26
|
+
* matches a framework failure or one of that procedure's declared errors. Network
|
|
27
|
+
* failures, aborts and unrecognized responses reject with the original error instead.
|
|
28
|
+
* Use the callable's `isError` to narrow a caught value: it recognizes only errors
|
|
29
|
+
* returned by that exact procedure, not `RpcError` instances created elsewhere.
|
|
30
|
+
*
|
|
31
|
+
* @typeParam Code - The error code.
|
|
32
|
+
* @typeParam Data - The error's data: `null` for framework failures, the data schema's
|
|
33
|
+
* output for declared errors.
|
|
34
|
+
*/
|
|
35
|
+
export class RpcError extends Error {
|
|
36
|
+
code;
|
|
37
|
+
status;
|
|
38
|
+
data;
|
|
39
|
+
/**
|
|
40
|
+
* Creates an error with the given code, status and data.
|
|
41
|
+
*
|
|
42
|
+
* @param code - The error code.
|
|
43
|
+
* @param status - The HTTP status of the response.
|
|
44
|
+
* @param data - The error's data. Defaults to `null`.
|
|
45
|
+
* @param message - The public message. Defaults to the framework message when `code`
|
|
46
|
+
* is a {@link FrameworkCode}.
|
|
47
|
+
* @throws TypeError when `message` is omitted and `code` isn't a framework code.
|
|
48
|
+
*/
|
|
49
|
+
constructor(
|
|
50
|
+
/** The framework or declared error code. */
|
|
51
|
+
code,
|
|
52
|
+
/** The HTTP status of the response. */
|
|
53
|
+
status,
|
|
54
|
+
/** The error's data: `null` for framework failures, validated data for declared errors. */
|
|
55
|
+
data = null, message) {
|
|
56
|
+
if (message === undefined) {
|
|
57
|
+
if (!Object.hasOwn(frameworkFailures, code)) {
|
|
58
|
+
throw new TypeError("Declared RPC errors require their public message");
|
|
59
|
+
}
|
|
60
|
+
message = frameworkFailures[code].message;
|
|
61
|
+
}
|
|
62
|
+
super(message);
|
|
63
|
+
this.code = code;
|
|
64
|
+
this.status = status;
|
|
65
|
+
this.data = data;
|
|
66
|
+
this.name = "RpcError";
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Recognizes a response body that is exactly a Launch framework failure.
|
|
71
|
+
*
|
|
72
|
+
* @remarks
|
|
73
|
+
* Internal; not exported from a public entry point. Used by the generated client to
|
|
74
|
+
* convert responses into {@link RpcError}s. The body must be strict JSON shaped
|
|
75
|
+
* exactly as `{ error: { code, message, data: null } }`, with the framework code's own
|
|
76
|
+
* status and message.
|
|
77
|
+
*
|
|
78
|
+
* @param value - The parsed response body.
|
|
79
|
+
* @param status - The response's HTTP status.
|
|
80
|
+
* @returns The framework code, or `undefined` when the body doesn't match exactly.
|
|
81
|
+
*/
|
|
82
|
+
export function recognizedFailure(value, status) {
|
|
83
|
+
try {
|
|
84
|
+
serializeJson(value);
|
|
85
|
+
}
|
|
86
|
+
catch {
|
|
87
|
+
return undefined;
|
|
88
|
+
}
|
|
89
|
+
if (typeof value !== "object" ||
|
|
90
|
+
value === null ||
|
|
91
|
+
!("error" in value) ||
|
|
92
|
+
Object.keys(value).length !== 1) {
|
|
93
|
+
return undefined;
|
|
94
|
+
}
|
|
95
|
+
const error = value.error;
|
|
96
|
+
if (typeof error !== "object" ||
|
|
97
|
+
error === null ||
|
|
98
|
+
!("code" in error) ||
|
|
99
|
+
!("message" in error) ||
|
|
100
|
+
!("data" in error) ||
|
|
101
|
+
Object.keys(error).length !== 3 ||
|
|
102
|
+
typeof error.code !== "string") {
|
|
103
|
+
return undefined;
|
|
104
|
+
}
|
|
105
|
+
for (const code of Object.keys(frameworkFailures)) {
|
|
106
|
+
const expected = frameworkFailures[code];
|
|
107
|
+
if (error.code === code &&
|
|
108
|
+
status === expected.status &&
|
|
109
|
+
error.message === expected.message &&
|
|
110
|
+
error.data === null) {
|
|
111
|
+
return code;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
return undefined;
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Recognizes a response body that is exactly a framework failure or one of the given
|
|
118
|
+
* declared errors.
|
|
119
|
+
*
|
|
120
|
+
* @remarks
|
|
121
|
+
* Internal; not exported from a public entry point. Used by the generated client to
|
|
122
|
+
* convert responses into {@link RpcError}s. A declared error matches only when its
|
|
123
|
+
* code, status and message all equal one entry of `declared`. Entries using framework
|
|
124
|
+
* codes are ignored.
|
|
125
|
+
*
|
|
126
|
+
* @param value - The parsed response body.
|
|
127
|
+
* @param status - The response's HTTP status.
|
|
128
|
+
* @param declared - The procedure's declared error metadata.
|
|
129
|
+
* @returns The recognized code, message and data, or `undefined` when the body doesn't
|
|
130
|
+
* match exactly.
|
|
131
|
+
*/
|
|
132
|
+
export function recognizedError(value, status, declared) {
|
|
133
|
+
const framework = recognizedFailure(value, status);
|
|
134
|
+
if (framework) {
|
|
135
|
+
return { code: framework, message: frameworkFailures[framework].message, data: null };
|
|
136
|
+
}
|
|
137
|
+
try {
|
|
138
|
+
serializeJson(value);
|
|
139
|
+
}
|
|
140
|
+
catch {
|
|
141
|
+
return undefined;
|
|
142
|
+
}
|
|
143
|
+
if (typeof value !== "object" ||
|
|
144
|
+
value === null ||
|
|
145
|
+
!("error" in value) ||
|
|
146
|
+
Object.keys(value).length !== 1) {
|
|
147
|
+
return undefined;
|
|
148
|
+
}
|
|
149
|
+
const error = value.error;
|
|
150
|
+
if (typeof error !== "object" ||
|
|
151
|
+
error === null ||
|
|
152
|
+
!("code" in error) ||
|
|
153
|
+
!("message" in error) ||
|
|
154
|
+
!("data" in error) ||
|
|
155
|
+
Object.keys(error).length !== 3 ||
|
|
156
|
+
typeof error.code !== "string" ||
|
|
157
|
+
typeof error.message !== "string") {
|
|
158
|
+
return undefined;
|
|
159
|
+
}
|
|
160
|
+
for (const definition of declared) {
|
|
161
|
+
if (!Object.hasOwn(frameworkFailures, definition.code) &&
|
|
162
|
+
definition.code === error.code &&
|
|
163
|
+
definition.status === status &&
|
|
164
|
+
definition.message === error.message) {
|
|
165
|
+
return { code: error.code, message: error.message, data: error.data };
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
return undefined;
|
|
169
|
+
}
|
package/generated.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { HttpContextToken } from "@angular/common/http";
|
|
2
|
+
import { type ErrorMetadata } from "./client.js";
|
|
3
|
+
export declare const RPC_CALL: HttpContextToken<boolean>;
|
|
4
|
+
export interface ClientMethod {
|
|
5
|
+
readonly name: string;
|
|
6
|
+
readonly input: boolean;
|
|
7
|
+
readonly kind: "query" | "mutation";
|
|
8
|
+
readonly errors: readonly ErrorMetadata[];
|
|
9
|
+
}
|
|
10
|
+
export declare function injectClient(groups: Readonly<Record<string, {
|
|
11
|
+
readonly id: string;
|
|
12
|
+
readonly methods: readonly ClientMethod[];
|
|
13
|
+
}>>): unknown;
|
package/generated.js
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
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, group.id, method, input, options);
|
|
72
|
+
}
|
|
73
|
+
else {
|
|
74
|
+
invoke = (options) => call(http, group.id, 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 === group.id && owner.method === method.name;
|
|
84
|
+
},
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
result[namespace] = methods;
|
|
88
|
+
}
|
|
89
|
+
return result;
|
|
90
|
+
}
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
/** Options for {@link generate} and {@link createGenerationSession}. */
|
|
2
|
+
export interface GenerationOptions {
|
|
3
|
+
/** The application's tsconfig, relative to the workspace. It must enable strict checking. */
|
|
4
|
+
tsconfig: string;
|
|
5
|
+
/** Exactly one route directory, relative to the tsconfig's directory. */
|
|
6
|
+
scanDirectories: string[];
|
|
7
|
+
/** Where the application-wide generated files go, relative to the workspace. */
|
|
8
|
+
outDirectory: string;
|
|
9
|
+
/**
|
|
10
|
+
* Verifies the generated files without writing them, failing when any is changed,
|
|
11
|
+
* missing or stale. Defaults to `false`.
|
|
12
|
+
*/
|
|
13
|
+
check?: boolean;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* A generation session created by {@link createGenerationSession}, reusing compiler
|
|
17
|
+
* work between calls.
|
|
18
|
+
*/
|
|
19
|
+
export interface GenerationSession {
|
|
20
|
+
/**
|
|
21
|
+
* Generates or checks the application's files, like {@link generate} with the
|
|
22
|
+
* session's options.
|
|
23
|
+
*
|
|
24
|
+
* @remarks
|
|
25
|
+
* Calls must be sequential. A failed call doesn't break the session; the next call
|
|
26
|
+
* starts clean.
|
|
27
|
+
*
|
|
28
|
+
* @param options - `check`: verify without writing. Defaults to `false`.
|
|
29
|
+
* @param stopped - Called once just before writing; returning `true` skips writing and
|
|
30
|
+
* resolves. Defaults to never stopping.
|
|
31
|
+
* @returns A promise that resolves when generation finishes.
|
|
32
|
+
* @throws Rejects when the session is disposed, when another call hasn't settled yet,
|
|
33
|
+
* and for the same failures as {@link generate}.
|
|
34
|
+
*/
|
|
35
|
+
generate(options?: Pick<GenerationOptions, "check">, stopped?: () => boolean): Promise<void>;
|
|
36
|
+
/**
|
|
37
|
+
* Disposes the session: stops an active call before it writes, waits for it to settle
|
|
38
|
+
* and makes later calls reject.
|
|
39
|
+
*
|
|
40
|
+
* @returns A promise that resolves once any active call has settled.
|
|
41
|
+
*/
|
|
42
|
+
dispose(): Promise<void>;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Generates the router's and Launch's files for an application, or checks them.
|
|
46
|
+
*
|
|
47
|
+
* @remarks
|
|
48
|
+
* Node-only. Generation reads your sources without running them. Besides the router's
|
|
49
|
+
* files, it writes an `rpc.generated.ts` beside each route that gets a
|
|
50
|
+
* `route.generated.ts`, and `context.generated.d.ts` and `server.generated.ts` in the
|
|
51
|
+
* output directory. Run it before the Angular build.
|
|
52
|
+
*
|
|
53
|
+
* Everything is validated, including a type check of the generated files with
|
|
54
|
+
* `skipLibCheck: false`, before anything is written, so a failed run changes nothing.
|
|
55
|
+
* Files are then written one at a time without rollback; a write error partway through
|
|
56
|
+
* can leave some files updated, which the next successful run repairs. Generation only
|
|
57
|
+
* replaces or removes files carrying its ownership marker, and removes stale ones.
|
|
58
|
+
*
|
|
59
|
+
* @example
|
|
60
|
+
* ```js
|
|
61
|
+
* import { generate } from "@loozo/angular-launch/generation";
|
|
62
|
+
*
|
|
63
|
+
* await generate(import.meta.dirname, {
|
|
64
|
+
* tsconfig: "tsconfig.app.json",
|
|
65
|
+
* scanDirectories: ["src/routes"],
|
|
66
|
+
* outDirectory: "generated",
|
|
67
|
+
* check: process.argv.includes("--check"),
|
|
68
|
+
* });
|
|
69
|
+
* ```
|
|
70
|
+
*
|
|
71
|
+
* @param workspace - The absolute path of the application directory, which contains
|
|
72
|
+
* its `package.json`. The tsconfig, scanned directory, sources and output directory
|
|
73
|
+
* must all be inside it.
|
|
74
|
+
* @param options - The generation options.
|
|
75
|
+
* @param stopped - Called once just before writing; returning `true` skips writing and
|
|
76
|
+
* resolves. Defaults to never stopping.
|
|
77
|
+
* @returns A promise that resolves when generation finishes.
|
|
78
|
+
* @throws Rejects when a path escapes the workspace, the tsconfig disables strict
|
|
79
|
+
* checking or enables `noCheck`, the application's `package.json` has no `name`, the
|
|
80
|
+
* sources or generated files fail validation, a file to replace lacks the ownership
|
|
81
|
+
* marker, or, with `check`, a generated file is out of date.
|
|
82
|
+
*/
|
|
83
|
+
export declare function generate(workspace: string, options: GenerationOptions, stopped?: () => boolean): Promise<void>;
|
|
84
|
+
/**
|
|
85
|
+
* Creates a generation session that reuses compiler work between sequential calls.
|
|
86
|
+
*
|
|
87
|
+
* @remarks
|
|
88
|
+
* Node-only. For long-running tools that regenerate repeatedly, such as after file
|
|
89
|
+
* changes. A session doesn't watch files; you decide when to call `generate`. Edits
|
|
90
|
+
* inside the application are picked up by the next call. Create a new session after
|
|
91
|
+
* moving the application directory or changing dependencies. Don't run two sessions or
|
|
92
|
+
* generators against the same output at once.
|
|
93
|
+
*
|
|
94
|
+
* @example
|
|
95
|
+
* ```js
|
|
96
|
+
* const session = createGenerationSession(workspace, {
|
|
97
|
+
* tsconfig: "tsconfig.app.json",
|
|
98
|
+
* scanDirectories: ["src/routes"],
|
|
99
|
+
* outDirectory: "generated",
|
|
100
|
+
* });
|
|
101
|
+
*
|
|
102
|
+
* try {
|
|
103
|
+
* await session.generate();
|
|
104
|
+
* await session.generate({ check: true });
|
|
105
|
+
* } finally {
|
|
106
|
+
* await session.dispose();
|
|
107
|
+
* }
|
|
108
|
+
* ```
|
|
109
|
+
*
|
|
110
|
+
* @param workspace - The absolute path of the application directory, as for
|
|
111
|
+
* {@link generate}.
|
|
112
|
+
* @param options - The generation options, fixed for the session's lifetime; `check` is
|
|
113
|
+
* passed per call instead.
|
|
114
|
+
* @returns The session. Dispose it when done.
|
|
115
|
+
*/
|
|
116
|
+
export declare function createGenerationSession(workspace: string, { tsconfig, scanDirectories, outDirectory }: Omit<GenerationOptions, "check">): GenerationSession;
|