vector-framework 1.0.0 → 1.2.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.
Files changed (104) hide show
  1. package/README.md +87 -634
  2. package/dist/auth/protected.d.ts.map +1 -1
  3. package/dist/auth/protected.js.map +1 -1
  4. package/dist/cache/manager.d.ts +5 -2
  5. package/dist/cache/manager.d.ts.map +1 -1
  6. package/dist/cache/manager.js +21 -12
  7. package/dist/cache/manager.js.map +1 -1
  8. package/dist/cli/index.js +60 -126
  9. package/dist/cli/index.js.map +1 -1
  10. package/dist/cli/option-resolution.d.ts +4 -0
  11. package/dist/cli/option-resolution.d.ts.map +1 -0
  12. package/dist/cli/option-resolution.js +28 -0
  13. package/dist/cli/option-resolution.js.map +1 -0
  14. package/dist/cli.js +2774 -599
  15. package/dist/constants/index.d.ts +3 -0
  16. package/dist/constants/index.d.ts.map +1 -1
  17. package/dist/constants/index.js +6 -0
  18. package/dist/constants/index.js.map +1 -1
  19. package/dist/core/config-loader.d.ts +2 -2
  20. package/dist/core/config-loader.d.ts.map +1 -1
  21. package/dist/core/config-loader.js +18 -18
  22. package/dist/core/config-loader.js.map +1 -1
  23. package/dist/core/router.d.ts +41 -15
  24. package/dist/core/router.d.ts.map +1 -1
  25. package/dist/core/router.js +465 -150
  26. package/dist/core/router.js.map +1 -1
  27. package/dist/core/server.d.ts +17 -3
  28. package/dist/core/server.d.ts.map +1 -1
  29. package/dist/core/server.js +274 -33
  30. package/dist/core/server.js.map +1 -1
  31. package/dist/core/vector.d.ts +9 -8
  32. package/dist/core/vector.d.ts.map +1 -1
  33. package/dist/core/vector.js +40 -32
  34. package/dist/core/vector.js.map +1 -1
  35. package/dist/dev/route-generator.d.ts.map +1 -1
  36. package/dist/dev/route-generator.js.map +1 -1
  37. package/dist/dev/route-scanner.d.ts +1 -1
  38. package/dist/dev/route-scanner.d.ts.map +1 -1
  39. package/dist/dev/route-scanner.js +37 -43
  40. package/dist/dev/route-scanner.js.map +1 -1
  41. package/dist/http.d.ts +14 -14
  42. package/dist/http.d.ts.map +1 -1
  43. package/dist/http.js +84 -84
  44. package/dist/http.js.map +1 -1
  45. package/dist/index.d.ts +3 -3
  46. package/dist/index.js +1314 -8
  47. package/dist/index.mjs +1314 -8
  48. package/dist/middleware/manager.d.ts +1 -1
  49. package/dist/middleware/manager.d.ts.map +1 -1
  50. package/dist/middleware/manager.js +4 -0
  51. package/dist/middleware/manager.js.map +1 -1
  52. package/dist/openapi/docs-ui.d.ts +2 -0
  53. package/dist/openapi/docs-ui.d.ts.map +1 -0
  54. package/dist/openapi/docs-ui.js +1313 -0
  55. package/dist/openapi/docs-ui.js.map +1 -0
  56. package/dist/openapi/generator.d.ts +12 -0
  57. package/dist/openapi/generator.d.ts.map +1 -0
  58. package/dist/openapi/generator.js +273 -0
  59. package/dist/openapi/generator.js.map +1 -0
  60. package/dist/types/index.d.ts +70 -11
  61. package/dist/types/index.d.ts.map +1 -1
  62. package/dist/types/standard-schema.d.ts +118 -0
  63. package/dist/types/standard-schema.d.ts.map +1 -0
  64. package/dist/types/standard-schema.js +2 -0
  65. package/dist/types/standard-schema.js.map +1 -0
  66. package/dist/utils/cors.d.ts +13 -0
  67. package/dist/utils/cors.d.ts.map +1 -0
  68. package/dist/utils/cors.js +89 -0
  69. package/dist/utils/cors.js.map +1 -0
  70. package/dist/utils/path.d.ts +7 -0
  71. package/dist/utils/path.d.ts.map +1 -1
  72. package/dist/utils/path.js +14 -3
  73. package/dist/utils/path.js.map +1 -1
  74. package/dist/utils/schema-validation.d.ts +31 -0
  75. package/dist/utils/schema-validation.d.ts.map +1 -0
  76. package/dist/utils/schema-validation.js +77 -0
  77. package/dist/utils/schema-validation.js.map +1 -0
  78. package/dist/utils/validation.d.ts.map +1 -1
  79. package/dist/utils/validation.js +1 -0
  80. package/dist/utils/validation.js.map +1 -1
  81. package/package.json +24 -19
  82. package/src/auth/protected.ts +3 -13
  83. package/src/cache/manager.ts +25 -30
  84. package/src/cli/index.ts +62 -141
  85. package/src/cli/option-resolution.ts +40 -0
  86. package/src/constants/index.ts +7 -0
  87. package/src/core/config-loader.ts +20 -22
  88. package/src/core/router.ts +535 -155
  89. package/src/core/server.ts +354 -45
  90. package/src/core/vector.ts +71 -61
  91. package/src/dev/route-generator.ts +1 -3
  92. package/src/dev/route-scanner.ts +38 -51
  93. package/src/http.ts +117 -187
  94. package/src/index.ts +3 -3
  95. package/src/middleware/manager.ts +8 -11
  96. package/src/openapi/assets/tailwindcdn.js +83 -0
  97. package/src/openapi/docs-ui.ts +1317 -0
  98. package/src/openapi/generator.ts +359 -0
  99. package/src/types/index.ts +104 -17
  100. package/src/types/standard-schema.ts +147 -0
  101. package/src/utils/cors.ts +101 -0
  102. package/src/utils/path.ts +19 -4
  103. package/src/utils/schema-validation.ts +123 -0
  104. package/src/utils/validation.ts +1 -0
@@ -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/path.ts CHANGED
@@ -1,9 +1,24 @@
1
1
  export function toFileUrl(path: string): string {
2
- return process.platform === 'win32'
3
- ? `file:///${path.replace(/\\/g, '/')}`
4
- : path;
2
+ return process.platform === 'win32' ? `file:///${path.replace(/\\/g, '/')}` : path;
5
3
  }
6
4
 
7
5
  export function normalizePath(path: string): string {
8
6
  return path.replace(/\\/g, '/').replace(/\/+/g, '/');
9
- }
7
+ }
8
+
9
+ export function buildRouteRegex(path: string): RegExp {
10
+ return RegExp(
11
+ `^${path
12
+ .replace(/\/+(\/|$)/g, '$1')
13
+ .replace(/(\/?\.?):(\w+)\+/g, '($1(?<$2>[\\s\\S]+))')
14
+ .replace(/(\/?\.?):(\w+)/g, '($1(?<$2>[^$1/]+?))')
15
+ .replace(/\./g, '\\.')
16
+ .replace(/(\/?)\*/g, '($1.*)?')}/*$`
17
+ );
18
+ }
19
+
20
+ export default {
21
+ toFileUrl,
22
+ normalizePath,
23
+ buildRouteRegex,
24
+ };
@@ -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
+ }
@@ -9,6 +9,7 @@ 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 || [],