@@ -4,14 +4,15 @@ import { zodToJsonSchema } from "zod-to-json-schema";
4
4
import openapi3 from "openapi3-ts/oas30" ;
5
5
6
6
import { getErrorResponseSchema , getPayloadFromParams , isZodTypeNull , processException } from "./helpers" ;
7
+ import { Context } from "./context" ;
7
8
8
9
export type RouteUpdater = ( r : Route < any , any > ) => void ;
9
10
10
11
export interface RouteConfig {
11
12
opId : string ;
12
13
opType : string ;
13
14
method : string ;
14
- url ? : string ;
15
+ url : string ;
15
16
jsonSchema : {
16
17
input : any ;
17
18
output : any ;
@@ -27,7 +28,7 @@ export interface ZodSchemas {
27
28
output ?: z . ZodType ;
28
29
}
29
30
30
- export type Query < I , O > = ( input : I ) => Promise < O > ;
31
+ export type Query < I , O > = ( ctx : Context , input : I ) => Promise < O > ;
31
32
32
33
export class Route < I , O > {
33
34
private config : RouteConfig ;
@@ -39,23 +40,36 @@ export class Route<I, O> {
39
40
constructor ( config : RouteConfig , updater : RouteUpdater ) {
40
41
this . config = config ;
41
42
this . updater = updater ;
42
-
43
- // Autogenerate a url if it wasn't already provided by the user
44
- if ( this . config . url === undefined ) {
45
- this . config . url = `/v1/${ this . config . opId } ` ;
46
- }
47
43
}
48
44
45
+ /**
46
+ * Method configures the HTTP method to use for this operation
47
+ * @param method
48
+ * @returns
49
+ */
49
50
public method ( method : string ) {
50
51
this . config . method = method ;
51
52
return this ;
52
53
}
53
54
55
+ /**
56
+ * Url configures the URL to use for this operation
57
+ * @param url
58
+ * @returns
59
+ */
54
60
public url ( url : string ) {
55
61
this . config . url = url ;
56
62
return this ;
57
63
}
58
64
65
+ /**
66
+ * Input configures the data type of the request object to pass in the handler function
67
+ * registered by the user. It also performs a JSONSchema check before calling the handler function.
68
+ * The proeprties specified by the input may be accepted via the body or as query parameters based
69
+ * on the method being used for the operation
70
+ * @param schema ZOD schema object
71
+ * @returns
72
+ */
59
73
public input < T > ( schema : z . ZodType < T > ) {
60
74
// Create json schema from zod schema
61
75
this . config . zodSchemas . input = schema ;
@@ -71,6 +85,13 @@ export class Route<I, O> {
71
85
return r ;
72
86
}
73
87
88
+ /**
89
+ * Output configures the data type of the response object to that the handler function registered
90
+ * by the user will return.
91
+ * The returned object will always be sent as a JSON payload in the response body.
92
+ * @param schema ZOD schema object
93
+ * @returns
94
+ */
74
95
public output < T > ( schema : z . ZodType < T > ) {
75
96
// Create json schema from zod schema
76
97
this . config . zodSchemas . output = schema ;
@@ -86,12 +107,17 @@ export class Route<I, O> {
86
107
return r ;
87
108
}
88
109
89
- public fn ( query : Query < I , O > ) {
90
- this . handler = query ;
110
+ /**
111
+ * fn accepts the procedure that needs to be called when this operation is invoked
112
+ * @param handlerFn
113
+ * @returns
114
+ */
115
+ public fn ( handlerFn : Query < I , O > ) {
116
+ this . handler = handlerFn ;
91
117
return this ;
92
118
}
93
119
94
- public getOpenAPIOperation ( ) {
120
+ public _getOpenAPIOperation ( ) {
95
121
// Process request schema
96
122
const isRequestNull = isZodTypeNull ( this . config . zodSchemas . input ) ;
97
123
const requestBodyObject : openapi3 . RequestBodyObject = {
@@ -131,12 +157,6 @@ export class Route<I, O> {
131
157
[ "x-request-op-type" ] : this . config . opType ,
132
158
} ;
133
159
134
- // Unnecessary check to remove linting error. We don't really require this
135
- // since we are already performing this step in the constructor.
136
- if ( ! this . config . url ) {
137
- this . config . url = `/v1/${ this . config . opId } ` ;
138
- }
139
-
140
160
return {
141
161
path : this . config . url ,
142
162
method : this . config . method ,
@@ -173,20 +193,15 @@ export class Route<I, O> {
173
193
174
194
// Simply return back the response
175
195
try {
176
- const output = await this . handler ( payload ) ;
196
+ const ctx = new Context ( req , res ) ;
197
+ const output = await this . handler ( ctx , payload ) ;
177
198
res . json ( output ) ;
178
199
} catch ( e : any ) {
179
200
// Return status code 500 if we catch an exception
180
201
res . status ( 500 ) . json ( { message : processException ( e ) } ) ;
181
202
}
182
203
} ;
183
204
184
- // Unnecessary check to remove linting error. We don't really require this
185
- // since we are already performing this step in the constructor.
186
- if ( ! this . config . url ) {
187
- this . config . url = `/v1/${ this . config . opId } ` ;
188
- }
189
-
190
205
// Register the handler for the appropriate method
191
206
switch ( this . config . method ) {
192
207
case "get" :
0 commit comments