@analogjs/router 3.0.0-alpha.96 → 3.0.0-alpha.97

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 @@
1
+ {"version":3,"file":"remove-request-context-interceptor.js","names":[],"sources":["../../../migrations/remove-request-context-interceptor/remove-request-context-interceptor.ts"],"sourcesContent":["import type { Rule, SchematicContext, Tree } from '@angular-devkit/schematics';\nimport * as ts from 'typescript';\n\nconst INTERCEPTOR = 'requestContextInterceptor';\n\ninterface Edit {\n start: number;\n end: number;\n}\n\ninterface NamedImport {\n declaration: ts.ImportDeclaration;\n bindings: ts.NamedImports;\n}\n\nfunction getNamedImports(sourceFile: ts.SourceFile): NamedImport[] {\n return sourceFile.statements.flatMap((declaration) => {\n const bindings = ts.isImportDeclaration(declaration)\n ? declaration.importClause?.namedBindings\n : undefined;\n return bindings && ts.isNamedImports(bindings)\n ? [{ declaration: declaration as ts.ImportDeclaration, bindings }]\n : [];\n });\n}\n\n/** Local names imported for `exportName` from `moduleName`. */\nfunction getImportNames(\n imports: NamedImport[],\n moduleName: string,\n exportName: string,\n): Set<string> {\n const names = new Set<string>();\n for (const { declaration, bindings } of imports) {\n if (\n !ts.isStringLiteral(declaration.moduleSpecifier) ||\n declaration.moduleSpecifier.text !== moduleName\n ) {\n continue;\n }\n for (const element of bindings.elements) {\n if ((element.propertyName ?? element.name).text === exportName) {\n names.add(element.name.text);\n }\n }\n }\n return names;\n}\n\nfunction isCallTo(\n node: ts.Node,\n names: Set<string>,\n): node is ts.CallExpression {\n return (\n ts.isCallExpression(node) &&\n ts.isIdentifier(node.expression) &&\n names.has(node.expression.text)\n );\n}\n\n/** Removes one element of a comma-separated list along with its separator. */\nfunction removeListElement(\n elements: ts.NodeArray<ts.Node>,\n index: number,\n listEnd: number,\n sourceFile: ts.SourceFile,\n): Edit {\n if (elements.length === 1) {\n return { start: elements.pos, end: listEnd };\n }\n if (index < elements.length - 1) {\n return {\n start: elements[index].getStart(sourceFile),\n end: elements[index + 1].getStart(sourceFile),\n };\n }\n return { start: elements[index - 1].getEnd(), end: elements[index].getEnd() };\n}\n\nfunction countReferences(sourceFile: ts.SourceFile, name: string): number {\n let count = 0;\n function visit(node: ts.Node): void {\n if (ts.isImportDeclaration(node)) return;\n if (ts.isIdentifier(node) && node.text === name) count++;\n ts.forEachChild(node, visit);\n }\n visit(sourceFile);\n return count;\n}\n\n/** Removes the import specifier for `name`, or the whole import if it is the only one. */\nfunction removeImport(\n imports: NamedImport[],\n name: string,\n sourceFile: ts.SourceFile,\n): Edit | undefined {\n for (const { declaration, bindings } of imports) {\n const index = bindings.elements.findIndex((el) => el.name.text === name);\n if (index === -1) continue;\n\n if (bindings.elements.length === 1 && !declaration.importClause?.name) {\n const end = declaration.getEnd();\n return {\n start: declaration.getStart(sourceFile),\n end: sourceFile.text[end] === '\\n' ? end + 1 : end,\n };\n }\n return removeListElement(\n bindings.elements,\n index,\n bindings.getEnd() - 1,\n sourceFile,\n );\n }\n return undefined;\n}\n\nfunction removeFromSource(filePath: string, source: string): string {\n const sourceFile = ts.createSourceFile(\n filePath,\n source,\n ts.ScriptTarget.Latest,\n true,\n );\n const imports = getNamedImports(sourceFile);\n const interceptorNames = getImportNames(\n imports,\n '@analogjs/router',\n INTERCEPTOR,\n );\n if (!interceptorNames.size) return source;\n\n const provideHttpClientNames = getImportNames(\n imports,\n '@angular/common/http',\n 'provideHttpClient',\n );\n const withInterceptorsNames = getImportNames(\n imports,\n '@angular/common/http',\n 'withInterceptors',\n );\n\n const edits: Edit[] = [];\n const removed = new Map<string, number>();\n const markRemoved = (name: string) =>\n removed.set(name, (removed.get(name) ?? 0) + 1);\n\n function visit(node: ts.Node): void {\n if (isCallTo(node, provideHttpClientNames)) {\n node.arguments.forEach((arg, argIndex) => {\n if (!isCallTo(arg, withInterceptorsNames)) return;\n const [interceptors] = arg.arguments;\n if (!interceptors || !ts.isArrayLiteralExpression(interceptors)) return;\n\n const index = interceptors.elements.findIndex(\n (el) => ts.isIdentifier(el) && interceptorNames.has(el.text),\n );\n if (index === -1) return;\n\n markRemoved((interceptors.elements[index] as ts.Identifier).text);\n if (interceptors.elements.length === 1) {\n markRemoved((arg.expression as ts.Identifier).text);\n edits.push(\n removeListElement(\n node.arguments,\n argIndex,\n node.getEnd() - 1,\n sourceFile,\n ),\n );\n } else {\n edits.push(\n removeListElement(\n interceptors.elements,\n index,\n interceptors.getEnd() - 1,\n sourceFile,\n ),\n );\n }\n });\n }\n ts.forEachChild(node, visit);\n }\n visit(sourceFile);\n\n for (const [name, count] of removed) {\n if (countReferences(sourceFile, name) === count) {\n const edit = removeImport(imports, name, sourceFile);\n if (edit) edits.push(edit);\n }\n }\n\n let result = source;\n for (const edit of edits.sort((a, b) => b.start - a.start)) {\n result = result.slice(0, edit.start) + result.slice(edit.end);\n }\n return result;\n}\n\nexport default function removeRequestContextInterceptor(): Rule {\n return (tree: Tree, context: SchematicContext) => {\n const updatedFiles: string[] = [];\n const remainingFiles: string[] = [];\n\n tree.visit((filePath) => {\n if (filePath.includes('/node_modules/') || !filePath.endsWith('.ts')) {\n return;\n }\n const source = tree.read(filePath)?.toString('utf-8');\n if (!source?.includes(INTERCEPTOR)) return;\n\n const result = removeFromSource(filePath, source);\n if (result !== source) {\n tree.overwrite(filePath, result);\n updatedFiles.push(filePath);\n }\n if (result.includes(INTERCEPTOR)) {\n remainingFiles.push(filePath);\n }\n });\n\n if (updatedFiles.length) {\n context.logger.info(\n `Removed requestContextInterceptor, now registered by provideFileRouter(), from: ${updatedFiles.join(', ')}`,\n );\n }\n if (remainingFiles.length) {\n context.logger.warn(\n `requestContextInterceptor is still referenced in: ${remainingFiles.join(', ')}. ` +\n 'provideFileRouter() now registers it automatically, so remove any manual registration.',\n );\n }\n\n return tree;\n };\n}\n"],"mappings":";;AAGA,IAAM,cAAc;AAYpB,SAAS,gBAAgB,YAAyB;AAChD,QAAO,WAAW,WAAW,SAAS,gBAAe;EACnD,MAAM,WAAW,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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@analogjs/router",
3
- "version": "3.0.0-alpha.96",
3
+ "version": "3.0.0-alpha.97",
4
4
  "description": "Filesystem-based routing for Angular",
5
5
  "type": "module",
6
6
  "author": "Brandon Roberts <robertsbt@gmail.com>",
@@ -69,7 +69,7 @@
69
69
  "url": "https://github.com/sponsors/brandonroberts"
70
70
  },
