vector-framework 0.8.1 → 0.9.0
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 +78 -56
- package/dist/cache/manager.d.ts +1 -1
- package/dist/cache/manager.d.ts.map +1 -1
- package/dist/cache/manager.js +8 -3
- package/dist/cache/manager.js.map +1 -1
- package/dist/cli/index.js +134 -68
- package/dist/cli/index.js.map +1 -1
- package/dist/core/config-loader.d.ts +13 -0
- package/dist/core/config-loader.d.ts.map +1 -0
- package/dist/core/config-loader.js +166 -0
- package/dist/core/config-loader.js.map +1 -0
- package/dist/core/router.d.ts +1 -0
- package/dist/core/router.d.ts.map +1 -1
- package/dist/core/router.js +5 -1
- package/dist/core/router.js.map +1 -1
- package/dist/core/vector.d.ts +10 -12
- package/dist/core/vector.d.ts.map +1 -1
- package/dist/core/vector.js +37 -31
- package/dist/core/vector.js.map +1 -1
- package/dist/dev/route-generator.js +3 -3
- package/dist/dev/route-generator.js.map +1 -1
- package/dist/dev/route-scanner.d.ts.map +1 -1
- package/dist/dev/route-scanner.js +15 -4
- package/dist/dev/route-scanner.js.map +1 -1
- package/dist/http.d.ts +6 -1
- package/dist/http.d.ts.map +1 -1
- package/dist/http.js +13 -4
- package/dist/http.js.map +1 -1
- package/dist/index.d.ts +4 -12
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -3
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +5 -5
- package/dist/middleware/manager.d.ts +2 -1
- package/dist/middleware/manager.d.ts.map +1 -1
- package/dist/middleware/manager.js +4 -0
- package/dist/middleware/manager.js.map +1 -1
- package/dist/types/index.d.ts +26 -0
- package/dist/types/index.d.ts.map +1 -1
- package/dist/utils/path.d.ts +3 -0
- package/dist/utils/path.d.ts.map +1 -0
- package/dist/utils/path.js +9 -0
- package/dist/utils/path.js.map +1 -0
- package/package.json +2 -2
- package/src/cache/manager.ts +15 -5
- package/src/cli/index.ts +156 -71
- package/src/core/config-loader.ts +193 -0
- package/src/core/router.ts +6 -1
- package/src/core/vector.ts +39 -39
- package/src/dev/route-generator.ts +3 -3
- package/src/dev/route-scanner.ts +15 -4
- package/src/http.ts +21 -5
- package/src/index.ts +11 -16
- package/src/middleware/manager.ts +16 -4
- package/src/types/index.ts +45 -1
- package/src/utils/path.ts +9 -0
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { promises as fs } from 'node:fs';
|
|
2
2
|
import { dirname, relative } from 'node:path';
|
|
3
3
|
import type { GeneratedRoute } from '../types';
|
|
4
4
|
|
|
@@ -11,7 +11,7 @@ export class RouteGenerator {
|
|
|
11
11
|
|
|
12
12
|
async generate(routes: GeneratedRoute[]): Promise<void> {
|
|
13
13
|
const outputDir = dirname(this.outputPath);
|
|
14
|
-
await mkdir(outputDir, { recursive: true });
|
|
14
|
+
await fs.mkdir(outputDir, { recursive: true });
|
|
15
15
|
|
|
16
16
|
const imports: string[] = [];
|
|
17
17
|
const groupedByFile = new Map<string, GeneratedRoute[]>();
|
|
@@ -64,7 +64,7 @@ ${routeEntries.join('\n')}
|
|
|
64
64
|
export default routes;
|
|
65
65
|
`;
|
|
66
66
|
|
|
67
|
-
await writeFile(this.outputPath, content, 'utf-8');
|
|
67
|
+
await fs.writeFile(this.outputPath, content, 'utf-8');
|
|
68
68
|
console.log(`Generated routes file: ${this.outputPath}`);
|
|
69
69
|
}
|
|
70
70
|
|
package/src/dev/route-scanner.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { promises as fs } from 'node:fs';
|
|
2
2
|
import { join, relative, resolve, sep } from 'node:path';
|
|
3
3
|
import type { GeneratedRoute } from '../types';
|
|
4
4
|
|
|
@@ -26,11 +26,11 @@ export class RouteScanner {
|
|
|
26
26
|
}
|
|
27
27
|
|
|
28
28
|
private async scanDirectory(dir: string, routes: GeneratedRoute[], basePath = ''): Promise<void> {
|
|
29
|
-
const entries = await readdir(dir);
|
|
29
|
+
const entries = await fs.readdir(dir);
|
|
30
30
|
|
|
31
31
|
for (const entry of entries) {
|
|
32
32
|
const fullPath = join(dir, entry);
|
|
33
|
-
const stats = await stat(fullPath);
|
|
33
|
+
const stats = await fs.stat(fullPath);
|
|
34
34
|
|
|
35
35
|
if (stats.isDirectory()) {
|
|
36
36
|
const newBasePath = basePath ? `${basePath}/${entry}` : entry;
|
|
@@ -64,7 +64,18 @@ export class RouteScanner {
|
|
|
64
64
|
for (const [name, value] of Object.entries(module)) {
|
|
65
65
|
if (name === 'default') continue;
|
|
66
66
|
|
|
67
|
-
|
|
67
|
+
// Check for new RouteDefinition format
|
|
68
|
+
if (value && typeof value === 'object' && 'entry' in value && 'options' in value && 'handler' in value) {
|
|
69
|
+
const routeDef = value as any;
|
|
70
|
+
routes.push({
|
|
71
|
+
name,
|
|
72
|
+
path: fullPath,
|
|
73
|
+
method: routeDef.options.method as string,
|
|
74
|
+
options: routeDef.options,
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
// Legacy RouteEntry format support
|
|
78
|
+
else if (Array.isArray(value) && value.length >= 4) {
|
|
68
79
|
const [method, , , path] = value;
|
|
69
80
|
routes.push({
|
|
70
81
|
name,
|
package/src/http.ts
CHANGED
|
@@ -33,13 +33,19 @@ interface ExtendedApiOptions extends ApiOptions {
|
|
|
33
33
|
path: string;
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
+
export interface RouteDefinition<TTypes extends VectorTypes = DefaultVectorTypes> {
|
|
37
|
+
entry: RouteEntry;
|
|
38
|
+
options: ExtendedApiOptions;
|
|
39
|
+
handler: (req: VectorRequest<TTypes>) => Promise<unknown>;
|
|
40
|
+
}
|
|
41
|
+
|
|
36
42
|
export function route<TTypes extends VectorTypes = DefaultVectorTypes>(
|
|
37
43
|
options: ExtendedApiOptions,
|
|
38
44
|
fn: (req: VectorRequest<TTypes>) => Promise<unknown>
|
|
39
|
-
):
|
|
45
|
+
): RouteDefinition<TTypes> {
|
|
40
46
|
const handler = api(options, fn);
|
|
41
47
|
|
|
42
|
-
|
|
48
|
+
const entry: RouteEntry = [
|
|
43
49
|
options.method.toUpperCase(),
|
|
44
50
|
RegExp(
|
|
45
51
|
`^${
|
|
@@ -54,6 +60,12 @@ export function route<TTypes extends VectorTypes = DefaultVectorTypes>(
|
|
|
54
60
|
[handler],
|
|
55
61
|
options.path,
|
|
56
62
|
];
|
|
63
|
+
|
|
64
|
+
return {
|
|
65
|
+
entry,
|
|
66
|
+
options,
|
|
67
|
+
handler: fn
|
|
68
|
+
};
|
|
57
69
|
}
|
|
58
70
|
|
|
59
71
|
function stringifyData(data: unknown): string {
|
|
@@ -251,9 +263,11 @@ export const protectedRoute = async <
|
|
|
251
263
|
responseContentType?: string
|
|
252
264
|
) => {
|
|
253
265
|
// Get the Vector instance to access the protected handler
|
|
254
|
-
const
|
|
266
|
+
const { getVectorInstance } = await import("./core/vector");
|
|
267
|
+
const vector = getVectorInstance();
|
|
255
268
|
|
|
256
|
-
|
|
269
|
+
const protectedHandler = vector.getProtectedHandler();
|
|
270
|
+
if (!protectedHandler) {
|
|
257
271
|
throw APIError.unauthorized(
|
|
258
272
|
"Authentication not configured",
|
|
259
273
|
responseContentType
|
|
@@ -261,7 +275,7 @@ export const protectedRoute = async <
|
|
|
261
275
|
}
|
|
262
276
|
|
|
263
277
|
try {
|
|
264
|
-
const authUser = await
|
|
278
|
+
const authUser = await protectedHandler(request as any);
|
|
265
279
|
request.authUser = authUser as GetAuthType<TTypes>;
|
|
266
280
|
} catch (error) {
|
|
267
281
|
throw APIError.unauthorized(
|
|
@@ -292,6 +306,8 @@ export function api<TTypes extends VectorTypes = DefaultVectorTypes>(
|
|
|
292
306
|
responseContentType = CONTENT_TYPES.JSON,
|
|
293
307
|
} = options;
|
|
294
308
|
|
|
309
|
+
// For backward compatibility with direct route usage (not auto-discovered)
|
|
310
|
+
// This wrapper is only used when routes are NOT auto-discovered
|
|
295
311
|
return async (request: IRequest) => {
|
|
296
312
|
if (!expose) {
|
|
297
313
|
return APIError.forbidden("Forbidden");
|
package/src/index.ts
CHANGED
|
@@ -1,19 +1,14 @@
|
|
|
1
|
-
|
|
2
|
-
import { route } from
|
|
3
|
-
import type { DefaultVectorTypes, VectorTypes } from './types';
|
|
1
|
+
// Public exports for route definitions only
|
|
2
|
+
import { route } from "./http";
|
|
4
3
|
|
|
5
|
-
|
|
6
|
-
export {
|
|
7
|
-
export { CacheManager } from './cache/manager';
|
|
8
|
-
export { APIError, createResponse } from './http';
|
|
9
|
-
export { MiddlewareManager } from './middleware/manager';
|
|
10
|
-
export * from './types';
|
|
4
|
+
// Export route function for defining routes
|
|
5
|
+
export { route };
|
|
11
6
|
|
|
12
|
-
//
|
|
13
|
-
export
|
|
14
|
-
return Vector.getInstance<TTypes>();
|
|
15
|
-
}
|
|
7
|
+
// Export utilities for route handlers
|
|
8
|
+
export { APIError, createResponse } from "./http";
|
|
16
9
|
|
|
17
|
-
//
|
|
18
|
-
|
|
19
|
-
|
|
10
|
+
// Export types for TypeScript users
|
|
11
|
+
export * from "./types";
|
|
12
|
+
|
|
13
|
+
// Note: Vector framework is now config-driven and runs via CLI
|
|
14
|
+
// Usage: Create vector.config.ts and run 'vector dev' or 'vector start'
|
|
@@ -4,9 +4,11 @@ import type {
|
|
|
4
4
|
DefaultVectorTypes,
|
|
5
5
|
VectorRequest,
|
|
6
6
|
VectorTypes,
|
|
7
|
-
} from
|
|
7
|
+
} from "../types";
|
|
8
8
|
|
|
9
|
-
export class MiddlewareManager<
|
|
9
|
+
export class MiddlewareManager<
|
|
10
|
+
TTypes extends VectorTypes = DefaultVectorTypes
|
|
11
|
+
> {
|
|
10
12
|
private beforeHandlers: BeforeMiddlewareHandler<TTypes>[] = [];
|
|
11
13
|
private finallyHandlers: AfterMiddlewareHandler<TTypes>[] = [];
|
|
12
14
|
|
|
@@ -18,7 +20,9 @@ export class MiddlewareManager<TTypes extends VectorTypes = DefaultVectorTypes>
|
|
|
18
20
|
this.finallyHandlers.push(...handlers);
|
|
19
21
|
}
|
|
20
22
|
|
|
21
|
-
async executeBefore(
|
|
23
|
+
async executeBefore(
|
|
24
|
+
request: VectorRequest<TTypes>
|
|
25
|
+
): Promise<VectorRequest<TTypes> | Response> {
|
|
22
26
|
let currentRequest = request;
|
|
23
27
|
|
|
24
28
|
for (const handler of this.beforeHandlers) {
|
|
@@ -34,7 +38,10 @@ export class MiddlewareManager<TTypes extends VectorTypes = DefaultVectorTypes>
|
|
|
34
38
|
return currentRequest;
|
|
35
39
|
}
|
|
36
40
|
|
|
37
|
-
async executeFinally(
|
|
41
|
+
async executeFinally(
|
|
42
|
+
response: Response,
|
|
43
|
+
request: VectorRequest<TTypes>
|
|
44
|
+
): Promise<Response> {
|
|
38
45
|
let currentResponse = response;
|
|
39
46
|
|
|
40
47
|
for (const handler of this.finallyHandlers) {
|
|
@@ -50,4 +57,9 @@ export class MiddlewareManager<TTypes extends VectorTypes = DefaultVectorTypes>
|
|
|
50
57
|
manager.finallyHandlers = [...this.finallyHandlers];
|
|
51
58
|
return manager;
|
|
52
59
|
}
|
|
60
|
+
|
|
61
|
+
clear(): void {
|
|
62
|
+
this.beforeHandlers = [];
|
|
63
|
+
this.finallyHandlers = [];
|
|
64
|
+
}
|
|
53
65
|
}
|
package/src/types/index.ts
CHANGED
|
@@ -64,7 +64,7 @@ export interface RouteOptions<TTypes extends VectorTypes = DefaultVectorTypes> {
|
|
|
64
64
|
method: string;
|
|
65
65
|
path: string;
|
|
66
66
|
auth?: boolean;
|
|
67
|
-
expose?: boolean;
|
|
67
|
+
expose?: boolean; // defaults to true
|
|
68
68
|
cache?: CacheOptions | number;
|
|
69
69
|
rawRequest?: boolean;
|
|
70
70
|
rawResponse?: boolean;
|
|
@@ -72,6 +72,7 @@ export interface RouteOptions<TTypes extends VectorTypes = DefaultVectorTypes> {
|
|
|
72
72
|
metadata?: GetMetadataType<TTypes>;
|
|
73
73
|
}
|
|
74
74
|
|
|
75
|
+
// Legacy config interface - will be deprecated
|
|
75
76
|
export interface VectorConfig<TTypes extends VectorTypes = DefaultVectorTypes> {
|
|
76
77
|
port?: number;
|
|
77
78
|
hostname?: string;
|
|
@@ -84,6 +85,49 @@ export interface VectorConfig<TTypes extends VectorTypes = DefaultVectorTypes> {
|
|
|
84
85
|
autoDiscover?: boolean;
|
|
85
86
|
}
|
|
86
87
|
|
|
88
|
+
// New config-driven schema
|
|
89
|
+
export interface VectorConfigSchema<TTypes extends VectorTypes = DefaultVectorTypes> {
|
|
90
|
+
// Server configuration
|
|
91
|
+
server?: {
|
|
92
|
+
port?: number;
|
|
93
|
+
hostname?: string;
|
|
94
|
+
reusePort?: boolean;
|
|
95
|
+
development?: boolean;
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
// Routes configuration
|
|
99
|
+
routes?: {
|
|
100
|
+
dir?: string;
|
|
101
|
+
autoDiscover?: boolean;
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
// Middleware configuration - supports both file paths and direct functions
|
|
105
|
+
middleware?: {
|
|
106
|
+
before?: string[];
|
|
107
|
+
after?: string[];
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
// Direct middleware functions (preferred approach)
|
|
111
|
+
before?: BeforeMiddlewareHandler<TTypes>[];
|
|
112
|
+
after?: AfterMiddlewareHandler<TTypes>[];
|
|
113
|
+
|
|
114
|
+
// Handler configuration - supports both file paths and direct functions
|
|
115
|
+
handlers?: {
|
|
116
|
+
auth?: string;
|
|
117
|
+
cache?: string;
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
// Direct handler functions (preferred approach)
|
|
121
|
+
auth?: ProtectedHandler<TTypes>;
|
|
122
|
+
cache?: CacheHandler;
|
|
123
|
+
|
|
124
|
+
// CORS configuration
|
|
125
|
+
cors?: CorsOptions | boolean;
|
|
126
|
+
|
|
127
|
+
// Custom types for TypeScript
|
|
128
|
+
types?: VectorTypes;
|
|
129
|
+
}
|
|
130
|
+
|
|
87
131
|
export interface CorsOptions {
|
|
88
132
|
origin?: string | string[] | ((origin: string) => boolean);
|
|
89
133
|
credentials?: boolean;
|