@@ -2,15 +2,16 @@ import Response from '../../openapi/Response.js';
22import type Responses from '../../openapi/Responses.js' ;
33import schemaToModel from '../model.js' ;
44import typeboxImports from '../model/typeboxImports.js' ;
5- import context from '../utility/context.js' ;
5+ import context , { ResponseEntry } from '../utility/context.js' ;
66import { GenerationError } from '../utility/errors.js' ;
77import {
88 ImportCollection ,
99 ImportMetadata ,
1010 ImportSource ,
1111 resolveImports ,
12+ toImportPath ,
1213} from '../utility/importSource.js' ;
13- import PathInfo from '../utility/PathInfo.js' ;
14+ import { FileInfo } from '../utility/PathInfo.js' ;
1415import template from '../utility/templater.js' ;
1516import writeSourceFile from '../utility/writeSourceFile.js' ;
1617
@@ -73,14 +74,13 @@ export const buildResponseType = (
7374
7475export type ResponseType = {
7576 responseCode : string ;
76- typeName ?: string ;
77- validatorName ?: string ;
7877 schema ?: string ;
79- imports ?: ImportCollection ;
78+ responseEntry ?: ResponseEntry ;
8079} ;
8180export type ResponseTypes = Array < ResponseType > ;
8281
8382const buildResponses = (
83+ outFile : FileInfo ,
8484 responses : Responses ,
8585) : {
8686 types : ResponseTypes ;
@@ -92,21 +92,19 @@ const buildResponses = (
9292 const imports = new ImportCollection ( ) ;
9393
9494 for ( const [ statusCode , response ] of Object . entries ( responses ) ) {
95+ if ( response == null ) {
96+ continue ;
97+ }
98+
9599 if ( '$ref' in response ) {
96100 const r = context . responses . lookup ( response . $ref ) ;
97101 if ( r === undefined ) {
98102 throw new GenerationError ( `Unresolved response reference ${ response . $ref } ` ) ;
99103 }
100104 types . push ( {
101105 responseCode : statusCode ,
102- typeName : r . typeName ,
103- validatorName : r . validatorName ,
104- imports : new ImportCollection ( r . importMeta . type , r . importMeta . validator ) ,
106+ responseEntry : r ,
105107 } ) ;
106- imports . append ( r . importMeta . type ) ;
107- if ( r . importMeta . validator ) {
108- imports . append ( r . importMeta . validator ) ;
109- }
110108 continue ;
111109 }
112110
@@ -120,27 +118,49 @@ const buildResponses = (
120118 code . push ( r . code ) ;
121119 types . push ( {
122120 responseCode : statusCode ,
123- typeName : r . typeName ,
124- validatorName : r . validatorName ,
125- imports : r . imports ,
121+ responseEntry : {
122+ typeName : r . typeName ,
123+ validatorName : r . validatorName ,
124+ importMeta : {
125+ type : {
126+ file : {
127+ path : toImportPath ( outFile ) ,
128+ internal : true ,
129+ } ,
130+ entries : [ { item : r . typeName , typeOnly : true } ] ,
131+ } ,
132+ ...( r . validatorName != null && {
133+ validator : {
134+ file : {
135+ path : toImportPath ( outFile ) ,
136+ internal : true ,
137+ } ,
138+ entries : [ { item : r . validatorName } ] ,
139+ } ,
140+ } ) ,
141+ } ,
142+ raw : response ,
143+ } ,
126144 } ) ;
145+ if ( r . imports ) {
146+ imports . append ( r . imports ) ;
147+ }
127148 } else {
128149 types . push ( {
129150 responseCode : statusCode ,
130- typeName : r . typeName ,
131- validatorName : r . validatorName ,
132- imports : new ImportCollection ( r . importMeta . type , r . importMeta . validator ) ,
151+ responseEntry : {
152+ ...r ,
153+ raw : response ,
154+ } ,
133155 } ) ;
134- }
135- if ( r . type === 'import' ) {
136156 imports . append ( r . importMeta . type ) ;
137157 }
138158 }
139159 return { types, code, imports } ;
140160} ;
141161
142162const buildResponseTypes = (
143- outFile : PathInfo ,
163+ outFile : FileInfo ,
144164 responseTypeName : string ,
145165 responsesRaw : Responses ,
146166 parameterType : { typename : string ; imports : ImportSource } | null ,
@@ -153,7 +173,7 @@ const buildResponseTypes = (
153173 if ( parameterType !== null ) {
154174 imports . append ( parameterType . imports ) ;
155175 }
156- const responses = buildResponses ( responsesRaw ) ;
176+ const responses = buildResponses ( outFile , responsesRaw ) ;
157177 lines . push ( ...responses . code ) ;
158178 imports . append ( responses . imports ) ;
159179
@@ -167,15 +187,15 @@ const buildResponseTypes = (
167187
168188 lines . push (
169189 template . lines (
170- ...responses . types . map ( ( { responseCode, typeName , validatorName } ) =>
190+ ...responses . types . map ( ( { responseCode, responseEntry } ) =>
171191 template . concat (
172192 '| {' ,
173193 ' response: Response;' ,
174194 ' request: Request;' ,
175195 // "default" is a special openapi case that is not a number
176196 ` status: ${ responseCode === 'default' ? "'default'" : responseCode } ;` ,
177- typeName && ` data: ${ typeName } ;` ,
178- validatorName && `validator: typeof ${ validatorName } ;` ,
197+ responseEntry ?. typeName && ` data: ${ responseEntry ?. typeName } ;` ,
198+ responseEntry ?. validatorName && `validator: typeof ${ responseEntry ?. validatorName } ;` ,
179199 ' }' ,
180200 ) ,
181201 ) ,
0 commit comments