@@ -7,7 +7,7 @@ import type { MswPlugin } from '../types';
77import { computeDominantResponse , type DominantResponse } from './computeDominantResponse' ;
88import { getOperationComment } from './operation' ;
99import { sanitizeParamName , sanitizePath } from './path' ;
10- import { createHandlerResponse } from './response' ;
10+ import { createHttpResponse } from './response' ;
1111
1212const emitToResponseUnion = ( plugin : MswPlugin [ 'Instance' ] ) => {
1313 const symbol = plugin . symbol ( 'ToResponseUnion' , {
@@ -42,21 +42,19 @@ const emitToResponseUnion = (plugin: MswPlugin['Instance']) => {
4242function createHandlerNode ( {
4343 baseUrl,
4444 bodyType,
45- method,
4645 operation,
4746 paramsType,
4847 plugin,
4948 response,
50- responseOrResolverType ,
49+ responseParamType ,
5150} : {
5251 baseUrl : string | undefined ;
5352 bodyType : ReturnType < typeof $ . type . idx | typeof $ . type > ;
54- method : string ;
5553 operation : IR . OperationObject ;
5654 paramsType : ReturnType < typeof $ . type | typeof $ . type . object > ;
5755 plugin : MswPlugin [ 'Instance' ] ;
5856 response : DominantResponse ;
59- responseOrResolverType : ReturnType < typeof $ . type | typeof $ . type . or > ;
57+ responseParamType : ReturnType < typeof $ . type | typeof $ . type . or > ;
6058} ) : Symbol {
6159 const symbolHttp = plugin . external ( 'msw.http' ) ;
6260 const symbolResponse = plugin . symbol ( 'response' ) ;
@@ -82,7 +80,7 @@ function createHandlerNode({
8280 const handlerFunc = $ . func ( symbol )
8381 . export ( )
8482 . $if ( plugin . config . comments && getOperationComment ( operation ) , ( f , v ) => f . doc ( v ) )
85- . param ( symbolResponse , ( p ) => p . optional ( ) . type ( responseOrResolverType ) )
83+ . param ( symbolResponse , ( p ) => p . optional ( ) . type ( responseParamType ) )
8684 . param ( symbolOptions , ( p ) =>
8785 p . optional ( ) . type (
8886 plugin . referenceSymbol ( {
@@ -95,7 +93,7 @@ function createHandlerNode({
9593 . returns ( plugin . external ( 'msw.HttpHandler' ) )
9694 . do (
9795 $ ( symbolHttp )
98- . attr ( method )
96+ . attr ( operation . method )
9997 . call (
10098 $ . template (
10199 $ ( symbolOptions )
@@ -116,7 +114,7 @@ function createHandlerNode({
116114 . $if ( hasResponse , ( c ) => c . coalesce ( $ . fromValue ( response . example ) ) ) ,
117115 ) ,
118116 $ . if ( $ ( 'body' ) . neq ( $ ( 'undefined' ) ) ) . do (
119- createHandlerResponse ( {
117+ createHttpResponse ( {
120118 plugin,
121119 response,
122120 symbol : symbolResponse ,
@@ -227,30 +225,43 @@ export function getHandler({
227225 response . example = undefined ;
228226 }
229227
230- let responseOrResolverType : ReturnType < typeof $ . type | typeof $ . type . or > ;
231- if ( response . statusCode != null && symbolResponsesType ) {
232- const responseType = $ . type
233- . object ( )
234- . prop ( 'body' , ( p ) =>
235- p . type ( $ . type ( symbolResponsesType ) . idx ( $ . type . literal ( response . statusCode ! ) ) ) ,
236- )
237- . prop ( 'status' , ( p ) => p . optional ( ) . type ( $ . type . literal ( response . statusCode ! ) ) ) ;
238- responseOrResolverType = $ . type . or (
239- responseType ,
240- responsesOverrideType ? $ . type . or ( responsesOverrideType , resolverType ) : resolverType ,
228+ const responseType = $ . type
229+ . object ( )
230+ . prop ( 'body' , ( p ) =>
231+ p . type (
232+ response . statusCode !== undefined && symbolResponsesType
233+ ? $ . type ( symbolResponsesType ) . idx ( $ . type . literal ( response . statusCode ) )
234+ : $ . type ( plugin . external ( 'msw.DefaultBodyType' ) ) ,
235+ ) ,
236+ )
237+ . prop ( 'status' , ( p ) =>
238+ p
239+ . optional ( )
240+ . type (
241+ response . statusCode !== undefined
242+ ? $ . type . literal ( response . statusCode )
243+ : $ . type ( 'number' ) ,
244+ ) ,
241245 ) ;
242- } else {
243- responseOrResolverType = resolverType ;
244- }
246+ const symbolResponseType = plugin . symbol (
247+ applyNaming ( `handle-${ operation . id } -response` , {
248+ casing : 'PascalCase' , // TODO: expose as a config option
249+ } ) ,
250+ ) ;
251+ plugin . node ( $ . type . alias ( symbolResponseType ) . export ( ) . type ( responseType ) ) ;
252+
253+ const responseParamType = $ . type . or (
254+ symbolResponseType ,
255+ responsesOverrideType ? $ . type . or ( responsesOverrideType , resolverType ) : resolverType ,
256+ ) ;
245257
246258 return createHandlerNode ( {
247259 baseUrl,
248260 bodyType,
249- method : operation . method ,
250261 operation,
251262 paramsType,
252263 plugin,
253264 response,
254- responseOrResolverType ,
265+ responseParamType ,
255266 } ) ;
256267}
0 commit comments