|
| 1 | +import type { OpenAPILatest } from '../../src'; |
| 2 | +import { Printer } from '../../src/printer'; |
| 3 | + |
| 4 | +const doc1: OpenAPILatest.Document = { |
| 5 | + openapi: '3.1.0', |
| 6 | + info: { |
| 7 | + title: 'test', |
| 8 | + version: '1.0.0', |
| 9 | + }, |
| 10 | + paths: { |
| 11 | + '/pets/{pet-id}': { |
| 12 | + get: { |
| 13 | + operationId: 'getPet', |
| 14 | + parameters: [ |
| 15 | + { |
| 16 | + in: 'path', |
| 17 | + name: 'pet-id', |
| 18 | + schema: { |
| 19 | + type: 'string', |
| 20 | + }, |
| 21 | + required: true, |
| 22 | + }, |
| 23 | + { |
| 24 | + in: 'query', |
| 25 | + name: 'category-id', |
| 26 | + schema: { |
| 27 | + type: 'string', |
| 28 | + }, |
| 29 | + }, |
| 30 | + ], |
| 31 | + responses: { |
| 32 | + 200: { |
| 33 | + description: 'pet name', |
| 34 | + content: { |
| 35 | + '*': {}, |
| 36 | + }, |
| 37 | + }, |
| 38 | + }, |
| 39 | + }, |
| 40 | + }, |
| 41 | + }, |
| 42 | +}; |
| 43 | +const doc2: OpenAPILatest.Document = { |
| 44 | + openapi: '3.1.0', |
| 45 | + info: { |
| 46 | + title: 'test', |
| 47 | + version: '1.0.0', |
| 48 | + }, |
| 49 | + paths: { |
| 50 | + '/pets/{pet-id}': { |
| 51 | + get: { |
| 52 | + operationId: 'getPet', |
| 53 | + parameters: [ |
| 54 | + { |
| 55 | + in: 'path', |
| 56 | + name: 'pet-id', |
| 57 | + schema: { |
| 58 | + type: 'string', |
| 59 | + }, |
| 60 | + required: true, |
| 61 | + }, |
| 62 | + { |
| 63 | + in: 'query', |
| 64 | + name: 'category-id', |
| 65 | + schema: { |
| 66 | + type: 'string', |
| 67 | + }, |
| 68 | + }, |
| 69 | + ], |
| 70 | + responses: { |
| 71 | + 200: { |
| 72 | + description: 'pet name', |
| 73 | + content: { |
| 74 | + '*': { |
| 75 | + schema: { |
| 76 | + type: 'string', |
| 77 | + }, |
| 78 | + }, |
| 79 | + }, |
| 80 | + }, |
| 81 | + }, |
| 82 | + }, |
| 83 | + }, |
| 84 | + }, |
| 85 | +}; |
| 86 | + |
| 87 | +describe('runtimeValidate = true', () => { |
| 88 | + it('response unknown', () => { |
| 89 | + const printer = new Printer(doc1, { |
| 90 | + runtimeValidate: true, |
| 91 | + }); |
| 92 | + const result = printer.print({ |
| 93 | + hideImports: true, |
| 94 | + hideHeaders: true, |
| 95 | + hideFooters: true, |
| 96 | + hideInfo: true, |
| 97 | + hideAlert: true, |
| 98 | + }); |
| 99 | + |
| 100 | + expect(result.main.code).toMatchInlineSnapshot(` |
| 101 | + "/** |
| 102 | + * @param petId request path "pet-id" |
| 103 | + * @param [categoryId] request params "category-id" |
| 104 | + * @param [config] request config |
| 105 | + */ |
| 106 | + export async function getPet(petId:Type.GetPetPath,categoryId?:Type.GetPetParams,config?:AxiosRequestConfig) { |
| 107 | + zGetPetPath.parse(petId) |
| 108 | + (categoryId !== undefined) && zGetPetParams.parse(categoryId) |
| 109 | + const resp = await axios<unknown>({ |
| 110 | + method: "GET", |
| 111 | + url: \`/pets/\${petId}\`, |
| 112 | + params: {"category-id": categoryId}, |
| 113 | + ...config |
| 114 | + }); |
| 115 | + return resp; |
| 116 | + }" |
| 117 | + `); |
| 118 | + }); |
| 119 | + |
| 120 | + it('response type', () => { |
| 121 | + const printer = new Printer(doc2, { |
| 122 | + runtimeValidate: true, |
| 123 | + }); |
| 124 | + const result = printer.print({ |
| 125 | + hideImports: true, |
| 126 | + hideHeaders: true, |
| 127 | + hideFooters: true, |
| 128 | + hideInfo: true, |
| 129 | + hideAlert: true, |
| 130 | + }); |
| 131 | + |
| 132 | + expect(result.main.code).toMatchInlineSnapshot(` |
| 133 | + "/** |
| 134 | + * @param petId request path "pet-id" |
| 135 | + * @param [categoryId] request params "category-id" |
| 136 | + * @param [config] request config |
| 137 | + * @returns pet name |
| 138 | + */ |
| 139 | + export async function getPet(petId:Type.GetPetPath,categoryId?:Type.GetPetParams,config?:AxiosRequestConfig) { |
| 140 | + zGetPetPath.parse(petId) |
| 141 | + (categoryId !== undefined) && zGetPetParams.parse(categoryId) |
| 142 | + const resp = await axios<Type.GetPetResponse>({ |
| 143 | + method: "GET", |
| 144 | + url: \`/pets/\${petId}\`, |
| 145 | + params: {"category-id": categoryId}, |
| 146 | + ...config |
| 147 | + }); |
| 148 | + zGetPetResponse.parse(resp["data"]); |
| 149 | + return resp; |
| 150 | + }" |
| 151 | + `); |
| 152 | + }); |
| 153 | +}); |
| 154 | + |
| 155 | +describe('runtimeValidate = {responseDataProps: []}', () => { |
| 156 | + it('response unknown', () => { |
| 157 | + const printer = new Printer(doc1, { |
| 158 | + runtimeValidate: { |
| 159 | + responseDataProps: [], |
| 160 | + }, |
| 161 | + }); |
| 162 | + const result = printer.print({ |
| 163 | + hideImports: true, |
| 164 | + hideHeaders: true, |
| 165 | + hideFooters: true, |
| 166 | + hideInfo: true, |
| 167 | + hideAlert: true, |
| 168 | + }); |
| 169 | + |
| 170 | + expect(result.main.code).toMatchInlineSnapshot(` |
| 171 | + "/** |
| 172 | + * @param petId request path "pet-id" |
| 173 | + * @param [categoryId] request params "category-id" |
| 174 | + * @param [config] request config |
| 175 | + */ |
| 176 | + export async function getPet(petId:Type.GetPetPath,categoryId?:Type.GetPetParams,config?:AxiosRequestConfig) { |
| 177 | + zGetPetPath.parse(petId) |
| 178 | + (categoryId !== undefined) && zGetPetParams.parse(categoryId) |
| 179 | + const resp = await axios<unknown>({ |
| 180 | + method: "GET", |
| 181 | + url: \`/pets/\${petId}\`, |
| 182 | + params: {"category-id": categoryId}, |
| 183 | + ...config |
| 184 | + }); |
| 185 | + return resp; |
| 186 | + }" |
| 187 | + `); |
| 188 | + }); |
| 189 | + |
| 190 | + it('response type', () => { |
| 191 | + const printer = new Printer(doc2, { |
| 192 | + runtimeValidate: { |
| 193 | + responseDataProps: [], |
| 194 | + }, |
| 195 | + }); |
| 196 | + const result = printer.print({ |
| 197 | + hideImports: true, |
| 198 | + hideHeaders: true, |
| 199 | + hideFooters: true, |
| 200 | + hideInfo: true, |
| 201 | + hideAlert: true, |
| 202 | + }); |
| 203 | + |
| 204 | + expect(result.main.code).toMatchInlineSnapshot(` |
| 205 | + "/** |
| 206 | + * @param petId request path "pet-id" |
| 207 | + * @param [categoryId] request params "category-id" |
| 208 | + * @param [config] request config |
| 209 | + * @returns pet name |
| 210 | + */ |
| 211 | + export async function getPet(petId:Type.GetPetPath,categoryId?:Type.GetPetParams,config?:AxiosRequestConfig) { |
| 212 | + zGetPetPath.parse(petId) |
| 213 | + (categoryId !== undefined) && zGetPetParams.parse(categoryId) |
| 214 | + const resp = await axios<Type.GetPetResponse>({ |
| 215 | + method: "GET", |
| 216 | + url: \`/pets/\${petId}\`, |
| 217 | + params: {"category-id": categoryId}, |
| 218 | + ...config |
| 219 | + }); |
| 220 | + zGetPetResponse.parse(resp); |
| 221 | + return resp; |
| 222 | + }" |
| 223 | + `); |
| 224 | + }); |
| 225 | +}); |
| 226 | + |
| 227 | +describe('runtimeValidate = {responseDataProps: [a, b-c]}', () => { |
| 228 | + it('response unknown', () => { |
| 229 | + const printer = new Printer(doc1, { |
| 230 | + runtimeValidate: { |
| 231 | + responseDataProps: ['a', 'b-c'], |
| 232 | + }, |
| 233 | + }); |
| 234 | + const result = printer.print({ |
| 235 | + hideImports: true, |
| 236 | + hideHeaders: true, |
| 237 | + hideFooters: true, |
| 238 | + hideInfo: true, |
| 239 | + hideAlert: true, |
| 240 | + }); |
| 241 | + |
| 242 | + expect(result.main.code).toMatchInlineSnapshot(` |
| 243 | + "/** |
| 244 | + * @param petId request path "pet-id" |
| 245 | + * @param [categoryId] request params "category-id" |
| 246 | + * @param [config] request config |
| 247 | + */ |
| 248 | + export async function getPet(petId:Type.GetPetPath,categoryId?:Type.GetPetParams,config?:AxiosRequestConfig) { |
| 249 | + zGetPetPath.parse(petId) |
| 250 | + (categoryId !== undefined) && zGetPetParams.parse(categoryId) |
| 251 | + const resp = await axios<unknown>({ |
| 252 | + method: "GET", |
| 253 | + url: \`/pets/\${petId}\`, |
| 254 | + params: {"category-id": categoryId}, |
| 255 | + ...config |
| 256 | + }); |
| 257 | + return resp; |
| 258 | + }" |
| 259 | + `); |
| 260 | + }); |
| 261 | + |
| 262 | + it('response type', () => { |
| 263 | + const printer = new Printer(doc2, { |
| 264 | + runtimeValidate: { |
| 265 | + responseDataProps: ['a', 'b-c'], |
| 266 | + }, |
| 267 | + }); |
| 268 | + const result = printer.print({ |
| 269 | + hideImports: true, |
| 270 | + hideHeaders: true, |
| 271 | + hideFooters: true, |
| 272 | + hideInfo: true, |
| 273 | + hideAlert: true, |
| 274 | + }); |
| 275 | + |
| 276 | + expect(result.main.code).toMatchInlineSnapshot(` |
| 277 | + "/** |
| 278 | + * @param petId request path "pet-id" |
| 279 | + * @param [categoryId] request params "category-id" |
| 280 | + * @param [config] request config |
| 281 | + * @returns pet name |
| 282 | + */ |
| 283 | + export async function getPet(petId:Type.GetPetPath,categoryId?:Type.GetPetParams,config?:AxiosRequestConfig) { |
| 284 | + zGetPetPath.parse(petId) |
| 285 | + (categoryId !== undefined) && zGetPetParams.parse(categoryId) |
| 286 | + const resp = await axios<Type.GetPetResponse>({ |
| 287 | + method: "GET", |
| 288 | + url: \`/pets/\${petId}\`, |
| 289 | + params: {"category-id": categoryId}, |
| 290 | + ...config |
| 291 | + }); |
| 292 | + zGetPetResponse.parse(resp["a"]["b-c"]); |
| 293 | + return resp; |
| 294 | + }" |
| 295 | + `); |
| 296 | + }); |
| 297 | +}); |
0 commit comments