@gravito/mass 3.0.1 → 3.0.2
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 +84 -76
- package/README.zh-TW.md +120 -11
- package/dist/coercion.d.ts +233 -0
- package/dist/coercion.d.ts.map +1 -0
- package/dist/errors.d.ts +169 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/formats.d.ts +98 -0
- package/dist/formats.d.ts.map +1 -0
- package/dist/index.d.ts +81 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +526 -5
- package/dist/openapi.d.ts +215 -0
- package/dist/openapi.d.ts.map +1 -0
- package/dist/types.d.ts +53 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/utils.d.ts +23 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/validator.d.ts +47 -0
- package/dist/validator.d.ts.map +1 -0
- package/package.json +9 -7
- package/dist/mass/src/index.d.ts +0 -30
- package/dist/mass/src/index.d.ts.map +0 -1
- package/dist/mass/src/validator.d.ts +0 -28
- package/dist/mass/src/validator.d.ts.map +0 -1
- package/dist/photon/src/index.d.ts +0 -21
- package/dist/photon/src/index.d.ts.map +0 -1
- package/dist/photon/src/middleware/binary.d.ts +0 -35
- package/dist/photon/src/middleware/binary.d.ts.map +0 -1
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
import type { TSchema } from '@sinclair/typebox';
|
|
2
|
+
/**
|
|
3
|
+
* OpenAPI Schema Object Type
|
|
4
|
+
*
|
|
5
|
+
* Represents a Schema Object in the OpenAPI 3.0 specification.
|
|
6
|
+
*/
|
|
7
|
+
export interface OpenApiSchema {
|
|
8
|
+
type?: string;
|
|
9
|
+
format?: string;
|
|
10
|
+
description?: string;
|
|
11
|
+
enum?: unknown[];
|
|
12
|
+
items?: OpenApiSchema;
|
|
13
|
+
properties?: Record<string, OpenApiSchema>;
|
|
14
|
+
required?: string[];
|
|
15
|
+
additionalProperties?: boolean | OpenApiSchema;
|
|
16
|
+
minLength?: number;
|
|
17
|
+
maxLength?: number;
|
|
18
|
+
minimum?: number;
|
|
19
|
+
maximum?: number;
|
|
20
|
+
pattern?: string;
|
|
21
|
+
default?: unknown;
|
|
22
|
+
example?: unknown;
|
|
23
|
+
nullable?: boolean;
|
|
24
|
+
readOnly?: boolean;
|
|
25
|
+
writeOnly?: boolean;
|
|
26
|
+
deprecated?: boolean;
|
|
27
|
+
[key: string]: unknown;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Astral Resource Definition Type
|
|
31
|
+
*
|
|
32
|
+
* Represents the resource definition for Gravito Astral automatic API documentation.
|
|
33
|
+
*/
|
|
34
|
+
export interface AstralResource {
|
|
35
|
+
path: string;
|
|
36
|
+
method: string;
|
|
37
|
+
summary?: string;
|
|
38
|
+
description?: string;
|
|
39
|
+
tags?: string[];
|
|
40
|
+
requestBody?: {
|
|
41
|
+
description?: string;
|
|
42
|
+
required?: boolean;
|
|
43
|
+
content: {
|
|
44
|
+
'application/json': {
|
|
45
|
+
schema: OpenApiSchema;
|
|
46
|
+
};
|
|
47
|
+
};
|
|
48
|
+
};
|
|
49
|
+
responses: {
|
|
50
|
+
[statusCode: string]: {
|
|
51
|
+
description: string;
|
|
52
|
+
content?: {
|
|
53
|
+
'application/json': {
|
|
54
|
+
schema: OpenApiSchema;
|
|
55
|
+
};
|
|
56
|
+
};
|
|
57
|
+
};
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Converts a TypeBox Schema to an OpenAPI Schema.
|
|
62
|
+
*
|
|
63
|
+
* This function transforms TypeBox schema definitions into OpenAPI 3.0 compliant schema objects,
|
|
64
|
+
* suitable for automatic API documentation generation or integration with third-party tools.
|
|
65
|
+
*
|
|
66
|
+
* @param schema - The TypeBox schema object
|
|
67
|
+
* @returns An OpenAPI Schema object
|
|
68
|
+
*
|
|
69
|
+
* @example Basic Type Conversion
|
|
70
|
+
* ```typescript
|
|
71
|
+
* import { Type } from '@sinclair/typebox'
|
|
72
|
+
*
|
|
73
|
+
* const schema = Type.String({ minLength: 3, maxLength: 50 })
|
|
74
|
+
* const openapi = typeboxToOpenApi(schema)
|
|
75
|
+
* // {
|
|
76
|
+
* // type: 'string',
|
|
77
|
+
* // minLength: 3,
|
|
78
|
+
* // maxLength: 50
|
|
79
|
+
* // }
|
|
80
|
+
* ```
|
|
81
|
+
*
|
|
82
|
+
* @example Object Type Conversion
|
|
83
|
+
* ```typescript
|
|
84
|
+
* const userSchema = Type.Object({
|
|
85
|
+
* name: Type.String(),
|
|
86
|
+
* email: Type.String({ format: 'email' }),
|
|
87
|
+
* age: Type.Optional(Type.Number({ minimum: 0 }))
|
|
88
|
+
* })
|
|
89
|
+
*
|
|
90
|
+
* const openapi = typeboxToOpenApi(userSchema)
|
|
91
|
+
* // {
|
|
92
|
+
* // type: 'object',
|
|
93
|
+
* // properties: {
|
|
94
|
+
* // name: { type: 'string' },
|
|
95
|
+
* // email: { type: 'string', format: 'email' },
|
|
96
|
+
* // age: { type: 'number', minimum: 0 }
|
|
97
|
+
* // },
|
|
98
|
+
* // required: ['name', 'email']
|
|
99
|
+
* // }
|
|
100
|
+
* ```
|
|
101
|
+
*/
|
|
102
|
+
export declare function typeboxToOpenApi(schema: TSchema): OpenApiSchema;
|
|
103
|
+
/**
|
|
104
|
+
* Creates an Astral Resource Definition.
|
|
105
|
+
*
|
|
106
|
+
* This function builds resource definitions for the Gravito Astral automatic API documentation system,
|
|
107
|
+
* combining TypeBox schemas with HTTP route information to generate complete API endpoint documentation.
|
|
108
|
+
*
|
|
109
|
+
* @param options - Resource definition options
|
|
110
|
+
* @returns Astral Resource Definition object
|
|
111
|
+
*
|
|
112
|
+
* @example Create POST endpoint resource
|
|
113
|
+
* ```typescript
|
|
114
|
+
* const createUserResource = createAstralResource({
|
|
115
|
+
* path: '/users',
|
|
116
|
+
* method: 'POST',
|
|
117
|
+
* summary: 'Create New User',
|
|
118
|
+
* description: 'Creates a new user account with the provided data',
|
|
119
|
+
* tags: ['users'],
|
|
120
|
+
* requestSchema: Type.Object({
|
|
121
|
+
* name: Type.String({ minLength: 1 }),
|
|
122
|
+
* email: Type.String({ format: 'email' }),
|
|
123
|
+
* password: Type.String({ minLength: 8 })
|
|
124
|
+
* }),
|
|
125
|
+
* responseSchema: Type.Object({
|
|
126
|
+
* id: Type.String({ format: 'uuid' }),
|
|
127
|
+
* name: Type.String(),
|
|
128
|
+
* email: Type.String(),
|
|
129
|
+
* createdAt: Type.String({ format: 'date-time' })
|
|
130
|
+
* }),
|
|
131
|
+
* responseStatusCode: 201
|
|
132
|
+
* })
|
|
133
|
+
* ```
|
|
134
|
+
*
|
|
135
|
+
* @example Create GET endpoint resource (no request body)
|
|
136
|
+
* ```typescript
|
|
137
|
+
* const getUsersResource = createAstralResource({
|
|
138
|
+
* path: '/users',
|
|
139
|
+
* method: 'GET',
|
|
140
|
+
* summary: 'Get User List',
|
|
141
|
+
* tags: ['users'],
|
|
142
|
+
* responseSchema: Type.Array(
|
|
143
|
+
* Type.Object({
|
|
144
|
+
* id: Type.String(),
|
|
145
|
+
* name: Type.String()
|
|
146
|
+
* })
|
|
147
|
+
* )
|
|
148
|
+
* })
|
|
149
|
+
* ```
|
|
150
|
+
*/
|
|
151
|
+
export declare function createAstralResource(options: {
|
|
152
|
+
path: string;
|
|
153
|
+
method: string;
|
|
154
|
+
summary?: string;
|
|
155
|
+
description?: string;
|
|
156
|
+
tags?: string[];
|
|
157
|
+
requestSchema?: TSchema;
|
|
158
|
+
requestDescription?: string;
|
|
159
|
+
requestRequired?: boolean;
|
|
160
|
+
responseSchema: TSchema;
|
|
161
|
+
responseDescription?: string;
|
|
162
|
+
responseStatusCode?: number;
|
|
163
|
+
}): AstralResource;
|
|
164
|
+
/**
|
|
165
|
+
* Batch creates CRUD Resource Definitions.
|
|
166
|
+
*
|
|
167
|
+
* Automatically generates all relevant Astral resource definitions for standard CRUD operations.
|
|
168
|
+
*
|
|
169
|
+
* @param options - CRUD resource options
|
|
170
|
+
* @returns CRUD Resource Definition object
|
|
171
|
+
*
|
|
172
|
+
* @example Create full User CRUD resources
|
|
173
|
+
* ```typescript
|
|
174
|
+
* const userCrudResources = createCrudResources({
|
|
175
|
+
* resourceName: 'user',
|
|
176
|
+
* basePath: '/users',
|
|
177
|
+
* tags: ['users'],
|
|
178
|
+
* schemas: {
|
|
179
|
+
* item: Type.Object({
|
|
180
|
+
* id: Type.String({ format: 'uuid' }),
|
|
181
|
+
* name: Type.String(),
|
|
182
|
+
* email: Type.String({ format: 'email' })
|
|
183
|
+
* }),
|
|
184
|
+
* create: Type.Object({
|
|
185
|
+
* name: Type.String(),
|
|
186
|
+
* email: Type.String({ format: 'email' })
|
|
187
|
+
* }),
|
|
188
|
+
* update: Type.Partial(Type.Object({
|
|
189
|
+
* name: Type.String(),
|
|
190
|
+
* email: Type.String({ format: 'email' })
|
|
191
|
+
* }))
|
|
192
|
+
* }
|
|
193
|
+
* })
|
|
194
|
+
*
|
|
195
|
+
* // Returns: { list, get, create, update, delete }
|
|
196
|
+
* ```
|
|
197
|
+
*/
|
|
198
|
+
export declare function createCrudResources(options: {
|
|
199
|
+
resourceName: string;
|
|
200
|
+
basePath: string;
|
|
201
|
+
tags?: string[];
|
|
202
|
+
schemas: {
|
|
203
|
+
item: TSchema;
|
|
204
|
+
list?: TSchema;
|
|
205
|
+
create: TSchema;
|
|
206
|
+
update: TSchema;
|
|
207
|
+
};
|
|
208
|
+
}): {
|
|
209
|
+
list: AstralResource;
|
|
210
|
+
get: AstralResource;
|
|
211
|
+
create: AstralResource;
|
|
212
|
+
update: AstralResource;
|
|
213
|
+
delete: AstralResource;
|
|
214
|
+
};
|
|
215
|
+
//# sourceMappingURL=openapi.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"openapi.d.ts","sourceRoot":"","sources":["../src/openapi.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAA4B,OAAO,EAAW,MAAM,mBAAmB,CAAA;AAGnF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IACF,IAAI,EAAE,cAAc,CAAA;IACpB,GAAG,EAAE,cAAc,CAAA;IACnB,MAAM,EAAE,cAAc,CAAA;IACtB,MAAM,EAAE,cAAc,CAAA;IACtB,MAAM,EAAE,cAAc,CAAA;CACvB,CAiDA"}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import type { Context, Env } from '@gravito/photon';
|
|
2
|
+
import type { Static, TSchema } from '@sinclair/typebox';
|
|
3
|
+
/**
|
|
4
|
+
* Supported data sources for validation.
|
|
5
|
+
*
|
|
6
|
+
* Determines which part of the HTTP request should be validated against the schema.
|
|
7
|
+
*/
|
|
8
|
+
export type ValidationSource = 'json' | 'query' | 'param' | 'form';
|
|
9
|
+
/**
|
|
10
|
+
* Details of a validation error.
|
|
11
|
+
*
|
|
12
|
+
* Provides structured information about why a specific field failed validation.
|
|
13
|
+
*/
|
|
14
|
+
export interface ValidationError {
|
|
15
|
+
/** The path to the invalid field (e.g., "/user/name") */
|
|
16
|
+
path: string;
|
|
17
|
+
/** A human-readable description of the error */
|
|
18
|
+
message: string;
|
|
19
|
+
/** The value or type that was expected */
|
|
20
|
+
expected?: string;
|
|
21
|
+
/** The value or type that was actually received */
|
|
22
|
+
received?: string;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* The outcome of a validation attempt.
|
|
26
|
+
*
|
|
27
|
+
* Passed to validation hooks to allow custom handling of success or failure states.
|
|
28
|
+
*
|
|
29
|
+
* @template T - The TypeBox schema type used for validation
|
|
30
|
+
*/
|
|
31
|
+
export interface ValidationResult<T extends TSchema> {
|
|
32
|
+
/** Indicates whether the data satisfied the schema */
|
|
33
|
+
success: boolean;
|
|
34
|
+
/** The validated and typed data (only present if success is true) */
|
|
35
|
+
data?: Static<T>;
|
|
36
|
+
/** A list of validation errors (only present if success is false) */
|
|
37
|
+
errors?: ValidationError[];
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* A callback function for intercepting validation results.
|
|
41
|
+
*
|
|
42
|
+
* Use this hook to customize error responses, log failures, or transform data
|
|
43
|
+
* after validation but before the main route handler.
|
|
44
|
+
*
|
|
45
|
+
* @template T - The TypeBox schema type
|
|
46
|
+
* @template E - The Photon environment type
|
|
47
|
+
*
|
|
48
|
+
* @param result - The outcome of the validation
|
|
49
|
+
* @param context - The Photon request context
|
|
50
|
+
* @returns A Response object to short-circuit the request, or undefined to proceed
|
|
51
|
+
*/
|
|
52
|
+
export type ValidationHook<T extends TSchema, E extends Env = Env> = (result: ValidationResult<T>, context: Context<E>) => Response | Promise<Response> | undefined;
|
|
53
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,GAAG,EAAE,MAAM,iBAAiB,CAAA;AACnD,OAAO,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAA;AAExD;;;;GAIG;AACH,MAAM,MAAM,gBAAgB,GAAG,MAAM,GAAG,OAAO,GAAG,OAAO,GAAG,MAAM,CAAA;AAElE;;;;GAIG;AACH,MAAM,WAAW,eAAe;IAC9B,yDAAyD;IACzD,IAAI,EAAE,MAAM,CAAA;IACZ,gDAAgD;IAChD,OAAO,EAAE,MAAM,CAAA;IACf,0CAA0C;IAC1C,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,mDAAmD;IACnD,QAAQ,CAAC,EAAE,MAAM,CAAA;CAClB;AAED;;;;;;GAMG;AACH,MAAM,WAAW,gBAAgB,CAAC,CAAC,SAAS,OAAO;IACjD,sDAAsD;IACtD,OAAO,EAAE,OAAO,CAAA;IAChB,qEAAqE;IACrE,IAAI,CAAC,EAAE,MAAM,CAAC,CAAC,CAAC,CAAA;IAChB,qEAAqE;IACrE,MAAM,CAAC,EAAE,eAAe,EAAE,CAAA;CAC3B;AAED;;;;;;;;;;;;GAYG;AACH,MAAM,MAAM,cAAc,CAAC,CAAC,SAAS,OAAO,EAAE,CAAC,SAAS,GAAG,GAAG,GAAG,IAAI,CACnE,MAAM,EAAE,gBAAgB,CAAC,CAAC,CAAC,EAC3B,OAAO,EAAE,OAAO,CAAC,CAAC,CAAC,KAChB,QAAQ,GAAG,OAAO,CAAC,QAAQ,CAAC,GAAG,SAAS,CAAA"}
|
package/dist/utils.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { TObject, TPartial } from '@sinclair/typebox';
|
|
2
|
+
/**
|
|
3
|
+
* Creates a new schema with all properties of the original schema set to optional.
|
|
4
|
+
*
|
|
5
|
+
* This utility is useful for creating "update" schemas (e.g., for PATCH requests)
|
|
6
|
+
* where clients can send a subset of fields to update.
|
|
7
|
+
*
|
|
8
|
+
* @param schema - The original TypeBox object schema
|
|
9
|
+
* @returns A new schema with all properties marked as optional
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```typescript
|
|
13
|
+
* const userSchema = Schema.Object({
|
|
14
|
+
* name: Schema.String(),
|
|
15
|
+
* email: Schema.String()
|
|
16
|
+
* })
|
|
17
|
+
*
|
|
18
|
+
* // All fields in updateSchema are optional
|
|
19
|
+
* const updateSchema = partial(userSchema)
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
export declare function partial<T extends TObject>(schema: T): TPartial<T>;
|
|
23
|
+
//# sourceMappingURL=utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAA;AAG1D;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,OAAO,CAAC,CAAC,SAAS,OAAO,EAAE,MAAM,EAAE,CAAC,GAAG,QAAQ,CAAC,CAAC,CAAC,CAEjE"}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type { Context, Env, MiddlewareHandler } from '@gravito/photon';
|
|
2
|
+
import type { Static, TSchema } from '@sinclair/typebox';
|
|
3
|
+
export type { ValidationError, ValidationHook, ValidationResult, ValidationSource, } from './types';
|
|
4
|
+
/**
|
|
5
|
+
* Creates a validation middleware using TypeBox schemas.
|
|
6
|
+
*
|
|
7
|
+
* This middleware validates incoming request data against the provided schema.
|
|
8
|
+
* It integrates seamlessly with Photon's type system, providing full type safety
|
|
9
|
+
* for validated data in the request context.
|
|
10
|
+
*
|
|
11
|
+
* @param source - The request data source to validate (json, query, param, form)
|
|
12
|
+
* @param schema - The TypeBox schema defining the expected data structure
|
|
13
|
+
* @param hook - Optional callback to handle validation results manually
|
|
14
|
+
* @returns A Photon middleware handler that enforces the schema
|
|
15
|
+
*
|
|
16
|
+
* @example Validating a JSON body
|
|
17
|
+
* ```typescript
|
|
18
|
+
* app.post('/users',
|
|
19
|
+
* validate('json', Schema.Object({
|
|
20
|
+
* name: Schema.String(),
|
|
21
|
+
* age: Schema.Number()
|
|
22
|
+
* })),
|
|
23
|
+
* (c) => {
|
|
24
|
+
* const user = c.req.valid('json')
|
|
25
|
+
* return c.json({ created: user.name })
|
|
26
|
+
* }
|
|
27
|
+
* )
|
|
28
|
+
* ```
|
|
29
|
+
*
|
|
30
|
+
* @example Custom error handling with hook
|
|
31
|
+
* ```typescript
|
|
32
|
+
* validate('query', schema, (result, c) => {
|
|
33
|
+
* if (!result.success) {
|
|
34
|
+
* return c.json({ error: 'Invalid query params' }, 400)
|
|
35
|
+
* }
|
|
36
|
+
* })
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
export declare function validate<T extends TSchema, S extends 'json' | 'query' | 'param' | 'form', E extends Env = Env, P extends string = string>(source: S, schema: T, hook?: (result: unknown, c: Context<E>) => Response | Promise<Response> | undefined): MiddlewareHandler<E, P, {
|
|
40
|
+
in: {
|
|
41
|
+
[K in S]: Static<T>;
|
|
42
|
+
};
|
|
43
|
+
out: {
|
|
44
|
+
[K in S]: Static<T>;
|
|
45
|
+
};
|
|
46
|
+
}>;
|
|
47
|
+
//# sourceMappingURL=validator.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validator.d.ts","sourceRoot":"","sources":["../src/validator.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,GAAG,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAA;AAEtE,OAAO,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAA;AAExD,YAAY,EACV,eAAe,EACf,cAAc,EACd,gBAAgB,EAChB,gBAAgB,GACjB,MAAM,SAAS,CAAA;AAEhB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,wBAAgB,QAAQ,CACtB,CAAC,SAAS,OAAO,EACjB,CAAC,SAAS,MAAM,GAAG,OAAO,GAAG,OAAO,GAAG,MAAM,EAC7C,CAAC,SAAS,GAAG,GAAG,GAAG,EACnB,CAAC,SAAS,MAAM,GAAG,MAAM,EAEzB,MAAM,EAAE,CAAC,EACT,MAAM,EAAE,CAAC,EACT,IAAI,CAAC,EAAE,CAAC,MAAM,EAAE,OAAO,EAAE,CAAC,EAAE,OAAO,CAAC,CAAC,CAAC,KAAK,QAAQ,GAAG,OAAO,CAAC,QAAQ,CAAC,GAAG,SAAS,GAClF,iBAAiB,CAClB,CAAC,EACD,CAAC,EACD;IACE,EAAE,EAAE;SAAG,CAAC,IAAI,CAAC,GAAG,MAAM,CAAC,CAAC,CAAC;KAAE,CAAA;IAC3B,GAAG,EAAE;SAAG,CAAC,IAAI,CAAC,GAAG,MAAM,CAAC,CAAC,CAAC;KAAE,CAAA;CAC7B,CACF,CAEA"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gravito/mass",
|
|
3
|
-
"version": "3.0.
|
|
3
|
+
"version": "3.0.2",
|
|
4
4
|
"description": "TypeBox-based validation for Gravito - High-performance schema validation with full TypeScript support",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -17,10 +17,12 @@
|
|
|
17
17
|
"scripts": {
|
|
18
18
|
"build": "bun run build.ts",
|
|
19
19
|
"typecheck": "bun tsc -p tsconfig.json --noEmit --skipLibCheck",
|
|
20
|
-
"test": "bun test",
|
|
20
|
+
"test": "bun test --timeout=10000",
|
|
21
21
|
"prepublishOnly": "bun run build",
|
|
22
|
-
"test:coverage": "bun test --coverage --coverage-
|
|
23
|
-
"test:ci": "bun test --coverage --coverage-
|
|
22
|
+
"test:coverage": "bun test --timeout=10000 --coverage --coverage-reporter=lcov --coverage-dir coverage && bun run --bun scripts/check-coverage.ts",
|
|
23
|
+
"test:ci": "bun test --timeout=10000 --coverage --coverage-reporter=lcov --coverage-dir coverage && bun run --bun scripts/check-coverage.ts",
|
|
24
|
+
"test:unit": "bun test tests/ --timeout=10000",
|
|
25
|
+
"test:integration": "test $(find tests -name '*.integration.test.ts' 2>/dev/null | wc -l) -gt 0 && find tests -name '*.integration.test.ts' -print0 | xargs -0 bun test --timeout=10000 || echo 'No integration tests found'"
|
|
24
26
|
},
|
|
25
27
|
"publishConfig": {
|
|
26
28
|
"access": "public"
|
|
@@ -30,11 +32,11 @@
|
|
|
30
32
|
"@sinclair/typebox": "^0.34.0"
|
|
31
33
|
},
|
|
32
34
|
"peerDependencies": {
|
|
33
|
-
"@gravito/core": "
|
|
35
|
+
"@gravito/core": "^1.6.1"
|
|
34
36
|
},
|
|
35
37
|
"devDependencies": {
|
|
36
38
|
"bun-types": "^1.1.0",
|
|
37
|
-
"@gravito/photon": "
|
|
39
|
+
"@gravito/photon": "^1.0.1",
|
|
38
40
|
"typescript": "^5.9.3"
|
|
39
41
|
},
|
|
40
42
|
"author": "Carl Lee <carllee0520@gmail.com>",
|
|
@@ -53,4 +55,4 @@
|
|
|
53
55
|
"typescript"
|
|
54
56
|
],
|
|
55
57
|
"homepage": "https://github.com/gravito-framework/gravito#readme"
|
|
56
|
-
}
|
|
58
|
+
}
|
package/dist/mass/src/index.d.ts
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @gravito/mass
|
|
3
|
-
*
|
|
4
|
-
* TypeBox-based validation for Gravito
|
|
5
|
-
* High-performance schema validation with full TypeScript support
|
|
6
|
-
*
|
|
7
|
-
* @example
|
|
8
|
-
* ```typescript
|
|
9
|
-
* import { Photon } from '@gravito/photon'
|
|
10
|
-
* import { Schema, validate } from '@gravito/mass'
|
|
11
|
-
*
|
|
12
|
-
* const app = new Photon()
|
|
13
|
-
*
|
|
14
|
-
* app.post('/login',
|
|
15
|
-
* validate('json', Schema.Object({
|
|
16
|
-
* username: Schema.String(),
|
|
17
|
-
* password: Schema.String()
|
|
18
|
-
* })),
|
|
19
|
-
* (c) => {
|
|
20
|
-
* const { username } = c.req.valid('json')
|
|
21
|
-
* return c.json({ success: true, message: `Welcome ${username}` })
|
|
22
|
-
* }
|
|
23
|
-
* )
|
|
24
|
-
* ```
|
|
25
|
-
*/
|
|
26
|
-
export { tbValidator as validator } from '@hono/typebox-validator';
|
|
27
|
-
export type { Static, TSchema } from '@sinclair/typebox';
|
|
28
|
-
export * as Schema from '@sinclair/typebox';
|
|
29
|
-
export { type ValidationSource, validate } from './validator';
|
|
30
|
-
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AAGH,OAAO,EAAE,WAAW,IAAI,SAAS,EAAE,MAAM,yBAAyB,CAAA;AAGlE,YAAY,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAA;AAExD,OAAO,KAAK,MAAM,MAAM,mBAAmB,CAAA;AAG3C,OAAO,EAAE,KAAK,gBAAgB,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAA"}
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import type { Env, MiddlewareHandler } from '@gravito/photon';
|
|
2
|
-
import type { Static, TSchema } from '@sinclair/typebox';
|
|
3
|
-
/**
|
|
4
|
-
* The source of the data to be validated.
|
|
5
|
-
* @public
|
|
6
|
-
*/
|
|
7
|
-
export type ValidationSource = 'json' | 'query' | 'param' | 'form';
|
|
8
|
-
/**
|
|
9
|
-
* Create a validation middleware using TypeBox.
|
|
10
|
-
*
|
|
11
|
-
* This function wraps the Hono TypeBox validator, providing a seamless
|
|
12
|
-
* integration with Gravito Photon for high-performance schema validation.
|
|
13
|
-
*
|
|
14
|
-
* @param source - The request part to validate (json, query, param, form)
|
|
15
|
-
* @param schema - The TypeBox schema to validate against
|
|
16
|
-
* @param hook - Optional callback to handle validation results manually
|
|
17
|
-
* @returns A Photon-compatible middleware handler.
|
|
18
|
-
* @public
|
|
19
|
-
*/
|
|
20
|
-
export declare function validate<T extends TSchema, S extends ValidationSource, E extends Env = any, P extends string = any>(source: S, schema: T, hook?: (result: any, c: any) => any): MiddlewareHandler<E, P, {
|
|
21
|
-
in: {
|
|
22
|
-
[K in S]: Static<T>;
|
|
23
|
-
};
|
|
24
|
-
out: {
|
|
25
|
-
[K in S]: Static<T>;
|
|
26
|
-
};
|
|
27
|
-
}>;
|
|
28
|
-
//# sourceMappingURL=validator.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"validator.d.ts","sourceRoot":"","sources":["../../../src/validator.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,GAAG,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAA;AAE7D,OAAO,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAA;AAExD;;;GAGG;AACH,MAAM,MAAM,gBAAgB,GAAG,MAAM,GAAG,OAAO,GAAG,OAAO,GAAG,MAAM,CAAA;AAElE;;;;;;;;;;;GAWG;AACH,wBAAgB,QAAQ,CACtB,CAAC,SAAS,OAAO,EACjB,CAAC,SAAS,gBAAgB,EAC1B,CAAC,SAAS,GAAG,GAAG,GAAG,EACnB,CAAC,SAAS,MAAM,GAAG,GAAG,EAEtB,MAAM,EAAE,CAAC,EACT,MAAM,EAAE,CAAC,EACT,IAAI,CAAC,EAAE,CAAC,MAAM,EAAE,GAAG,EAAE,CAAC,EAAE,GAAG,KAAK,GAAG,GAClC,iBAAiB,CAClB,CAAC,EACD,CAAC,EACD;IACE,EAAE,EAAE;SAAG,CAAC,IAAI,CAAC,GAAG,MAAM,CAAC,CAAC,CAAC;KAAE,CAAA;IAC3B,GAAG,EAAE;SAAG,CAAC,IAAI,CAAC,GAAG,MAAM,CAAC,CAAC,CAAC;KAAE,CAAA;CAC7B,CACF,CAEA"}
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @gravito/photon - High-performance web framework based on Hono.
|
|
3
|
-
*
|
|
4
|
-
* Photon is the primary web engine for Gravito, providing a fast,
|
|
5
|
-
* flexible, and standard-compliant API for building web applications.
|
|
6
|
-
* It re-exports Hono while adding enterprise-grade middleware and utilities.
|
|
7
|
-
*
|
|
8
|
-
* @example
|
|
9
|
-
* ```typescript
|
|
10
|
-
* import { Photon } from '@gravito/photon'
|
|
11
|
-
* const app = new Photon()
|
|
12
|
-
* app.get('/', (c) => c.text('Hello!'))
|
|
13
|
-
* ```
|
|
14
|
-
*/
|
|
15
|
-
export * from 'hono';
|
|
16
|
-
export { Hono as Photon } from 'hono';
|
|
17
|
-
/**
|
|
18
|
-
* Binary-related middleware for Photon.
|
|
19
|
-
*/
|
|
20
|
-
export * from './middleware/binary';
|
|
21
|
-
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../photon/src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AACH,cAAc,MAAM,CAAA;AACpB,OAAO,EAAE,IAAI,IAAI,MAAM,EAAE,MAAM,MAAM,CAAA;AACrC;;GAEG;AACH,cAAc,qBAAqB,CAAA"}
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
import type { MiddlewareHandler } from 'hono';
|
|
2
|
-
/**
|
|
3
|
-
* Binary Middleware for Photon
|
|
4
|
-
*
|
|
5
|
-
* Automatically detects 'Accept: application/cbor' and encodes
|
|
6
|
-
* JSON responses using the CBOR binary format for high-performance communication.
|
|
7
|
-
*
|
|
8
|
-
* @example
|
|
9
|
-
* ```typescript
|
|
10
|
-
* import { Photon } from '@gravito/photon'
|
|
11
|
-
* import { binaryMiddleware } from '@gravito/photon/middleware/binary'
|
|
12
|
-
*
|
|
13
|
-
* const app = new Photon()
|
|
14
|
-
* app.use(binaryMiddleware())
|
|
15
|
-
*
|
|
16
|
-
* app.get('/api/data', (c) => c.json({ items: [...] }))
|
|
17
|
-
* ```
|
|
18
|
-
*
|
|
19
|
-
* @performance
|
|
20
|
-
* - CBOR encoding is ~2-3x faster than JSON.stringify for large objects
|
|
21
|
-
* - Binary format reduces payload size by 20-40% on average
|
|
22
|
-
* - Recommended for high-frequency API calls with large datasets
|
|
23
|
-
*
|
|
24
|
-
* @client_usage
|
|
25
|
-
* ```typescript
|
|
26
|
-
* import { decode } from 'cborg'
|
|
27
|
-
*
|
|
28
|
-
* const res = await fetch('/api/data', {
|
|
29
|
-
* headers: { Accept: 'application/cbor' }
|
|
30
|
-
* })
|
|
31
|
-
* const data = decode(new Uint8Array(await res.arrayBuffer()))
|
|
32
|
-
* ```
|
|
33
|
-
*/
|
|
34
|
-
export declare const binaryMiddleware: () => MiddlewareHandler;
|
|
35
|
-
//# sourceMappingURL=binary.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"binary.d.ts","sourceRoot":"","sources":["../../../../../photon/src/middleware/binary.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,MAAM,CAAA;AAE7C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AACH,eAAO,MAAM,gBAAgB,QAAO,iBAyBnC,CAAA"}
|