@loozo/angular-launch 0.0.1 → 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.
@@ -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;
@@ -167,7 +167,7 @@ function emitLaunch(sources, desired, context, groups, appId) {
167
167
  const server = [
168
168
  launchMarkers["server.generated.ts"],
169
169
  payloadImport(registry, context.file, "context"),
170
- `import { createRequestScope, handleRpc, provideLaunchServer } from '@loozo/angular-launch/server';`,
170
+ `import { createRequestScope, handleRpc, provideLaunchServer } from '@loozo/angular-launch/_server';`,
171
171
  ];
172
172
  for (let index = 0; index < groups.length; index += 1) {
173
173
  server.push(payloadImport(registry, groups[index].declaration.file, `group${index}`));
@@ -425,7 +425,8 @@ function checkGeneratedDomain(sources, validation, generated) {
425
425
  }
426
426
  }
427
427
  async function runGeneration(workspace, options, stopped, compiler) {
428
- const root = fileURLToPath(new URL("../../", import.meta.url));
428
+ // The built package root (dist/ in the workspace, the tarball root when installed).
429
+ const root = fileURLToPath(new URL("../", import.meta.url));
429
430
  const manifest = JSON.parse(await readFile(resolve(root, "package.json"), "utf8"));
430
431
  const typeEntry = await realpath(resolve(root, manifest.exports["./server"].types));
431
432
  const application = await realpath(workspace);
@@ -497,10 +498,80 @@ async function runGeneration(workspace, options, stopped, compiler) {
497
498
  checkGeneratedDomain(sources, validation, desired);
498
499
  publish(plan, options.check === true, stopped, new Map(), new Set());
499
500
  }
501
+ /**
502
+ * Generates the router's and Launch's files for an application, or checks them.
503
+ *
504
+ * @remarks
505
+ * Node-only. Generation reads your sources without running them. Besides the router's
506
+ * files, it writes an `rpc.generated.ts` beside each route that gets a
507
+ * `route.generated.ts`, and `context.generated.d.ts` and `server.generated.ts` in the
508
+ * output directory. Run it before the Angular build.
509
+ *
510
+ * Everything is validated, including a type check of the generated files with
511
+ * `skipLibCheck: false`, before anything is written, so a failed run changes nothing.
512
+ * Files are then written one at a time without rollback; a write error partway through
513
+ * can leave some files updated, which the next successful run repairs. Generation only
514
+ * replaces or removes files carrying its ownership marker, and removes stale ones.
515
+ *
516
+ * @example
517
+ * ```js
518
+ * import { generate } from "@loozo/angular-launch/generation";
519
+ *
520
+ * await generate(import.meta.dirname, {
521
+ * tsconfig: "tsconfig.app.json",
522
+ * scanDirectories: ["src/routes"],
523
+ * outDirectory: "generated",
524
+ * check: process.argv.includes("--check"),
525
+ * });
526
+ * ```
527
+ *
528
+ * @param workspace - The absolute path of the application directory, which contains
529
+ * its `package.json`. The tsconfig, scanned directory, sources and output directory
530
+ * must all be inside it.
531
+ * @param options - The generation options.
532
+ * @param stopped - Called once just before writing; returning `true` skips writing and
533
+ * resolves. Defaults to never stopping.
534
+ * @returns A promise that resolves when generation finishes.
535
+ * @throws Rejects when a path escapes the workspace, the tsconfig disables strict
536
+ * checking or enables `noCheck`, the application's `package.json` has no `name`, the
537
+ * sources or generated files fail validation, a file to replace lacks the ownership
538
+ * marker, or, with `check`, a generated file is out of date.
539
+ */
500
540
  export function generate(workspace, options, stopped = () => false) {
501
541
  return runGeneration(workspace, options, stopped);
502
542
  }
503
- /** Owns compiler reuse for one explicitly sequenced application-generation lifetime. */
543
+ /**
544
+ * Creates a generation session that reuses compiler work between sequential calls.
545
+ *
546
+ * @remarks
547
+ * Node-only. For long-running tools that regenerate repeatedly, such as after file
548
+ * changes. A session doesn't watch files; you decide when to call `generate`. Edits
549
+ * inside the application are picked up by the next call. Create a new session after
550
+ * moving the application directory or changing dependencies. Don't run two sessions or
551
+ * generators against the same output at once.
552
+ *
553
+ * @example
554
+ * ```js
555
+ * const session = createGenerationSession(workspace, {
556
+ * tsconfig: "tsconfig.app.json",
557
+ * scanDirectories: ["src/routes"],
558
+ * outDirectory: "generated",
559
+ * });
560
+ *
561
+ * try {
562
+ * await session.generate();
563
+ * await session.generate({ check: true });
564
+ * } finally {
565
+ * await session.dispose();
566
+ * }
567
+ * ```
568
+ *
569
+ * @param workspace - The absolute path of the application directory, as for
570
+ * {@link generate}.
571
+ * @param options - The generation options, fixed for the session's lifetime; `check` is
572
+ * passed per call instead.
573
+ * @returns The session. Dispose it when done.
574
+ */
504
575
  export function createGenerationSession(workspace, { tsconfig, scanDirectories, outDirectory }) {
505
576
  const application = resolve(workspace);
506
577
  const fixed = { tsconfig, scanDirectories: [...scanDirectories], outDirectory };
package/index.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ export { RpcError, frameworkFailures } from "./client.js";
2
+ export type { CallOptions, MutationCallOptions, Client, ClientProcedure, ClientRpcError, FrameworkCode, } from "./client.js";
package/index.js ADDED
@@ -0,0 +1 @@
1
+ export { RpcError, frameworkFailures } from "./client.js";
@@ -1,6 +1,12 @@
1
- export type Json = null | boolean | number | string | readonly Json[] | {
2
- readonly [key: string]: Json;
3
- };
1
+ /**
2
+ * The strict JSON shape of `Value`: `never` wherever `Value` isn't strict JSON.
3
+ *
4
+ * @remarks
5
+ * Procedure inputs, results and error data must satisfy it. Functions become `never`;
6
+ * arrays and objects are checked element by element.
7
+ *
8
+ * @typeParam Value - The type to check.
9
+ */
4
10
  export type JsonShape<Value> = Value extends null | boolean | number | string ? Value : Value extends (...args: never[]) => unknown ? never : Value extends readonly (infer Element)[] ? readonly JsonShape<Element>[] : Value extends object ? {
5
11
  readonly [Key in keyof Value]-?: Key extends string | number ? JsonShape<Value[Key]> : never;
6
12
  } : never;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@loozo/angular-launch",
3
- "version": "0.0.1",
3
+ "version": "0.0.2",
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",
@@ -10,27 +10,27 @@
10
10
  "url": "git+https://github.com/theo-matzavinos/loozo.git",
11
11
  "directory": "packages/angular-launch"
12
12
  },
13
- "files": [
14
- "dist",
15
- "README.md"
16
- ],
17
13
  "type": "module",
18
14
  "exports": {
19
15
  ".": {
20
- "types": "./dist/client.d.ts",
21
- "default": "./dist/client.js"
16
+ "types": "./index.d.ts",
17
+ "default": "./index.js"
22
18
  },
23
19
  "./server": {
24
- "types": "./dist/server/index.d.ts",
25
- "default": "./dist/server/index.js"
20
+ "types": "./server/index.d.ts",
21
+ "default": "./server/index.js"
22
+ },
23
+ "./_server": {
24
+ "types": "./server/generated.d.ts",
25
+ "default": "./server/generated.js"
26
26
  },
27
27
  "./_generated": {
28
- "types": "./dist/generated.d.ts",
29
- "default": "./dist/generated.js"
28
+ "types": "./generated.d.ts",
29
+ "default": "./generated.js"
30
30
  },
31
31
  "./generation": {
32
- "types": "./dist/generation/index.d.ts",
33
- "node": "./dist/generation/index.js"
32
+ "types": "./generation/index.d.ts",
33
+ "node": "./generation/index.js"
34
34
  },
35
35
  "./package.json": "./package.json"
36
36
  },
@@ -41,23 +41,10 @@
41
41
  "dependencies": {
42
42
  "typescript": "6.0.3"
43
43
  },
44
- "devDependencies": {
45
- "@angular/common": "22.2.2",
46
- "@angular/compiler": "22.2.2",
47
- "@angular/core": "22.2.2",
48
- "@loozo/angular-router": "0.0.3",
49
- "@types/node": "24.13.3",
50
- "playwright": "1.63.0"
51
- },
52
44
  "peerDependencies": {
53
45
  "@angular/common": "^22.0.0",
54
46
  "@angular/core": "^22.0.0",
55
- "@loozo/angular-router": "0.0.3",
47
+ "@loozo/angular-router": "0.0.4",
56
48
  "rxjs": "^7.8.2"
57
- },
58
- "scripts": {
59
- "build": "rm -rf dist && tsc -p tsconfig.json",
60
- "typecheck": "tsc -p tsconfig.json --noEmit",
61
- "test": "node --test tests/*.test.mjs"
62
49
  }
63
- }
50
+ }
@@ -0,0 +1,58 @@
1
+ import type { ApplicationRef, EnvironmentProviders } from "@angular/core";
2
+ import { type ApplicationBinding, type Registry } from "./dispatch.js";
3
+ /**
4
+ * Provides the server-rendering transport that runs RPC calls in-process with the
5
+ * current request's context.
6
+ *
7
+ * @remarks
8
+ * Internal: exported only from `@loozo/angular-launch/_server` for the generated
9
+ * `provideServer` in `server.generated.ts`; add that to your server application config,
10
+ * after `provideHttpClient()`, instead of calling this.
11
+ *
12
+ * It replaces Angular's `HttpBackend` on the server. Calls from generated clients, and
13
+ * any other request to `/_rpc/` on the current request's origin, are intercepted and
14
+ * run in-process in `"rendering"` mode, so mutations are refused with `FORBIDDEN`. All
15
+ * other requests, including ones to `/_rpc/` on another origin that don't come from a
16
+ * generated client, go to Angular's `FetchBackend`.
17
+ *
18
+ * For intercepted calls, Angular's `REQUEST_CONTEXT` must be the {@link RequestScope}
19
+ * created for Angular's `REQUEST`, or the calls fail with `INTERNAL`. A generated-client
20
+ * call outside `/_rpc/` on the current request's origin fails with `INVALID_INPUT`, as
21
+ * does any intercepted call with a query string, fragment, URL credentials, a method
22
+ * other than `POST`, a non-JSON content type, its own `Authorization`,
23
+ * `Proxy-Authorization` or `Cookie` header, or its own credentials setting.
24
+ *
25
+ * Without a live request, as during prerendering, calls from generated clients and
26
+ * requests to a relative `/_rpc/` path fail and the fault is recorded for
27
+ * {@link finishLaunchRendering}; other requests still go to `FetchBackend`.
28
+ *
29
+ * @param registry - The application's RPC groups.
30
+ * @param application - The application binding.
31
+ * @returns The environment providers for the server application.
32
+ */
33
+ export declare function provideLaunchServer(registry: Registry, application: ApplicationBinding): EnvironmentProviders;
34
+ /**
35
+ * Waits for the server application to become stable and fails the render if an RPC was
36
+ * called without a live request.
37
+ *
38
+ * @remarks
39
+ * Angular's prerendering can swallow resolver errors and still report success. Return
40
+ * this from your server bootstrap so a page that calls an RPC during prerendering fails
41
+ * the build instead of producing a broken page. The application must include the
42
+ * generated `provideServer()`.
43
+ *
44
+ * @example
45
+ * ```ts
46
+ * export default async function bootstrap(context: BootstrapContext) {
47
+ * const app = await bootstrapApplication(App, serverConfig, context);
48
+ *
49
+ * return finishLaunchRendering(app);
50
+ * }
51
+ * ```
52
+ *
53
+ * @param app - The bootstrapped server application.
54
+ * @returns The same application once it is stable.
55
+ * @throws The recorded error when an RPC was called without a live request; the
56
+ * application is destroyed first.
57
+ */
58
+ export declare function finishLaunchRendering(app: ApplicationRef): Promise<ApplicationRef>;
@@ -4,6 +4,36 @@ import { Observable } from "rxjs";
4
4
  import { RPC_CALL } from "../generated.js";