71
71
  "peerDependencies": {
72
- "@analogjs/content": "3.0.0-alpha.96",
72
+ "@analogjs/content": "3.0.0-alpha.97",
73
73
  "@angular/core": "^20.0.0 || ^21.0.0 || ^22.0.0",
74
74
  "@angular/platform-server": "^20.0.0 || ^21.0.0 || ^22.0.0",
75
75
  "@angular/router": "^20.0.0 || ^21.0.0 || ^22.0.0",
@@ -105,8 +105,8 @@
105
105
  "tslib": "^2.3.0"
106
106
  },
107
107
  "devDependencies": {
108
- "@analogjs/vite-plugin-angular": "3.0.0-alpha.96",
109
- "@analogjs/vitest-angular": "3.0.0-alpha.96"
108
+ "@analogjs/vite-plugin-angular": "3.0.0-alpha.97",
109
+ "@analogjs/vitest-angular": "3.0.0-alpha.97"
110
110
  },
111
111
  "ng-update": {
112
112
  "packageGroup": [
@@ -0,0 +1,2 @@
1
+ import type { Rule } from '@angular-devkit/schematics';
2
+ export default function removeRequestContextInterceptor(): Rule;
@@ -1,6 +1,17 @@
1
1
  import type { StandardSchemaV1 } from './standard-schema';
2
2
  import type { H3Event } from 'h3';
3
3
  export type DefineApiRouteResult = Response | unknown;
4
+ export interface ApiRouteHandler<TQuery = unknown, TBody = unknown, TResult = unknown> {
5
+ (event: H3Event): Promise<Response>;
6
+ readonly _types: {
7
+ readonly query: TQuery;
8
+ readonly body: TBody;
9
+ readonly result: TResult;
10
+ };
11
+ }
12
+ export type InferRouteQuery<T> = T extends ApiRouteHandler<infer Q, any, any> ? Q : never;
13
+ export type InferRouteBody<T> = T extends ApiRouteHandler<any, infer B, any> ? B : never;
14
+ export type InferRouteResult<T> = T extends ApiRouteHandler<any, any, infer R> ? Exclude<R, Response> : never;
4
15
  type OptionalSchema = StandardSchemaV1 | undefined;
5
16
  type InferSchema<TSchema extends OptionalSchema, TFallback = unknown> = TSchema extends StandardSchemaV1 ? StandardSchemaV1.InferOutput<TSchema> : TFallback;
6
17
  type ResolveDataSchema<TInput extends OptionalSchema, TQuery extends OptionalSchema, TBody extends OptionalSchema> = TInput extends StandardSchemaV1 ? StandardSchemaV1.InferOutput<TInput> : TQuery extends StandardSchemaV1 ? TBody extends StandardSchemaV1 ? StandardSchemaV1.InferOutput<TQuery> | StandardSchemaV1.InferOutput<TBody> : StandardSchemaV1.InferOutput<TQuery> : TBody extends StandardSchemaV1 ? StandardSchemaV1.InferOutput<TBody> : unknown;
@@ -52,5 +63,5 @@ export interface DefineApiRouteOptions<TInput extends StandardSchemaV1 | undefin
52
63
  * });
