vector-framework 1.1.1 → 1.2.1
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 +99 -628
- package/dist/auth/protected.d.ts +1 -0
- package/dist/auth/protected.d.ts.map +1 -1
- package/dist/auth/protected.js +3 -0
- package/dist/auth/protected.js.map +1 -1
- package/dist/cache/manager.d.ts +1 -0
- package/dist/cache/manager.d.ts.map +1 -1
- package/dist/cache/manager.js +5 -7
- package/dist/cache/manager.js.map +1 -1
- package/dist/cli/graceful-shutdown.d.ts +15 -0
- package/dist/cli/graceful-shutdown.d.ts.map +1 -0
- package/dist/cli/graceful-shutdown.js +42 -0
- package/dist/cli/graceful-shutdown.js.map +1 -0
- package/dist/cli/index.js +46 -97
- package/dist/cli/index.js.map +1 -1
- package/dist/cli/option-resolution.d.ts +4 -0
- package/dist/cli/option-resolution.d.ts.map +1 -0
- package/dist/cli/option-resolution.js +28 -0
- package/dist/cli/option-resolution.js.map +1 -0
- package/dist/cli.js +3423 -660
- package/dist/constants/index.d.ts +3 -0
- package/dist/constants/index.d.ts.map +1 -1
- package/dist/constants/index.js +6 -0
- package/dist/constants/index.js.map +1 -1
- package/dist/core/config-loader.d.ts.map +1 -1
- package/dist/core/config-loader.js +7 -2
- package/dist/core/config-loader.js.map +1 -1
- package/dist/core/router.d.ts +41 -17
- package/dist/core/router.d.ts.map +1 -1
- package/dist/core/router.js +432 -153
- package/dist/core/router.js.map +1 -1
- package/dist/core/server.d.ts +17 -1
- package/dist/core/server.d.ts.map +1 -1
- package/dist/core/server.js +471 -31
- package/dist/core/server.js.map +1 -1
- package/dist/core/vector.d.ts +8 -5
- package/dist/core/vector.d.ts.map +1 -1
- package/dist/core/vector.js +53 -14
- package/dist/core/vector.js.map +1 -1
- package/dist/dev/route-generator.d.ts.map +1 -1
- 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 +1 -5
- package/dist/dev/route-scanner.js.map +1 -1
- package/dist/http.d.ts +14 -14
- package/dist/http.d.ts.map +1 -1
- package/dist/http.js +34 -41
- package/dist/http.js.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1420 -8
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1420 -8
- 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/openapi/docs-ui.d.ts +2 -0
- package/dist/openapi/docs-ui.d.ts.map +1 -0
- package/dist/openapi/docs-ui.js +1425 -0
- package/dist/openapi/docs-ui.js.map +1 -0
- package/dist/openapi/generator.d.ts +12 -0
- package/dist/openapi/generator.d.ts.map +1 -0
- package/dist/openapi/generator.js +502 -0
- package/dist/openapi/generator.js.map +1 -0
- package/dist/start-vector.d.ts +3 -0
- package/dist/start-vector.d.ts.map +1 -0
- package/dist/start-vector.js +38 -0
- package/dist/start-vector.js.map +1 -0
- package/dist/types/index.d.ts +95 -11
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/standard-schema.d.ts +118 -0
- package/dist/types/standard-schema.d.ts.map +1 -0
- package/dist/types/standard-schema.js +2 -0
- package/dist/types/standard-schema.js.map +1 -0
- package/dist/utils/cors.d.ts +13 -0
- package/dist/utils/cors.d.ts.map +1 -0
- package/dist/utils/cors.js +89 -0
- package/dist/utils/cors.js.map +1 -0
- package/dist/utils/logger.js +1 -1
- package/dist/utils/path.d.ts +6 -0
- package/dist/utils/path.d.ts.map +1 -1
- package/dist/utils/path.js +5 -0
- package/dist/utils/path.js.map +1 -1
- package/dist/utils/schema-validation.d.ts +31 -0
- package/dist/utils/schema-validation.d.ts.map +1 -0
- package/dist/utils/schema-validation.js +77 -0
- package/dist/utils/schema-validation.js.map +1 -0
- package/dist/utils/validation.d.ts.map +1 -1
- package/dist/utils/validation.js +3 -0
- package/dist/utils/validation.js.map +1 -1
- package/package.json +15 -12
- package/src/auth/protected.ts +7 -13
- package/src/cache/manager.ts +8 -18
- package/src/cli/graceful-shutdown.ts +60 -0
- package/src/cli/index.ts +52 -115
- package/src/cli/option-resolution.ts +40 -0
- package/src/constants/index.ts +7 -0
- package/src/core/config-loader.ts +7 -4
- package/src/core/router.ts +502 -156
- package/src/core/server.ts +610 -33
- package/src/core/vector.ts +87 -33
- package/src/dev/route-generator.ts +1 -3
- package/src/dev/route-scanner.ts +2 -9
- package/src/http.ts +85 -125
- package/src/index.ts +4 -3
- package/src/middleware/manager.ts +4 -0
- package/src/openapi/assets/favicon/android-chrome-192x192.png +0 -0
- package/src/openapi/assets/favicon/android-chrome-512x512.png +0 -0
- package/src/openapi/assets/favicon/apple-touch-icon.png +0 -0
- package/src/openapi/assets/favicon/favicon-16x16.png +0 -0
- package/src/openapi/assets/favicon/favicon-32x32.png +0 -0
- package/src/openapi/assets/favicon/favicon.ico +0 -0
- package/src/openapi/assets/favicon/site.webmanifest +11 -0
- package/src/openapi/assets/logo.svg +12 -0
- package/src/openapi/assets/logo_dark.svg +6 -0
- package/src/openapi/assets/logo_icon.png +0 -0
- package/src/openapi/assets/logo_white.svg +6 -0
- package/src/openapi/assets/tailwindcdn.js +83 -0
- package/src/openapi/docs-ui.ts +1435 -0
- package/src/openapi/generator.ts +586 -0
- package/src/start-vector.ts +50 -0
- package/src/types/index.ts +138 -17
- package/src/types/standard-schema.ts +147 -0
- package/src/utils/cors.ts +101 -0
- package/src/utils/logger.ts +1 -1
- package/src/utils/path.ts +6 -0
- package/src/utils/schema-validation.ts +123 -0
- package/src/utils/validation.ts +3 -0
package/src/types/index.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { StandardJSONSchemaV1, StandardSchemaV1, StandardTypedV1 } from './standard-schema';
|
|
2
|
+
import type { Server } from 'bun';
|
|
2
3
|
|
|
3
4
|
// Default AuthUser type - users can override this with their own type
|
|
4
5
|
export interface DefaultAuthUser {
|
|
@@ -27,13 +28,9 @@ export interface DefaultVectorTypes extends VectorTypes {
|
|
|
27
28
|
}
|
|
28
29
|
|
|
29
30
|
// Type helpers
|
|
30
|
-
export type GetAuthType<T extends VectorTypes> = T['auth'] extends undefined
|
|
31
|
-
? DefaultAuthUser
|
|
32
|
-
: T['auth'];
|
|
31
|
+
export type GetAuthType<T extends VectorTypes> = T['auth'] extends undefined ? DefaultAuthUser : T['auth'];
|
|
33
32
|
|
|
34
|
-
export type GetContextType<T extends VectorTypes> = T['context'] extends undefined
|
|
35
|
-
? Record<string, any>
|
|
36
|
-
: T['context'];
|
|
33
|
+
export type GetContextType<T extends VectorTypes> = T['context'] extends undefined ? Record<string, any> : T['context'];
|
|
37
34
|
|
|
38
35
|
export type GetCacheType<T extends VectorTypes> = T['cache'] extends undefined ? any : T['cache'];
|
|
39
36
|
|
|
@@ -44,16 +41,39 @@ export type GetMetadataType<T extends VectorTypes> = T['metadata'] extends undef
|
|
|
44
41
|
// Legacy support - keep AuthUser for backward compatibility
|
|
45
42
|
export type AuthUser = DefaultAuthUser;
|
|
46
43
|
|
|
47
|
-
|
|
48
|
-
|
|
44
|
+
type DefaultQueryShape = { [key: string]: string | string[] | undefined };
|
|
45
|
+
type DefaultParamsShape = Record<string, string>;
|
|
46
|
+
type DefaultCookiesShape = Record<string, string>;
|
|
47
|
+
|
|
48
|
+
type BaseVectorRequest = Omit<Request, 'body' | 'json' | 'text' | 'formData' | 'arrayBuffer' | 'blob'>;
|
|
49
|
+
|
|
50
|
+
type InferValidatedSection<TValidatedInput, TKey extends string, TFallback> = [TValidatedInput] extends [undefined]
|
|
51
|
+
? TFallback
|
|
52
|
+
: TValidatedInput extends Record<string, unknown>
|
|
53
|
+
? TKey extends keyof TValidatedInput
|
|
54
|
+
? TValidatedInput[TKey]
|
|
55
|
+
: TFallback
|
|
56
|
+
: TFallback;
|
|
57
|
+
|
|
58
|
+
type InferValidatedInputValue<TValidatedInput> = [TValidatedInput] extends [undefined] ? unknown : TValidatedInput;
|
|
59
|
+
|
|
60
|
+
export type BunRouteHandler = (req: Request) => Response | Promise<Response>;
|
|
61
|
+
export type BunMethodMap = Record<string, BunRouteHandler>;
|
|
62
|
+
export type BunRouteTable = Record<string, BunMethodMap | Response>;
|
|
63
|
+
export type LegacyRouteEntry = [string, RegExp, [BunRouteHandler, ...BunRouteHandler[]], string?];
|
|
64
|
+
|
|
65
|
+
export interface VectorRequest<TTypes extends VectorTypes = DefaultVectorTypes, TValidatedInput = undefined>
|
|
66
|
+
extends BaseVectorRequest {
|
|
49
67
|
authUser?: GetAuthType<TTypes>;
|
|
50
68
|
context: GetContextType<TTypes>;
|
|
51
69
|
metadata?: GetMetadataType<TTypes>;
|
|
52
|
-
content?: any
|
|
53
|
-
|
|
54
|
-
|
|
70
|
+
content?: InferValidatedSection<TValidatedInput, 'body', any>;
|
|
71
|
+
body?: InferValidatedSection<TValidatedInput, 'body', any>;
|
|
72
|
+
params?: InferValidatedSection<TValidatedInput, 'params', DefaultParamsShape>;
|
|
73
|
+
query: InferValidatedSection<TValidatedInput, 'query', DefaultQueryShape>;
|
|
55
74
|
headers: Headers;
|
|
56
|
-
cookies?:
|
|
75
|
+
cookies?: InferValidatedSection<TValidatedInput, 'cookies', DefaultCookiesShape>;
|
|
76
|
+
validatedInput?: InferValidatedInputValue<TValidatedInput>;
|
|
57
77
|
startTime?: number;
|
|
58
78
|
[key: string]: any;
|
|
59
79
|
}
|
|
@@ -63,6 +83,33 @@ export interface CacheOptions {
|
|
|
63
83
|
ttl?: number;
|
|
64
84
|
}
|
|
65
85
|
|
|
86
|
+
export type StandardRouteSchema = StandardSchemaV1<any, any>;
|
|
87
|
+
export type RouteSchemaStatusCode = number | `${number}` | 'default';
|
|
88
|
+
export type RouteSchemaOutputMap = Partial<Record<RouteSchemaStatusCode, StandardRouteSchema>>;
|
|
89
|
+
|
|
90
|
+
export interface RouteSchemaDefinition<
|
|
91
|
+
TInput extends StandardRouteSchema | undefined = StandardRouteSchema | undefined,
|
|
92
|
+
TOutput extends RouteSchemaOutputMap | StandardRouteSchema | undefined =
|
|
93
|
+
| RouteSchemaOutputMap
|
|
94
|
+
| StandardRouteSchema
|
|
95
|
+
| undefined,
|
|
96
|
+
> {
|
|
97
|
+
input?: TInput;
|
|
98
|
+
output?: TOutput;
|
|
99
|
+
tag?: string;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export type InferStandardSchemaInput<TSchema extends StandardRouteSchema> = StandardSchemaV1.InferInput<TSchema>;
|
|
103
|
+
export type InferStandardSchemaOutput<TSchema extends StandardRouteSchema> = StandardSchemaV1.InferOutput<TSchema>;
|
|
104
|
+
export type StandardJSONSchemaCapable = StandardJSONSchemaV1<any, any>;
|
|
105
|
+
|
|
106
|
+
export type InferRouteInputFromSchemaDefinition<TSchemaDef extends RouteSchemaDefinition | undefined> =
|
|
107
|
+
TSchemaDef extends { input: infer TInputSchema }
|
|
108
|
+
? TInputSchema extends StandardRouteSchema
|
|
109
|
+
? InferStandardSchemaOutput<TInputSchema>
|
|
110
|
+
: undefined
|
|
111
|
+
: undefined;
|
|
112
|
+
|
|
66
113
|
export interface RouteOptions<TTypes extends VectorTypes = DefaultVectorTypes> {
|
|
67
114
|
method: string;
|
|
68
115
|
path: string;
|
|
@@ -70,9 +117,23 @@ export interface RouteOptions<TTypes extends VectorTypes = DefaultVectorTypes> {
|
|
|
70
117
|
expose?: boolean; // defaults to true
|
|
71
118
|
cache?: CacheOptions | number;
|
|
72
119
|
rawRequest?: boolean;
|
|
120
|
+
validate?: boolean; // defaults to validating schema.input unless false
|
|
73
121
|
rawResponse?: boolean;
|
|
74
122
|
responseContentType?: string;
|
|
75
123
|
metadata?: GetMetadataType<TTypes>;
|
|
124
|
+
schema?: RouteSchemaDefinition;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export interface RouteBooleanDefaults {
|
|
128
|
+
auth?: boolean;
|
|
129
|
+
expose?: boolean;
|
|
130
|
+
rawRequest?: boolean;
|
|
131
|
+
validate?: boolean;
|
|
132
|
+
rawResponse?: boolean;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
export interface VectorDefaults {
|
|
136
|
+
route?: RouteBooleanDefaults;
|
|
76
137
|
}
|
|
77
138
|
|
|
78
139
|
// Legacy config interface - will be deprecated
|
|
@@ -88,6 +149,33 @@ export interface VectorConfig<TTypes extends VectorTypes = DefaultVectorTypes> {
|
|
|
88
149
|
routeExcludePatterns?: string[];
|
|
89
150
|
autoDiscover?: boolean;
|
|
90
151
|
idleTimeout?: number;
|
|
152
|
+
defaults?: VectorDefaults;
|
|
153
|
+
openapi?: OpenAPIOptions | boolean;
|
|
154
|
+
startup?: StartupHandler;
|
|
155
|
+
shutdown?: ShutdownHandler;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
export interface StartVectorContext {
|
|
159
|
+
configSource: 'user' | 'default';
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
export interface StartVectorOptions<TTypes extends VectorTypes = DefaultVectorTypes> {
|
|
163
|
+
configPath?: string;
|
|
164
|
+
config?: Partial<VectorConfig<TTypes>>;
|
|
165
|
+
autoDiscover?: boolean;
|
|
166
|
+
protectedHandler?: ProtectedHandler<TTypes> | null;
|
|
167
|
+
cacheHandler?: CacheHandler | null;
|
|
168
|
+
mutateConfig?: (
|
|
169
|
+
config: VectorConfig<TTypes>,
|
|
170
|
+
context: StartVectorContext
|
|
171
|
+
) => VectorConfig<TTypes> | Promise<VectorConfig<TTypes>>;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
export interface StartedVectorApp<TTypes extends VectorTypes = DefaultVectorTypes> {
|
|
175
|
+
server: Server;
|
|
176
|
+
config: VectorConfig<TTypes>;
|
|
177
|
+
stop: () => void;
|
|
178
|
+
shutdown: () => Promise<void>;
|
|
91
179
|
}
|
|
92
180
|
|
|
93
181
|
// New config-driven schema - flat structure
|
|
@@ -100,6 +188,7 @@ export interface VectorConfigSchema<TTypes extends VectorTypes = DefaultVectorTy
|
|
|
100
188
|
routesDir?: string;
|
|
101
189
|
routeExcludePatterns?: string[];
|
|
102
190
|
idleTimeout?: number;
|
|
191
|
+
defaults?: VectorDefaults;
|
|
103
192
|
|
|
104
193
|
// Middleware functions
|
|
105
194
|
before?: BeforeMiddlewareHandler<TTypes>[];
|
|
@@ -112,6 +201,13 @@ export interface VectorConfigSchema<TTypes extends VectorTypes = DefaultVectorTy
|
|
|
112
201
|
// CORS configuration
|
|
113
202
|
cors?: CorsOptions | boolean;
|
|
114
203
|
|
|
204
|
+
// OpenAPI/docs configuration
|
|
205
|
+
openapi?: OpenAPIOptions | boolean;
|
|
206
|
+
|
|
207
|
+
// Startup lifecycle
|
|
208
|
+
startup?: StartupHandler;
|
|
209
|
+
shutdown?: ShutdownHandler;
|
|
210
|
+
|
|
115
211
|
// Custom types for TypeScript
|
|
116
212
|
types?: VectorTypes;
|
|
117
213
|
}
|
|
@@ -125,6 +221,26 @@ export interface CorsOptions {
|
|
|
125
221
|
maxAge?: number;
|
|
126
222
|
}
|
|
127
223
|
|
|
224
|
+
export interface OpenAPIDocsOptions {
|
|
225
|
+
enabled?: boolean;
|
|
226
|
+
path?: string;
|
|
227
|
+
exposePaths?: string[];
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
export interface OpenAPIInfoOptions {
|
|
231
|
+
title?: string;
|
|
232
|
+
version?: string;
|
|
233
|
+
description?: string;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
export interface OpenAPIOptions {
|
|
237
|
+
enabled?: boolean;
|
|
238
|
+
path?: string;
|
|
239
|
+
target?: 'openapi-3.0' | 'draft-2020-12' | 'draft-07' | ({} & string);
|
|
240
|
+
docs?: boolean | OpenAPIDocsOptions;
|
|
241
|
+
info?: OpenAPIInfoOptions;
|
|
242
|
+
}
|
|
243
|
+
|
|
128
244
|
export type BeforeMiddlewareHandler<TTypes extends VectorTypes = DefaultVectorTypes> = (
|
|
129
245
|
request: VectorRequest<TTypes>
|
|
130
246
|
) => Promise<VectorRequest<TTypes> | Response> | VectorRequest<TTypes> | Response;
|
|
@@ -135,8 +251,11 @@ export type AfterMiddlewareHandler<TTypes extends VectorTypes = DefaultVectorTyp
|
|
|
135
251
|
) => Promise<Response> | Response;
|
|
136
252
|
export type MiddlewareHandler = BeforeMiddlewareHandler | AfterMiddlewareHandler;
|
|
137
253
|
|
|
138
|
-
export type
|
|
139
|
-
|
|
254
|
+
export type StartupHandler = () => Promise<void> | void;
|
|
255
|
+
export type ShutdownHandler = () => Promise<void> | void;
|
|
256
|
+
|
|
257
|
+
export type RouteHandler<TTypes extends VectorTypes = DefaultVectorTypes, TValidatedInput = undefined> = (
|
|
258
|
+
request: VectorRequest<TTypes, TValidatedInput>
|
|
140
259
|
) => Promise<any> | any;
|
|
141
260
|
|
|
142
261
|
export type ProtectedHandler<TTypes extends VectorTypes = DefaultVectorTypes> = (
|
|
@@ -145,9 +264,9 @@ export type ProtectedHandler<TTypes extends VectorTypes = DefaultVectorTypes> =
|
|
|
145
264
|
|
|
146
265
|
export type CacheHandler = (key: string, factory: () => Promise<any>, ttl: number) => Promise<any>;
|
|
147
266
|
|
|
148
|
-
export interface RouteDefinition<TTypes extends VectorTypes = DefaultVectorTypes> {
|
|
267
|
+
export interface RouteDefinition<TTypes extends VectorTypes = DefaultVectorTypes, TValidatedInput = undefined> {
|
|
149
268
|
options: RouteOptions<TTypes>;
|
|
150
|
-
handler: RouteHandler<TTypes>;
|
|
269
|
+
handler: RouteHandler<TTypes, TValidatedInput>;
|
|
151
270
|
}
|
|
152
271
|
|
|
153
272
|
export interface GeneratedRoute<TTypes extends VectorTypes = DefaultVectorTypes> {
|
|
@@ -156,3 +275,5 @@ export interface GeneratedRoute<TTypes extends VectorTypes = DefaultVectorTypes>
|
|
|
156
275
|
method: string;
|
|
157
276
|
options: RouteOptions<TTypes>;
|
|
158
277
|
}
|
|
278
|
+
|
|
279
|
+
export type { StandardJSONSchemaV1, StandardSchemaV1, StandardTypedV1 };
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
/** The Standard Typed interface. This is a base type extended by other specs. */
|
|
2
|
+
export interface StandardTypedV1<Input = unknown, Output = Input> {
|
|
3
|
+
/** The Standard properties. */
|
|
4
|
+
readonly '~standard': StandardTypedV1.Props<Input, Output>;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export declare namespace StandardTypedV1 {
|
|
8
|
+
/** The Standard Typed properties interface. */
|
|
9
|
+
export interface Props<Input = unknown, Output = Input> {
|
|
10
|
+
/** The version number of the standard. */
|
|
11
|
+
readonly version: 1;
|
|
12
|
+
/** The vendor name of the schema library. */
|
|
13
|
+
readonly vendor: string;
|
|
14
|
+
/** Inferred types associated with the schema. */
|
|
15
|
+
readonly types?: Types<Input, Output> | undefined;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** The Standard Typed types interface. */
|
|
19
|
+
export interface Types<Input = unknown, Output = Input> {
|
|
20
|
+
/** The input type of the schema. */
|
|
21
|
+
readonly input: Input;
|
|
22
|
+
/** The output type of the schema. */
|
|
23
|
+
readonly output: Output;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** Infers the input type of a Standard Typed. */
|
|
27
|
+
export type InferInput<Schema extends StandardTypedV1> = NonNullable<Schema['~standard']['types']>['input'];
|
|
28
|
+
|
|
29
|
+
/** Infers the output type of a Standard Typed. */
|
|
30
|
+
export type InferOutput<Schema extends StandardTypedV1> = NonNullable<Schema['~standard']['types']>['output'];
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** The Standard Schema interface. */
|
|
34
|
+
export interface StandardSchemaV1<Input = unknown, Output = Input> {
|
|
35
|
+
/** The Standard Schema properties. */
|
|
36
|
+
readonly '~standard': StandardSchemaV1.Props<Input, Output>;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export declare namespace StandardSchemaV1 {
|
|
40
|
+
/** The Standard Schema properties interface. */
|
|
41
|
+
export interface Props<Input = unknown, Output = Input> extends StandardTypedV1.Props<Input, Output> {
|
|
42
|
+
/** Validates unknown input values. */
|
|
43
|
+
readonly validate: (
|
|
44
|
+
value: unknown,
|
|
45
|
+
options?: StandardSchemaV1.Options | undefined
|
|
46
|
+
) => Result<Output> | Promise<Result<Output>>;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** The result interface of the validate function. */
|
|
50
|
+
export type Result<Output> = SuccessResult<Output> | FailureResult;
|
|
51
|
+
|
|
52
|
+
/** The result interface if validation succeeds. */
|
|
53
|
+
export interface SuccessResult<Output> {
|
|
54
|
+
/** The typed output value. */
|
|
55
|
+
readonly value: Output;
|
|
56
|
+
/** A falsy value for `issues` indicates success. */
|
|
57
|
+
readonly issues?: undefined;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export interface Options {
|
|
61
|
+
/** Explicit support for additional vendor-specific parameters, if needed. */
|
|
62
|
+
readonly libraryOptions?: Record<string, unknown> | undefined;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** The result interface if validation fails. */
|
|
66
|
+
export interface FailureResult {
|
|
67
|
+
/** The issues of failed validation. */
|
|
68
|
+
readonly issues: ReadonlyArray<Issue>;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** The issue interface of the failure output. */
|
|
72
|
+
export interface Issue {
|
|
73
|
+
/** The error message of the issue. */
|
|
74
|
+
readonly message: string;
|
|
75
|
+
/** The path of the issue, if any. */
|
|
76
|
+
readonly path?: ReadonlyArray<PropertyKey | PathSegment> | undefined;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** The path segment interface of the issue. */
|
|
80
|
+
export interface PathSegment {
|
|
81
|
+
/** The key representing a path segment. */
|
|
82
|
+
readonly key: PropertyKey;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** The Standard types interface. */
|
|
86
|
+
export interface Types<Input = unknown, Output = Input> extends StandardTypedV1.Types<Input, Output> {}
|
|
87
|
+
|
|
88
|
+
/** Infers the input type of a Standard. */
|
|
89
|
+
export type InferInput<Schema extends StandardTypedV1> = StandardTypedV1.InferInput<Schema>;
|
|
90
|
+
|
|
91
|
+
/** Infers the output type of a Standard. */
|
|
92
|
+
export type InferOutput<Schema extends StandardTypedV1> = StandardTypedV1.InferOutput<Schema>;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** The Standard JSON Schema interface. */
|
|
96
|
+
export interface StandardJSONSchemaV1<Input = unknown, Output = Input> {
|
|
97
|
+
/** The Standard JSON Schema properties. */
|
|
98
|
+
readonly '~standard': StandardJSONSchemaV1.Props<Input, Output>;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export declare namespace StandardJSONSchemaV1 {
|
|
102
|
+
/** The Standard JSON Schema properties interface. */
|
|
103
|
+
export interface Props<Input = unknown, Output = Input> extends StandardTypedV1.Props<Input, Output> {
|
|
104
|
+
/** Methods for generating the input/output JSON Schema. */
|
|
105
|
+
readonly jsonSchema: StandardJSONSchemaV1.Converter;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** The Standard JSON Schema converter interface. */
|
|
109
|
+
export interface Converter {
|
|
110
|
+
/** Converts the input type to JSON Schema. May throw if conversion is not supported. */
|
|
111
|
+
readonly input: (options: StandardJSONSchemaV1.Options) => Record<string, unknown>;
|
|
112
|
+
/** Converts the output type to JSON Schema. May throw if conversion is not supported. */
|
|
113
|
+
readonly output: (options: StandardJSONSchemaV1.Options) => Record<string, unknown>;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* The target version of the generated JSON Schema.
|
|
118
|
+
*
|
|
119
|
+
* It is *strongly recommended* that implementers support `"draft-2020-12"` and `"draft-07"`, as they are both in wide use. All other targets can be implemented on a best-effort basis. Libraries should throw if they don't support a specified target.
|
|
120
|
+
*
|
|
121
|
+
* The `"openapi-3.0"` target is intended as a standardized specifier for OpenAPI 3.0 which is a superset of JSON Schema `"draft-04"`.
|
|
122
|
+
*/
|
|
123
|
+
export type Target =
|
|
124
|
+
| 'draft-2020-12'
|
|
125
|
+
| 'draft-07'
|
|
126
|
+
| 'openapi-3.0'
|
|
127
|
+
// Accepts any string: allows future targets while preserving autocomplete
|
|
128
|
+
| ({} & string);
|
|
129
|
+
|
|
130
|
+
/** The options for the input/output methods. */
|
|
131
|
+
export interface Options {
|
|
132
|
+
/** Specifies the target version of the generated JSON Schema. Support for all versions is on a best-effort basis. If a given version is not supported, the library should throw. */
|
|
133
|
+
readonly target: Target;
|
|
134
|
+
|
|
135
|
+
/** Explicit support for additional vendor-specific parameters, if needed. */
|
|
136
|
+
readonly libraryOptions?: Record<string, unknown> | undefined;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** The Standard types interface. */
|
|
140
|
+
export interface Types<Input = unknown, Output = Input> extends StandardTypedV1.Types<Input, Output> {}
|
|
141
|
+
|
|
142
|
+
/** Infers the input type of a Standard. */
|
|
143
|
+
export type InferInput<Schema extends StandardTypedV1> = StandardTypedV1.InferInput<Schema>;
|
|
144
|
+
|
|
145
|
+
/** Infers the output type of a Standard. */
|
|
146
|
+
export type InferOutput<Schema extends StandardTypedV1> = StandardTypedV1.InferOutput<Schema>;
|
|
147
|
+
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
export interface CorsConfig {
|
|
2
|
+
origin: string | string[] | ((origin: string) => boolean);
|
|
3
|
+
credentials: boolean;
|
|
4
|
+
allowHeaders: string;
|
|
5
|
+
allowMethods: string;
|
|
6
|
+
exposeHeaders: string;
|
|
7
|
+
maxAge: number;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
function getAllowedOrigin(origin: string | undefined, config: CorsConfig): string | null {
|
|
11
|
+
if (!origin) {
|
|
12
|
+
if (typeof config.origin === 'string') {
|
|
13
|
+
// Credentials cannot be combined with wildcard; only reflect concrete request origins.
|
|
14
|
+
if (config.origin === '*' && config.credentials) return null;
|
|
15
|
+
return config.origin;
|
|
16
|
+
}
|
|
17
|
+
return null;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
if (typeof config.origin === 'string') {
|
|
21
|
+
if (config.origin === '*') {
|
|
22
|
+
return config.credentials ? origin : '*';
|
|
23
|
+
}
|
|
24
|
+
return config.origin === origin ? origin : null;
|
|
25
|
+
}
|
|
26
|
+
if (Array.isArray(config.origin)) {
|
|
27
|
+
return config.origin.includes(origin) ? origin : null;
|
|
28
|
+
}
|
|
29
|
+
if (typeof config.origin === 'function') {
|
|
30
|
+
return config.origin(origin) ? origin : null;
|
|
31
|
+
}
|
|
32
|
+
return null;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function shouldVaryByOrigin(config: CorsConfig): boolean {
|
|
36
|
+
return (
|
|
37
|
+
(typeof config.origin === 'string' && config.origin === '*' && config.credentials) ||
|
|
38
|
+
Array.isArray(config.origin) ||
|
|
39
|
+
typeof config.origin === 'function'
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function buildCorsHeaders(origin: string | null, config: CorsConfig, varyByOrigin: boolean): Record<string, string> {
|
|
44
|
+
const headers: Record<string, string> = {};
|
|
45
|
+
if (origin) {
|
|
46
|
+
headers['access-control-allow-origin'] = origin;
|
|
47
|
+
headers['access-control-allow-methods'] = config.allowMethods;
|
|
48
|
+
headers['access-control-allow-headers'] = config.allowHeaders;
|
|
49
|
+
headers['access-control-expose-headers'] = config.exposeHeaders;
|
|
50
|
+
headers['access-control-max-age'] = String(config.maxAge);
|
|
51
|
+
if (config.credentials) {
|
|
52
|
+
headers['access-control-allow-credentials'] = 'true';
|
|
53
|
+
}
|
|
54
|
+
if (varyByOrigin) {
|
|
55
|
+
headers.vary = 'Origin';
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
return headers;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function mergeVary(existing: string | null, nextValue: string): string {
|
|
62
|
+
if (!existing) return nextValue;
|
|
63
|
+
const parts = existing
|
|
64
|
+
.split(',')
|
|
65
|
+
.map((v) => v.trim())
|
|
66
|
+
.filter(Boolean);
|
|
67
|
+
const lower = parts.map((v) => v.toLowerCase());
|
|
68
|
+
if (!lower.includes(nextValue.toLowerCase())) {
|
|
69
|
+
parts.push(nextValue);
|
|
70
|
+
}
|
|
71
|
+
return parts.join(', ');
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export function cors(config: CorsConfig) {
|
|
75
|
+
return {
|
|
76
|
+
preflight(request: Request): Response {
|
|
77
|
+
const origin = request.headers.get('origin') ?? undefined;
|
|
78
|
+
const allowed = getAllowedOrigin(origin, config);
|
|
79
|
+
const varyByOrigin = Boolean(origin && allowed && shouldVaryByOrigin(config));
|
|
80
|
+
return new Response(null, {
|
|
81
|
+
status: 204,
|
|
82
|
+
headers: buildCorsHeaders(allowed, config, varyByOrigin),
|
|
83
|
+
});
|
|
84
|
+
},
|
|
85
|
+
corsify(response: Response, request: Request): Response {
|
|
86
|
+
const origin = request.headers.get('origin') ?? undefined;
|
|
87
|
+
const allowed = getAllowedOrigin(origin, config);
|
|
88
|
+
if (!allowed) return response;
|
|
89
|
+
const varyByOrigin = Boolean(origin && shouldVaryByOrigin(config));
|
|
90
|
+
const headers = buildCorsHeaders(allowed, config, varyByOrigin);
|
|
91
|
+
for (const [k, v] of Object.entries(headers)) {
|
|
92
|
+
if (k === 'vary') {
|
|
93
|
+
response.headers.set('vary', mergeVary(response.headers.get('vary'), v));
|
|
94
|
+
continue;
|
|
95
|
+
}
|
|
96
|
+
response.headers.set(k, v);
|
|
97
|
+
}
|
|
98
|
+
return response;
|
|
99
|
+
},
|
|
100
|
+
};
|
|
101
|
+
}
|
package/src/utils/logger.ts
CHANGED
|
@@ -17,7 +17,7 @@ export class Logger {
|
|
|
17
17
|
constructor(config: Partial<LoggerConfig> = {}) {
|
|
18
18
|
this.config = {
|
|
19
19
|
level: config.level ?? LogLevel.INFO,
|
|
20
|
-
prefix: config.prefix ?? '[
|
|
20
|
+
prefix: config.prefix ?? '[vector]',
|
|
21
21
|
timestamp: config.timestamp ?? true,
|
|
22
22
|
};
|
|
23
23
|
}
|
package/src/utils/path.ts
CHANGED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import type { StandardRouteSchema } from '../types';
|
|
2
|
+
|
|
3
|
+
export interface NormalizedValidationIssue {
|
|
4
|
+
message: string;
|
|
5
|
+
path: Array<string | number>;
|
|
6
|
+
code?: string;
|
|
7
|
+
raw?: unknown;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
interface StandardValidationSuccess {
|
|
11
|
+
success: true;
|
|
12
|
+
value: unknown;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
interface StandardValidationFailure {
|
|
16
|
+
success: false;
|
|
17
|
+
issues: readonly unknown[];
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export type StandardValidationResult = StandardValidationSuccess | StandardValidationFailure;
|
|
21
|
+
|
|
22
|
+
export function isStandardRouteSchema(schema: unknown): schema is StandardRouteSchema {
|
|
23
|
+
const standard = (schema as any)?.['~standard'];
|
|
24
|
+
return (
|
|
25
|
+
!!standard && typeof standard === 'object' && typeof standard.validate === 'function' && standard.version === 1
|
|
26
|
+
);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export async function runStandardValidation(
|
|
30
|
+
schema: StandardRouteSchema,
|
|
31
|
+
value: unknown
|
|
32
|
+
): Promise<StandardValidationResult> {
|
|
33
|
+
const result = await schema['~standard'].validate(value);
|
|
34
|
+
const issues = (result as any)?.issues;
|
|
35
|
+
|
|
36
|
+
if (Array.isArray(issues) && issues.length > 0) {
|
|
37
|
+
return { success: false, issues };
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return { success: true, value: (result as any)?.value };
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function extractThrownIssues(error: unknown): readonly unknown[] | null {
|
|
44
|
+
if (Array.isArray(error)) {
|
|
45
|
+
return error;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
if (error && typeof error === 'object' && Array.isArray((error as any).issues)) {
|
|
49
|
+
return (error as any).issues;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
if (error && typeof error === 'object' && (error as any).cause && Array.isArray((error as any).cause.issues)) {
|
|
53
|
+
return (error as any).cause.issues;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return null;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function normalizePath(path: unknown): Array<string | number> {
|
|
60
|
+
if (!Array.isArray(path)) return [];
|
|
61
|
+
|
|
62
|
+
const normalized: Array<string | number> = [];
|
|
63
|
+
|
|
64
|
+
for (let i = 0; i < path.length; i++) {
|
|
65
|
+
const segment = path[i];
|
|
66
|
+
let value = segment;
|
|
67
|
+
|
|
68
|
+
if (segment && typeof segment === 'object' && 'key' in (segment as any)) {
|
|
69
|
+
value = (segment as any).key;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
if (typeof value === 'string' || typeof value === 'number') {
|
|
73
|
+
normalized.push(value);
|
|
74
|
+
} else if (typeof value === 'symbol') {
|
|
75
|
+
normalized.push(String(value));
|
|
76
|
+
} else if (value !== undefined && value !== null) {
|
|
77
|
+
normalized.push(String(value));
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return normalized;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export function normalizeValidationIssues(
|
|
85
|
+
issues: readonly unknown[],
|
|
86
|
+
includeRawIssues: boolean
|
|
87
|
+
): NormalizedValidationIssue[] {
|
|
88
|
+
const normalized: NormalizedValidationIssue[] = [];
|
|
89
|
+
|
|
90
|
+
for (let i = 0; i < issues.length; i++) {
|
|
91
|
+
const issue = issues[i];
|
|
92
|
+
const maybeIssue = issue as any;
|
|
93
|
+
const normalizedIssue: NormalizedValidationIssue = {
|
|
94
|
+
message:
|
|
95
|
+
typeof maybeIssue?.message === 'string' && maybeIssue.message.length > 0 ? maybeIssue.message : 'Invalid value',
|
|
96
|
+
path: normalizePath(maybeIssue?.path),
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
if (typeof maybeIssue?.code === 'string') {
|
|
100
|
+
normalizedIssue.code = maybeIssue.code;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
if (includeRawIssues) {
|
|
104
|
+
normalizedIssue.raw = issue;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
normalized.push(normalizedIssue);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
return normalized;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export function createValidationErrorPayload(target: 'input' | 'output', issues: NormalizedValidationIssue[]) {
|
|
114
|
+
return {
|
|
115
|
+
error: true,
|
|
116
|
+
message: 'Validation failed',
|
|
117
|
+
statusCode: 422,
|
|
118
|
+
source: 'validation',
|
|
119
|
+
target,
|
|
120
|
+
issues,
|
|
121
|
+
timestamp: new Date().toISOString(),
|
|
122
|
+
};
|
|
123
|
+
}
|
package/src/utils/validation.ts
CHANGED
|
@@ -9,9 +9,12 @@ export function validateConfig(config: VectorConfig): VectorConfig {
|
|
|
9
9
|
development: config.development || false,
|
|
10
10
|
routesDir: config.routesDir || DEFAULT_CONFIG.ROUTES_DIR,
|
|
11
11
|
autoDiscover: config.autoDiscover !== false,
|
|
12
|
+
defaults: config.defaults,
|
|
12
13
|
cors: config.cors ? validateCorsOptions(config.cors) : undefined,
|
|
13
14
|
before: config.before || [],
|
|
14
15
|
finally: config.finally || [],
|
|
16
|
+
startup: config.startup,
|
|
17
|
+
shutdown: config.shutdown,
|
|
15
18
|
};
|
|
16
19
|
|
|
17
20
|
return validatedConfig;
|