1+ import { IncomingHttpHeaders } from 'http' ;
12import { ServerRequest } from '../modules/Request' ;
23import { ServerResponse } from '../modules/Response' ;
34
@@ -89,7 +90,7 @@ export type Method =
8990 | 'delete'
9091 | '*' ;
9192
92- export type Url = string ;
93+ export type Path = string ;
9394
9495export type RouteId = number ;
9596
@@ -99,7 +100,7 @@ export type Parameter = string | number | boolean;
99100
100101export interface Next {
101102 ( ) : true ;
102- reset : ( ) => boolean ;
103+ reset : ( ) => void ;
103104 status : ( ) => boolean ;
104105}
105106
@@ -109,8 +110,7 @@ export type Callback<
109110> = (
110111 request : Rq ,
111112 response : Rs ,
112- params : ObjectOfAny ,
113- options ?: ObjectOfAny
113+ options : { pathParams : PathParameters } & Record < string , any >
114114) => Promise < boolean > ;
115115
116116export type ErrorCallback <
@@ -130,44 +130,18 @@ export type Middleware<
130130 request : Rq ,
131131 response : Rs ,
132132 next : Next ,
133- params : ObjectOfAny ,
134- options ?: ObjectOfAny
133+ options : { pathParams : PathParameters } & Record < string , any >
135134) => Promise < boolean > | boolean ;
136135
137136export type ListenerCallback = ( ) => void ;
138137
139- export interface CallbackOptions {
140- use : Middleware | Middleware [ ] ;
141- options ?: ObjectOfAny ;
142- }
143-
144- export interface MiddlewareOptions {
145- method : Method | Method [ ] ;
146- options ?: ObjectOfAny ;
147- }
148-
149- export interface ResolvedCallbackOptions {
150- use : Middleware [ ] ;
151- options ?: ObjectOfAny ;
152- }
153-
154- export interface ResolvedMiddlewareOptions {
155- method : Method [ ] ;
156- options ?: ObjectOfAny ;
157- }
158-
159- export type RouteInstance = [
160- RouteId ,
161- Url ,
162- Callback ,
163- null | ResolvedCallbackOptions
164- ] ;
138+ export type RouteInstance = [ RouteId , Path , Callback , Middleware [ ] ] ;
165139
166140export type MiddlewareInstance = [
167141 MiddlewareId ,
168- Url ,
142+ Path ,
169143 Middleware [ ] ,
170- null | ResolvedMiddlewareOptions
144+ Set < Method >
171145] ;
172146
173147export interface FileEntry {
@@ -232,6 +206,11 @@ export interface Data {
232206 [ propName : string ] : string | string [ ] ;
233207}
234208
209+ export type PathParameters < T extends string = string > = Record <
210+ T ,
211+ string | number | boolean
212+ > ;
213+
235214export interface Headers {
236215 [ propName : string ] : string ;
237216}
@@ -256,11 +235,37 @@ export interface RouteParameter {
256235 value : string | number | boolean ;
257236}
258237
259- export interface Routes {
260- options : RouteInstance [ ] ;
261- head : RouteInstance [ ] ;
262- get : RouteInstance [ ] ;
263- post : RouteInstance [ ] ;
264- put : RouteInstance [ ] ;
265- delete : RouteInstance [ ] ;
238+ export type Routes = Record < Exclude < Method , '*' > , RouteInstance [ ] > ;
239+
240+ export interface RouteResponse < ResponseData = { } > {
241+ status ?: 'success' | 'error' ;
242+ statusCode ?: number ;
243+ message ?: string ;
244+ data ?: ResponseData ;
245+ headers ?: IncomingHttpHeaders ;
246+ ttl ?: number ;
247+ }
248+
249+ export interface APIExecutor < RequestBody , ResponseData > {
250+ ( arg : {
251+ /**
252+ * request body, should be a combination of parsed post body and url search params
253+ */
254+ body : RequestBody ;
255+
256+ /**
257+ * request http headers
258+ */
259+ headers : IncomingHttpHeaders ;
260+
261+ /**
262+ * request path parameters, as contained in the routing path
263+ */
264+ pathParams : PathParameters ;
265+ } ) : Promise < RouteResponse < ResponseData > | null > ;
266+
267+ /**
268+ * assigned name of the handler
269+ */
270+ apiName ?: string ;
266271}
0 commit comments