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

chore: update fastify example

Lubos 04f9318f 8a15a35e

+138 -136
+1 -1
examples/openapi-ts-fastify/openapi-ts.config.ts
··· 10 10 lint: 'eslint', 11 11 path: './src/client', 12 12 }, 13 - plugins: ['@hey-api/types', '@hey-api/services', 'fastify'], 13 + plugins: ['fastify'], 14 14 });
+12 -33
examples/openapi-ts-fastify/src/client/fastify.gen.ts
··· 2 2 3 3 import type { RouteHandler } from 'fastify'; 4 4 5 - export type Pet = { 6 - id: number; 7 - name: string; 8 - tag?: string; 9 - }; 10 - 11 - export type Pets = Array<Pet>; 12 - 13 - export type Error = { 14 - code: number; 15 - message: string; 16 - }; 5 + import type { 6 + CreatePetsResponses, 7 + ListPetsData, 8 + ListPetsResponses, 9 + ShowPetByIdData, 10 + ShowPetByIdResponses, 11 + } from './types.gen'; 17 12 18 13 export type RouteHandlers = { 19 14 createPets: RouteHandler<{ 20 - Reply: { 21 - 201: unknown; 22 - }; 15 + Reply: CreatePetsResponses; 23 16 }>; 24 17 listPets: RouteHandler<{ 25 - Querystring?: { 26 - /** 27 - * How many items to return at one time (max 100) 28 - */ 29 - limit?: number; 30 - }; 31 - Reply: { 32 - 200: Pets; 33 - }; 18 + Querystring?: ListPetsData['query']; 19 + Reply: ListPetsResponses; 34 20 }>; 35 21 showPetById: RouteHandler<{ 36 - Params: { 37 - /** 38 - * The id of the pet to retrieve 39 - */ 40 - petId: string; 41 - }; 42 - Reply: { 43 - 200: Pet; 44 - }; 22 + Params: ShowPetByIdData['path']; 23 + Reply: ShowPetByIdResponses; 45 24 }>; 46 25 };
-1
examples/openapi-ts-fastify/src/client/index.ts
··· 1 1 // This file is auto-generated by @hey-api/openapi-ts 2 - export * from './services.gen'; 3 2 export * from './types.gen';
-60
examples/openapi-ts-fastify/src/client/services.gen.ts
··· 1 - // This file is auto-generated by @hey-api/openapi-ts 2 - 3 - import { 4 - createClient, 5 - createConfig, 6 - type Options, 7 - } from '@hey-api/client-fetch'; 8 - 9 - import type { 10 - CreatePetsError, 11 - ListPetsData, 12 - ListPetsError, 13 - ListPetsResponse, 14 - ShowPetByIdData, 15 - ShowPetByIdError, 16 - ShowPetByIdResponse, 17 - } from './types.gen'; 18 - 19 - export const client = createClient(createConfig()); 20 - 21 - /** 22 - * List all pets 23 - */ 24 - export const listPets = <ThrowOnError extends boolean = false>( 25 - options?: Options<ListPetsData, ThrowOnError>, 26 - ) => 27 - (options?.client ?? client).get< 28 - ListPetsResponse, 29 - ListPetsError, 30 - ThrowOnError 31 - >({ 32 - ...options, 33 - url: '/pets', 34 - }); 35 - 36 - /** 37 - * Create a pet 38 - */ 39 - export const createPets = <ThrowOnError extends boolean = false>( 40 - options?: Options<unknown, ThrowOnError>, 41 - ) => 42 - (options?.client ?? client).post<unknown, CreatePetsError, ThrowOnError>({ 43 - ...options, 44 - url: '/pets', 45 - }); 46 - 47 - /** 48 - * Info for a specific pet 49 - */ 50 - export const showPetById = <ThrowOnError extends boolean = false>( 51 - options: Options<ShowPetByIdData, ThrowOnError>, 52 - ) => 53 - (options?.client ?? client).get< 54 - ShowPetByIdResponse, 55 - ShowPetByIdError, 56 - ThrowOnError 57 - >({ 58 - ...options, 59 - url: '/pets/{petId}', 60 - });
+48 -5
examples/openapi-ts-fastify/src/client/types.gen.ts
··· 24 24 }; 25 25 }; 26 26 27 - export type ListPetsError = Error; 27 + export type ListPetsErrors = { 28 + /** 29 + * unexpected error 30 + */ 31 + default: Error; 32 + }; 33 + 34 + export type ListPetsError = ListPetsErrors[keyof ListPetsErrors]; 35 + 36 + export type ListPetsResponses = { 37 + /** 38 + * A paged array of pets 39 + */ 40 + 200: Pets; 41 + }; 42 + 43 + export type ListPetsResponse = ListPetsResponses[keyof ListPetsResponses]; 44 + 45 + export type CreatePetsErrors = { 46 + /** 47 + * unexpected error 48 + */ 49 + default: Error; 50 + }; 28 51 29 - export type ListPetsResponse = Pets; 52 + export type CreatePetsError = CreatePetsErrors[keyof CreatePetsErrors]; 30 53 31 - export type CreatePetsError = Error; 54 + export type CreatePetsResponses = { 55 + /** 56 + * Null response 57 + */ 58 + 201: unknown; 59 + }; 32 60 33 61 export type ShowPetByIdData = { 34 62 body?: never; ··· 41 69 query?: never; 42 70 }; 43 71 44 - export type ShowPetByIdError = Error; 72 + export type ShowPetByIdErrors = { 73 + /** 74 + * unexpected error 75 + */ 76 + default: Error; 77 + }; 45 78 46 - export type ShowPetByIdResponse = Pet; 79 + export type ShowPetByIdError = ShowPetByIdErrors[keyof ShowPetByIdErrors]; 80 + 81 + export type ShowPetByIdResponses = { 82 + /** 83 + * Expected response to a valid request 84 + */ 85 + 200: Pet; 86 + }; 87 + 88 + export type ShowPetByIdResponse = 89 + ShowPetByIdResponses[keyof ShowPetByIdResponses];
+2 -1
examples/openapi-ts-fastify/src/handlers.ts
··· 1 - import type { Pet, RouteHandlers } from './client/fastify.gen'; 1 + import type { RouteHandlers } from './client/fastify.gen'; 2 + import type { Pet } from './client/types.gen'; 2 3 3 4 export const serviceHandlers: Pick<RouteHandlers, 'showPetById'> = { 4 5 showPetById(request, reply) {
+65 -19
packages/openapi-ts/src/plugins/fastify/plugin.ts
··· 7 7 IRPathItemObject, 8 8 IRPathsObject, 9 9 } from '../../ir/ir'; 10 + import { operationResponsesMap } from '../../ir/operation'; 10 11 import { hasParameterGroupObjectRequired } from '../../ir/parameter'; 11 12 import { operationIrRef } from '../@hey-api/services/plugin'; 12 13 import type { PluginHandler } from '../types'; ··· 30 31 $ref: operationIrRef({ id: operation.id, type: 'data' }), 31 32 namespace: 'type', 32 33 }); 33 - 34 34 if (identifierData.name) { 35 35 if (operation.body) { 36 36 file.import({ ··· 93 93 } 94 94 } 95 95 96 + const { errors, responses } = operationResponsesMap(operation); 97 + 96 98 let errorsTypeReference: ts.TypeReferenceNode | undefined = undefined; 97 99 const identifierErrors = fileTypes.identifier({ 98 100 $ref: operationIrRef({ id: operation.id, type: 'errors' }), 99 101 namespace: 'type', 100 102 }); 101 - if (identifierErrors.name) { 102 - file.import({ 103 - asType: true, 104 - module: file.relativePathToFile({ context, id: 'types' }), 105 - name: identifierErrors.name, 106 - }); 107 - errorsTypeReference = compiler.typeReferenceNode({ 108 - typeName: identifierErrors.name, 109 - }); 103 + if (identifierErrors.name && errors && errors.properties) { 104 + const keys = Object.keys(errors.properties); 105 + if (keys.length) { 106 + const hasDefaultResponse = keys.includes('default'); 107 + if (!hasDefaultResponse) { 108 + file.import({ 109 + asType: true, 110 + module: file.relativePathToFile({ context, id: 'types' }), 111 + name: identifierErrors.name, 112 + }); 113 + errorsTypeReference = compiler.typeReferenceNode({ 114 + typeName: identifierErrors.name, 115 + }); 116 + } else if (keys.length > 1) { 117 + file.import({ 118 + asType: true, 119 + module: file.relativePathToFile({ context, id: 'types' }), 120 + name: identifierErrors.name, 121 + }); 122 + const errorsType = compiler.typeReferenceNode({ 123 + typeName: identifierErrors.name, 124 + }); 125 + const defaultType = compiler.literalTypeNode({ 126 + literal: compiler.stringLiteral({ text: 'default' }), 127 + }); 128 + errorsTypeReference = compiler.typeReferenceNode({ 129 + typeArguments: [errorsType, defaultType], 130 + typeName: 'Omit', 131 + }); 132 + } 133 + } 110 134 } 111 135 112 136 let responsesTypeReference: ts.TypeReferenceNode | undefined = undefined; ··· 114 138 $ref: operationIrRef({ id: operation.id, type: 'responses' }), 115 139 namespace: 'type', 116 140 }); 117 - if (identifierResponses.name) { 118 - file.import({ 119 - asType: true, 120 - module: file.relativePathToFile({ context, id: 'types' }), 121 - name: identifierResponses.name, 122 - }); 123 - responsesTypeReference = compiler.typeReferenceNode({ 124 - typeName: identifierResponses.name, 125 - }); 141 + if (identifierResponses.name && responses && responses.properties) { 142 + const keys = Object.keys(responses.properties); 143 + if (keys.length) { 144 + const hasDefaultResponse = keys.includes('default'); 145 + if (!hasDefaultResponse) { 146 + file.import({ 147 + asType: true, 148 + module: file.relativePathToFile({ context, id: 'types' }), 149 + name: identifierResponses.name, 150 + }); 151 + responsesTypeReference = compiler.typeReferenceNode({ 152 + typeName: identifierResponses.name, 153 + }); 154 + } else if (keys.length > 1) { 155 + file.import({ 156 + asType: true, 157 + module: file.relativePathToFile({ context, id: 'types' }), 158 + name: identifierResponses.name, 159 + }); 160 + const responsesType = compiler.typeReferenceNode({ 161 + typeName: identifierResponses.name, 162 + }); 163 + const defaultType = compiler.literalTypeNode({ 164 + literal: compiler.stringLiteral({ text: 'default' }), 165 + }); 166 + responsesTypeReference = compiler.typeReferenceNode({ 167 + typeArguments: [responsesType, defaultType], 168 + typeName: 'Omit', 169 + }); 170 + } 171 + } 126 172 } 127 173 128 174 const replyTypes = [errorsTypeReference, responsesTypeReference].filter(
+5 -8
packages/openapi-ts/test/__snapshots__/3.0.x/plugins/fastify/default/fastify.gen.ts
··· 1 1 // This file is auto-generated by @hey-api/openapi-ts 2 2 3 - import type { ImportData, ImportResponses, ApiVversionOdataControllerCountResponses, GetApiVbyApiVersionSimpleOperationData, GetApiVbyApiVersionSimpleOperationErrors, GetApiVbyApiVersionSimpleOperationResponses, DeleteFooData3, CallWithDescriptionsData, DeprecatedCallData, CallWithParametersData, CallWithWeirdParameterNamesData, GetCallWithOptionalParamData, PostCallWithOptionalParamData, PostCallWithOptionalParamResponses, PostApiVbyApiVersionRequestBodyData, PostApiVbyApiVersionFormDataData, CallWithDefaultParametersData, CallWithDefaultOptionalParametersData, CallToTestOrderOfParamsData, CallWithNoContentResponseResponses, CallWithResponseAndNoContentResponseResponses, DummyAResponses, DummyBResponses, CallWithResponseResponses, CallWithDuplicateResponsesErrors, CallWithDuplicateResponsesResponses, CallWithResponsesErrors, CallWithResponsesResponses, CollectionFormatData, TypesData, TypesResponses, UploadFileData, UploadFileResponses, FileResponseData, FileResponseResponses, ComplexTypesData, ComplexTypesErrors, ComplexTypesResponses, MultipartResponseResponses, MultipartRequestData, ComplexParamsData, ComplexParamsResponses, CallWithResultFromHeaderErrors, CallWithResultFromHeaderResponses, TestErrorCodeData, TestErrorCodeErrors, TestErrorCodeResponses, NonAsciiæøåÆøÅöôêÊ字符串Data, NonAsciiæøåÆøÅöôêÊ字符串Responses, PutWithFormUrlEncodedData } from './types.gen'; 3 + import type { ImportData, ImportResponses, ApiVversionOdataControllerCountResponses, GetApiVbyApiVersionSimpleOperationData, GetApiVbyApiVersionSimpleOperationResponses, DeleteFooData3, CallWithDescriptionsData, DeprecatedCallData, CallWithParametersData, CallWithWeirdParameterNamesData, GetCallWithOptionalParamData, PostCallWithOptionalParamData, PostCallWithOptionalParamResponses, PostApiVbyApiVersionRequestBodyData, PostApiVbyApiVersionFormDataData, CallWithDefaultParametersData, CallWithDefaultOptionalParametersData, CallToTestOrderOfParamsData, CallWithNoContentResponseResponses, CallWithResponseAndNoContentResponseResponses, DummyAResponses, DummyBResponses, CallWithDuplicateResponsesErrors, CallWithDuplicateResponsesResponses, CallWithResponsesErrors, CallWithResponsesResponses, CollectionFormatData, TypesData, TypesResponses, UploadFileData, UploadFileResponses, FileResponseData, FileResponseResponses, ComplexTypesData, ComplexTypesErrors, ComplexTypesResponses, MultipartResponseResponses, MultipartRequestData, ComplexParamsData, ComplexParamsResponses, CallWithResultFromHeaderErrors, CallWithResultFromHeaderResponses, TestErrorCodeData, TestErrorCodeErrors, TestErrorCodeResponses, NonAsciiæøåÆøÅöôêÊ字符串Data, NonAsciiæøåÆøÅöôêÊ字符串Responses, PutWithFormUrlEncodedData } from './types.gen'; 4 4 import type { RouteHandler } from 'fastify'; 5 5 6 6 export type RouteHandlers = { 7 7 import: RouteHandler<{ 8 8 Body: ImportData['body']; 9 - Reply: ImportResponses; 9 + Reply: Omit<ImportResponses, 'default'>; 10 10 }>; 11 11 apiVVersionOdataControllerCount: RouteHandler<{ 12 12 Reply: ApiVversionOdataControllerCountResponses; 13 13 }>; 14 14 getApiVbyApiVersionSimpleOperation: RouteHandler<{ 15 15 Params: GetApiVbyApiVersionSimpleOperationData['path']; 16 - Reply: GetApiVbyApiVersionSimpleOperationErrors & GetApiVbyApiVersionSimpleOperationResponses; 16 + Reply: GetApiVbyApiVersionSimpleOperationResponses; 17 17 }>; 18 18 deleteFoo: RouteHandler<{ 19 19 Headers: DeleteFooData3['headers']; ··· 75 75 dummyB: RouteHandler<{ 76 76 Reply: DummyBResponses; 77 77 }>; 78 - callWithResponse: RouteHandler<{ 79 - Reply: CallWithResponseResponses; 80 - }>; 81 78 callWithDuplicateResponses: RouteHandler<{ 82 - Reply: CallWithDuplicateResponsesErrors & CallWithDuplicateResponsesResponses; 79 + Reply: Omit<CallWithDuplicateResponsesErrors, 'default'> & CallWithDuplicateResponsesResponses; 83 80 }>; 84 81 callWithResponses: RouteHandler<{ 85 - Reply: CallWithResponsesErrors & CallWithResponsesResponses; 82 + Reply: Omit<CallWithResponsesErrors, 'default'> & CallWithResponsesResponses; 86 83 }>; 87 84 collectionFormat: RouteHandler<{ 88 85 Querystring: CollectionFormatData['query'];
+5 -8
packages/openapi-ts/test/__snapshots__/3.1.x/plugins/fastify/default/fastify.gen.ts
··· 1 1 // This file is auto-generated by @hey-api/openapi-ts 2 2 3 - import type { ImportData, ImportResponses, ApiVversionOdataControllerCountResponses, GetApiVbyApiVersionSimpleOperationData, GetApiVbyApiVersionSimpleOperationErrors, GetApiVbyApiVersionSimpleOperationResponses, DeleteFooData3, CallWithDescriptionsData, DeprecatedCallData, CallWithParametersData, CallWithWeirdParameterNamesData, GetCallWithOptionalParamData, PostCallWithOptionalParamData, PostCallWithOptionalParamResponses, PostApiVbyApiVersionRequestBodyData, PostApiVbyApiVersionFormDataData, CallWithDefaultParametersData, CallWithDefaultOptionalParametersData, CallToTestOrderOfParamsData, CallWithNoContentResponseResponses, CallWithResponseAndNoContentResponseResponses, DummyAResponses, DummyBResponses, CallWithResponseResponses, CallWithDuplicateResponsesErrors, CallWithDuplicateResponsesResponses, CallWithResponsesErrors, CallWithResponsesResponses, CollectionFormatData, TypesData, TypesResponses, UploadFileData, UploadFileResponses, FileResponseData, FileResponseResponses, ComplexTypesData, ComplexTypesErrors, ComplexTypesResponses, MultipartResponseResponses, MultipartRequestData, ComplexParamsData, ComplexParamsResponses, CallWithResultFromHeaderErrors, CallWithResultFromHeaderResponses, TestErrorCodeData, TestErrorCodeErrors, TestErrorCodeResponses, NonAsciiæøåÆøÅöôêÊ字符串Data, NonAsciiæøåÆøÅöôêÊ字符串Responses, PutWithFormUrlEncodedData } from './types.gen'; 3 + import type { ImportData, ImportResponses, ApiVversionOdataControllerCountResponses, GetApiVbyApiVersionSimpleOperationData, GetApiVbyApiVersionSimpleOperationResponses, DeleteFooData3, CallWithDescriptionsData, DeprecatedCallData, CallWithParametersData, CallWithWeirdParameterNamesData, GetCallWithOptionalParamData, PostCallWithOptionalParamData, PostCallWithOptionalParamResponses, PostApiVbyApiVersionRequestBodyData, PostApiVbyApiVersionFormDataData, CallWithDefaultParametersData, CallWithDefaultOptionalParametersData, CallToTestOrderOfParamsData, CallWithNoContentResponseResponses, CallWithResponseAndNoContentResponseResponses, DummyAResponses, DummyBResponses, CallWithDuplicateResponsesErrors, CallWithDuplicateResponsesResponses, CallWithResponsesErrors, CallWithResponsesResponses, CollectionFormatData, TypesData, TypesResponses, UploadFileData, UploadFileResponses, FileResponseData, FileResponseResponses, ComplexTypesData, ComplexTypesErrors, ComplexTypesResponses, MultipartResponseResponses, MultipartRequestData, ComplexParamsData, ComplexParamsResponses, CallWithResultFromHeaderErrors, CallWithResultFromHeaderResponses, TestErrorCodeData, TestErrorCodeErrors, TestErrorCodeResponses, NonAsciiæøåÆøÅöôêÊ字符串Data, NonAsciiæøåÆøÅöôêÊ字符串Responses, PutWithFormUrlEncodedData } from './types.gen'; 4 4 import type { RouteHandler } from 'fastify'; 5 5 6 6 export type RouteHandlers = { 7 7 import: RouteHandler<{ 8 8 Body: ImportData['body']; 9 - Reply: ImportResponses; 9 + Reply: Omit<ImportResponses, 'default'>; 10 10 }>; 11 11 apiVVersionOdataControllerCount: RouteHandler<{ 12 12 Reply: ApiVversionOdataControllerCountResponses; 13 13 }>; 14 14 getApiVbyApiVersionSimpleOperation: RouteHandler<{ 15 15 Params: GetApiVbyApiVersionSimpleOperationData['path']; 16 - Reply: GetApiVbyApiVersionSimpleOperationErrors & GetApiVbyApiVersionSimpleOperationResponses; 16 + Reply: GetApiVbyApiVersionSimpleOperationResponses; 17 17 }>; 18 18 deleteFoo: RouteHandler<{ 19 19 Headers: DeleteFooData3['headers']; ··· 75 75 dummyB: RouteHandler<{ 76 76 Reply: DummyBResponses; 77 77 }>; 78 - callWithResponse: RouteHandler<{ 79 - Reply: CallWithResponseResponses; 80 - }>; 81 78 callWithDuplicateResponses: RouteHandler<{ 82 - Reply: CallWithDuplicateResponsesErrors & CallWithDuplicateResponsesResponses; 79 + Reply: Omit<CallWithDuplicateResponsesErrors, 'default'> & CallWithDuplicateResponsesResponses; 83 80 }>; 84 81 callWithResponses: RouteHandler<{ 85 - Reply: CallWithResponsesErrors & CallWithResponsesResponses; 82 + Reply: Omit<CallWithResponsesErrors, 'default'> & CallWithResponsesResponses; 86 83 }>; 87 84 collectionFormat: RouteHandler<{ 88 85 Querystring: CollectionFormatData['query'];