53
64
  * ```
54
65
  */
55
- export declare function defineApiRoute<TInput extends StandardSchemaV1 | undefined = undefined, TOutput extends StandardSchemaV1 | undefined = undefined, TQuery extends StandardSchemaV1 | undefined = undefined, TBody extends StandardSchemaV1 | undefined = undefined, TParams extends StandardSchemaV1 | undefined = undefined, TResult extends DefineApiRouteResult = DefineApiRouteResult>(options: DefineApiRouteOptions<TInput, TOutput, TQuery, TBody, TParams, TResult>): (event: H3Event) => Promise<Response>;
66
+ export declare function defineApiRoute<TInput extends StandardSchemaV1 | undefined = undefined, TOutput extends StandardSchemaV1 | undefined = undefined, TQuery extends StandardSchemaV1 | undefined = undefined, TBody extends StandardSchemaV1 | undefined = undefined, TParams extends StandardSchemaV1 | undefined = undefined, TResult extends DefineApiRouteResult = DefineApiRouteResult>(options: DefineApiRouteOptions<TInput, TOutput, TQuery, TBody, TParams, TResult>): ApiRouteHandler<InferSchema<TQuery, undefined>, InferSchema<TBody, undefined>, TResult>;
56
67
  export {};
@@ -20,7 +20,7 @@ export interface DefinePageLoadOptions<TParamsSchema extends OptionalSchema = un
20
20
  * Creates a typed page server load function with optional
21
21
  * Standard Schema validation for route params and query.
22
22
  *
23
- * Follows the same validation pattern as `defineServerRoute`:
23
+ * Follows the same validation pattern as `defineApiRoute`:
24
24
  * validates before invoking the handler and returns
25
25
  * `fail(422, issues)` on validation failure.
26
26
  *
@@ -1,10 +1,8 @@
1
1
  export type { PageServerAction } from './actions';
2
2
  export { json, redirect, fail } from './actions';
3
- export { defineServerRoute } from './define-server-route';
4
- export type { DefineServerRouteContext, DefineServerRouteOptions, DefineServerRouteResult, ServerRouteHandler, InferRouteQuery, InferRouteBody, InferRouteResult, } from './define-server-route';
5
3
  export { definePageLoad } from './define-page-load';
6
4
  export type { PageLoadContext, DefinePageLoadOptions, } from './define-page-load';
7
5
  export { validateWithSchema } from './validate';
8
6
  export type { StandardSchemaV1 } from './standard-schema';
9
7
  export { defineAction, type DefineActionContext, type DefineActionOptions, } from './define-action';
10
- export { defineApiRoute, type DefineApiRouteContext, type DefineApiRouteOptions, type DefineApiRouteResult, } from './define-api-route';
8
+ export { defineApiRoute, type DefineApiRouteContext, type DefineApiRouteOptions, type DefineApiRouteResult, type ApiRouteHandler, type InferRouteQuery, type InferRouteBody, type InferRouteResult, } from './define-api-route';
@@ -11,6 +11,7 @@ export declare class FormAction {
11
11
  private injector;
12
12
  private router;
13
13
  private route;
14
+ private apiPrefix;
14
15
  private path;
15
16
  constructor();
16
17
  submitted($event: any): void;
@@ -1,12 +1,12 @@
1
1
  import type { HttpClient } from '@angular/common/http';
2
2
  import type { CreateQueryOptions, CreateMutationOptions, CreateInfiniteQueryOptions, DefaultError, InfiniteData, QueryKey } from '@tanstack/angular-query-experimental';
3
- import type { ServerRouteHandler, InferRouteQuery, InferRouteBody, InferRouteResult } from '@analogjs/router/server/actions';
4
- export declare function serverQueryOptions<TRoute extends ServerRouteHandler<any, any, any>, TError = DefaultError, TData = InferRouteResult<TRoute>, TQueryKey extends QueryKey = QueryKey>(http: HttpClient, url: string, options: {
3
+ import type { ApiRouteHandler, InferRouteQuery, InferRouteBody, InferRouteResult } from '@analogjs/router/server/actions';
4
+ export declare function serverQueryOptions<TRoute extends ApiRouteHandler<any, any, any>, TError = DefaultError, TData = InferRouteResult<TRoute>, TQueryKey extends QueryKey = QueryKey>(http: HttpClient, url: string, options: {
5
5
  queryKey: TQueryKey;
6
6
  query?: InferRouteQuery<TRoute>;
7
7
  } & Omit<CreateQueryOptions<InferRouteResult<TRoute>, TError, TData, TQueryKey>, 'queryKey' | 'queryFn'>): CreateQueryOptions<InferRouteResult<TRoute>, TError, TData, TQueryKey>;
8
- export declare function serverMutationOptions<TRoute extends ServerRouteHandler<any, any, any>, TError = DefaultError, TOnMutateResult = unknown>(http: HttpClient, url: string, options?: Omit<CreateMutationOptions<InferRouteResult<TRoute>, TError, InferRouteBody<TRoute>, TOnMutateResult>, 'mutationFn'>): CreateMutationOptions<InferRouteResult<TRoute>, TError, InferRouteBody<TRoute>, TOnMutateResult>;
9
- export declare function serverInfiniteQueryOptions<TRoute extends ServerRouteHandler<any, any, any>, TError = DefaultError, TData = InfiniteData<InferRouteResult<TRoute>>, TQueryKey extends QueryKey = QueryKey, TPageParam = unknown>(http: HttpClient, url: string, options: {
8
+ export declare function serverMutationOptions<TRoute extends ApiRouteHandler<any, any, any>, TError = DefaultError, TOnMutateResult = unknown>(http: HttpClient, url: string, options?: Omit<CreateMutationOptions<InferRouteResult<TRoute>, TError, InferRouteBody<TRoute>, TOnMutateResult>, 'mutationFn'>): CreateMutationOptions<InferRouteResult<TRoute>, TError, InferRouteBody<TRoute>, TOnMutateResult>;
9
+ export declare function serverInfiniteQueryOptions<TRoute extends ApiRouteHandler<any, any, any>, TError = DefaultError, TData = InfiniteData<InferRouteResult<TRoute>>, TQueryKey extends QueryKey = QueryKey, TPageParam = unknown>(http: HttpClient, url: string, options: {
10
10
  queryKey: TQueryKey;
11
11
  query: (context: {
12
12
  pageParam: TPageParam;
@@ -1,68 +0,0 @@
1
- import type { StandardSchemaV1 } from './standard-schema';
2
- import type { H3Event } from 'h3';
3
- export type DefineServerRouteResult = Response | unknown;
4
- export interface ServerRouteHandler<TQuery = unknown, TBody = unknown, TResult = unknown> {
5
- (event: H3Event): Promise<Response>;
6
- readonly _types: {
7
- readonly query: TQuery;
8
- readonly body: TBody;
9
- readonly result: TResult;
10
- };
11
- }
12
- export type InferRouteQuery<T> = T extends ServerRouteHandler<infer Q, any, any> ? Q : never;
13
- export type InferRouteBody<T> = T extends ServerRouteHandler<any, infer B, any> ? B : never;
14
- export type InferRouteResult<T> = T extends ServerRouteHandler<any, any, infer R> ? Exclude<R, Response> : never;
15
- type OptionalSchema = StandardSchemaV1 | undefined;
16
- type InferSchema<TSchema extends OptionalSchema, TFallback = unknown> = TSchema extends StandardSchemaV1 ? StandardSchemaV1.InferOutput<TSchema> : TFallback;
17
- type ResolveDataSchema<TInput extends OptionalSchema, TQuery extends OptionalSchema, TBody extends OptionalSchema> = TInput extends StandardSchemaV1 ? StandardSchemaV1.InferOutput<TInput> : TQuery extends StandardSchemaV1 ? TBody extends StandardSchemaV1 ? StandardSchemaV1.InferOutput<TQuery> | StandardSchemaV1.InferOutput<TBody> : StandardSchemaV1.InferOutput<TQuery> : TBody extends StandardSchemaV1 ? StandardSchemaV1.InferOutput<TBody> : unknown;
18
- export interface DefineServerRouteContext<TInput extends StandardSchemaV1 | undefined = undefined, TQuery extends StandardSchemaV1 | undefined = undefined, TBody extends StandardSchemaV1 | undefined = undefined, TParams extends StandardSchemaV1 | undefined = undefined> {
19
- data: ResolveDataSchema<TInput, TQuery, TBody>;
20
- query: InferSchema<TQuery, undefined>;
21
- body: InferSchema<TBody, undefined>;
22
- params: InferSchema<TParams, H3Event['context']['params']>;
23
- event: H3Event;
24
- }
25
- export interface DefineServerRouteOptions<TInput extends StandardSchemaV1 | undefined = undefined, TOutput extends StandardSchemaV1 | undefined = undefined, TQuery extends StandardSchemaV1 | undefined = undefined, TBody extends StandardSchemaV1 | undefined = undefined, TParams extends StandardSchemaV1 | undefined = undefined, TResult extends DefineServerRouteResult = DefineServerRouteResult> {
26
- input?: TInput;
27
- query?: TQuery;
28
- body?: TBody;
29
- params?: TParams;
30
- output?: TOutput;
31
- handler: (context: DefineServerRouteContext<TInput, TQuery, TBody, TParams>) => Promise<TResult> | TResult;
32
- }
33
- /**
34
- * Creates an h3-compatible event handler with Standard Schema validation.
35
- *
36
- * - `input` schema validates the request body (POST/PUT/PATCH) or query
37
- * params (GET). Returns 422 with `StandardSchemaV1.Issue[]` on failure.
38
- * - `output` schema validates the response in development only (stripped
39
- * in production for zero overhead). Logs a warning on mismatch.
40
- * - Plain return values are serialized with `json(...)`; raw `Response`
41
- * objects are returned unchanged.
42
- *
43
- * @example
44
- * ```typescript
45
- * import { defineServerRoute } from '@analogjs/router/server/actions';
46
- * import * as v from 'valibot';
47
- *
48
- * const Input = v.object({
49
- * name: v.pipe(v.string(), v.minLength(1)),
50
- * email: v.pipe(v.string(), v.email()),
51
- * });
52
- * const Output = v.object({
53
- * id: v.string(),
54
- * name: v.string(),
55
- * });
56
- *
57
- * export default defineServerRoute({
58
- * input: Input,
59
- * output: Output,
60
- * handler: async ({ data }) => {
61
- * const user = await db.users.create(data);
62
- * return user;
63
- * },
64
- * });
65
- * ```
66
- */
67
- export declare function defineServerRoute<TInput extends StandardSchemaV1 | undefined = undefined, TOutput extends StandardSchemaV1 | undefined = undefined, TQuery extends StandardSchemaV1 | undefined = undefined, TBody extends StandardSchemaV1 | undefined = undefined, TParams extends StandardSchemaV1 | undefined = undefined, TResult extends DefineServerRouteResult = DefineServerRouteResult>(options: DefineServerRouteOptions<TInput, TOutput, TQuery, TBody, TParams, TResult>): ServerRouteHandler<InferSchema<TQuery, undefined>, InferSchema<TBody, undefined>, TResult>;
68
- export {};