@gravito/mass 3.0.1 → 3.0.3
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 +109 -75
- package/README.zh-TW.md +120 -11
- package/dist/coercion.d.ts +222 -0
- package/dist/errors.d.ts +168 -0
- package/dist/formats.d.ts +97 -0
- package/dist/index.d.ts +79 -0
- package/dist/index.js +26 -11
- package/dist/openapi.d.ts +214 -0
- package/dist/types.d.ts +50 -0
- package/dist/utils.d.ts +22 -0
- package/dist/validator.d.ts +40 -0
- package/package.json +11 -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,40 @@
|
|
|
1
|
+
import type { GravitoContext, GravitoMiddleware } from '@gravito/core';
|
|
2
|
+
import type { TSchema } from '@sinclair/typebox';
|
|
3
|
+
export type { ValidationError, ValidationHook, ValidationResult, } from './types';
|
|
4
|
+
/**
|
|
5
|
+
* Validation source types.
|
|
6
|
+
*/
|
|
7
|
+
export type ValidationSource = 'json' | 'form' | 'query' | 'param' | 'header' | 'cookie';
|
|
8
|
+
/**
|
|
9
|
+
* Validates request data using TypeBox schema.
|
|
10
|
+
*
|
|
11
|
+
* This middleware provides high-performance validation with full TypeScript
|
|
12
|
+
* support. It leverages @hono/typebox-validator for runtime validation.
|
|
13
|
+
*
|
|
14
|
+
* @param source - The request data source to validate (json, query, param, form)
|
|
15
|
+
* @param schema - The TypeBox schema defining the expected data structure
|
|
16
|
+
* @param hook - Optional callback to handle validation results manually
|
|
17
|
+
* @returns A Gravito middleware handler that enforces the schema
|
|
18
|
+
*
|
|
19
|
+
* @example Validating a JSON body
|
|
20
|
+
* ```typescript
|
|
21
|
+
* import { Type } from '@sinclair/typebox'
|
|
22
|
+
* import { validate } from '@gravito/mass'
|
|
23
|
+
*
|
|
24
|
+
* const schema = Type.Object({
|
|
25
|
+
* name: Type.String()
|
|
26
|
+
* })
|
|
27
|
+
*
|
|
28
|
+
* app.post('/users', validate('json', schema), (c) => {
|
|
29
|
+
* const data = c.req.valid('json')
|
|
30
|
+
* return c.json(data)
|
|
31
|
+
* })
|
|
32
|
+
* ```
|
|
33
|
+
*
|
|
34
|
+
* @param source - The data source to validate (json, query, param, etc.)
|
|
35
|
+
* @param schema - TypeBox schema
|
|
36
|
+
* @param hook - Optional validation hook for custom error handling
|
|
37
|
+
* @returns Photon middleware handler
|
|
38
|
+
* @public
|
|
39
|
+
*/
|
|
40
|
+
export declare function validate<T extends TSchema>(source: 'json' | 'query' | 'param' | 'form', schema: T, hook?: (result: unknown, c: GravitoContext) => Response | Promise<Response> | undefined): GravitoMiddleware;
|
package/package.json
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gravito/mass",
|
|
3
|
-
"
|
|
3
|
+
"sideEffects": false,
|
|
4
|
+
"version": "3.0.3",
|
|
4
5
|
"description": "TypeBox-based validation for Gravito - High-performance schema validation with full TypeScript support",
|
|
5
6
|
"type": "module",
|
|
6
7
|
"main": "./dist/index.js",
|
|
@@ -16,23 +17,26 @@
|
|
|
16
17
|
],
|
|
17
18
|
"scripts": {
|
|
18
19
|
"build": "bun run build.ts",
|
|
20
|
+
"build:dts": "bun run build.ts --dts-only",
|
|
19
21
|
"typecheck": "bun tsc -p tsconfig.json --noEmit --skipLibCheck",
|
|
20
|
-
"test": "bun test",
|
|
22
|
+
"test": "bun test --timeout=10000",
|
|
21
23
|
"prepublishOnly": "bun run build",
|
|
22
|
-
"test:coverage": "bun test --coverage --coverage-
|
|
23
|
-
"test:ci": "bun test --coverage --coverage-
|
|
24
|
+
"test:coverage": "bun test --timeout=10000 --coverage --coverage-reporter=lcov --coverage-dir coverage && bun run --bun scripts/check-coverage.ts",
|
|
25
|
+
"test:ci": "bun test --timeout=10000 --coverage --coverage-reporter=lcov --coverage-dir coverage && bun run --bun scripts/check-coverage.ts",
|
|
26
|
+
"test:unit": "bun test tests/ --timeout=10000",
|
|
27
|
+
"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
28
|
},
|
|
25
29
|
"publishConfig": {
|
|
26
30
|
"access": "public"
|
|
27
31
|
},
|
|
28
32
|
"dependencies": {
|
|
29
|
-
"@hono/typebox-validator": "^1.1.0",
|
|
30
33
|
"@sinclair/typebox": "^0.34.0"
|
|
31
34
|
},
|
|
32
35
|
"peerDependencies": {
|
|
33
|
-
"@gravito/core": "
|
|
36
|
+
"@gravito/core": "^2.0.0"
|
|
34
37
|
},
|
|
35
38
|
"devDependencies": {
|
|
39
|
+
"@gravito/core": "workspace:*",
|
|
36
40
|
"bun-types": "^1.1.0",
|
|
37
41
|
"@gravito/photon": "workspace:*",
|
|
38
42
|
"typescript": "^5.9.3"
|
|
@@ -53,4 +57,4 @@
|
|
|
53
57
|
"typescript"
|
|
54
58
|
],
|
|
55
59
|
"homepage": "https://github.com/gravito-framework/gravito#readme"
|
|
56
|
-
}
|
|
60
|
+
}
|
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"}
|