Skip to content

Commit 9cda3ad

Browse files
Copilothotlong
andcommitted
Move escapeRegexPath to utils.ts, normalize all paths in resolveApiRoutes, remove unused import
Co-authored-by: hotlong <50353452+hotlong@users.noreply.github.com>
1 parent 66675bc commit 9cda3ad

File tree

8 files changed

+34
-23
lines changed

8 files changed

+34
-23
lines changed

packages/foundation/types/src/api.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -561,12 +561,15 @@ export const DEFAULT_API_ROUTES: ResolvedApiRouteConfig = {
561561

562562
/**
563563
* Resolve API route configuration by merging user config with defaults
564+
* All paths are normalized to start with '/'
564565
*/
565566
export function resolveApiRoutes(config?: ApiRouteConfig): ResolvedApiRouteConfig {
567+
const normalizePath = (path: string): string => path.startsWith('/') ? path : `/${path}`;
568+
566569
return {
567-
rpc: config?.rpc ?? DEFAULT_API_ROUTES.rpc,
568-
data: config?.data ?? DEFAULT_API_ROUTES.data,
569-
metadata: config?.metadata ?? DEFAULT_API_ROUTES.metadata,
570-
files: config?.files ?? DEFAULT_API_ROUTES.files
570+
rpc: normalizePath(config?.rpc ?? DEFAULT_API_ROUTES.rpc),
571+
data: normalizePath(config?.data ?? DEFAULT_API_ROUTES.data),
572+
metadata: normalizePath(config?.metadata ?? DEFAULT_API_ROUTES.metadata),
573+
files: normalizePath(config?.files ?? DEFAULT_API_ROUTES.files)
571574
};
572575
}

packages/runtime/server/src/adapters/node.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { IncomingMessage, ServerResponse } from 'http';
55
import { generateOpenAPI } from '../openapi';
66
import { createFileUploadHandler, createBatchFileUploadHandler, createFileDownloadHandler } from '../file-handler';
77
import { LocalFileStorage } from '../storage';
8-
import { escapeRegexPath } from '../types';
8+
import { escapeRegexPath } from '../utils';
99

1010
/**
1111
* Options for createNodeHandler
@@ -25,8 +25,7 @@ export function createNodeHandler(app: IObjectQL, options?: NodeHandlerOptions)
2525
const routes = resolveApiRoutes(options?.routes);
2626

2727
// Initialize file storage
28-
const filesPath = routes.files.startsWith('/') ? routes.files : `/${routes.files}`;
29-
const defaultBaseUrl = process.env.OBJECTQL_BASE_URL || `http://localhost:3000${filesPath}`;
28+
const defaultBaseUrl = process.env.OBJECTQL_BASE_URL || `http://localhost:3000${routes.files}`;
3029
const fileStorage = options?.fileStorage || new LocalFileStorage({
3130
baseDir: process.env.OBJECTQL_UPLOAD_DIR || './uploads',
3231
baseUrl: defaultBaseUrl

packages/runtime/server/src/adapters/rest.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { IObjectQL, ApiRouteConfig, resolveApiRoutes } from '@objectql/types';
22
import { ObjectQLServer } from '../server';
33
import { ObjectQLRequest, ErrorCode } from '../types';
44
import { IncomingMessage, ServerResponse } from 'http';
5-
import { escapeRegexPath } from '../types';
5+
import { escapeRegexPath } from '../utils';
66

77
/**
88
* Parse query string parameters

packages/runtime/server/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export * from './types';
2+
export * from './utils';
23
export * from './openapi';
34
export * from './server';
45
export * from './metadata';

packages/runtime/server/src/metadata.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { IObjectQL, ApiRouteConfig, resolveApiRoutes } from '@objectql/types';
22
import { IncomingMessage, ServerResponse } from 'http';
33
import { ErrorCode } from './types';
4-
import { escapeRegexPath } from './types';
4+
import { escapeRegexPath } from './utils';
55

66
function readBody(req: IncomingMessage): Promise<any> {
77
return new Promise((resolve, reject) => {

packages/runtime/server/src/openapi.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { IObjectQL, ObjectConfig, FieldType, FieldConfig, ApiRouteConfig, resolveApiRoutes } from '@objectql/types';
1+
import { IObjectQL, ObjectConfig, FieldConfig, ApiRouteConfig, resolveApiRoutes } from '@objectql/types';
22

33
interface OpenAPISchema {
44
openapi: string;

packages/runtime/server/src/types.ts

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -171,16 +171,3 @@ export interface FileStorageOptions {
171171
/** User ID who uploaded the file */
172172
userId?: string;
173173
}
174-
175-
/**
176-
* Utility functions for route handling
177-
*/
178-
179-
/**
180-
* Escapes special regex characters in a path string for use in RegExp
181-
* @param path - The path string to escape
182-
* @returns Escaped path string safe for use in RegExp
183-
*/
184-
export function escapeRegexPath(path: string): string {
185-
return path.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
186-
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* Utility functions for server operations
3+
*/
4+
5+
/**
6+
* Escapes special regex characters in a path string for use in RegExp
7+
* @param path - The path string to escape
8+
* @returns Escaped path string safe for use in RegExp
9+
*/
10+
export function escapeRegexPath(path: string): string {
11+
return path.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
12+
}
13+
14+
/**
15+
* Normalizes a path to ensure it starts with a forward slash
16+
* @param path - The path string to normalize
17+
* @returns Normalized path string starting with '/'
18+
*/
19+
export function normalizePath(path: string): string {
20+
return path.startsWith('/') ? path : `/${path}`;
21+
}

0 commit comments

Comments
 (0)