11import pluralize from 'pluralize'
22import {
3- RestContext ,
4- RestRequest ,
53 ResponseResolver ,
6- rest ,
4+ http ,
75 DefaultBodyType ,
86 PathParams ,
7+ HttpResponse ,
98} from 'msw'
109import {
1110 Entity ,
@@ -65,25 +64,18 @@ export function getResponseStatusByErrorType(
6564export function withErrors <
6665 RequestBodyType extends DefaultBodyType = any ,
6766 RequestParamsType extends PathParams = any ,
68- > (
69- handler : ResponseResolver <
70- RestRequest < RequestBodyType , RequestParamsType > ,
71- RestContext
72- > ,
73- ) : ResponseResolver <
74- RestRequest < RequestBodyType , RequestParamsType > ,
75- RestContext
76- > {
77- return ( req , res , ctx ) => {
67+ > ( resolver : ResponseResolver < any , RequestBodyType , DefaultBodyType > ) {
68+ return async ( ...args : Parameters < ResponseResolver > ) : Promise < any > => {
7869 try {
79- return handler ( req , res , ctx )
70+ const response = await resolver ( ...args )
71+ return response
8072 } catch ( error ) {
8173 if ( error instanceof Error ) {
82- return res (
83- ctx . status ( getResponseStatusByErrorType ( error as HTTPError ) ) ,
84- ctx . json ( {
85- message : error . message ,
86- } ) ,
74+ return HttpResponse . json (
75+ { message : error . message } ,
76+ {
77+ status : getResponseStatusByErrorType ( error as HTTPError ) ,
78+ } ,
8779 )
8880 }
8981 }
@@ -153,13 +145,14 @@ export function generateRestHandlers<
153145 }
154146
155147 return [
156- rest . get (
148+ http . get (
157149 buildUrl ( modelPath ) ,
158- withErrors < Entity < Dictionary , ModelName > > ( ( req , res , ctx ) => {
150+ withErrors < Entity < Dictionary , ModelName > > ( ( { request } ) => {
151+ const url = new URL ( request . url )
159152 const { skip, take, cursor, filters } = parseQueryParams (
160153 modelName ,
161154 modelDefinition ,
162- req . url . searchParams ,
155+ url . searchParams ,
163156 )
164157
165158 let options = { where : filters }
@@ -172,14 +165,14 @@ export function generateRestHandlers<
172165
173166 const records = model . findMany ( options )
174167
175- return res ( ctx . json ( records ) )
168+ return HttpResponse . json ( records )
176169 } ) ,
177170 ) ,
178- rest . get (
171+ http . get (
179172 buildUrl ( `${ modelPath } /:${ primaryKey } ` ) ,
180173 withErrors < Entity < Dictionary , ModelName > , RequestParams < PrimaryKeyType > > (
181- ( req , res , ctx ) => {
182- const id = extractPrimaryKey ( req . params )
174+ ( { params } ) => {
175+ const id = extractPrimaryKey ( params )
183176 const where : WeakQuerySelectorWhere < PrimaryKeyType > = {
184177 [ primaryKey ] : {
185178 equals : id as string ,
@@ -190,22 +183,24 @@ export function generateRestHandlers<
190183 where : where as any ,
191184 } )
192185
193- return res ( ctx . json ( entity ) )
186+ return HttpResponse . json ( entity )
194187 } ,
195188 ) ,
196189 ) ,
197- rest . post (
190+ http . post (
198191 buildUrl ( modelPath ) ,
199- withErrors < Entity < Dictionary , ModelName > > ( ( req , res , ctx ) => {
200- const createdEntity = model . create ( req . body )
201- return res ( ctx . status ( 201 ) , ctx . json ( createdEntity ) )
192+ withErrors < Entity < Dictionary , ModelName > > ( async ( { request } ) => {
193+ const definition = await request . json ( )
194+ const createdEntity = model . create ( definition )
195+
196+ return HttpResponse . json ( createdEntity , { status : 201 } )
202197 } ) ,
203198 ) ,
204- rest . put (
199+ http . put (
205200 buildUrl ( `${ modelPath } /:${ primaryKey } ` ) ,
206201 withErrors < Entity < Dictionary , ModelName > , RequestParams < PrimaryKeyType > > (
207- ( req , res , ctx ) => {
208- const id = extractPrimaryKey ( req . params )
202+ async ( { request , params } ) => {
203+ const id = extractPrimaryKey ( params )
209204 const where : WeakQuerySelectorWhere < PrimaryKeyType > = {
210205 [ primaryKey ] : {
211206 equals : id as string ,
@@ -214,18 +209,18 @@ export function generateRestHandlers<
214209 const updatedEntity = model . update ( {
215210 strict : true ,
216211 where : where as any ,
217- data : req . body ,
212+ data : await request . json ( ) ,
218213 } ) !
219214
220- return res ( ctx . json ( updatedEntity ) )
215+ return HttpResponse . json ( updatedEntity )
221216 } ,
222217 ) ,
223218 ) ,
224- rest . delete (
219+ http . delete (
225220 buildUrl ( `${ modelPath } /:${ primaryKey } ` ) ,
226221 withErrors < Entity < Dictionary , ModelName > , RequestParams < PrimaryKeyType > > (
227- ( req , res , ctx ) => {
228- const id = extractPrimaryKey ( req . params )
222+ ( { params } ) => {
223+ const id = extractPrimaryKey ( params )
229224 const where : WeakQuerySelectorWhere < PrimaryKeyType > = {
230225 [ primaryKey ] : {
231226 equals : id as string ,
@@ -236,7 +231,7 @@ export function generateRestHandlers<
236231 where : where as any ,
237232 } ) !
238233
239- return res ( ctx . json ( deletedEntity ) )
234+ return HttpResponse . json ( deletedEntity )
240235 } ,
241236 ) ,
242237 ) ,
0 commit comments