fork of hey-api/openapi-ts because I need some additional things
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

chore: resolve error types

Lubos e45e97c1 db92b225

+69 -44
+2 -1
examples/openapi-ts-fetch/src/App.tsx
··· 11 11 } from './client/services.gen'; 12 12 13 13 createClient({ 14 - baseUrl: 'https://petstore3.swagger.io/api/v3', 14 + // baseUrl: 'https://petstore3.swagger.io/api/v3', 15 + baseUrl: 'https://api.fake-museum-example.com/v1.1', 15 16 }); 16 17 17 18 function App() {
+3 -3
examples/openapi-ts-fetch/src/client/types.gen.ts
··· 333 333 }; 334 334 '/special-events': { 335 335 post: { 336 - req: ListSpecialEventsData; 336 + req: CreateSpecialEventData; 337 337 res: { 338 338 /** 339 339 * Success. ··· 369 369 }; 370 370 '/special-events/{eventId}': { 371 371 get: { 372 - req: DeleteSpecialEventData; 372 + req: GetSpecialEventData; 373 373 res: { 374 374 /** 375 375 * Success. ··· 386 386 }; 387 387 }; 388 388 patch: { 389 - req: DeleteSpecialEventData; 389 + req: UpdateSpecialEventData; 390 390 res: { 391 391 /** 392 392 * Success.
+2 -2
packages/openapi-ts/src/index.ts
··· 6 6 import { parse } from './openApi'; 7 7 import type { Client } from './types/client'; 8 8 import type { ClientConfig, Config, UserConfig } from './types/config'; 9 - import { getConfig, setConfig } from './utils/config'; 9 + import { getConfig, isStandaloneClient, setConfig } from './utils/config'; 10 10 import { getOpenApiSpec } from './utils/getOpenApiSpec'; 11 11 import { registerHandlebarTemplates } from './utils/handlebars'; 12 12 import { postProcessClient } from './utils/postprocess'; ··· 234 234 client, 235 235 debug, 236 236 dryRun, 237 - exportCore: client.startsWith('@hey-api') ? false : exportCore, 237 + exportCore: isStandaloneClient(client) ? false : exportCore, 238 238 input, 239 239 name, 240 240 output,
+7 -1
packages/openapi-ts/src/openApi/v2/parser/getServices.ts
··· 1 1 import type { Client } from '../../../types/client'; 2 + import { getConfig, isStandaloneClient } from '../../../utils/config'; 2 3 import { unique } from '../../../utils/unique'; 3 4 import type { Service } from '../../common/interfaces/client'; 4 5 import type { OpenApi } from '../interfaces/OpenApi'; ··· 15 16 openApi: OpenApi; 16 17 types: Client['types']; 17 18 }): Service[] => { 19 + const config = getConfig(); 20 + 18 21 const services = new Map<string, Service>(); 19 22 20 23 Object.entries(openApi.paths).forEach(([url, path]) => { ··· 37 40 case 'patch': { 38 41 // Each method contains an OpenAPI operation, we parse the operation 39 42 const op = path[method]!; 40 - const tags = op.tags?.length ? op.tags.filter(unique) : ['Default']; 43 + const tags = 44 + op.tags?.length && !isStandaloneClient(config) 45 + ? op.tags.filter(unique) 46 + : ['Default']; 41 47 tags.forEach((tag) => { 42 48 const operation = getOperation({ 43 49 method,
+7 -1
packages/openapi-ts/src/openApi/v3/parser/getServices.ts
··· 1 1 import type { Client } from '../../../types/client'; 2 + import { getConfig, isStandaloneClient } from '../../../utils/config'; 2 3 import { unique } from '../../../utils/unique'; 3 4 import type { Operation, Service } from '../../common/interfaces/client'; 4 5 import type { OpenApi } from '../interfaces/OpenApi'; ··· 31 32 openApi: OpenApi; 32 33 types: Client['types']; 33 34 }): Service[] => { 35 + const config = getConfig(); 36 + 34 37 const services = new Map<string, Service>(); 35 38 36 39 for (const url in openApi.paths) { ··· 45 48 const method = key as Lowercase<Operation['method']>; 46 49 if (allowedServiceMethods.includes(method)) { 47 50 const op = path[method]!; 48 - const tags = op.tags?.length ? op.tags.filter(unique) : ['Default']; 51 + const tags = 52 + op.tags?.length && !isStandaloneClient(config) 53 + ? op.tags.filter(unique) 54 + : ['Default']; 49 55 tags.forEach((tag) => { 50 56 const operation = getOperation({ 51 57 method,
+5
packages/openapi-ts/src/utils/config.ts
··· 8 8 _config = config; 9 9 return getConfig(); 10 10 }; 11 + 12 + export const isStandaloneClient = (config: Config | Config['client']) => { 13 + const client = typeof config === 'string' ? config : config.client; 14 + return client.startsWith('@hey-api'); 15 + };
+19 -22
packages/openapi-ts/src/utils/write/services.ts
··· 16 16 Service, 17 17 } from '../../openApi'; 18 18 import type { Client } from '../../types/client'; 19 - import { getConfig } from '../config'; 19 + import { getConfig, isStandaloneClient } from '../config'; 20 20 import { escapeComment, escapeName } from '../escape'; 21 21 import { transformServiceName } from '../transform'; 22 22 import { unique } from '../unique'; ··· 74 74 (parameter) => parameter.isRequired, 75 75 ); 76 76 77 - if (config.client.startsWith('@hey-api')) { 77 + if (isStandaloneClient(config)) { 78 78 return [ 79 79 { 80 80 isRequired, ··· 158 158 const toOperationComment = (operation: Operation): Comments => { 159 159 const config = getConfig(); 160 160 161 - if (config.client.startsWith('@hey-api')) { 161 + if (isStandaloneClient(config)) { 162 162 const comment = [ 163 163 operation.deprecated && '@deprecated', 164 164 operation.summary && escapeComment(operation.summary), ··· 203 203 const toRequestOptions = (operation: Operation) => { 204 204 const config = getConfig(); 205 205 206 - if (config.client.startsWith('@hey-api')) { 206 + if (isStandaloneClient(config)) { 207 207 const obj: ObjectValue[] = [ 208 208 { 209 209 spread: 'options', ··· 305 305 306 306 const options = toRequestOptions(operation); 307 307 308 - if (config.client.startsWith('@hey-api')) { 308 + if (isStandaloneClient(config)) { 309 309 const errorType = uniqueTypeName({ 310 310 client, 311 311 meta: { ··· 393 393 }); 394 394 } 395 395 396 - if (config.client.startsWith('@hey-api')) { 397 - // TODO: improve error type detection 398 - if (operation.errors.length) { 399 - generateImport({ 400 - client, 401 - meta: { 402 - // TODO: this should be exact ref to operation for consistency, 403 - // but name should work too as operation ID is unique 404 - $ref: operation.name, 405 - name: operation.name, 406 - }, 407 - nameTransformer: operationErrorTypeName, 408 - onImport, 409 - }); 410 - } 396 + if (isStandaloneClient(config)) { 397 + generateImport({ 398 + client, 399 + meta: { 400 + // TODO: this should be exact ref to operation for consistency, 401 + // but name should work too as operation ID is unique 402 + $ref: operation.name, 403 + name: operation.name, 404 + }, 405 + nameTransformer: operationErrorTypeName, 406 + onImport, 407 + }); 411 408 } 412 409 413 410 // TODO: improve response type detection ··· 426 423 } 427 424 }); 428 425 429 - if (config.client.startsWith('@hey-api')) { 426 + if (isStandaloneClient(config)) { 430 427 service.operations.forEach((operation) => { 431 428 const expression = compiler.types.function({ 432 429 parameters: toOperationParamType(client, operation), ··· 526 523 } 527 524 528 525 // Import required packages and core files. 529 - if (config.client.startsWith('@hey-api')) { 526 + if (isStandaloneClient(config)) { 530 527 files.services?.addImport( 531 528 [ 532 529 'client',
+24 -14
packages/openapi-ts/src/utils/write/types.ts
··· 7 7 import type { Model, OperationParameter } from '../../openApi'; 8 8 import type { Method } from '../../openApi/common/interfaces/client'; 9 9 import type { Client } from '../../types/client'; 10 - import { getConfig } from '../config'; 10 + import { getConfig, isStandaloneClient } from '../config'; 11 11 import { enumEntry, enumUnionType } from '../enum'; 12 12 import { escapeComment } from '../escape'; 13 13 import { sortByName, sorterByName } from '../sort'; ··· 282 282 .filter((parameter) => parameter.in === 'query') 283 283 .sort(sorterByName), 284 284 }; 285 - const operationProperties = config.client.startsWith('@hey-api') 285 + const operationProperties = isStandaloneClient(config) 286 286 ? [ 287 287 bodyParameters, 288 288 headerParameters, ··· 354 354 }), 355 355 }); 356 356 357 - if (config.client.startsWith('@hey-api')) { 357 + if (isStandaloneClient(config)) { 358 + // TODO: improve error type detection 359 + const errorResults = operation.errors.filter( 360 + (result) => 361 + result.code === 'default' || 362 + (result.code >= 400 && result.code < 600), 363 + ); 358 364 // create type export for operation error 359 365 generateType({ 360 366 client, ··· 366 372 }, 367 373 nameTransformer: operationErrorTypeName, 368 374 onNode, 369 - type: toType({ 370 - ...emptyModel, 371 - export: 'all-of', 372 - isRequired: true, 373 - // TODO: improve error type detection 374 - properties: operation.errors.filter( 375 - (result) => 376 - result.code === 'default' || 377 - (result.code >= 400 && result.code < 600), 378 - ), 379 - }), 375 + type: toType( 376 + errorResults.length 377 + ? { 378 + ...emptyModel, 379 + export: 'all-of', 380 + isRequired: true, 381 + properties: errorResults, 382 + } 383 + : { 384 + ...emptyModel, 385 + base: 'unknown', 386 + isRequired: true, 387 + type: 'unknown', 388 + }, 389 + ), 380 390 }); 381 391 } 382 392 }