@loozo/angular-launch 0.0.4 → 0.0.5

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.
@@ -5,16 +5,46 @@ import { readFile, realpath } from "node:fs/promises";
5
5
  import { dirname, resolve, relative, sep } from "node:path";
6
6
  import { fileURLToPath } from "node:url";
7
7
  import { prepareLaunchRoutes, createValidationProgram, validateRoutes, planOutputs, publish, walk, auditPath, launchMarkers, fail, properties, namedImport, genuineImport, specifier, CompilerState, AnchorState, } from "@loozo/angular-router/_generation";
8
+ import { contextFiles } from "./context-file.js";
8
9
  function directCall(node, label) {
9
10
  if (!ts.isCallExpression(node) || node.typeArguments?.length) {
10
11
  fail(node, `${label} requires a direct nongeneric factory call`);
11
12
  }
12
13
  return node;
13
14
  }
14
- function guards(node) {
15
- if (node && (!ts.isArrayLiteralExpression(node) || node.elements.some(ts.isSpreadElement))) {
16
- fail(node, "canActivate must be an explicit inline array without spreads");
17
- }
15
+ const builderShape = "defineRpc requires an arrow function destructuring query and/or mutation, returning a parenthesized object of procedures";
16
+ // `({ query, mutation }) => ({ ... })`: the builder names it binds and its procedures.
17
+ function builderCallback(node) {
18
+ if (!ts.isArrowFunction(node) ||
19
+ node.modifiers?.length ||
20
+ node.typeParameters?.length ||
21
+ node.type ||
22
+ node.parameters.length !== 1) {
23
+ fail(node, builderShape);
24
+ }
25
+ const [parameter] = node.parameters;
26
+ if (!ts.isObjectBindingPattern(parameter.name) ||
27
+ parameter.type ||
28
+ parameter.initializer ||
29
+ parameter.dotDotDotToken) {
30
+ fail(parameter, builderShape);
31
+ }
32
+ const bound = new Set();
33
+ for (const element of parameter.name.elements) {
34
+ if (element.propertyName ||
35
+ element.initializer ||
36
+ element.dotDotDotToken ||
37
+ !ts.isIdentifier(element.name) ||
38
+ (element.name.text !== "query" && element.name.text !== "mutation") ||
39
+ bound.has(element.name.text)) {
40
+ fail(element, "defineRpc's callback may only destructure query and mutation, without renaming");
41
+ }
42
+ bound.add(element.name.text);
43
+ }
44
+ if (!ts.isParenthesizedExpression(node.body)) {
45
+ fail(node.body, builderShape);
46
+ }
47
+ return { bound, procedures: properties(node.body.expression, "RPC procedures") };
18
48
  }
19
49
  function onlyKeys(fields, allowed, node) {
20
50
  for (const key of fields.keys()) {
@@ -93,14 +123,14 @@ async function groupsOf(sources) {
93
123
  }
94
124
  owners.add(owner);
95
125
  const call = directCall(declaration.call, "defineRpc");
96
- const [namespace, methodsNode, options] = call.arguments;
126
+ const [namespace, build, options] = call.arguments;
97
127
  if (call.arguments.length < 2 ||
98
128
  call.arguments.length > 3 ||
99
129
  !namespace ||
100
130
  !ts.isStringLiteral(namespace) ||
101
131
  !/^[A-Za-z_][A-Za-z0-9_]*$/.test(namespace.text) ||
102
- !methodsNode) {
103
- fail(call, "defineRpc requires a literal identifier namespace, inline methods and optional inline options");
132
+ !build) {
133
+ fail(call, "defineRpc requires a literal identifier namespace, a procedure callback and optional inline options");
104
134
  }
105
135
  if (namespace.text === "__proto__") {
106
136
  fail(namespace, "RPC namespace '__proto__' is reserved");
@@ -112,11 +142,11 @@ async function groupsOf(sources) {
112
142
  namespaces.set(namespace.text, declaration);
113
143
  if (options) {
114
144
  const fields = properties(options, "RPC group options");
115
- onlyKeys(fields, ["canActivate"], options);
116
- guards(fields.get("canActivate"));
145
+ onlyKeys(fields, ["use"], options);
117
146
  }
147
+ const { bound, procedures } = builderCallback(build);
118
148
  const methods = [];
119
- for (const [name, value] of properties(methodsNode, "RPC methods")) {
149
+ for (const [name, value] of procedures) {
120
150
  if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(name)) {
121
151
  fail(value, "RPC methods must be identifier names");
122
152
  }
@@ -124,31 +154,24 @@ async function groupsOf(sources) {
124
154
  fail(value, "RPC procedure '__proto__' is reserved");
125
155
  }
126
156
  const procedure = directCall(value, "query/mutation");
127
- let kind;
128
- for (const candidate of ["query", "mutation"]) {
129
- const imported = namedImport(value.getSourceFile(), procedure.expression, candidate);
130
- if (imported && (await genuineImport(imported, launch.typeEntry, sources.parsed.options))) {
131
- kind = candidate;
132
- }
133
- }
134
- if (!kind) {
135
- fail(procedure, "Procedure must directly call a genuine named query import or mutation import");
157
+ // The callback's own binding: its body is an object literal, so nothing can shadow it.
158
+ if (!ts.isIdentifier(procedure.expression) || !bound.has(procedure.expression.text)) {
159
+ fail(procedure, "Procedure must directly call the callback's query or mutation");
136
160
  }
161
+ const kind = procedure.expression.text;
137
162
  if (procedure.arguments.length !== 1) {
138
163
  fail(procedure, `${kind} requires one inline options object`);
139
164
  }
140
165
  const fields = properties(procedure.arguments[0], "Procedure options");
141
- onlyKeys(fields, ["handler", "input", "canActivate", "errors"], procedure);
166
+ onlyKeys(fields, ["handler", "input", "use", "errors"], procedure);
142
167
  if (!fields.has("handler")) {
143
168
  fail(procedure, `${kind} requires handler`);
144
169
  }
145
- guards(fields.get("canActivate"));
146
170
  methods.push({
147
171
  name,
148
172
  input: fields.has("input"),
149
173
  kind,
150
174
  errors: errorMetadata(fields.get("errors")),
151
- call: procedure,
152
175
  });
153
176
  }
154
177
  groups.push({ declaration, owner, namespace: namespace.text, methods });
@@ -171,12 +194,13 @@ function emitLaunch(sources, desired, context, groups, appId, tanstackQuery) {
171
194
  const server = [
172
195
  launchMarkers["server.generated.ts"],
173
196
  payloadImport(registry, context.file, "context"),
197
+ `import type { HandleOptions } from '@loozo/angular-launch/server';`,
174
198
  `import { createRequestScope, handleRpc, provideLaunchServer } from '@loozo/angular-launch/_server';`,
175
199
  ];
176
200
  for (let index = 0; index < groups.length; index += 1) {
177
201
  server.push(payloadImport(registry, groups[index].declaration.file, `group${index}`));
178
202
  }
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);`, "");
203
+ 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, options: HandleOptions) => handleRpc(registry, appBinding, request, options);`, `export const provideServer = () => provideLaunchServer(registry, appBinding);`, "");
180
204
  add(desired, registry, server.join("\n"));
181
205
  const byOwner = new Map(groups.map((group) => [group.owner, group]));
182
206
  for (const owner of sources.declarations) {
@@ -267,7 +291,7 @@ async function validateBindings(sources, validation, context, groups, compiler)
267
291
  baseUrl: undefined,
268
292
  skipLibCheck: false,
269
293
  };
270
- const anchor = await (compiler?.launchAnchor ?? new AnchorState()).anchor(entryFile, ["defineContext", "defineRpc", "query", "mutation"], canonicalOptions);
294
+ const anchor = await (compiler?.launchAnchor ?? new AnchorState()).anchor(entryFile, ["defineContext", "defineRpc"], canonicalOptions);
271
295
  const actualEntry = program.getSourceFile(entryFile);
272
296
  const actualModule = checker.getSymbolAtLocation(actualEntry);
273
297
  const expectedCalls = new Map();
@@ -277,9 +301,6 @@ async function validateBindings(sources, validation, context, groups, compiler)
277
301
  expectedCalls.set(key(context.call), "defineContext");
278
302
  for (const group of groups) {
279
303
  expectedCalls.set(key(group.declaration.call), "defineRpc");
280
- for (const method of group.methods) {
281
- expectedCalls.set(key(method.call), method.kind);
282
- }
283
304
  }
284
305
  const promises = [];
285
306
  for (const source of program.getSourceFiles()) {
@@ -291,7 +312,7 @@ async function validateBindings(sources, validation, context, groups, compiler)
291
312
  const expected = expectedCalls.get(key(node));
292
313
  let spelling;
293
314
  const invoked = unalias(checker, checker.getSymbolAtLocation(node.expression));
294
- for (const name of ["defineContext", "defineRpc", "query", "mutation"]) {
315
+ for (const name of ["defineContext", "defineRpc"]) {
295
316
  const exported = unalias(checker, checker.getExportsOfModule(actualModule).find((symbol) => symbol.name === name));
296
317
  if (invoked && invoked === exported) {
297
318
  spelling = name;
@@ -302,7 +323,7 @@ async function validateBindings(sources, validation, context, groups, compiler)
302
323
  }
303
324
  if (expected || spelling) {
304
325
  if (!expected || spelling !== expected) {
305
- fail(node, "Launch factory calls must be default declarations or inline group procedures");
326
+ fail(node, "defineContext and defineRpc calls must be default declarations");
306
327
  }
307
328
  const imported = namedImport(source, node.expression, expected);
308
329
  promises.push((async () => {
@@ -339,7 +360,7 @@ async function validateBindings(sources, validation, context, groups, compiler)
339
360
  const source = program.getSourceFile(group.declaration.file);
340
361
  const assignment = source.statements.find(ts.isExportAssignment);
341
362
  const call = assignment.expression;
342
- for (const expression of properties(call.arguments[1], "RPC methods").values()) {
363
+ for (const expression of builderCallback(call.arguments[1]).procedures.values()) {
343
364
  const procedure = expression;
344
365
  const fields = properties(procedure.arguments[0], "Procedure options");
345
366
  const input = fields.get("input");
@@ -434,7 +455,7 @@ function checkGeneratedDomain(sources, validation, generated) {
434
455
  }));
435
456
  }
436
457
  }
437
- async function runGeneration(workspace, options, stopped, compiler) {
458
+ async function runGeneration(workspace, options, stopped, compiler, discovered = () => undefined) {
438
459
  // The built package root (dist/ in the workspace, the tarball root when installed).
439
460
  const root = fileURLToPath(new URL("../", import.meta.url));
440
461
  const manifest = JSON.parse(await readFile(resolve(root, "package.json"), "utf8"));
@@ -477,6 +498,7 @@ async function runGeneration(workspace, options, stopped, compiler) {
477
498
  throw new Error("Launch generation requires effective strict TypeScript checking: noCheck must be false");
478
499
  }
479
500
  const { context, groups } = await groupsOf(sources);
501
+ discovered(context.file);
480
502
  const appManifest = JSON.parse(await readFile(resolve(application, "package.json"), "utf8"));
481
503
  if (typeof appManifest.name !== "string" || appManifest.name.length === 0) {
482
504
  throw new Error("Launch application package.json requires a name for generated app identity");
@@ -598,7 +620,7 @@ export function createGenerationSession(workspace, { tsconfig, scanDirectories,
598
620
  slot.clear();
599
621
  }
600
622
  }
601
- return {
623
+ const session = {
602
624
  async generate({ check = false } = {}, stopped = () => false) {
603
625
  if (closed) {
604
626
  throw new Error("Generation session is disposed");
@@ -619,7 +641,9 @@ export function createGenerationSession(workspace, { tsconfig, scanDirectories,
619
641
  }
620
642
  return false;
621
643
  }
622
- const work = runGeneration(application, { ...fixed, check }, isStopped, compiler);
644
+ const work = runGeneration(application, { ...fixed, check }, isStopped, compiler, (file) => {
645
+ contextFiles.set(session, file);
646
+ });
623
647
  active = work;
624
648
  try {
625
649
  await work;
@@ -641,4 +665,5 @@ export function createGenerationSession(workspace, { tsconfig, scanDirectories,
641
665
  clear();
642
666
  },
643
667
  };
668
+ return session;
644
669
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@loozo/angular-launch",
3
- "version": "0.0.4",
3
+ "version": "0.0.5",
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",
@@ -47,9 +47,12 @@
47
47
  "registry": "https://registry.npmjs.org/"
48
48
  },
49
49
  "dependencies": {
50
+ "@angular-devkit/architect": "0.2202.2",
51
+ "chokidar": "5.0.0",
50
52
  "typescript": "6.0.3"
51
53
  },
52
54
  "peerDependencies": {
55
+ "@angular/build": "~22.2.0",
53
56
  "@angular/common": "^22.0.0",
54
57
  "@angular/core": "^22.0.0",
55
58
  "@loozo/angular-router": "0.0.6",
@@ -60,5 +63,6 @@
60
63
  "@tanstack/angular-query": {
61
64
  "optional": true
62
65
  }
63
- }
66
+ },
67
+ "builders": "./builders.json"
64
68
  }