fets 0.2.1 → 0.2.2-alpha-20230605165125-e9d94d2

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.
@@ -5,6 +5,7 @@ const tslib_1 = require("tslib");
5
5
  const DefaultFetchAPI = tslib_1.__importStar(require("@whatwg-node/fetch"));
6
6
  const server_1 = require("@whatwg-node/server");
7
7
  const openapi_js_1 = require("./plugins/openapi.js");
8
+ const uws_js_1 = require("./plugins/uws.js");
8
9
  const Response_js_1 = require("./Response.js");
9
10
  const zod_js_1 = require("./zod/zod.js");
10
11
  const HTTP_METHODS = [
@@ -26,7 +27,7 @@ function createRouterBase({ fetchAPI: givenFetchAPI, base: basePath = '/', plugi
26
27
  };
27
28
  const __onRouterInitHooks = [];
28
29
  const onRouteHooks = [];
29
- const onSerializeResponseHooks = [];
30
+ const __onSerializeResponseHooks = [];
30
31
  for (const plugin of plugins) {
31
32
  if (plugin.onRouterInit) {
32
33
  __onRouterInitHooks.push(plugin.onRouterInit);
@@ -35,7 +36,7 @@ function createRouterBase({ fetchAPI: givenFetchAPI, base: basePath = '/', plugi
35
36
  onRouteHooks.push(plugin.onRoute);
36
37
  }
37
38
  if (plugin.onSerializeResponse) {
38
- onSerializeResponseHooks.push(plugin.onSerializeResponse);
39
+ __onSerializeResponseHooks.push(plugin.onSerializeResponse);
39
40
  }
40
41
  }
41
42
  const routesByMethod = new Map();
@@ -126,7 +127,7 @@ function createRouterBase({ fetchAPI: givenFetchAPI, base: basePath = '/', plugi
126
127
  for (const handler of handlers) {
127
128
  const handlerResult = await handler(routerRequest, context);
128
129
  if ((0, Response_js_1.isLazySerializedResponse)(handlerResult)) {
129
- for (const onSerializeResponseHook of onSerializeResponseHooks) {
130
+ for (const onSerializeResponseHook of __onSerializeResponseHooks) {
130
131
  onSerializeResponseHook({
131
132
  request: routerRequest,
132
133
  lazyResponse: handlerResult,
@@ -178,10 +179,13 @@ function createRouterBase({ fetchAPI: givenFetchAPI, base: basePath = '/', plugi
178
179
  },
179
180
  __client: {},
180
181
  __onRouterInitHooks,
182
+ __onSerializeResponseHooks,
183
+ plugins,
184
+ fetchAPI,
181
185
  };
182
186
  }
183
187
  exports.createRouterBase = createRouterBase;
184
- function createRouter({ openAPI: { endpoint: oasEndpoint = '/openapi.json', ...openAPIDocument } = {}, swaggerUI: { endpoint: swaggerUIEndpoint = '/docs', ...swaggerUIOpts } = {}, plugins: userPlugins = [], base = '/', ...options } = {}) {
188
+ function createRouter({ openAPI: { endpoint: oasEndpoint = '/openapi.json', ...openAPIDocument } = {}, swaggerUI: { endpoint: swaggerUIEndpoint = '/docs', ...swaggerUIOpts } = {}, plugins: userPlugins = [], base = '/', app, ...options } = {}) {
185
189
  openAPIDocument.openapi = openAPIDocument.openapi || '3.0.1';
186
190
  const oasInfo = (openAPIDocument.info ||= {});
187
191
  oasInfo.title ||= 'feTS API';
@@ -209,6 +213,7 @@ function createRouter({ openAPI: { endpoint: oasEndpoint = '/openapi.json', ...o
209
213
  }),
210
214
  ]
211
215
  : []),
216
+ ...(app ? [(0, uws_js_1.useUWS)(app)] : []),
212
217
  (0, zod_js_1.useZod)(),
213
218
  ...userPlugins,
214
219
  ];
package/cjs/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.useAjv = exports.HTTPError = exports.useErrorHandling = exports.useCORS = exports.URLPattern = void 0;
3
+ exports.useUWS = exports.useAjv = exports.HTTPError = exports.useErrorHandling = exports.useCORS = exports.URLPattern = void 0;
4
4
  const tslib_1 = require("tslib");
5
5
  tslib_1.__exportStar(require("./types.js"), exports);
6
6
  tslib_1.__exportStar(require("./createRouter.js"), exports);
@@ -14,3 +14,5 @@ tslib_1.__exportStar(require("./client/index.js"), exports);
14
14
  tslib_1.__exportStar(require("./Response.js"), exports);
15
15
  var ajv_js_1 = require("./plugins/ajv.js");
16
16
  Object.defineProperty(exports, "useAjv", { enumerable: true, get: function () { return ajv_js_1.useAjv; } });
17
+ var uws_js_1 = require("./plugins/uws.js");
18
+ Object.defineProperty(exports, "useUWS", { enumerable: true, get: function () { return uws_js_1.useUWS; } });
@@ -0,0 +1,124 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useUWS = void 0;
4
+ const server_1 = require("@whatwg-node/server");
5
+ const Response_js_1 = require("../Response.js");
6
+ async function handleOnResponse({ request, response, res, serverContext, onResponseHooks, }) {
7
+ for (const onResponseHook of onResponseHooks) {
8
+ await onResponseHook({
9
+ request: request,
10
+ response,
11
+ serverContext,
12
+ });
13
+ }
14
+ await (0, server_1.sendResponseToUwsOpts)({
15
+ response,
16
+ res,
17
+ });
18
+ }
19
+ function useUWS(app) {
20
+ const onRequestHooks = [];
21
+ const onResponseHooks = [];
22
+ const requestMap = new WeakMap();
23
+ const serverContextMap = new WeakMap();
24
+ let router;
25
+ return {
26
+ onRouterInit(_router) {
27
+ router = _router;
28
+ for (const plugin of router.plugins) {
29
+ if (plugin.onRequest) {
30
+ onRequestHooks.push(plugin.onRequest);
31
+ }
32
+ if (plugin.onResponse) {
33
+ onResponseHooks.push(plugin.onResponse);
34
+ }
35
+ }
36
+ app.any('/*', async function (res, req) {
37
+ // Create a Request and attach it to the uWS request
38
+ const request = (0, server_1.getRequestFromUWSRequest)({
39
+ req,
40
+ res,
41
+ fetchAPI: router.fetchAPI,
42
+ });
43
+ requestMap.set(req, request);
44
+ const serverContext = {
45
+ req,
46
+ res,
47
+ };
48
+ serverContextMap.set(req, serverContext);
49
+ const { response } = await (0, server_1.handleOnRequestHook)({
50
+ fetchAPI: router.fetchAPI,
51
+ request: request,
52
+ givenHandleRequest: (() => { }),
53
+ onRequestHooks,
54
+ serverContext,
55
+ });
56
+ if (!response) {
57
+ req.setYield(true);
58
+ }
59
+ else {
60
+ await handleOnResponse({
61
+ request,
62
+ response,
63
+ res,
64
+ serverContext,
65
+ onResponseHooks,
66
+ });
67
+ }
68
+ });
69
+ },
70
+ onRoute({ method, path, handlers }) {
71
+ let appMethod = method.toLowerCase();
72
+ let normalizedPath = path;
73
+ if (!path.startsWith('/')) {
74
+ normalizedPath = '/' + path;
75
+ }
76
+ for (const handler of handlers) {
77
+ if (!(appMethod in app)) {
78
+ appMethod = 'any';
79
+ }
80
+ app[appMethod](normalizedPath, async function (res, req) {
81
+ if (appMethod === 'any') {
82
+ if (req.getMethod().toLowerCase() !== method.toLowerCase()) {
83
+ req.setYield(true);
84
+ return;
85
+ }
86
+ }
87
+ const request = requestMap.get(req);
88
+ const serverContext = requestMap.get(req);
89
+ let response = (await handler(request, serverContext));
90
+ if ((0, Response_js_1.isLazySerializedResponse)(response)) {
91
+ for (const onSerializeResponseHook of router.__onSerializeResponseHooks) {
92
+ onSerializeResponseHook({
93
+ request,
94
+ lazyResponse: response,
95
+ serverContext,
96
+ });
97
+ }
98
+ if (!response.serializerSet) {
99
+ response = router.fetchAPI.Response.json(response.jsonObj, response.init);
100
+ }
101
+ else {
102
+ response = await response.responsePromise;
103
+ }
104
+ }
105
+ if (!response) {
106
+ req.setYield(true);
107
+ }
108
+ else {
109
+ if (response) {
110
+ await handleOnResponse({
111
+ request,
112
+ response,
113
+ res,
114
+ serverContext,
115
+ onResponseHooks,
116
+ });
117
+ }
118
+ }
119
+ });
120
+ }
121
+ },
122
+ };
123
+ }
124
+ exports.useUWS = useUWS;
@@ -1,6 +1,7 @@
1
1
  import * as DefaultFetchAPI from '@whatwg-node/fetch';
2
2
  import { createServerAdapter } from '@whatwg-node/server';
3
3
  import { useOpenAPI } from './plugins/openapi.js';
4
+ import { useUWS } from './plugins/uws.js';
4
5
  import { isLazySerializedResponse } from './Response.js';
5
6
  import { useZod } from './zod/zod.js';
6
7
  const HTTP_METHODS = [
@@ -22,7 +23,7 @@ export function createRouterBase({ fetchAPI: givenFetchAPI, base: basePath = '/'
22
23
  };
23
24
  const __onRouterInitHooks = [];
24
25
  const onRouteHooks = [];
25
- const onSerializeResponseHooks = [];
26
+ const __onSerializeResponseHooks = [];
26
27
  for (const plugin of plugins) {
27
28
  if (plugin.onRouterInit) {
28
29
  __onRouterInitHooks.push(plugin.onRouterInit);
@@ -31,7 +32,7 @@ export function createRouterBase({ fetchAPI: givenFetchAPI, base: basePath = '/'
31
32
  onRouteHooks.push(plugin.onRoute);
32
33
  }
33
34
  if (plugin.onSerializeResponse) {
34
- onSerializeResponseHooks.push(plugin.onSerializeResponse);
35
+ __onSerializeResponseHooks.push(plugin.onSerializeResponse);
35
36
  }
36
37
  }
37
38
  const routesByMethod = new Map();
@@ -122,7 +123,7 @@ export function createRouterBase({ fetchAPI: givenFetchAPI, base: basePath = '/'
122
123
  for (const handler of handlers) {
123
124
  const handlerResult = await handler(routerRequest, context);
124
125
  if (isLazySerializedResponse(handlerResult)) {
125
- for (const onSerializeResponseHook of onSerializeResponseHooks) {
126
+ for (const onSerializeResponseHook of __onSerializeResponseHooks) {
126
127
  onSerializeResponseHook({
127
128
  request: routerRequest,
128
129
  lazyResponse: handlerResult,
@@ -174,9 +175,12 @@ export function createRouterBase({ fetchAPI: givenFetchAPI, base: basePath = '/'
174
175
  },
175
176
  __client: {},
176
177
  __onRouterInitHooks,
178
+ __onSerializeResponseHooks,
179
+ plugins,
180
+ fetchAPI,
177
181
  };
178
182
  }
179
- export function createRouter({ openAPI: { endpoint: oasEndpoint = '/openapi.json', ...openAPIDocument } = {}, swaggerUI: { endpoint: swaggerUIEndpoint = '/docs', ...swaggerUIOpts } = {}, plugins: userPlugins = [], base = '/', ...options } = {}) {
183
+ export function createRouter({ openAPI: { endpoint: oasEndpoint = '/openapi.json', ...openAPIDocument } = {}, swaggerUI: { endpoint: swaggerUIEndpoint = '/docs', ...swaggerUIOpts } = {}, plugins: userPlugins = [], base = '/', app, ...options } = {}) {
180
184
  openAPIDocument.openapi = openAPIDocument.openapi || '3.0.1';
181
185
  const oasInfo = (openAPIDocument.info ||= {});
182
186
  oasInfo.title ||= 'feTS API';
@@ -204,6 +208,7 @@ export function createRouter({ openAPI: { endpoint: oasEndpoint = '/openapi.json
204
208
  }),
205
209
  ]
206
210
  : []),
211
+ ...(app ? [useUWS(app)] : []),
207
212
  useZod(),
208
213
  ...userPlugins,
209
214
  ];
package/esm/index.js CHANGED
@@ -5,3 +5,4 @@ export { useCORS, useErrorHandling, HTTPError } from '@whatwg-node/server';
5
5
  export * from './client/index.js';
6
6
  export * from './Response.js';
7
7
  export { useAjv } from './plugins/ajv.js';
8
+ export { useUWS } from './plugins/uws.js';
@@ -0,0 +1,120 @@
1
+ import { getRequestFromUWSRequest, handleOnRequestHook, sendResponseToUwsOpts, } from '@whatwg-node/server';
2
+ import { isLazySerializedResponse } from '../Response.js';
3
+ async function handleOnResponse({ request, response, res, serverContext, onResponseHooks, }) {
4
+ for (const onResponseHook of onResponseHooks) {
5
+ await onResponseHook({
6
+ request: request,
7
+ response,
8
+ serverContext,
9
+ });
10
+ }
11
+ await sendResponseToUwsOpts({
12
+ response,
13
+ res,
14
+ });
15
+ }
16
+ export function useUWS(app) {
17
+ const onRequestHooks = [];
18
+ const onResponseHooks = [];
19
+ const requestMap = new WeakMap();
20
+ const serverContextMap = new WeakMap();
21
+ let router;
22
+ return {
23
+ onRouterInit(_router) {
24
+ router = _router;
25
+ for (const plugin of router.plugins) {
26
+ if (plugin.onRequest) {
27
+ onRequestHooks.push(plugin.onRequest);
28
+ }
29
+ if (plugin.onResponse) {
30
+ onResponseHooks.push(plugin.onResponse);
31
+ }
32
+ }
33
+ app.any('/*', async function (res, req) {
34
+ // Create a Request and attach it to the uWS request
35
+ const request = getRequestFromUWSRequest({
36
+ req,
37
+ res,
38
+ fetchAPI: router.fetchAPI,
39
+ });
40
+ requestMap.set(req, request);
41
+ const serverContext = {
42
+ req,
43
+ res,
44
+ };
45
+ serverContextMap.set(req, serverContext);
46
+ const { response } = await handleOnRequestHook({
47
+ fetchAPI: router.fetchAPI,
48
+ request: request,
49
+ givenHandleRequest: (() => { }),
50
+ onRequestHooks,
51
+ serverContext,
52
+ });
53
+ if (!response) {
54
+ req.setYield(true);
55
+ }
56
+ else {
57
+ await handleOnResponse({
58
+ request,
59
+ response,
60
+ res,
61
+ serverContext,
62
+ onResponseHooks,
63
+ });
64
+ }
65
+ });
66
+ },
67
+ onRoute({ method, path, handlers }) {
68
+ let appMethod = method.toLowerCase();
69
+ let normalizedPath = path;
70
+ if (!path.startsWith('/')) {
71
+ normalizedPath = '/' + path;
72
+ }
73
+ for (const handler of handlers) {
74
+ if (!(appMethod in app)) {
75
+ appMethod = 'any';
76
+ }
77
+ app[appMethod](normalizedPath, async function (res, req) {
78
+ if (appMethod === 'any') {
79
+ if (req.getMethod().toLowerCase() !== method.toLowerCase()) {
80
+ req.setYield(true);
81
+ return;
82
+ }
83
+ }
84
+ const request = requestMap.get(req);
85
+ const serverContext = requestMap.get(req);
86
+ let response = (await handler(request, serverContext));
87
+ if (isLazySerializedResponse(response)) {
88
+ for (const onSerializeResponseHook of router.__onSerializeResponseHooks) {
89
+ onSerializeResponseHook({
90
+ request,
91
+ lazyResponse: response,
92
+ serverContext,
93
+ });
94
+ }
95
+ if (!response.serializerSet) {
96
+ response = router.fetchAPI.Response.json(response.jsonObj, response.init);
97
+ }
98
+ else {
99
+ response = await response.responsePromise;
100
+ }
101
+ }
102
+ if (!response) {
103
+ req.setYield(true);
104
+ }
105
+ else {
106
+ if (response) {
107
+ await handleOnResponse({
108
+ request,
109
+ response,
110
+ res,
111
+ serverContext,
112
+ onResponseHooks,
113
+ });
114
+ }
115
+ }
116
+ });
117
+ }
118
+ },
119
+ };
120
+ }
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "fets",
3
- "version": "0.2.1",
3
+ "version": "0.2.2-alpha-20230605165125-e9d94d2",
4
4
  "description": "TypeScript HTTP Framework focusing on e2e type-safety, easy setup, performance & great developer experience",
5
5
  "sideEffects": false,
6
6
  "dependencies": {
7
7
  "@ardatan/fast-json-stringify": "^0.0.6",
8
8
  "@whatwg-node/cookie-store": "^0.1.0",
9
- "@whatwg-node/fetch": "^0.9.0",
10
- "@whatwg-node/server": "^0.8.1",
9
+ "@whatwg-node/fetch": "^0.9.3",
10
+ "@whatwg-node/server": "^0.8.4",
11
11
  "ajv": "^8.12.0",
12
12
  "ajv-formats": "^2.1.1",
13
13
  "hotscript": "^1.0.11",
@@ -4,4 +4,4 @@ import type { Router, RouterBaseObject, RouterComponentsBase, RouterOptions, Rou
4
4
  export declare function createRouterBase({ fetchAPI: givenFetchAPI, base: basePath, plugins }: RouterOptions<any, any> | undefined, openAPIDocument: OpenAPIV3_1.Document): RouterBaseObject<any, any, any>;
5
5
  export declare function createRouter<TServerContext, TComponents extends RouterComponentsBase = {}, TRouterSDK extends RouterSDK<string, TypedRequest, TypedResponse> = {
6
6
  [TKey: string]: never;
7
- }>({ openAPI: { endpoint: oasEndpoint, ...openAPIDocument }, swaggerUI: { endpoint: swaggerUIEndpoint, ...swaggerUIOpts }, plugins: userPlugins, base, ...options }?: RouterOptions<TServerContext, TComponents>): Router<TServerContext, TComponents, TRouterSDK>;
7
+ }>({ openAPI: { endpoint: oasEndpoint, ...openAPIDocument }, swaggerUI: { endpoint: swaggerUIEndpoint, ...swaggerUIOpts }, plugins: userPlugins, base, app, ...options }?: RouterOptions<TServerContext, TComponents>): Router<TServerContext, TComponents, TRouterSDK>;
@@ -4,4 +4,4 @@ import type { Router, RouterBaseObject, RouterComponentsBase, RouterOptions, Rou
4
4
  export declare function createRouterBase({ fetchAPI: givenFetchAPI, base: basePath, plugins }: RouterOptions<any, any> | undefined, openAPIDocument: OpenAPIV3_1.Document): RouterBaseObject<any, any, any>;
5
5
  export declare function createRouter<TServerContext, TComponents extends RouterComponentsBase = {}, TRouterSDK extends RouterSDK<string, TypedRequest, TypedResponse> = {
6
6
  [TKey: string]: never;
7
- }>({ openAPI: { endpoint: oasEndpoint, ...openAPIDocument }, swaggerUI: { endpoint: swaggerUIEndpoint, ...swaggerUIOpts }, plugins: userPlugins, base, ...options }?: RouterOptions<TServerContext, TComponents>): Router<TServerContext, TComponents, TRouterSDK>;
7
+ }>({ openAPI: { endpoint: oasEndpoint, ...openAPIDocument }, swaggerUI: { endpoint: swaggerUIEndpoint, ...swaggerUIOpts }, plugins: userPlugins, base, app, ...options }?: RouterOptions<TServerContext, TComponents>): Router<TServerContext, TComponents, TRouterSDK>;
@@ -6,3 +6,4 @@ export { FromSchema } from 'json-schema-to-ts';
6
6
  export * from './client/index.cjs';
7
7
  export * from './Response.cjs';
8
8
  export { useAjv } from './plugins/ajv.cjs';
9
+ export { useUWS } from './plugins/uws.cjs';
@@ -6,3 +6,4 @@ export { FromSchema } from 'json-schema-to-ts';
6
6
  export * from './client/index.js';
7
7
  export * from './Response.js';
8
8
  export { useAjv } from './plugins/ajv.js';
9
+ export { useUWS } from './plugins/uws.js';
@@ -0,0 +1,2 @@
1
+ import { RouterPlugin, UWSApp } from '../types.cjs';
2
+ export declare function useUWS(app: UWSApp): RouterPlugin<any>;
@@ -0,0 +1,2 @@
1
+ import { RouterPlugin, UWSApp } from '../types.js';
2
+ export declare function useUWS(app: UWSApp): RouterPlugin<any>;
@@ -1,6 +1,6 @@
1
1
  import { FromSchema as FromSchemaOriginal, JSONSchema as JSONSchemaOrBoolean } from 'json-schema-to-ts';
2
2
  import { OpenAPIV3_1 } from 'openapi-types';
3
- import { ServerAdapter, ServerAdapterOptions, ServerAdapterPlugin, ServerAdapterRequestHandler } from '@whatwg-node/server';
3
+ import { FetchAPI, ServerAdapter, ServerAdapterOptions, ServerAdapterPlugin, ServerAdapterRequestHandler, UWSRequest, UWSResponse } from '@whatwg-node/server';
4
4
  import { SwaggerUIOpts } from './plugins/openapi.cjs';
5
5
  import { LazySerializedResponse } from './Response.cjs';
6
6
  import type { HTTPMethod, StatusCode, TypedRequest, TypedResponse, TypedResponseWithJSONStatusMap } from './typed-fetch.cjs';
@@ -15,11 +15,14 @@ export interface RouterOpenAPIOptions<TComponents extends RouterComponentsBase>
15
15
  export interface RouterSwaggerUIOptions extends SwaggerUIOpts {
16
16
  endpoint?: string | false;
17
17
  }
18
+ type UWSRequestHandler = (res: UWSResponse, req: UWSRequest) => void | Promise<void>;
19
+ export type UWSApp = Record<'get' | 'post' | 'put' | 'patch' | 'any', (path: string, handler: UWSRequestHandler) => UWSApp>;
18
20
  export interface RouterOptions<TServerContext, TComponents extends RouterComponentsBase> extends ServerAdapterOptions<TServerContext> {
19
21
  base?: string;
20
22
  plugins?: RouterPlugin<TServerContext>[];
21
23
  openAPI?: RouterOpenAPIOptions<TComponents>;
22
24
  swaggerUI?: RouterSwaggerUIOptions;
25
+ app?: UWSApp;
23
26
  }
24
27
  export type RouterComponentsBase = {
25
28
  schemas?: Record<string, JSONSchema>;
@@ -73,11 +76,14 @@ export type TypedResponseFromTypeConfig<TTypeConfig extends TypedRouterHandlerTy
73
76
  export interface RouterBaseObject<TServerContext, TComponents extends RouterComponentsBase, TRouterSDK extends RouterSDK<string, TypedRequest, TypedResponse>> {
74
77
  openAPIDocument: OpenAPIV3_1.Document;
75
78
  handle: ServerAdapterRequestHandler<TServerContext>;
79
+ plugins: RouterPlugin<TServerContext>[];
80
+ fetchAPI: FetchAPI;
76
81
  route<TRouteSchemas extends RouteSchemas, TMethod extends HTTPMethod, TPath extends string, TTypedRequest extends TypedRequestFromRouteSchemas<TComponents, TRouteSchemas, TMethod>, TTypedResponse extends TypedResponseFromRouteSchemas<TComponents, TRouteSchemas>>(opts: AddRouteWithSchemasOpts<TServerContext, TComponents, TRouteSchemas, TMethod, TPath, TTypedRequest, TTypedResponse>): Router<TServerContext, TComponents, TRouterSDK & RouterSDK<TPath, TTypedRequest, TTypedResponse>>;
77
82
  route<TRouteZodSchemas extends RouteZodSchemas, TMethod extends HTTPMethod, TPath extends string, TTypedRequest extends TypedRequestFromRouteZodSchemas<TRouteZodSchemas, TMethod>, TTypedResponse extends TypedResponseFromRouteZodSchemas<TRouteZodSchemas>>(opts: AddRouteWithZodSchemasOpts<TServerContext, TRouteZodSchemas, TMethod, TPath, TTypedRequest, TTypedResponse>): Router<TServerContext, TComponents, TRouterSDK & RouterSDK<TPath, TTypedRequest, TTypedResponse>>;
78
83
  route<TTypeConfig extends TypedRouterHandlerTypeConfig, TMethod extends HTTPMethod = HTTPMethod, TTypedRequest extends TypedRequestFromTypeConfig<TMethod, TTypeConfig> = TypedRequestFromTypeConfig<TMethod, TTypeConfig>, TTypedResponse extends TypedResponseFromTypeConfig<TTypeConfig> = TypedResponseFromTypeConfig<TTypeConfig>, TPath extends string = string>(opts: AddRouteWithTypesOpts<TServerContext, TMethod, TPath, TTypedRequest, TTypedResponse>): Router<TServerContext, TComponents, TRouterSDK & RouterSDK<TPath, TTypedRequest, TTypedResponse>>;
79
84
  __client: TRouterSDK;
80
85
  __onRouterInitHooks: OnRouterInitHook<TServerContext>[];
86
+ __onSerializeResponseHooks: OnSerializeResponseHook<TServerContext>[];
81
87
  }
82
88
  export type Router<TServerContext, TComponents extends RouterComponentsBase, TRouterSDK extends RouterSDK<string, TypedRequest, TypedResponse>> = ServerAdapter<TServerContext, RouterBaseObject<TServerContext, TComponents, TRouterSDK>>;
83
89
  export type OnRouteHook<TServerContext> = (payload: OnRouteHookPayload<TServerContext>) => void;
@@ -1,6 +1,6 @@
1
1
  import { FromSchema as FromSchemaOriginal, JSONSchema as JSONSchemaOrBoolean } from 'json-schema-to-ts';
2
2
  import { OpenAPIV3_1 } from 'openapi-types';
3
- import { ServerAdapter, ServerAdapterOptions, ServerAdapterPlugin, ServerAdapterRequestHandler } from '@whatwg-node/server';
3
+ import { FetchAPI, ServerAdapter, ServerAdapterOptions, ServerAdapterPlugin, ServerAdapterRequestHandler, UWSRequest, UWSResponse } from '@whatwg-node/server';
4
4
  import { SwaggerUIOpts } from './plugins/openapi.js';
5
5
  import { LazySerializedResponse } from './Response.js';
6
6
  import type { HTTPMethod, StatusCode, TypedRequest, TypedResponse, TypedResponseWithJSONStatusMap } from './typed-fetch.js';
@@ -15,11 +15,14 @@ export interface RouterOpenAPIOptions<TComponents extends RouterComponentsBase>
15
15
  export interface RouterSwaggerUIOptions extends SwaggerUIOpts {
16
16
  endpoint?: string | false;
17
17
  }
18
+ type UWSRequestHandler = (res: UWSResponse, req: UWSRequest) => void | Promise<void>;
19
+ export type UWSApp = Record<'get' | 'post' | 'put' | 'patch' | 'any', (path: string, handler: UWSRequestHandler) => UWSApp>;
18
20
  export interface RouterOptions<TServerContext, TComponents extends RouterComponentsBase> extends ServerAdapterOptions<TServerContext> {
19
21
  base?: string;
20
22
  plugins?: RouterPlugin<TServerContext>[];
21
23
  openAPI?: RouterOpenAPIOptions<TComponents>;
22
24
  swaggerUI?: RouterSwaggerUIOptions;
25
+ app?: UWSApp;
23
26
  }
24
27
  export type RouterComponentsBase = {
25
28
  schemas?: Record<string, JSONSchema>;
@@ -73,11 +76,14 @@ export type TypedResponseFromTypeConfig<TTypeConfig extends TypedRouterHandlerTy
73
76
  export interface RouterBaseObject<TServerContext, TComponents extends RouterComponentsBase, TRouterSDK extends RouterSDK<string, TypedRequest, TypedResponse>> {
74
77
  openAPIDocument: OpenAPIV3_1.Document;
75
78
  handle: ServerAdapterRequestHandler<TServerContext>;
79
+ plugins: RouterPlugin<TServerContext>[];
80
+ fetchAPI: FetchAPI;
76
81
  route<TRouteSchemas extends RouteSchemas, TMethod extends HTTPMethod, TPath extends string, TTypedRequest extends TypedRequestFromRouteSchemas<TComponents, TRouteSchemas, TMethod>, TTypedResponse extends TypedResponseFromRouteSchemas<TComponents, TRouteSchemas>>(opts: AddRouteWithSchemasOpts<TServerContext, TComponents, TRouteSchemas, TMethod, TPath, TTypedRequest, TTypedResponse>): Router<TServerContext, TComponents, TRouterSDK & RouterSDK<TPath, TTypedRequest, TTypedResponse>>;
77
82
  route<TRouteZodSchemas extends RouteZodSchemas, TMethod extends HTTPMethod, TPath extends string, TTypedRequest extends TypedRequestFromRouteZodSchemas<TRouteZodSchemas, TMethod>, TTypedResponse extends TypedResponseFromRouteZodSchemas<TRouteZodSchemas>>(opts: AddRouteWithZodSchemasOpts<TServerContext, TRouteZodSchemas, TMethod, TPath, TTypedRequest, TTypedResponse>): Router<TServerContext, TComponents, TRouterSDK & RouterSDK<TPath, TTypedRequest, TTypedResponse>>;
78
83
  route<TTypeConfig extends TypedRouterHandlerTypeConfig, TMethod extends HTTPMethod = HTTPMethod, TTypedRequest extends TypedRequestFromTypeConfig<TMethod, TTypeConfig> = TypedRequestFromTypeConfig<TMethod, TTypeConfig>, TTypedResponse extends TypedResponseFromTypeConfig<TTypeConfig> = TypedResponseFromTypeConfig<TTypeConfig>, TPath extends string = string>(opts: AddRouteWithTypesOpts<TServerContext, TMethod, TPath, TTypedRequest, TTypedResponse>): Router<TServerContext, TComponents, TRouterSDK & RouterSDK<TPath, TTypedRequest, TTypedResponse>>;
79
84
  __client: TRouterSDK;
80
85
  __onRouterInitHooks: OnRouterInitHook<TServerContext>[];
86
+ __onSerializeResponseHooks: OnSerializeResponseHook<TServerContext>[];
81
87
  }
82
88
  export type Router<TServerContext, TComponents extends RouterComponentsBase, TRouterSDK extends RouterSDK<string, TypedRequest, TypedResponse>> = ServerAdapter<TServerContext, RouterBaseObject<TServerContext, TComponents, TRouterSDK>>;
83
89
  export type OnRouteHook<TServerContext> = (payload: OnRouteHookPayload<TServerContext>) => void;