elysia 0.5.17 → 0.5.19
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/cjs/compose.js +1 -1
- package/dist/cjs/context.d.ts +3 -7
- package/dist/cjs/ws/index.d.ts +10 -11
- package/dist/cjs/ws/types.d.ts +22 -18
- package/dist/compose.js +23 -23
- package/dist/context.d.ts +3 -7
- package/dist/ws/index.d.ts +10 -11
- package/dist/ws/types.d.ts +22 -18
- package/package.json +1 -1
package/dist/cjs/compose.js
CHANGED
package/dist/cjs/context.d.ts
CHANGED
|
@@ -1,20 +1,16 @@
|
|
|
1
1
|
/// <reference types="bun-types" />
|
|
2
|
-
import type { Elysia, TypedRoute,
|
|
3
|
-
export
|
|
2
|
+
import type { Elysia, TypedRoute, SCHEMA } from '.';
|
|
3
|
+
export type Context<Route extends TypedRoute = TypedRoute, Store extends Elysia['store'] = Elysia['store']> = {
|
|
4
4
|
request: Request;
|
|
5
5
|
headers: undefined extends Route['headers'] ? Record<string, string | null> : Route['headers'];
|
|
6
6
|
query: undefined extends Route['query'] ? Record<string, unknown> : Route['query'];
|
|
7
7
|
params: Route['params'];
|
|
8
8
|
body: Route['body'];
|
|
9
9
|
store: Store;
|
|
10
|
-
[SCHEMA]?: TypedSchema;
|
|
11
|
-
[DEFS]?: {
|
|
12
|
-
[index: string]: Record<string, unknown>;
|
|
13
|
-
};
|
|
14
10
|
set: {
|
|
15
11
|
headers: Record<string, string>;
|
|
16
12
|
status?: number;
|
|
17
13
|
redirect?: string;
|
|
18
14
|
};
|
|
19
|
-
}
|
|
15
|
+
} & Record<typeof SCHEMA, Route>;
|
|
20
16
|
export type PreContext<Route extends TypedRoute = TypedRoute, Store extends Elysia['store'] = Elysia['store']> = Omit<Context<Route, Store>, 'query' | 'params' | 'body'>;
|
package/dist/cjs/ws/index.d.ts
CHANGED
|
@@ -1,17 +1,15 @@
|
|
|
1
1
|
/// <reference types="bun-types" />
|
|
2
|
-
import type { ServerWebSocket, WebSocketHandler } from 'bun';
|
|
3
|
-
import type { Elysia } from '..';
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
import type { ElysiaInstance, UnwrapSchema } from '../types';
|
|
7
|
-
export declare class ElysiaWS<WS extends ElysiaWSContext<any> = ElysiaWSContext, Schema extends WSTypedSchema = WSTypedSchema, Definitions extends ElysiaInstance['meta'][typeof DEFS] = {}> {
|
|
2
|
+
import type { ServerWebSocket, ServerWebSocketSendStatus, WebSocketHandler } from 'bun';
|
|
3
|
+
import type { Elysia, SCHEMA } from '..';
|
|
4
|
+
import type { ElysiaWSContext } from './types';
|
|
5
|
+
export declare class ElysiaWS<WS extends ElysiaWSContext> {
|
|
8
6
|
raw: WS;
|
|
9
7
|
data: WS['data'];
|
|
10
8
|
isSubscribed: WS['isSubscribed'];
|
|
11
9
|
constructor(ws: WS);
|
|
12
|
-
publish(topic: string, data?:
|
|
13
|
-
publishToSelf(topic: string, data?:
|
|
14
|
-
send(data:
|
|
10
|
+
publish(topic: string, data?: WS['data'][typeof SCHEMA]['response'], compress?: boolean): this;
|
|
11
|
+
publishToSelf(topic: string, data?: WS['data'][typeof SCHEMA]['response'], compress?: boolean): this;
|
|
12
|
+
send(data: WS['data'][typeof SCHEMA]['response']): this;
|
|
15
13
|
subscribe(room: string): this;
|
|
16
14
|
unsubscribe(room: string): this;
|
|
17
15
|
cork(callback: (ws: ServerWebSocket<any>) => any): this;
|
|
@@ -20,9 +18,10 @@ export declare class ElysiaWS<WS extends ElysiaWSContext<any> = ElysiaWSContext,
|
|
|
20
18
|
export declare const ws: (config?: Omit<WebSocketHandler, 'open' | 'message' | 'close' | 'drain'>) => (app: Elysia) => Elysia<{
|
|
21
19
|
store: {};
|
|
22
20
|
request: {
|
|
23
|
-
publish:
|
|
21
|
+
publish: WSPublish;
|
|
24
22
|
};
|
|
25
23
|
schema: {};
|
|
26
|
-
meta: Record<typeof
|
|
24
|
+
meta: Record<typeof SCHEMA, {}> & Record<typeof import("..").DEFS, {}> & Record<typeof import("..").EXPOSED, {}>;
|
|
27
25
|
}>;
|
|
26
|
+
type WSPublish = (topic: string, data: string | ArrayBufferView | ArrayBuffer | SharedArrayBuffer, compress?: boolean) => ServerWebSocketSendStatus;
|
|
28
27
|
export type { WSTypedSchema, WebSocketHeaderHandler, WebSocketSchemaToRoute, ElysiaWSContext, ElysiaWSOptions, TransformMessageHandler } from './types';
|
package/dist/cjs/ws/types.d.ts
CHANGED
|
@@ -1,14 +1,18 @@
|
|
|
1
1
|
/// <reference types="bun-types" />
|
|
2
2
|
import type { ServerWebSocket, WebSocketHandler } from 'bun';
|
|
3
|
-
import type { TSchema } from '@sinclair/typebox';
|
|
3
|
+
import type { TObject, TSchema } from '@sinclair/typebox';
|
|
4
4
|
import type { TypeCheck } from '@sinclair/typebox/compiler';
|
|
5
|
-
import type { ElysiaWS } from '.';
|
|
6
5
|
import type { Context } from '../context';
|
|
7
6
|
import type { DEFS } from '../utils';
|
|
8
|
-
import type { ElysiaInstance, UnwrapSchema,
|
|
9
|
-
|
|
7
|
+
import type { ElysiaInstance, UnwrapSchema, ExtractPath, WithArray, NoReturnHandler, HookHandler } from '../types';
|
|
8
|
+
import type { ElysiaWS } from '.';
|
|
9
|
+
export interface WSTypedSchema<ModelName extends string = string> {
|
|
10
|
+
body?: TSchema | ModelName;
|
|
11
|
+
headers?: TObject | ModelName;
|
|
12
|
+
query?: TObject | ModelName;
|
|
13
|
+
params?: TObject | ModelName;
|
|
10
14
|
response?: TSchema | ModelName;
|
|
11
|
-
}
|
|
15
|
+
}
|
|
12
16
|
export type TypedWSSchemaToRoute<Schema extends WSTypedSchema = WSTypedSchema, Definitions extends ElysiaInstance['meta'][typeof DEFS] = {}> = {
|
|
13
17
|
body: UnwrapSchema<Schema['body'], Definitions>;
|
|
14
18
|
headers: UnwrapSchema<Schema['headers'], Definitions> extends infer Result extends Record<string, any> ? Result : undefined;
|
|
@@ -17,17 +21,19 @@ export type TypedWSSchemaToRoute<Schema extends WSTypedSchema = WSTypedSchema, D
|
|
|
17
21
|
response: UnwrapSchema<Schema['params'], Definitions> extends infer Result extends Record<string, any> ? Result : undefined;
|
|
18
22
|
};
|
|
19
23
|
export type WebSocketSchemaToRoute<Schema extends WSTypedSchema, Definitions extends ElysiaInstance['meta'][typeof DEFS] = {}> = {
|
|
20
|
-
body: UnwrapSchema<Schema['body'], Definitions
|
|
21
|
-
headers: UnwrapSchema<Schema['headers'], Definitions
|
|
22
|
-
query: UnwrapSchema<Schema['query'], Definitions
|
|
23
|
-
params: UnwrapSchema<Schema['params'], Definitions
|
|
24
|
-
response: UnwrapSchema<Schema['response'], Definitions
|
|
24
|
+
body: UnwrapSchema<Schema['body'], Definitions, undefined>;
|
|
25
|
+
headers: UnwrapSchema<Schema['headers'], Definitions, undefined>;
|
|
26
|
+
query: UnwrapSchema<Schema['query'], Definitions, undefined>;
|
|
27
|
+
params: UnwrapSchema<Schema['params'], Definitions, undefined>;
|
|
28
|
+
response: UnwrapSchema<Schema['response'], Definitions, undefined>;
|
|
25
29
|
};
|
|
26
30
|
export type TransformMessageHandler<Message extends WSTypedSchema['body']> = (message: UnwrapSchema<Message>) => void | UnwrapSchema<Message>;
|
|
27
|
-
export type ElysiaWSContext<Schema extends WSTypedSchema = WSTypedSchema
|
|
28
|
-
|
|
29
|
-
headers:
|
|
30
|
-
|
|
31
|
+
export type ElysiaWSContext<Schema extends WSTypedSchema<any> = WSTypedSchema<never>, Definitions extends ElysiaInstance['meta'][typeof DEFS] = {}, Path extends string = never> = ServerWebSocket<Context<{
|
|
32
|
+
body: UnwrapSchema<Schema['body'], Definitions, undefined>;
|
|
33
|
+
headers: UnwrapSchema<Schema['headers'], Definitions, undefined>;
|
|
34
|
+
query: UnwrapSchema<Schema['query'], Definitions, undefined>;
|
|
35
|
+
params: ExtractPath<Path> extends infer Params extends string ? Record<Params, string> : UnwrapSchema<Schema['params'], Definitions, undefined>;
|
|
36
|
+
response: UnwrapSchema<Schema['response'], Definitions, undefined>;
|
|
31
37
|
}> & {
|
|
32
38
|
id: number;
|
|
33
39
|
message: TypeCheck<any>;
|
|
@@ -40,8 +46,7 @@ export type WebSocketHeaderHandler<Schema extends WSTypedSchema = WSTypedSchema,
|
|
|
40
46
|
}, 'params'> & {
|
|
41
47
|
params: Record<ExtractPath<Path>, string>;
|
|
42
48
|
}) => HeadersInit;
|
|
43
|
-
type
|
|
44
|
-
export type ElysiaWSOptions<Path extends string = '', Schema extends WSTypedSchema = {}, Definitions extends ElysiaInstance['meta'][typeof DEFS] = {}> = PartialWebSocketHandler & ElysiaWS<ElysiaWSContext<Schema, Path>, Schema, Definitions> extends infer WS ? Partial<Schema> & {
|
|
49
|
+
export type ElysiaWSOptions<Path extends string, Schema extends WSTypedSchema<any>, Definitions extends ElysiaInstance['meta'][typeof DEFS]> = Omit<Partial<WebSocketHandler<Context>>, 'open' | 'message' | 'close' | 'drain' | 'publish' | 'publishToSelf'> & (ElysiaWS<ElysiaWSContext<Schema, Definitions, Path>> extends infer WS ? Partial<Schema> & {
|
|
45
50
|
beforeHandle?: WithArray<HookHandler<Schema>>;
|
|
46
51
|
transform?: WithArray<NoReturnHandler<TypedWSSchemaToRoute<Schema>>>;
|
|
47
52
|
transformMessage?: WithArray<TransformMessageHandler<Schema['body']>>;
|
|
@@ -50,5 +55,4 @@ export type ElysiaWSOptions<Path extends string = '', Schema extends WSTypedSche
|
|
|
50
55
|
message?: (ws: WS, message: UnwrapSchema<Schema['body'], Definitions>) => any;
|
|
51
56
|
close?: (ws: WS, code: number, message: string) => void | Promise<void>;
|
|
52
57
|
drain?: (ws: WS) => void | Promise<void>;
|
|
53
|
-
} : never;
|
|
54
|
-
export {};
|
|
58
|
+
} : never);
|
package/dist/compose.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import{parse as e}from"fast-querystring";import{mapEarlyResponse as r,mapResponse as t,mapCompactResponse as s}from"./handler";import{SCHEMA as n,DEFS as o}from"./utils";import{NotFoundError as a,ValidationError as c,InternalServerError as
|
|
1
|
+
import{parse as e}from"fast-querystring";import{mapEarlyResponse as r,mapResponse as t,mapCompactResponse as s}from"./handler";import{SCHEMA as n,DEFS as o}from"./utils";import{NotFoundError as a,ValidationError as c,InternalServerError as i}from"./error";let l="AsyncFunction",f=e=>e.constructor.name===l,u=new Headers,d=RegExp(" (\\w+) = context","g");export const hasReturn=e=>{let r=e.indexOf(")");return 61===e.charCodeAt(r+2)&&123!==e.charCodeAt(r+5)||e.includes("return")};let p=e=>({composeValidation:(r,t=`c.${r}`)=>e?`throw new ValidationError(
|
|
2
2
|
'${r}',
|
|
3
3
|
${r},
|
|
4
4
|
${t}
|
|
@@ -14,7 +14,7 @@ ${r}
|
|
|
14
14
|
'response',
|
|
15
15
|
response[c.set.status],
|
|
16
16
|
${r}
|
|
17
|
-
).toResponse(c.set.headers)`});export const isFnUse=(e,r)=>{r=r.trimStart();let t=40===r.charCodeAt(0)||r.startsWith("function")?r.slice(r.indexOf("(")+1,r.indexOf(")")):r.slice(0,r.indexOf("=")-1);if(""===t)return!1;if(123===t.charCodeAt(0))return!!(t.includes(`{ ${e}`)||t.includes(`, ${e}`)||t.includes(`,${e}`));if(r.match(RegExp(`${t}(.${e}|\\["${e}"\\])`)))return!0;let s=[t];for(let e of r.matchAll(d))s.push(e[1]);let n=RegExp(`{.*?} = (${s.join("|")})`,"g");for(let[t]of r.matchAll(n))if(t.includes(`{ ${e}`)||t.includes(`, ${e}`))return!0;return!1};export const findElysiaMeta=(e,r,t=[],s="")=>{if("object"===r.type){let n=r.properties;for(let r in n){let o=n[r],a=s?s+"."+r:r;if("object"===o.type){findElysiaMeta(e,o,t,a);continue}if(o.anyOf){for(let r of o.anyOf)findElysiaMeta(e,r,t,a);continue}o.elysiaMeta===e&&t.push(a)}return 0===t.length?null:t}return r?.elysiaMeta===e?(s&&t.push(s),"root"):null};let y=e=>{if(!e)return;let r=e?.schema;if(r&&"anyOf"in r){let e=!1,t=r.anyOf[0].type;for(let s of r.anyOf)if(s.type!==t){e=!0;break}if(!e)return t}};export const composeHandler=({method:d,hooks:m,validator:h,handler:$,handleError:b,meta:q,onRequest:E,config:g})=>{let w=g.forceErrorEncapsulation||m.error.length>0||"undefined"==typeof Bun,{composeValidation:x,composeResponseValidation:k}=p(w),H=w?"try {\n":"",R=h||"GET"!==d?[$,...m.transform,...m.beforeHandle,...m.afterHandle].map(e=>e.toString()):[],v="GET"!==d&&"none"!==m.type&&(h.body||m.type||R.some(e=>isFnUse("body",e))),O=h.headers||R.some(e=>isFnUse("headers",e));O&&(H+=
|
|
17
|
+
).toResponse(c.set.headers)`});export const isFnUse=(e,r)=>{r=r.trimStart();let t=40===r.charCodeAt(0)||r.startsWith("function")?r.slice(r.indexOf("(")+1,r.indexOf(")")):r.slice(0,r.indexOf("=")-1);if(""===t)return!1;if(123===t.charCodeAt(0))return!!(t.includes(`{ ${e}`)||t.includes(`, ${e}`)||t.includes(`,${e}`));if(r.match(RegExp(`${t}(.${e}|\\["${e}"\\])`)))return!0;let s=[t];for(let e of r.matchAll(d))s.push(e[1]);let n=RegExp(`{.*?} = (${s.join("|")})`,"g");for(let[t]of r.matchAll(n))if(t.includes(`{ ${e}`)||t.includes(`, ${e}`))return!0;return!1};export const findElysiaMeta=(e,r,t=[],s="")=>{if("object"===r.type){let n=r.properties;for(let r in n){let o=n[r],a=s?s+"."+r:r;if("object"===o.type){findElysiaMeta(e,o,t,a);continue}if(o.anyOf){for(let r of o.anyOf)findElysiaMeta(e,r,t,a);continue}o.elysiaMeta===e&&t.push(a)}return 0===t.length?null:t}return r?.elysiaMeta===e?(s&&t.push(s),"root"):null};let y=e=>{if(!e)return;let r=e?.schema;if(r&&"anyOf"in r){let e=!1,t=r.anyOf[0].type;for(let s of r.anyOf)if(s.type!==t){e=!0;break}if(!e)return t}};export const composeHandler=({method:d,hooks:m,validator:h,handler:$,handleError:b,meta:q,onRequest:E,config:g})=>{let w=g.forceErrorEncapsulation||m.error.length>0||"undefined"==typeof Bun,{composeValidation:x,composeResponseValidation:k}=p(w),H=w?"try {\n":"",R=h||"GET"!==d?[$,...m.transform,...m.beforeHandle,...m.afterHandle].map(e=>e.toString()):[],v="GET"!==d&&"none"!==m.type&&(h.body||m.type||R.some(e=>isFnUse("body",e))),O=h.headers||R.some(e=>isFnUse("headers",e));O&&(H+=u.toJSON?`c.headers = c.request.headers.toJSON()
|
|
18
18
|
`:`c.headers = {}
|
|
19
19
|
for (const [key, value] of c.request.headers.entries())
|
|
20
20
|
c.headers[key] = value
|
|
@@ -25,7 +25,7 @@ ${r}
|
|
|
25
25
|
} else {
|
|
26
26
|
c.query = {}
|
|
27
27
|
}
|
|
28
|
-
`);let C=R.some(e=>isFnUse("set",e))||E.some(e=>isFnUse("set",e.toString())),F=v||$.constructor.name===
|
|
28
|
+
`);let C=R.some(e=>isFnUse("set",e))||E.some(e=>isFnUse("set",e.toString())),F=v||$.constructor.name===l||m.parse.length||m.afterHandle.find(f)||m.beforeHandle.find(f)||m.transform.find(f);if(v){let e=y(h?.body);if(m.type||e){if(m.type)switch(m.type){case"application/json":H+="c.body = JSON.parse(await c.request.text());";break;case"text/plain":H+="c.body = await c.request.text();";break;case"application/x-www-form-urlencoded":H+="c.body = parseQuery(await c.request.text());";break;case"application/octet-stream":H+="c.body = await c.request.arrayBuffer();";break;case"multipart/form-data":H+=`c.body = {}
|
|
29
29
|
|
|
30
30
|
const form = await c.request.formData()
|
|
31
31
|
for (const key of form.keys()) {
|
|
@@ -89,28 +89,28 @@ ${r}
|
|
|
89
89
|
break
|
|
90
90
|
}
|
|
91
91
|
}
|
|
92
|
-
`}H+="\n"}if(h.params){let e=findElysiaMeta("Numeric",h.params.schema);if(e){if("object"==typeof e)for(let r of e)H+=`c.params.${r} = +c.params.${r};`;H+="\n"}}if(h.query){let e=findElysiaMeta("Numeric",h.query.schema);if(e){if("object"==typeof e)for(let r of e)H+=`c.query.${r} = +c.query.${r};`;H+="\n"}}if(h.headers){let e=findElysiaMeta("Numeric",h.headers.schema);if(e){if("object"==typeof e)for(let r of e)H+=`c.headers.${r} = +c.headers.${r};`;H+="\n"}}if(h.body){let e=findElysiaMeta("Numeric",h.body.schema);if(e){switch(typeof e){case"string":H+="c.body = +c.body;";break;case"object":for(let r of e)H+=`c.body.${r} = +c.body.${r};`}H+="\n"}}if(m?.transform)for(let e=0;e<m.transform.length;e++){let r=m.transform[e];"derive"===r.$elysia?H+=m.transform[e].constructor.name===
|
|
92
|
+
`}H+="\n"}if(h.params){let e=findElysiaMeta("Numeric",h.params.schema);if(e){if("object"==typeof e)for(let r of e)H+=`c.params.${r} = +c.params.${r};`;H+="\n"}}if(h.query){let e=findElysiaMeta("Numeric",h.query.schema);if(e){if("object"==typeof e)for(let r of e)H+=`c.query.${r} = +c.query.${r};`;H+="\n"}}if(h.headers){let e=findElysiaMeta("Numeric",h.headers.schema);if(e){if("object"==typeof e)for(let r of e)H+=`c.headers.${r} = +c.headers.${r};`;H+="\n"}}if(h.body){let e=findElysiaMeta("Numeric",h.body.schema);if(e){switch(typeof e){case"string":H+="c.body = +c.body;";break;case"object":for(let r of e)H+=`c.body.${r} = +c.body.${r};`}H+="\n"}}if(m?.transform)for(let e=0;e<m.transform.length;e++){let r=m.transform[e];"derive"===r.$elysia?H+=m.transform[e].constructor.name===l?`Object.assign(c, await transform[${e}](c));`:`Object.assign(c, transform[${e}](c));`:H+=m.transform[e].constructor.name===l?`await transform[${e}](c);`:`transform[${e}](c);`}if(h&&(h.headers&&(H+=`
|
|
93
93
|
if (headers.Check(c.headers) === false) {
|
|
94
94
|
${x("headers")}
|
|
95
95
|
}
|
|
96
|
-
`),h.params&&(H+=`if(params.Check(c.params) === false) { ${x("params")} }`),h.query&&(H+=`if(query.Check(c.query) === false) { ${x("query")} }`),h.body&&(H+=`if(body.Check(c.body) === false) { ${x("body")} }`)),m?.beforeHandle)for(let e=0;e<m.beforeHandle.length;e++){let r=`be${e}`,t=hasReturn(m.beforeHandle[e].toString());if(t){if(H+=(m.beforeHandle[e].constructor.name===
|
|
96
|
+
`),h.params&&(H+=`if(params.Check(c.params) === false) { ${x("params")} }`),h.query&&(H+=`if(query.Check(c.query) === false) { ${x("query")} }`),h.body&&(H+=`if(body.Check(c.body) === false) { ${x("body")} }`)),m?.beforeHandle)for(let e=0;e<m.beforeHandle.length;e++){let r=`be${e}`,t=hasReturn(m.beforeHandle[e].toString());if(t){if(H+=(m.beforeHandle[e].constructor.name===l?`let ${r} = await beforeHandle[${e}](c);
|
|
97
97
|
`:`let ${r} = beforeHandle[${e}](c);
|
|
98
98
|
`)+`if(${r} !== undefined) {
|
|
99
|
-
`,m?.afterHandle)for(let e=0;e<m.afterHandle.length;e++){let t=hasReturn(m.afterHandle[e].toString());if(t){let t=`af${e}`;H+=(m.afterHandle[e].constructor.name===
|
|
99
|
+
`,m?.afterHandle)for(let e=0;e<m.afterHandle.length;e++){let t=hasReturn(m.afterHandle[e].toString());if(t){let t=`af${e}`;H+=(m.afterHandle[e].constructor.name===l?`const ${t} = await afterHandle[${e}](c, ${r});
|
|
100
100
|
`:`const ${t} = afterHandle[${e}](c, ${r});
|
|
101
101
|
`)+`if(${t} !== undefined) { ${r} = ${t} }
|
|
102
|
-
`}else H+=m.afterHandle[e].constructor.name===
|
|
102
|
+
`}else H+=m.afterHandle[e].constructor.name===l?`await afterHandle[${e}](c, ${r});
|
|
103
103
|
`:`afterHandle[${e}](c, ${r});
|
|
104
104
|
`}h.response&&(H+=`if(response[c.set.status]?.Check(${r}) === false) {
|
|
105
105
|
if(!(response instanceof Error))
|
|
106
106
|
${k(r)}
|
|
107
107
|
}
|
|
108
108
|
`),H+=`return mapEarlyResponse(${r}, c.set)}
|
|
109
|
-
`}else H+=m.beforeHandle[e].constructor.name===
|
|
109
|
+
`}else H+=m.beforeHandle[e].constructor.name===l?`await beforeHandle[${e}](c);
|
|
110
110
|
`:`beforeHandle[${e}](c);
|
|
111
|
-
`}if(m?.afterHandle.length){H+=$.constructor.name===
|
|
111
|
+
`}if(m?.afterHandle.length){H+=$.constructor.name===l?`let r = await handler(c);
|
|
112
112
|
`:`let r = handler(c);
|
|
113
|
-
`;for(let e=0;e<m.afterHandle.length;e++){let r=`af${e}`,t=hasReturn(m.afterHandle[e].toString());t?(H+=m.afterHandle[e].constructor.name===
|
|
113
|
+
`;for(let e=0;e<m.afterHandle.length;e++){let r=`af${e}`,t=hasReturn(m.afterHandle[e].toString());t?(H+=m.afterHandle[e].constructor.name===l?`let ${r} = await afterHandle[${e}](c, r)
|
|
114
114
|
`:`let ${r} = afterHandle[${e}](c, r)
|
|
115
115
|
`,h.response?H+=`if(${r} !== undefined) {if(response[c.set.status]?.Check(${r}) === false) {
|
|
116
116
|
if(!(response instanceof Error))
|
|
@@ -119,7 +119,7 @@ ${r}
|
|
|
119
119
|
${r} = mapEarlyResponse(${r}, c.set)
|
|
120
120
|
if(${r}) return ${r};
|
|
121
121
|
}`:H+=`if(${r}) return ${r};
|
|
122
|
-
`):H+=m.afterHandle[e].constructor.name===
|
|
122
|
+
`):H+=m.afterHandle[e].constructor.name===l?`await afterHandle[${e}](c, r)
|
|
123
123
|
`:`afterHandle[${e}](c, r)
|
|
124
124
|
`}h.response&&(H+=`if(response[c.set.status]?.Check(r) === false) {
|
|
125
125
|
if(!(response instanceof Error))
|
|
@@ -127,7 +127,7 @@ if(${r}) return ${r};
|
|
|
127
127
|
}
|
|
128
128
|
`),C?H+=`return mapResponse(r, c.set)
|
|
129
129
|
`:H+=`return mapCompactResponse(r)
|
|
130
|
-
`}else if(h.response)H+=($.constructor.name===
|
|
130
|
+
`}else if(h.response)H+=($.constructor.name===l?`const r = await handler(c);
|
|
131
131
|
`:`const r = handler(c);
|
|
132
132
|
`)+`if(response[c.set.status]?.Check(r) === false) {
|
|
133
133
|
if(!(response instanceof Error))
|
|
@@ -135,7 +135,7 @@ if(${r}) return ${r};
|
|
|
135
135
|
}
|
|
136
136
|
`,C?H+=`return mapResponse(r, c.set)
|
|
137
137
|
`:H+=`return mapCompactResponse(r)
|
|
138
|
-
`;else{let e=$.constructor.name===
|
|
138
|
+
`;else{let e=$.constructor.name===l?"await handler(c) ":"handler(c)";C?H+=`return mapResponse(${e}, c.set)
|
|
139
139
|
`:H+=`return mapCompactResponse(${e})
|
|
140
140
|
`}w&&(H+=`
|
|
141
141
|
} catch(error) {
|
|
@@ -198,7 +198,7 @@ if(${r}) return ${r};
|
|
|
198
198
|
return ${F?"async":""} function(c) {
|
|
199
199
|
${q?"c[SCHEMA] = meta[SCHEMA]; c[DEFS] = meta[DEFS];":""}
|
|
200
200
|
${H}
|
|
201
|
-
}`;let N=Function("hooks",H);return N({handler:$,hooks:m,validator:h,handleError:b,utils:{mapResponse:t,mapCompactResponse:s,mapEarlyResponse:r,parseQuery:e},error:{NotFoundError:a,ValidationError:c,InternalServerError:
|
|
201
|
+
}`;let N=Function("hooks",H);return N({handler:$,hooks:m,validator:h,handleError:b,utils:{mapResponse:t,mapCompactResponse:s,mapEarlyResponse:r,parseQuery:e},error:{NotFoundError:a,ValidationError:c,InternalServerError:i},meta:q,SCHEMA:q?n:void 0,DEFS:q?o:void 0})};export const composeGeneralHandler=e=>{let t="";for(let r of Object.keys(e.decorators))t+=`,${r}: app.decorators.${r}`;let{router:s,staticRouter:n}=e,o=`
|
|
202
202
|
const route = find(request.method, path) ${s.root.ALL?'?? find("ALL", path)':""}
|
|
203
203
|
if (route === null)
|
|
204
204
|
return ${e.event.error.length?`handleError(
|
|
@@ -216,10 +216,10 @@ switch(request.method) {
|
|
|
216
216
|
${r}
|
|
217
217
|
${t??`default: ${o}`}}
|
|
218
218
|
|
|
219
|
-
`;let
|
|
219
|
+
`;let i=`const {
|
|
220
220
|
app,
|
|
221
221
|
app: { store, router, staticRouter },
|
|
222
|
-
|
|
222
|
+
mapEarlyResponse,
|
|
223
223
|
NotFoundError
|
|
224
224
|
} = data
|
|
225
225
|
|
|
@@ -235,7 +235,7 @@ ${t??`default: ${o}`}}
|
|
|
235
235
|
${e.event.error.length?"":"const error404 = notFound.message.toString()"}
|
|
236
236
|
|
|
237
237
|
return function(request) {
|
|
238
|
-
`;if(e.event.request.length){
|
|
238
|
+
`;if(e.event.request.length){i+=`
|
|
239
239
|
const ctx = {
|
|
240
240
|
request,
|
|
241
241
|
store,
|
|
@@ -247,19 +247,19 @@ ${t??`default: ${o}`}}
|
|
|
247
247
|
}
|
|
248
248
|
|
|
249
249
|
try {
|
|
250
|
-
`;for(let r=0;r<e.event.request.length;r++){let t=hasReturn(e.event.request[r].toString());
|
|
250
|
+
`;for(let r=0;r<e.event.request.length;r++){let t=hasReturn(e.event.request[r].toString());i+=t?`const response = mapEarlyResponse(
|
|
251
251
|
onRequest[${r}](ctx),
|
|
252
252
|
ctx.set
|
|
253
253
|
)
|
|
254
254
|
if (response) return response
|
|
255
|
-
`:`mapEarlyResponse(onRequest[${r}](ctx), ctx.set);`}
|
|
255
|
+
`:`mapEarlyResponse(onRequest[${r}](ctx), ctx.set);`}i+=`} catch (error) {
|
|
256
256
|
return handleError(request, error, ctx.set)
|
|
257
257
|
}
|
|
258
258
|
|
|
259
259
|
const url = request.url,
|
|
260
260
|
s = url.indexOf('/', 12),
|
|
261
261
|
i = ctx.query = url.indexOf('?', s + 1),
|
|
262
|
-
path = i === -1 ? url.substring(s) : url.substring(s, i);`}else
|
|
262
|
+
path = i === -1 ? url.substring(s) : url.substring(s, i);`}else i+=`
|
|
263
263
|
const url = request.url,
|
|
264
264
|
s = url.indexOf('/', 12)
|
|
265
265
|
|
|
@@ -277,19 +277,19 @@ ${t??`default: ${o}`}}
|
|
|
277
277
|
const path =
|
|
278
278
|
ctx.query === -1
|
|
279
279
|
? url.substring(s)
|
|
280
|
-
: url.substring(s, ctx.query);`;return
|
|
280
|
+
: url.substring(s, ctx.query);`;return i+=`
|
|
281
281
|
switch(path) {
|
|
282
282
|
${c}
|
|
283
283
|
|
|
284
284
|
default:
|
|
285
285
|
${o}
|
|
286
286
|
}
|
|
287
|
-
}`,e.handleError=composeErrorHandler(e),Function("data",
|
|
287
|
+
}`,e.handleError=composeErrorHandler(e),Function("data",i)({app:e,mapEarlyResponse:r,NotFoundError:a})};export const composeErrorHandler=e=>{let r=`const {
|
|
288
288
|
app: { event: { error: onError } },
|
|
289
289
|
mapResponse
|
|
290
290
|
} = inject
|
|
291
291
|
|
|
292
|
-
return ${e.event.error.find(e=>e.constructor.name===
|
|
292
|
+
return ${e.event.error.find(e=>e.constructor.name===l)?"async":""} function(request, error, set) {`;for(let t=0;t<e.event.error.length;t++){let s=e.event.error[t],n=`${s.constructor.name===l?"await ":""}onError[${t}]({
|
|
293
293
|
request,
|
|
294
294
|
code: error.code ?? 'UNKNOWN',
|
|
295
295
|
error,
|
package/dist/context.d.ts
CHANGED
|
@@ -1,20 +1,16 @@
|
|
|
1
1
|
/// <reference types="bun-types" />
|
|
2
|
-
import type { Elysia, TypedRoute,
|
|
3
|
-
export
|
|
2
|
+
import type { Elysia, TypedRoute, SCHEMA } from '.';
|
|
3
|
+
export type Context<Route extends TypedRoute = TypedRoute, Store extends Elysia['store'] = Elysia['store']> = {
|
|
4
4
|
request: Request;
|
|
5
5
|
headers: undefined extends Route['headers'] ? Record<string, string | null> : Route['headers'];
|
|
6
6
|
query: undefined extends Route['query'] ? Record<string, unknown> : Route['query'];
|
|
7
7
|
params: Route['params'];
|
|
8
8
|
body: Route['body'];
|
|
9
9
|
store: Store;
|
|
10
|
-
[SCHEMA]?: TypedSchema;
|
|
11
|
-
[DEFS]?: {
|
|
12
|
-
[index: string]: Record<string, unknown>;
|
|
13
|
-
};
|
|
14
10
|
set: {
|
|
15
11
|
headers: Record<string, string>;
|
|
16
12
|
status?: number;
|
|
17
13
|
redirect?: string;
|
|
18
14
|
};
|
|
19
|
-
}
|
|
15
|
+
} & Record<typeof SCHEMA, Route>;
|
|
20
16
|
export type PreContext<Route extends TypedRoute = TypedRoute, Store extends Elysia['store'] = Elysia['store']> = Omit<Context<Route, Store>, 'query' | 'params' | 'body'>;
|
package/dist/ws/index.d.ts
CHANGED
|
@@ -1,17 +1,15 @@
|
|
|
1
1
|
/// <reference types="bun-types" />
|
|
2
|
-
import type { ServerWebSocket, WebSocketHandler } from 'bun';
|
|
3
|
-
import type { Elysia } from '..';
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
import type { ElysiaInstance, UnwrapSchema } from '../types';
|
|
7
|
-
export declare class ElysiaWS<WS extends ElysiaWSContext<any> = ElysiaWSContext, Schema extends WSTypedSchema = WSTypedSchema, Definitions extends ElysiaInstance['meta'][typeof DEFS] = {}> {
|
|
2
|
+
import type { ServerWebSocket, ServerWebSocketSendStatus, WebSocketHandler } from 'bun';
|
|
3
|
+
import type { Elysia, SCHEMA } from '..';
|
|
4
|
+
import type { ElysiaWSContext } from './types';
|
|
5
|
+
export declare class ElysiaWS<WS extends ElysiaWSContext> {
|
|
8
6
|
raw: WS;
|
|
9
7
|
data: WS['data'];
|
|
10
8
|
isSubscribed: WS['isSubscribed'];
|
|
11
9
|
constructor(ws: WS);
|
|
12
|
-
publish(topic: string, data?:
|
|
13
|
-
publishToSelf(topic: string, data?:
|
|
14
|
-
send(data:
|
|
10
|
+
publish(topic: string, data?: WS['data'][typeof SCHEMA]['response'], compress?: boolean): this;
|
|
11
|
+
publishToSelf(topic: string, data?: WS['data'][typeof SCHEMA]['response'], compress?: boolean): this;
|
|
12
|
+
send(data: WS['data'][typeof SCHEMA]['response']): this;
|
|
15
13
|
subscribe(room: string): this;
|
|
16
14
|
unsubscribe(room: string): this;
|
|
17
15
|
cork(callback: (ws: ServerWebSocket<any>) => any): this;
|
|
@@ -20,9 +18,10 @@ export declare class ElysiaWS<WS extends ElysiaWSContext<any> = ElysiaWSContext,
|
|
|
20
18
|
export declare const ws: (config?: Omit<WebSocketHandler, 'open' | 'message' | 'close' | 'drain'>) => (app: Elysia) => Elysia<{
|
|
21
19
|
store: {};
|
|
22
20
|
request: {
|
|
23
|
-
publish:
|
|
21
|
+
publish: WSPublish;
|
|
24
22
|
};
|
|
25
23
|
schema: {};
|
|
26
|
-
meta: Record<typeof
|
|
24
|
+
meta: Record<typeof SCHEMA, {}> & Record<typeof import("..").DEFS, {}> & Record<typeof import("..").EXPOSED, {}>;
|
|
27
25
|
}>;
|
|
26
|
+
type WSPublish = (topic: string, data: string | ArrayBufferView | ArrayBuffer | SharedArrayBuffer, compress?: boolean) => ServerWebSocketSendStatus;
|
|
28
27
|
export type { WSTypedSchema, WebSocketHeaderHandler, WebSocketSchemaToRoute, ElysiaWSContext, ElysiaWSOptions, TransformMessageHandler } from './types';
|
package/dist/ws/types.d.ts
CHANGED
|
@@ -1,14 +1,18 @@
|
|
|
1
1
|
/// <reference types="bun-types" />
|
|
2
2
|
import type { ServerWebSocket, WebSocketHandler } from 'bun';
|
|
3
|
-
import type { TSchema } from '@sinclair/typebox';
|
|
3
|
+
import type { TObject, TSchema } from '@sinclair/typebox';
|
|
4
4
|
import type { TypeCheck } from '@sinclair/typebox/compiler';
|
|
5
|
-
import type { ElysiaWS } from '.';
|
|
6
5
|
import type { Context } from '../context';
|
|
7
6
|
import type { DEFS } from '../utils';
|
|
8
|
-
import type { ElysiaInstance, UnwrapSchema,
|
|
9
|
-
|
|
7
|
+
import type { ElysiaInstance, UnwrapSchema, ExtractPath, WithArray, NoReturnHandler, HookHandler } from '../types';
|
|
8
|
+
import type { ElysiaWS } from '.';
|
|
9
|
+
export interface WSTypedSchema<ModelName extends string = string> {
|
|
10
|
+
body?: TSchema | ModelName;
|
|
11
|
+
headers?: TObject | ModelName;
|
|
12
|
+
query?: TObject | ModelName;
|
|
13
|
+
params?: TObject | ModelName;
|
|
10
14
|
response?: TSchema | ModelName;
|
|
11
|
-
}
|
|
15
|
+
}
|
|
12
16
|
export type TypedWSSchemaToRoute<Schema extends WSTypedSchema = WSTypedSchema, Definitions extends ElysiaInstance['meta'][typeof DEFS] = {}> = {
|
|
13
17
|
body: UnwrapSchema<Schema['body'], Definitions>;
|
|
14
18
|
headers: UnwrapSchema<Schema['headers'], Definitions> extends infer Result extends Record<string, any> ? Result : undefined;
|
|
@@ -17,17 +21,19 @@ export type TypedWSSchemaToRoute<Schema extends WSTypedSchema = WSTypedSchema, D
|
|
|
17
21
|
response: UnwrapSchema<Schema['params'], Definitions> extends infer Result extends Record<string, any> ? Result : undefined;
|
|
18
22
|
};
|
|
19
23
|
export type WebSocketSchemaToRoute<Schema extends WSTypedSchema, Definitions extends ElysiaInstance['meta'][typeof DEFS] = {}> = {
|
|
20
|
-
body: UnwrapSchema<Schema['body'], Definitions
|
|
21
|
-
headers: UnwrapSchema<Schema['headers'], Definitions
|
|
22
|
-
query: UnwrapSchema<Schema['query'], Definitions
|
|
23
|
-
params: UnwrapSchema<Schema['params'], Definitions
|
|
24
|
-
response: UnwrapSchema<Schema['response'], Definitions
|
|
24
|
+
body: UnwrapSchema<Schema['body'], Definitions, undefined>;
|
|
25
|
+
headers: UnwrapSchema<Schema['headers'], Definitions, undefined>;
|
|
26
|
+
query: UnwrapSchema<Schema['query'], Definitions, undefined>;
|
|
27
|
+
params: UnwrapSchema<Schema['params'], Definitions, undefined>;
|
|
28
|
+
response: UnwrapSchema<Schema['response'], Definitions, undefined>;
|
|
25
29
|
};
|
|
26
30
|
export type TransformMessageHandler<Message extends WSTypedSchema['body']> = (message: UnwrapSchema<Message>) => void | UnwrapSchema<Message>;
|
|
27
|
-
export type ElysiaWSContext<Schema extends WSTypedSchema = WSTypedSchema
|
|
28
|
-
|
|
29
|
-
headers:
|
|
30
|
-
|
|
31
|
+
export type ElysiaWSContext<Schema extends WSTypedSchema<any> = WSTypedSchema<never>, Definitions extends ElysiaInstance['meta'][typeof DEFS] = {}, Path extends string = never> = ServerWebSocket<Context<{
|
|
32
|
+
body: UnwrapSchema<Schema['body'], Definitions, undefined>;
|
|
33
|
+
headers: UnwrapSchema<Schema['headers'], Definitions, undefined>;
|
|
34
|
+
query: UnwrapSchema<Schema['query'], Definitions, undefined>;
|
|
35
|
+
params: ExtractPath<Path> extends infer Params extends string ? Record<Params, string> : UnwrapSchema<Schema['params'], Definitions, undefined>;
|
|
36
|
+
response: UnwrapSchema<Schema['response'], Definitions, undefined>;
|
|
31
37
|
}> & {
|
|
32
38
|
id: number;
|
|
33
39
|
message: TypeCheck<any>;
|
|
@@ -40,8 +46,7 @@ export type WebSocketHeaderHandler<Schema extends WSTypedSchema = WSTypedSchema,
|
|
|
40
46
|
}, 'params'> & {
|
|
41
47
|
params: Record<ExtractPath<Path>, string>;
|
|
42
48
|
}) => HeadersInit;
|
|
43
|
-
type
|
|
44
|
-
export type ElysiaWSOptions<Path extends string = '', Schema extends WSTypedSchema = {}, Definitions extends ElysiaInstance['meta'][typeof DEFS] = {}> = PartialWebSocketHandler & ElysiaWS<ElysiaWSContext<Schema, Path>, Schema, Definitions> extends infer WS ? Partial<Schema> & {
|
|
49
|
+
export type ElysiaWSOptions<Path extends string, Schema extends WSTypedSchema<any>, Definitions extends ElysiaInstance['meta'][typeof DEFS]> = Omit<Partial<WebSocketHandler<Context>>, 'open' | 'message' | 'close' | 'drain' | 'publish' | 'publishToSelf'> & (ElysiaWS<ElysiaWSContext<Schema, Definitions, Path>> extends infer WS ? Partial<Schema> & {
|
|
45
50
|
beforeHandle?: WithArray<HookHandler<Schema>>;
|
|
46
51
|
transform?: WithArray<NoReturnHandler<TypedWSSchemaToRoute<Schema>>>;
|
|
47
52
|
transformMessage?: WithArray<TransformMessageHandler<Schema['body']>>;
|
|
@@ -50,5 +55,4 @@ export type ElysiaWSOptions<Path extends string = '', Schema extends WSTypedSche
|
|
|
50
55
|
message?: (ws: WS, message: UnwrapSchema<Schema['body'], Definitions>) => any;
|
|
51
56
|
close?: (ws: WS, code: number, message: string) => void | Promise<void>;
|
|
52
57
|
drain?: (ws: WS) => void | Promise<void>;
|
|
53
|
-
} : never;
|
|
54
|
-
export {};
|
|
58
|
+
} : never);
|