11import { OperationObject } from "../types" ;
2- import { comment } from "../utils" ;
2+ import { comment , tsReadonly } from "../utils" ;
33import { transformHeaderObjMap } from "./headers" ;
44import { transformOperationObj } from "./operation" ;
55import { transformPathsObj } from "./paths" ;
66import { transformResponsesObj , transformRequestBodies } from "./responses" ;
77import { transformSchemaObjMap } from "./schema" ;
88
99interface TransformOptions {
10+ immutableTypes : boolean ;
1011 rawSchema ?: boolean ;
1112 version : number ;
1213}
1314
14- export function transformAll ( schema : any , { version, rawSchema } : TransformOptions ) : string {
15+ export function transformAll ( schema : any , { immutableTypes, rawSchema, version } : TransformOptions ) : string {
16+ const readonly = tsReadonly ( immutableTypes ) ;
17+
1518 let output = "" ;
1619
1720 let operations : Record < string , OperationObject > = { } ;
@@ -21,11 +24,13 @@ export function transformAll(schema: any, { version, rawSchema }: TransformOptio
2124 switch ( version ) {
2225 case 2 : {
2326 return `export interface definitions {\n ${ transformSchemaObjMap ( schema , {
27+ immutableTypes,
2428 required : Object . keys ( schema ) ,
2529 } ) } \n}`;
2630 }
2731 case 3 : {
2832 return `export interface schemas {\n ${ transformSchemaObjMap ( schema , {
33+ immutableTypes,
2934 required : Object . keys ( schema ) ,
3035 } ) } \n }\n\n`;
3136 }
@@ -36,8 +41,9 @@ export function transformAll(schema: any, { version, rawSchema }: TransformOptio
3641 output += `export interface paths {\n` ; // open paths
3742 if ( schema . paths ) {
3843 output += transformPathsObj ( schema . paths , {
39- operations,
4044 globalParameters : ( schema . components && schema . components . parameters ) || schema . parameters ,
45+ immutableTypes,
46+ operations,
4147 version,
4248 } ) ;
4349 }
@@ -48,6 +54,7 @@ export function transformAll(schema: any, { version, rawSchema }: TransformOptio
4854 // #/definitions
4955 if ( schema . definitions ) {
5056 output += `export interface definitions {\n ${ transformSchemaObjMap ( schema . definitions , {
57+ immutableTypes,
5158 required : Object . keys ( schema . definitions ) ,
5259 } ) } \n}\n\n`;
5360 }
@@ -56,13 +63,16 @@ export function transformAll(schema: any, { version, rawSchema }: TransformOptio
5663 if ( schema . parameters ) {
5764 const required = Object . keys ( schema . parameters ) ;
5865 output += `export interface parameters {\n ${ transformSchemaObjMap ( schema . parameters , {
66+ immutableTypes,
5967 required,
6068 } ) } \n }\n\n`;
6169 }
6270
6371 // #/parameters
6472 if ( schema . responses ) {
65- output += `export interface responses {\n ${ transformResponsesObj ( schema . responses ) } \n }\n\n` ;
73+ output += `export interface responses {\n ${ transformResponsesObj ( schema . responses , {
74+ immutableTypes,
75+ } ) } \n }\n\n`;
6676 }
6777 break ;
6878 }
@@ -74,30 +84,40 @@ export function transformAll(schema: any, { version, rawSchema }: TransformOptio
7484 // #/components/schemas
7585 if ( schema . components . schemas ) {
7686 const required = Object . keys ( schema . components . schemas ) ;
77- output += ` schemas: {\n ${ transformSchemaObjMap ( schema . components . schemas , { required } ) } \n }\n` ;
87+ output += ` ${ readonly } schemas: {\n ${ transformSchemaObjMap ( schema . components . schemas , {
88+ immutableTypes,
89+ required,
90+ } ) } \n }\n`;
7891 }
7992
8093 // #/components/responses
8194 if ( schema . components . responses ) {
82- output += ` responses: {\n ${ transformResponsesObj ( schema . components . responses ) } \n }\n` ;
95+ output += ` ${ readonly } responses: {\n ${ transformResponsesObj ( schema . components . responses , {
96+ immutableTypes,
97+ } ) } \n }\n`;
8398 }
8499
85100 // #/components/parameters
86101 if ( schema . components . parameters ) {
87102 const required = Object . keys ( schema . components . parameters ) ;
88- output += ` parameters: {\n ${ transformSchemaObjMap ( schema . components . parameters , {
103+ output += ` ${ readonly } parameters: {\n ${ transformSchemaObjMap ( schema . components . parameters , {
104+ immutableTypes,
89105 required,
90106 } ) } \n }\n`;
91107 }
92108
93109 // #/components/requestBodies
94110 if ( schema . components . requestBodies ) {
95- output += ` requestBodies: {\n ${ transformRequestBodies ( schema . components . requestBodies ) } \n }\n` ;
111+ output += ` ${ readonly } requestBodies: {\n ${ transformRequestBodies ( schema . components . requestBodies , {
112+ immutableTypes,
113+ } ) } \n }\n`;
96114 }
97115
98116 // #/components/headers
99117 if ( schema . components . headers ) {
100- output += ` headers: {\n ${ transformHeaderObjMap ( schema . components . headers ) } }\n` ;
118+ output += ` ${ readonly } headers: {\n ${ transformHeaderObjMap ( schema . components . headers , {
119+ immutableTypes,
120+ } ) } }\n`;
101121 }
102122 }
103123
@@ -110,9 +130,10 @@ export function transformAll(schema: any, { version, rawSchema }: TransformOptio
110130 if ( Object . keys ( operations ) . length ) {
111131 Object . entries ( operations ) . forEach ( ( [ operationId , operation ] ) => {
112132 if ( operation . description ) output += comment ( operation . description ) ; // handle comment
113- output += ` "${ operationId } ": {\n ${ transformOperationObj ( operation , {
114- version,
133+ output += ` ${ readonly } "${ operationId } ": {\n ${ transformOperationObj ( operation , {
115134 globalParameters : ( schema . components && schema . components . parameters ) || schema . parameters ,
135+ immutableTypes,
136+ version,
116137 } ) } \n }\n`;
117138 } ) ;
118139 }
0 commit comments