elysia 0.2.0-beta.1 → 0.2.0-beta.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/dist/index.d.ts +4 -4
- package/dist/index.js +2 -4
- package/dist/schema.d.ts +4 -3
- package/dist/schema.js +3 -1
- package/dist/types.d.ts +35 -42
- package/package.json +9 -8
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
/// <reference types="bun-types" />
|
|
2
2
|
import type { Serve, Server } from 'bun';
|
|
3
|
+
import { Router } from './router';
|
|
3
4
|
import { SCHEMA } from './utils';
|
|
4
5
|
import type { Context } from './context';
|
|
5
6
|
import type { Handler, BeforeRequestHandler, TypedRoute, ElysiaInstance, ElysiaConfig, HTTPMethod, InternalRoute, BodyParser, ErrorHandler, TypedSchema, LocalHook, LocalHandler, LifeCycle, LifeCycleEvent, LifeCycleStore, VoidLifeCycle, AfterRequestHandler, MergeIfNotNull, IsAny, OverwritableTypeRoute, MergeSchema, ListenCallback, NoReturnHandler, ElysiaRoute, MaybePromise, IsNever } from './types';
|
|
@@ -46,7 +47,7 @@ export default class Elysia<Instance extends ElysiaInstance = ElysiaInstance> {
|
|
|
46
47
|
use<NewElysia extends MaybePromise<Elysia<any>> = Elysia<any>, Params extends Elysia = Elysia<any>, LazyLoadElysia extends never | ElysiaInstance = never>(plugin: MaybePromise<(app: Params extends Elysia<infer ParamsInstance> ? IsAny<ParamsInstance> extends true ? this : Params : Params) => MaybePromise<NewElysia>> | Promise<{
|
|
47
48
|
default: (elysia: Elysia<any>) => MaybePromise<Elysia<LazyLoadElysia>>;
|
|
48
49
|
}>): IsNever<LazyLoadElysia> extends false ? Elysia<LazyLoadElysia & Instance> : NewElysia extends Elysia<infer NewInstance> ? IsNever<NewInstance> extends true ? Elysia<Instance> : Elysia<NewInstance & Instance> : NewElysia extends Promise<Elysia<infer NewInstance>> ? Elysia<NewInstance & Instance> : this;
|
|
49
|
-
get<Schema extends TypedSchema =
|
|
50
|
+
get<Schema extends TypedSchema = TypedSchema, Path extends string = string, Response = unknown>(path: Path, handler: LocalHandler<Schema, Instance, Path, Response>, hook?: LocalHook<Schema, Instance, Path>): ElysiaRoute<'GET', Schema, Instance, Path, Response>;
|
|
50
51
|
post<Schema extends TypedSchema = {}, Path extends string = string, Response = unknown>(path: Path, handler: LocalHandler<Schema, Instance, Path, Response>, hook?: LocalHook<Schema, Instance, Path>): ElysiaRoute<'POST', Schema, Instance, Path, Response>;
|
|
51
52
|
put<Schema extends TypedSchema = {}, Path extends string = string, Response = unknown>(path: Path, handler: LocalHandler<Schema, Instance, Path, Response>, hook?: LocalHook<Schema, Instance, Path>): ElysiaRoute<'PUT', Schema, Instance, Path, Response>;
|
|
52
53
|
patch<Schema extends TypedSchema = {}, Path extends string = string, Response = unknown>(path: Path, handler: LocalHandler<Schema, Instance, Path, Response>, hook?: LocalHook<Schema, Instance, Path>): ElysiaRoute<'PATCH', Schema, Instance, Path, Response>;
|
|
@@ -98,9 +99,8 @@ export default class Elysia<Instance extends ElysiaInstance = ElysiaInstance> {
|
|
|
98
99
|
schema: {};
|
|
99
100
|
}>>[]>;
|
|
100
101
|
}
|
|
101
|
-
export { Elysia };
|
|
102
|
+
export { Elysia, Router };
|
|
102
103
|
export { Type as t } from '@sinclair/typebox';
|
|
103
104
|
export { SCHEMA, getPath, createValidationError, getSchemaValidator } from './utils';
|
|
104
|
-
export { Router } from './router';
|
|
105
105
|
export type { Context, PreContext } from './context';
|
|
106
|
-
export type { Handler, RegisteredHook, BeforeRequestHandler, TypedRoute, OverwritableTypeRoute, ElysiaInstance, ElysiaConfig, HTTPMethod, ComposedHandler, InternalRoute, BodyParser, ErrorHandler, ErrorCode, TypedSchema, LocalHook, LocalHandler, LifeCycle, LifeCycleEvent, AfterRequestHandler, HookHandler, TypedSchemaToRoute, UnwrapSchema, LifeCycleStore, VoidLifeCycle, SchemaValidator, ElysiaRoute, ExtractPath } from './types';
|
|
106
|
+
export type { Handler, RegisteredHook, BeforeRequestHandler, TypedRoute, OverwritableTypeRoute, ElysiaInstance, ElysiaConfig, HTTPMethod, ComposedHandler, InternalRoute, BodyParser, ErrorHandler, ErrorCode, TypedSchema, LocalHook, LocalHandler, LifeCycle, LifeCycleEvent, AfterRequestHandler, HookHandler, TypedSchemaToRoute, UnwrapSchema, LifeCycleStore, VoidLifeCycle, SchemaValidator, ElysiaRoute, ExtractPath, IsPathParameter, IsAny, IsNever, UnknownFallback, WithArray, ObjectValues, PickInOrder, MaybePromise, MergeIfNotNull } from './types';
|
package/dist/index.js
CHANGED
|
@@ -34,7 +34,6 @@ export default class Elysia {
|
|
|
34
34
|
};
|
|
35
35
|
this.config = {
|
|
36
36
|
strictPath: false,
|
|
37
|
-
queryLimit: 48,
|
|
38
37
|
...config
|
|
39
38
|
};
|
|
40
39
|
}
|
|
@@ -259,7 +258,7 @@ export default class Elysia {
|
|
|
259
258
|
headers: getSchemaValidator(schema?.headers),
|
|
260
259
|
params: getSchemaValidator(schema?.params),
|
|
261
260
|
query: getSchemaValidator(schema?.query),
|
|
262
|
-
response: getSchemaValidator(schema?.response)
|
|
261
|
+
response: getSchemaValidator(schema?.response?.['200'] ?? schema.response)
|
|
263
262
|
};
|
|
264
263
|
return this;
|
|
265
264
|
}
|
|
@@ -454,7 +453,6 @@ export default class Elysia {
|
|
|
454
453
|
return Promise.all(this.lazyLoadModules);
|
|
455
454
|
}
|
|
456
455
|
}
|
|
457
|
-
export { Elysia };
|
|
456
|
+
export { Elysia, Router };
|
|
458
457
|
export { Type as t } from '@sinclair/typebox';
|
|
459
458
|
export { SCHEMA, getPath, createValidationError, getSchemaValidator } from './utils';
|
|
460
|
-
export { Router } from './router';
|
package/dist/schema.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { TSchema } from '@sinclair/typebox';
|
|
2
|
+
import type { OpenAPIV2 } from 'openapi-types';
|
|
2
3
|
import type { HTTPMethod, LocalHook } from './types';
|
|
3
4
|
export declare const toOpenAPIPath: (path: string) => string;
|
|
4
5
|
export declare const mapProperties: (name: string, schema: TSchema | undefined) => {
|
|
@@ -8,12 +9,12 @@ export declare const mapProperties: (name: string, schema: TSchema | undefined)
|
|
|
8
9
|
required: any;
|
|
9
10
|
}[];
|
|
10
11
|
export declare const registerSchemaPath: ({ schema, path, method, hook }: {
|
|
11
|
-
schema:
|
|
12
|
+
schema: OpenAPIV2.PathsObject;
|
|
12
13
|
path: string;
|
|
13
14
|
method: HTTPMethod;
|
|
14
|
-
hook?: LocalHook<
|
|
15
|
+
hook?: LocalHook<import("./types").TypedSchema, import("./types").ElysiaInstance<{
|
|
15
16
|
store: Record<typeof import("./utils").SCHEMA, {}>;
|
|
16
17
|
request: {};
|
|
17
18
|
schema: {};
|
|
18
|
-
}>, string
|
|
19
|
+
}>, string, import("./types").MergeSchema<import("./types").TypedSchema, {}>> | undefined;
|
|
19
20
|
}) => void;
|
package/dist/schema.js
CHANGED
|
@@ -15,6 +15,7 @@ export const registerSchemaPath = ({ schema, path, method, hook }) => {
|
|
|
15
15
|
const headerSchema = hook?.schema?.headers;
|
|
16
16
|
const querySchema = hook?.schema?.query;
|
|
17
17
|
const responseSchema = hook?.schema?.response;
|
|
18
|
+
const detail = hook?.schema?.detail;
|
|
18
19
|
const parameters = [
|
|
19
20
|
...mapProperties('header', headerSchema),
|
|
20
21
|
...mapProperties('path', paramsSchema),
|
|
@@ -42,7 +43,8 @@ export const registerSchemaPath = ({ schema, path, method, hook }) => {
|
|
|
42
43
|
}
|
|
43
44
|
}
|
|
44
45
|
}
|
|
45
|
-
: {})
|
|
46
|
+
: {}),
|
|
47
|
+
...detail
|
|
46
48
|
}
|
|
47
49
|
};
|
|
48
50
|
};
|
package/dist/types.d.ts
CHANGED
|
@@ -2,12 +2,14 @@
|
|
|
2
2
|
import type { Elysia } from '.';
|
|
3
3
|
import type { Serve, Server } from 'bun';
|
|
4
4
|
import type { Context, PreContext } from './context';
|
|
5
|
-
import type { Static, TSchema } from '@sinclair/typebox';
|
|
5
|
+
import type { Static, TObject, TSchema } from '@sinclair/typebox';
|
|
6
6
|
import type { TypeCheck } from '@sinclair/typebox/compiler';
|
|
7
7
|
import type { SCHEMA } from './utils';
|
|
8
|
+
import type { OpenAPIV2 } from 'openapi-types';
|
|
8
9
|
export type WithArray<T> = T | T[];
|
|
10
|
+
export type ObjectValues<T extends object> = T[keyof T];
|
|
9
11
|
export interface ElysiaInstance<Instance extends {
|
|
10
|
-
store?: Record<any, any> & Record<typeof SCHEMA,
|
|
12
|
+
store?: Record<any, any> & Record<typeof SCHEMA, Partial<OpenAPIV2.PathsObject>>;
|
|
11
13
|
request?: Record<any, any>;
|
|
12
14
|
schema?: TypedSchema;
|
|
13
15
|
} = {
|
|
@@ -15,11 +17,11 @@ export interface ElysiaInstance<Instance extends {
|
|
|
15
17
|
request: {};
|
|
16
18
|
schema: {};
|
|
17
19
|
}> {
|
|
18
|
-
request: Instance['request'];
|
|
19
|
-
store: Instance['store'];
|
|
20
|
-
schema: Instance['schema'];
|
|
20
|
+
request: Instance['request'] extends undefined ? Record<typeof SCHEMA, {}> : Instance['request'];
|
|
21
|
+
store: Instance['store'] extends undefined ? {} : Instance['store'];
|
|
22
|
+
schema: Instance['schema'] extends undefined ? TypedSchema : Instance['schema'];
|
|
21
23
|
}
|
|
22
|
-
export type Handler<Route extends TypedRoute = TypedRoute, Instance extends ElysiaInstance = ElysiaInstance, CatchResponse = Route['response']> = (context: Context<Route, Instance['store']> & Instance['request']) => Route['response'] extends CatchResponse ?
|
|
24
|
+
export type Handler<Route extends TypedRoute = TypedRoute, Instance extends ElysiaInstance = ElysiaInstance, CatchResponse = Route['response']> = (context: Context<Route, Instance['store']> & Instance['request']) => Route['response'] extends CatchResponse ? MaybePromise<CatchResponse> | Response : MaybePromise<Route['response']> | Response;
|
|
23
25
|
export type NoReturnHandler<Route extends TypedRoute = TypedRoute, Instance extends ElysiaInstance = ElysiaInstance> = (context: Context<Route, Instance['store']> & Instance['request']) => void | Promise<void>;
|
|
24
26
|
export type LifeCycleEvent = 'start' | 'request' | 'parse' | 'transform' | 'beforeHandle' | 'afterHandle' | 'error' | 'stop';
|
|
25
27
|
export type ListenCallback = ((server: Server) => void) | ((server: Server) => Promise<void>);
|
|
@@ -54,32 +56,22 @@ export interface RegisteredHook<Instance extends ElysiaInstance = ElysiaInstance
|
|
|
54
56
|
afterHandle: AfterRequestHandler<any, Instance>[];
|
|
55
57
|
error: ErrorHandler[];
|
|
56
58
|
}
|
|
57
|
-
export interface TypedSchema
|
|
58
|
-
body
|
|
59
|
-
headers
|
|
60
|
-
query
|
|
61
|
-
params
|
|
62
|
-
response
|
|
63
|
-
} = {
|
|
64
|
-
body: TSchema;
|
|
65
|
-
headers: TSchema;
|
|
66
|
-
query: TSchema;
|
|
67
|
-
params: TSchema;
|
|
68
|
-
response: TSchema;
|
|
69
|
-
}> {
|
|
70
|
-
body?: Schema['body'];
|
|
71
|
-
headers?: Schema['headers'];
|
|
72
|
-
query?: Schema['query'];
|
|
73
|
-
params?: Schema['params'];
|
|
74
|
-
response?: Schema['response'];
|
|
59
|
+
export interface TypedSchema {
|
|
60
|
+
body?: TSchema;
|
|
61
|
+
headers?: TObject;
|
|
62
|
+
query?: TObject;
|
|
63
|
+
params?: TObject;
|
|
64
|
+
response?: TSchema | Record<string | '200', TSchema>;
|
|
75
65
|
}
|
|
76
66
|
export type UnwrapSchema<Schema extends TSchema | undefined, Fallback = unknown> = Schema extends NonNullable<Schema> ? Static<NonNullable<Schema>> : Fallback;
|
|
77
67
|
export type TypedSchemaToRoute<Schema extends TypedSchema> = {
|
|
78
68
|
body: UnwrapSchema<Schema['body']>;
|
|
79
|
-
headers: UnwrapSchema<Schema['headers']> extends Record<string, any> ?
|
|
80
|
-
query: UnwrapSchema<Schema['query']> extends Record<string, any> ?
|
|
81
|
-
params: UnwrapSchema<Schema['params']> extends Record<string, any> ?
|
|
82
|
-
response: UnwrapSchema<Schema['response']
|
|
69
|
+
headers: UnwrapSchema<Schema['headers']> extends infer Result extends Record<string, any> ? Result : undefined;
|
|
70
|
+
query: UnwrapSchema<Schema['query']> extends infer Result extends Record<string, any> ? Result : undefined;
|
|
71
|
+
params: UnwrapSchema<Schema['params']> extends infer Result extends Record<string, any> ? Result : undefined;
|
|
72
|
+
response: Schema['response'] extends TSchema ? UnwrapSchema<Schema['response']> : Schema['response'] extends {
|
|
73
|
+
[k in string]: TSchema;
|
|
74
|
+
} ? UnwrapSchema<ObjectValues<Schema['response']>> : unknown;
|
|
83
75
|
};
|
|
84
76
|
export type SchemaValidator = {
|
|
85
77
|
body?: TypeCheck<any>;
|
|
@@ -88,10 +80,10 @@ export type SchemaValidator = {
|
|
|
88
80
|
params?: TypeCheck<any>;
|
|
89
81
|
response?: TypeCheck<any>;
|
|
90
82
|
};
|
|
91
|
-
export type HookHandler<Schema extends TypedSchema = TypedSchema, Instance extends ElysiaInstance = ElysiaInstance, Path extends string = string> =
|
|
92
|
-
response: void |
|
|
93
|
-
} : Omit<Omit<
|
|
94
|
-
response: void |
|
|
83
|
+
export type HookHandler<Schema extends TypedSchema = TypedSchema, Instance extends ElysiaInstance = ElysiaInstance, Path extends string = string, Typed extends TypedSchemaToRoute<Schema> = TypedSchemaToRoute<Schema>> = Handler<Typed['params'] extends {} ? Omit<Typed, 'response'> & {
|
|
84
|
+
response: void | Typed['response'];
|
|
85
|
+
} : Omit<Omit<Typed, 'response'> & {
|
|
86
|
+
response: void | Typed['response'];
|
|
95
87
|
}, 'params'> & {
|
|
96
88
|
params: Record<ExtractPath<Path>, string>;
|
|
97
89
|
}, Instance>;
|
|
@@ -105,24 +97,26 @@ export type MergeSchema<A extends TypedSchema, B extends TypedSchema> = {
|
|
|
105
97
|
params: PickInOrder<PickInOrder<A['params'], B['params']>, undefined>;
|
|
106
98
|
response: PickInOrder<PickInOrder<A['response'], B['response']>, undefined>;
|
|
107
99
|
};
|
|
108
|
-
export interface LocalHook<Schema extends TypedSchema
|
|
109
|
-
schema?: Schema
|
|
110
|
-
|
|
111
|
-
|
|
100
|
+
export interface LocalHook<Schema extends TypedSchema = TypedSchema, Instance extends ElysiaInstance<any> = ElysiaInstance, Path extends string = string, FinalSchema extends MergeSchema<Schema, Instance['schema']> = MergeSchema<Schema, Instance['schema']>> {
|
|
101
|
+
schema?: Schema & {
|
|
102
|
+
detail?: Partial<OpenAPIV2.OperationObject>;
|
|
103
|
+
};
|
|
104
|
+
transform?: WithArray<HookHandler<FinalSchema, Instance, Path>>;
|
|
105
|
+
beforeHandle?: WithArray<HookHandler<FinalSchema, Instance, Path>>;
|
|
112
106
|
afterHandle?: WithArray<AfterRequestHandler<any, Instance>>;
|
|
113
107
|
error?: WithArray<ErrorHandler>;
|
|
114
108
|
}
|
|
115
|
-
export type RouteToSchema<Schema extends TypedSchema
|
|
109
|
+
export type RouteToSchema<Schema extends TypedSchema = TypedSchema, Instance extends ElysiaInstance<any> = ElysiaInstance, Path extends string = string, FinalSchema extends MergeSchema<Schema, Instance['schema']> = MergeSchema<Schema, Instance['schema']>> = FinalSchema['params'] extends NonNullable<Schema['params']> ? TypedSchemaToRoute<FinalSchema> : Omit<TypedSchemaToRoute<FinalSchema>, 'params'> & {
|
|
116
110
|
params: Record<ExtractPath<Path>, string>;
|
|
117
111
|
};
|
|
118
|
-
export type ElysiaRoute<Method extends string = string, Schema extends TypedSchema
|
|
112
|
+
export type ElysiaRoute<Method extends string = string, Schema extends TypedSchema = TypedSchema, Instance extends ElysiaInstance = ElysiaInstance, Path extends string = string, CatchResponse = unknown, Typed extends RouteToSchema<Schema, Instance, Path> = RouteToSchema<Schema, Instance, Path>> = Elysia<{
|
|
119
113
|
request: Instance['request'];
|
|
120
|
-
store: Instance['store'] & Record<typeof SCHEMA, Record<Path, Record<Method,
|
|
121
|
-
response:
|
|
114
|
+
store: Instance['store'] & Record<typeof SCHEMA, Record<Path, Record<Method, Typed & {
|
|
115
|
+
response: Typed['response'] extends CatchResponse ? CatchResponse : Typed['response'];
|
|
122
116
|
}>>>;
|
|
123
117
|
schema: Instance['schema'];
|
|
124
118
|
}>;
|
|
125
|
-
export type LocalHandler<Schema extends TypedSchema
|
|
119
|
+
export type LocalHandler<Schema extends TypedSchema = TypedSchema, Instance extends ElysiaInstance = ElysiaInstance, Path extends string = string, CatchResponse = unknown> = Handler<RouteToSchema<Schema, Instance, Path>, Instance, CatchResponse>;
|
|
126
120
|
export interface TypedRoute {
|
|
127
121
|
body?: unknown;
|
|
128
122
|
headers?: Record<string, any>;
|
|
@@ -144,7 +138,6 @@ export type ComposedHandler = {
|
|
|
144
138
|
};
|
|
145
139
|
export interface ElysiaConfig {
|
|
146
140
|
strictPath: boolean;
|
|
147
|
-
queryLimit: number;
|
|
148
141
|
serve?: Partial<Serve>;
|
|
149
142
|
}
|
|
150
143
|
export type IsPathParameter<Part> = Part extends `:${infer Parameter}` ? Parameter : never;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "elysia",
|
|
3
3
|
"description": "Fast, and friendly Bun web framework",
|
|
4
|
-
"version": "0.2.0-beta.
|
|
4
|
+
"version": "0.2.0-beta.2",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "saltyAom",
|
|
7
7
|
"url": "https://github.com/SaltyAom",
|
|
@@ -35,15 +35,16 @@
|
|
|
35
35
|
"release": "npm run build && npm run test && npm publish"
|
|
36
36
|
},
|
|
37
37
|
"dependencies": {
|
|
38
|
-
"@sinclair/typebox": "0.25.
|
|
38
|
+
"@sinclair/typebox": "0.25.21",
|
|
39
|
+
"openapi-types": "^12.1.0"
|
|
39
40
|
},
|
|
40
41
|
"devDependencies": {
|
|
41
|
-
"@types/node": "^18.11.
|
|
42
|
-
"@typescript-eslint/eslint-plugin": "^5.
|
|
43
|
-
"@typescript-eslint/parser": "^5.
|
|
44
|
-
"bun-types": "^0.
|
|
45
|
-
"eslint": "^8.
|
|
42
|
+
"@types/node": "^18.11.18",
|
|
43
|
+
"@typescript-eslint/eslint-plugin": "^5.48.2",
|
|
44
|
+
"@typescript-eslint/parser": "^5.48.2",
|
|
45
|
+
"bun-types": "^0.5.0",
|
|
46
|
+
"eslint": "^8.32.0",
|
|
46
47
|
"rimraf": "^3.0.2",
|
|
47
|
-
"typescript": "^4.9.
|
|
48
|
+
"typescript": "^4.9.4"
|
|
48
49
|
}
|
|
49
50
|
}
|