@loozo/angular-launch 0.0.2 → 0.0.3
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 +12 -12
- package/generated.d.ts +0 -1
- package/generated.js +3 -3
- package/generation/index.js +8 -10
- package/package.json +2 -2
- package/server/definitions.d.ts +2 -1
- package/server/definitions.js +2 -1
- package/server/dispatch.d.ts +2 -2
- package/server/dispatch.js +2 -2
package/README.md
CHANGED
|
@@ -1,24 +1,24 @@
|
|
|
1
1
|
# @loozo/angular-launch
|
|
2
2
|
|
|
3
|
-
Typed server functions for Angular applications built with [`@loozo/angular-router`](
|
|
3
|
+
Typed server functions for Angular applications built with [`@loozo/angular-router`](https://github.com/theo-matzavinos/loozo/blob/angular-launch@0.0.3/packages/angular-router/README.md). You define queries and mutations in an `rpc.ts` file next to a route, and call them from that route's pages and resolvers through a generated, fully typed client.
|
|
4
4
|
|
|
5
5
|
Calls go through Angular's `HttpClient`. In the browser they're ordinary HTTP requests. During server rendering they run in-process with the current request's context, and hydration reuses their responses through Angular's transfer cache. Errors you declare on a procedure come back typed on the client.
|
|
6
6
|
|
|
7
7
|
Use it when you want typed reads and writes between an Angular SSR app and its own server, without maintaining a separate API layer.
|
|
8
8
|
|
|
9
|
-
**Status:** experimental.
|
|
9
|
+
**Status:** experimental. MIT license. It currently covers queries, mutations, guards and declared errors. There's no builder or watch mode yet, and no subscriptions, optimistic updates, automatic invalidation or storage adapters.
|
|
10
10
|
|
|
11
11
|
## Requirements
|
|
12
12
|
|
|
13
13
|
- Angular 22 with SSR: `@angular/core` and `@angular/common` `^22.0.0` and `rxjs` `^7.8.2` (the peer dependencies), plus `@angular/router`, `@angular/ssr` and `@angular/platform-server` as in the example. Tested with Angular 22.2.2.
|
|
14
|
-
- `@loozo/angular-router` at exactly the version Launch
|
|
14
|
+
- `@loozo/angular-router` at exactly the version Launch pins in its `peerDependencies`, as a dependency of your app. Launch generation writes the router's files too, through an internal router entry point, so the two must match; the exact peer dependency enforces that.
|
|
15
15
|
- TypeScript 6 with strict checking. Generation rejects a tsconfig where any of `noImplicitAny`, `strictNullChecks`, `strictFunctionTypes`, `strictBindCallApply`, `strictPropertyInitialization`, `strictBuiltinIteratorReturn`, `noImplicitThis` or `useUnknownInCatchVariables` is off, or where `noCheck` is on. `"strict": true` is enough unless a flag is explicitly turned off, including in an extended config. Generation never edits your tsconfig.
|
|
16
16
|
- A `package.json` with a `name` in the application directory. The name is part of the generated app identity.
|
|
17
17
|
|
|
18
18
|
Install both packages:
|
|
19
19
|
|
|
20
20
|
```sh
|
|
21
|
-
pnpm add @loozo/angular-launch @loozo/angular-router@0.0.
|
|
21
|
+
pnpm add @loozo/angular-launch @loozo/angular-router@0.0.5
|
|
22
22
|
```
|
|
23
23
|
|
|
24
24
|
## How it fits together
|
|
@@ -50,7 +50,7 @@ The route tree is the router's. An `rpc.ts` belongs to the route declared in the
|
|
|
50
50
|
|
|
51
51
|
## Setup
|
|
52
52
|
|
|
53
|
-
The [todos example](
|
|
53
|
+
The [todos example](https://github.com/theo-matzavinos/loozo/tree/angular-launch@0.0.3/examples/angular-launch) is a complete app, and the snippets below are simplified from it. `db` and `sessions` stand for your own server-only modules: the example uses a test cookie instead of sessions, and its in-memory `db.todos.listForUser` also takes a test-only delay flag.
|
|
54
54
|
|
|
55
55
|
### 1. Define the context
|
|
56
56
|
|
|
@@ -247,7 +247,7 @@ mutation({ input?, canActivate?, errors?, handler });
|
|
|
247
247
|
|
|
248
248
|
The handler receives `{ context, input, errors }` and returns a value or a promise.
|
|
249
249
|
|
|
250
|
-
- **Namespaces and procedure names** are literal ASCII identifiers. `__proto__` is not allowed; `constructor` and `toString` are fine. One `defineRpc` per route
|
|
250
|
+
- **Namespaces and procedure names** are literal ASCII identifiers. `__proto__` is not allowed; `constructor` and `toString` are fine. One `defineRpc` per route, and each namespace is unique across the app because it names the group's URL.
|
|
251
251
|
- **Inputs** are [Standard Schema](https://standardschema.dev) V1 schemas, from any library or written by hand. Validation can be async and can transform the value: callers pass the schema's input type, and guards and handlers receive its output. The `input` schema must have a concrete type without `undefined` or `null` branches, though its caller input type may be a union such as `Schema<string | number, …>`. Omit `input` for a procedure without input.
|
|
252
252
|
- **Guards** return `boolean` or `Promise<boolean>`; `false` fails the call with `FORBIDDEN`. Group guards (in `defineRpc` options) get `{ context, request }`; procedure guards also get the validated `input`. Guards are never inherited through the route tree. A call runs input validation, then the group's guards, then the procedure's guards, then the handler.
|
|
253
253
|
- Guards don't narrow types. Use an ordinary function to narrow, such as the example's `requireUser`:
|
|
@@ -408,9 +408,9 @@ The server bundle includes your dependencies by default, so the host doesn't nee
|
|
|
408
408
|
|
|
409
409
|
The host check covers page rendering only. `handleRequest` runs before Angular and answers RPC requests for any host.
|
|
410
410
|
|
|
411
|
-
Launch calls `/_rpc/<
|
|
411
|
+
Launch calls `/_rpc/<namespace>/<procedure>` from the root of the domain, even when the app has a different `<base href>`. If the app is served under a path prefix, route `/_rpc/` to the same server.
|
|
412
412
|
|
|
413
|
-
Browser tabs opened before a deploy keep calling the procedures they were built with. After you
|
|
413
|
+
Browser tabs opened before a deploy keep calling the procedures they were built with. After you rename a namespace or procedure, or change an input schema, those calls fail with `RPC_NOT_FOUND` or `INVALID_INPUT` until the page reloads. The same applies while old and new servers run side by side during a rolling deploy.
|
|
414
414
|
|
|
415
415
|
Launch has been verified on Node only.
|
|
416
416
|
|
|
@@ -491,7 +491,7 @@ Generated browser code contains only names, kinds and error metadata, never your
|
|
|
491
491
|
|
|
492
492
|
`server.generated.ts` binds Launch's internal `createRequestScope`, `handleRpc` and `provideLaunchServer` to your app. It imports them from `@loozo/angular-launch/_server`, an entry point for generated code only that isn't public API and can change in any release; use the generated functions instead. The app identity comes from the `package.json` name, the generation tsconfig's path within the app, and the context file's path and contents, so moving the whole app directory doesn't change it.
|
|
493
493
|
|
|
494
|
-
|
|
494
|
+
RPC URLs are `/_rpc/<namespace>/<procedure>`, so they change only when you rename a namespace or procedure. Moving an `rpc.ts` or the generation tsconfig doesn't change them. The example doesn't commit generated files: it regenerates before every build, and `node generate.mjs --check` verifies them without writing.
|
|
495
495
|
|
|
496
496
|
`@loozo/angular-launch/generation` is Node-only.
|
|
497
497
|
|
|
@@ -531,9 +531,9 @@ The options are fixed when the session is created. Edits inside the app, includi
|
|
|
531
531
|
|
|
532
532
|
## Further reading
|
|
533
533
|
|
|
534
|
-
- [`specs/angular-launch.md`](
|
|
535
|
-
- [`examples/angular-launch`](
|
|
536
|
-
- [`@loozo/angular-router`](
|
|
534
|
+
- [`specs/angular-launch.md`](https://github.com/theo-matzavinos/loozo/blob/angular-launch@0.0.3/specs/angular-launch.md): design and full contract
|
|
535
|
+
- [`examples/angular-launch`](https://github.com/theo-matzavinos/loozo/tree/angular-launch@0.0.3/examples/angular-launch): the todos app. Its cookie identity and in-memory database are test fixtures, not security or deployment guidance.
|
|
536
|
+
- [`@loozo/angular-router`](https://github.com/theo-matzavinos/loozo/blob/angular-launch@0.0.3/packages/angular-router/README.md): routes, codecs and navigation
|
|
537
537
|
|
|
538
538
|
## Developing this package
|
|
539
539
|
|
package/generated.d.ts
CHANGED
package/generated.js
CHANGED
|
@@ -68,10 +68,10 @@ export function injectClient(groups) {
|
|
|
68
68
|
for (const method of group.methods) {
|
|
69
69
|
let invoke;
|
|
70
70
|
if (method.input) {
|
|
71
|
-
invoke = (input, options) => call(http,
|
|
71
|
+
invoke = (input, options) => call(http, namespace, method, input, options);
|
|
72
72
|
}
|
|
73
73
|
else {
|
|
74
|
-
invoke = (options) => call(http,
|
|
74
|
+
invoke = (options) => call(http, namespace, method, null, options);
|
|
75
75
|
}
|
|
76
76
|
methods[method.name] = Object.assign(invoke, {
|
|
77
77
|
kind: method.kind,
|
|
@@ -80,7 +80,7 @@ export function injectClient(groups) {
|
|
|
80
80
|
return false;
|
|
81
81
|
}
|
|
82
82
|
const owner = errorOwners.get(error);
|
|
83
|
-
return owner?.group ===
|
|
83
|
+
return owner?.group === namespace && owner.method === method.name;
|
|
84
84
|
},
|
|
85
85
|
});
|
|
86
86
|
}
|
package/generation/index.js
CHANGED
|
@@ -79,6 +79,7 @@ async function groupsOf(sources) {
|
|
|
79
79
|
}
|
|
80
80
|
const groups = [];
|
|
81
81
|
const owners = new Set();
|
|
82
|
+
const namespaces = new Map();
|
|
82
83
|
for (const declaration of launch.declarations) {
|
|
83
84
|
if (declaration.kind !== "defineRpc") {
|
|
84
85
|
continue;
|
|
@@ -104,6 +105,11 @@ async function groupsOf(sources) {
|
|
|
104
105
|
if (namespace.text === "__proto__") {
|
|
105
106
|
fail(namespace, "RPC namespace '__proto__' is reserved");
|
|
106
107
|
}
|
|
108
|
+
const previous = namespaces.get(namespace.text);
|
|
109
|
+
if (previous) {
|
|
110
|
+
fail(namespace, `Duplicate RPC namespace: ${namespace.text}; also defined in ${previous.file}`);
|
|
111
|
+
}
|
|
112
|
+
namespaces.set(namespace.text, declaration);
|
|
107
113
|
if (options) {
|
|
108
114
|
const fields = properties(options, "RPC group options");
|
|
109
115
|
onlyKeys(fields, ["canActivate"], options);
|
|
@@ -145,9 +151,7 @@ async function groupsOf(sources) {
|
|
|
145
151
|
call: procedure,
|
|
146
152
|
});
|
|
147
153
|
}
|
|
148
|
-
|
|
149
|
-
const id = createHash("sha256").update(`launch-v1\0${identity}`).digest("hex").slice(0, 24);
|
|
150
|
-
groups.push({ declaration, owner, namespace: namespace.text, id, methods });
|
|
154
|
+
groups.push({ declaration, owner, namespace: namespace.text, methods });
|
|
151
155
|
}
|
|
152
156
|
return { context, groups };
|
|
153
157
|
}
|
|
@@ -172,7 +176,7 @@ function emitLaunch(sources, desired, context, groups, appId) {
|
|
|
172
176
|
for (let index = 0; index < groups.length; index += 1) {
|
|
173
177
|
server.push(payloadImport(registry, groups[index].declaration.file, `group${index}`));
|
|
174
178
|
}
|
|
175
|
-
server.push(`export const registry = { ${groups.map((group, index) => `${JSON.stringify(group.
|
|
179
|
+
server.push(`export const registry = { ${groups.map((group, index) => `${JSON.stringify(group.namespace)}: group${index}`).join(", ")} };`, `export const appBinding = { id: ${JSON.stringify(appId)}, context };`, `export const createContext = (request: Request) => createRequestScope(appBinding, request);`, `export const handleRequest = (request: Request) => handleRpc(registry, appBinding, request);`, `export const provideServer = () => provideLaunchServer(registry, appBinding);`, "");
|
|
176
180
|
add(desired, registry, server.join("\n"));
|
|
177
181
|
const byOwner = new Map(groups.map((group) => [group.owner, group]));
|
|
178
182
|
for (const owner of sources.declarations) {
|
|
@@ -180,15 +184,10 @@ function emitLaunch(sources, desired, context, groups, appId) {
|
|
|
180
184
|
continue;
|
|
181
185
|
}
|
|
182
186
|
const available = [];
|
|
183
|
-
const namespaces = new Set();
|
|
184
187
|
let current = owner;
|
|
185
188
|
while (current) {
|
|
186
189
|
const group = byOwner.get(current);
|
|
187
190
|
if (group) {
|
|
188
|
-
if (namespaces.has(group.namespace)) {
|
|
189
|
-
fail(group.declaration.call, `Duplicate RPC namespace in ancestry: ${group.namespace}`);
|
|
190
|
-
}
|
|
191
|
-
namespaces.add(group.namespace);
|
|
192
191
|
available.unshift(group);
|
|
193
192
|
}
|
|
194
193
|
current = current.parent;
|
|
@@ -204,7 +203,6 @@ function emitLaunch(sources, desired, context, groups, appId) {
|
|
|
204
203
|
const group = available[index];
|
|
205
204
|
lines.push(payloadImport(file, group.declaration.file, `group${index}`, true));
|
|
206
205
|
metadata[group.namespace] = {
|
|
207
|
-
id: group.id,
|
|
208
206
|
methods: group.methods.map(({ name, input, kind, errors }) => ({
|
|
209
207
|
name,
|
|
210
208
|
input,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@loozo/angular-launch",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.3",
|
|
4
4
|
"description": "Typed queries, mutations and declared errors on Angular's native router, HTTP and SSR pipeline.",
|
|
5
5
|
"bugs": "https://github.com/theo-matzavinos/loozo/issues",
|
|
6
6
|
"license": "MIT",
|
|
@@ -44,7 +44,7 @@
|
|
|
44
44
|
"peerDependencies": {
|
|
45
45
|
"@angular/common": "^22.0.0",
|
|
46
46
|
"@angular/core": "^22.0.0",
|
|
47
|
-
"@loozo/angular-router": "0.0.
|
|
47
|
+
"@loozo/angular-router": "0.0.5",
|
|
48
48
|
"rxjs": "^7.8.2"
|
|
49
49
|
}
|
|
50
50
|
}
|
package/server/definitions.d.ts
CHANGED
|
@@ -447,7 +447,8 @@ export interface Group<Name extends string, Procedures extends Record<string, Pr
|
|
|
447
447
|
* identifiers other than `__proto__` for the namespace and procedure names, and
|
|
448
448
|
* write the procedures, options and `canActivate` arrays inline. Each route's
|
|
449
449
|
* generated `injectRpc()` exposes its own group and its ancestors' groups under their
|
|
450
|
-
* namespaces
|
|
450
|
+
* namespaces. Each namespace must be unique across the app: calls go to
|
|
451
|
+
* `/_rpc/<namespace>/<procedure>`.
|
|
451
452
|
*
|
|
452
453
|
* Exposure isn't authorization: anyone who can reach the server can call any
|
|
453
454
|
* procedure, so guard every group and procedure that needs it.
|
package/server/definitions.js
CHANGED
|
@@ -136,7 +136,8 @@ export function mutation(options) {
|
|
|
136
136
|
* identifiers other than `__proto__` for the namespace and procedure names, and
|
|
137
137
|
* write the procedures, options and `canActivate` arrays inline. Each route's
|
|
138
138
|
* generated `injectRpc()` exposes its own group and its ancestors' groups under their
|
|
139
|
-
* namespaces
|
|
139
|
+
* namespaces. Each namespace must be unique across the app: calls go to
|
|
140
|
+
* `/_rpc/<namespace>/<procedure>`.
|
|
140
141
|
*
|
|
141
142
|
* Exposure isn't authorization: anyone who can reach the server can call any
|
|
142
143
|
* procedure, so guard every group and procedure that needs it.
|
package/server/dispatch.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { type FrameworkCode } from "../client.js";
|
|
2
2
|
import type { ContextDefinition, ContextOf, Group, ProcedureDefinition } from "./definitions.js";
|
|
3
3
|
/**
|
|
4
|
-
* The application's RPC groups, keyed by
|
|
4
|
+
* The application's RPC groups, keyed by namespace.
|
|
5
5
|
*
|
|
6
6
|
* @remarks
|
|
7
7
|
* Internal; not exported from a public entry point. Generation writes it as
|
|
@@ -97,7 +97,7 @@ export declare function isJsonMediaType(contentType: string | null): boolean;
|
|
|
97
97
|
* unexpected errors are logged on the server.
|
|
98
98
|
*
|
|
99
99
|
* In order: the scope must belong to `application` and `request` (`INTERNAL`); the
|
|
100
|
-
* path must be `/_rpc/<
|
|
100
|
+
* path must be `/_rpc/<namespace>/<procedure>` for a registered procedure
|
|
101
101
|
* (`RPC_NOT_FOUND`); a mutation in `"rendering"` mode is refused (`FORBIDDEN`); the
|
|
102
102
|
* envelope must be strict JSON (`INTERNAL` otherwise, such as for `{ input: undefined }`,
|
|
103
103
|
* a cycle or an accessor); it must then be exactly `{ input }` (`INVALID_INPUT`) with
|
package/server/dispatch.js
CHANGED
|
@@ -85,7 +85,7 @@ export function isJsonMediaType(contentType) {
|
|
|
85
85
|
* unexpected errors are logged on the server.
|
|
86
86
|
*
|
|
87
87
|
* In order: the scope must belong to `application` and `request` (`INTERNAL`); the
|
|
88
|
-
* path must be `/_rpc/<
|
|
88
|
+
* path must be `/_rpc/<namespace>/<procedure>` for a registered procedure
|
|
89
89
|
* (`RPC_NOT_FOUND`); a mutation in `"rendering"` mode is refused (`FORBIDDEN`); the
|
|
90
90
|
* envelope must be strict JSON (`INTERNAL` otherwise, such as for `{ input: undefined }`,
|
|
91
91
|
* a cycle or an accessor); it must then be exactly `{ input }` (`INVALID_INPUT`) with
|
|
@@ -107,7 +107,7 @@ export function isJsonMediaType(contentType) {
|
|
|
107
107
|
export async function dispatch(registry, path, envelope, scope, application, request, mode = "http") {
|
|
108
108
|
try {
|
|
109
109
|
assertBoundScope(scope, application, request);
|
|
110
|
-
const parts = /^\/_rpc\/([
|
|
110
|
+
const parts = /^\/_rpc\/([A-Za-z_][A-Za-z0-9_]*)\/([A-Za-z_][A-Za-z0-9_]*)$/.exec(path);
|
|
111
111
|
if (!parts || !Object.hasOwn(registry, parts[1])) {
|
|
112
112
|
return failure("RPC_NOT_FOUND");
|
|
113
113
|
}
|