5
5
  import { dispatch, failure, isJsonMediaType, assertBoundScope, } from "./dispatch.js";
6
6
  const renderFaults = new InjectionToken("Launch render faults");
7
+ /**
8
+ * Provides the server-rendering transport that runs RPC calls in-process with the
9
+ * current request's context.
10
+ *
11
+ * @remarks
12
+ * Internal: exported only from `@loozo/angular-launch/_server` for the generated
13
+ * `provideServer` in `server.generated.ts`; add that to your server application config,
14
+ * after `provideHttpClient()`, instead of calling this.
15
+ *
16
+ * It replaces Angular's `HttpBackend` on the server. Calls from generated clients, and
17
+ * any other request to `/_rpc/` on the current request's origin, are intercepted and
18
+ * run in-process in `"rendering"` mode, so mutations are refused with `FORBIDDEN`. All
19
+ * other requests, including ones to `/_rpc/` on another origin that don't come from a
20
+ * generated client, go to Angular's `FetchBackend`.
21
+ *
22
+ * For intercepted calls, Angular's `REQUEST_CONTEXT` must be the {@link RequestScope}
23
+ * created for Angular's `REQUEST`, or the calls fail with `INTERNAL`. A generated-client
24
+ * call outside `/_rpc/` on the current request's origin fails with `INVALID_INPUT`, as
25
+ * does any intercepted call with a query string, fragment, URL credentials, a method
26
+ * other than `POST`, a non-JSON content type, its own `Authorization`,
27
+ * `Proxy-Authorization` or `Cookie` header, or its own credentials setting.
28
+ *
29
+ * Without a live request, as during prerendering, calls from generated clients and
30
+ * requests to a relative `/_rpc/` path fail and the fault is recorded for
31
+ * {@link finishLaunchRendering}; other requests still go to `FetchBackend`.
32
+ *
33
+ * @param registry - The application's RPC groups.
34
+ * @param application - The application binding.
35
+ * @returns The environment providers for the server application.
36
+ */
7
37
  export function provideLaunchServer(registry, application) {
8
38
  return makeEnvironmentProviders([
9
39
  { provide: renderFaults, useFactory: () => ({}) },
@@ -99,6 +129,30 @@ export function provideLaunchServer(registry, application) {
99
129
  },
100
130
  ]);
101
131
  }
132
+ /**
133
+ * Waits for the server application to become stable and fails the render if an RPC was
134
+ * called without a live request.
135
+ *
136
+ * @remarks
137
+ * Angular's prerendering can swallow resolver errors and still report success. Return
138
+ * this from your server bootstrap so a page that calls an RPC during prerendering fails
139
+ * the build instead of producing a broken page. The application must include the
140
+ * generated `provideServer()`.
141
+ *
142
+ * @example
143
+ * ```ts
144
+ * export default async function bootstrap(context: BootstrapContext) {
145
+ * const app = await bootstrapApplication(App, serverConfig, context);
146
+ *
147
+ * return finishLaunchRendering(app);
148
+ * }
149
+ * ```
150
+ *
151
+ * @param app - The bootstrapped server application.
152
+ * @returns The same application once it is stable.
153
+ * @throws The recorded error when an RPC was called without a live request; the
154
+ * application is destroyed first.
155
+ */
102
156
  // Native prerender can swallow resolver errors. Propagate only the unsupported
103
157
  // Launch context fault after native stability, without manufacturing a request.
104
158
  export async function finishLaunchRendering(app) {