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

feat: Enhance Angular SDK with injectable services and class name customization

Max Scopp 2dd4461c 6ab6bb57

+539 -453
+3 -2
docs/openapi-ts/clients/angular.md
··· 64 64 65 65 ### Injectable Classes Configuration 66 66 67 - You can configure the Angular client to generate injectable classes by setting the `asClass` option to `true` in your plugin configuration. This will generate Angular services with `@Injectable()` decorators, making them available for dependency injection. 67 + You can configure the SDK to generate injectable classes by setting the `asClass` option to `true` in your plugin configuration. This will generate Angular services with `@Injectable()` decorators, making them available for dependency injection. 68 68 69 69 ```js 70 70 export default { 71 71 input: 'https://get.heyapi.dev/hey-api/backend', 72 72 output: 'src/client', 73 73 plugins: [ 74 + '@hey-api/client-angular', 74 75 { 75 - name: '@hey-api/client-angular', 76 + name: '@hey-api/sdk', 76 77 asClass: true, // [!code ++] 77 78 }, 78 79 ],
+4 -2
examples/openapi-ts-angular/openapi-ts.config.ts
··· 11 11 plugins: [ 12 12 { 13 13 name: '@hey-api/client-angular', 14 - // throwOnError: true, 15 14 }, 16 15 '@hey-api/schemas', 17 - '@hey-api/sdk', 16 + { 17 + asClass: true, 18 + name: '@hey-api/sdk', 19 + }, 18 20 { 19 21 enums: 'javascript', 20 22 name: '@hey-api/typescript',
+4 -3
examples/openapi-ts-angular/src/app/demo/demo.ts
··· 5 5 import { RouterOutlet } from '@angular/router'; 6 6 7 7 import type { AddPetErrors, Pet } from '../../client'; 8 - import { getPetById } from '../../client'; 8 + import { PetService } from '../../client'; 9 9 import { createClient } from '../../client/client'; 10 10 11 11 const localClient = createClient({ ··· 38 38 } 39 39 >(undefined); 40 40 41 + #petService = inject(PetService); 41 42 #http = inject(HttpClient); 42 43 43 44 // // you can set a global httpClient for this client like so, in your app.config, or on each request (like below) ··· 48 49 // } 49 50 50 51 onGetPetByIdLocalClient = async () => { 51 - const { data, error, response } = await getPetById({ 52 + const { data, error, response } = await this.#petService.getPetById({ 52 53 client: localClient, 53 54 httpClient: this.#http, 54 55 path: { ··· 71 72 }; 72 73 73 74 onGetPetById = async () => { 74 - const { data, error, response } = await getPetById({ 75 + const { data, error, response } = await this.#petService.getPetById({ 75 76 path: { 76 77 // random id 1-10 77 78 petId: Math.floor(Math.random() * (10 - 1 + 1) + 1),
+62 -25
examples/openapi-ts-angular/src/client/client/client.gen.ts
··· 16 16 import { firstValueFrom } from 'rxjs'; 17 17 import { filter } from 'rxjs/operators'; 18 18 19 - import type { Client, Config, ResolvedRequestOptions } from './types.gen'; 19 + import type { 20 + Client, 21 + Config, 22 + RequestOptions, 23 + ResolvedRequestOptions, 24 + ResponseStyle, 25 + } from './types.gen'; 20 26 import { 21 27 buildUrl, 22 28 createConfig, ··· 50 56 ResolvedRequestOptions 51 57 >(); 52 58 53 - const request: Client['request'] = async (options) => { 59 + const requestOptions = < 60 + ThrowOnError extends boolean = false, 61 + TResponseStyle extends ResponseStyle = 'fields', 62 + >( 63 + options: RequestOptions<TResponseStyle, ThrowOnError>, 64 + ) => { 54 65 const opts = { 55 66 ..._config, 56 67 ...options, 57 68 headers: mergeHeaders(_config.headers, options.headers), 58 69 httpClient: options.httpClient ?? _config.httpClient, 59 - serializedBody: undefined, 70 + method: 'GET', 71 + serializedBody: options.body as any, 60 72 }; 61 73 62 74 if (!opts.httpClient) { ··· 65 77 inject(HttpClient), 66 78 ); 67 79 } else { 68 - assertInInjectionContext(request); 80 + assertInInjectionContext(requestOptions); 69 81 opts.httpClient = inject(HttpClient); 70 82 } 71 83 } 72 84 85 + if (opts.body && opts.bodySerializer) { 86 + opts.serializedBody = opts.bodySerializer(opts.body); 87 + } 88 + 89 + // remove Content-Type header if body is empty to avoid sending invalid requests 90 + if (opts.serializedBody === undefined || opts.serializedBody === '') { 91 + opts.headers.delete('Content-Type'); 92 + } 93 + 94 + const url = buildUrl(opts as any); 95 + 96 + const req = new HttpRequest<unknown>( 97 + opts.method, 98 + url, 99 + opts.serializedBody || null, 100 + { 101 + redirect: 'follow', 102 + ...opts, 103 + }, 104 + ); 105 + 106 + return { opts, req }; 107 + }; 108 + 109 + const request: Client['request'] = async (options) => { 110 + const { opts, req: initialReq } = requestOptions(options); 111 + 73 112 if (opts.security) { 74 113 await setAuthParams({ 75 114 ...opts, ··· 81 120 await opts.requestValidator(opts); 82 121 } 83 122 84 - if (opts.body && opts.bodySerializer) { 85 - opts.serializedBody = opts.bodySerializer(opts.body); 86 - } 87 - 88 - // remove Content-Type header if body is empty to avoid sending invalid requests 89 - if (opts.serializedBody === undefined || opts.serializedBody === '') { 90 - opts.headers.delete('Content-Type'); 91 - } 92 - 93 - const url = buildUrl(opts); 94 - 95 - let req = new HttpRequest<unknown>(opts.method, url, { 96 - redirect: 'follow', 97 - ...opts, 98 - body: opts.serializedBody, 99 - }); 123 + let req = initialReq; 100 124 101 125 for (const fn of interceptors.request._fns) { 102 126 if (fn) { 103 - req = await fn(req, opts); 127 + req = await fn(req, opts as any); 104 128 } 105 129 } 106 130 ··· 112 136 113 137 try { 114 138 response = await firstValueFrom( 115 - opts.httpClient 116 - .request(req) 139 + opts 140 + .httpClient!.request(req) 117 141 .pipe(filter((event) => event.type === HttpEventType.Response)), 118 142 ); 119 143 120 144 for (const fn of interceptors.response._fns) { 121 145 if (fn) { 122 - response = await fn(response, req, opts); 146 + response = await fn(response, req, opts as any); 123 147 } 124 148 } 125 149 ··· 149 173 finalError, 150 174 response as HttpResponse<unknown>, 151 175 req, 152 - opts, 176 + opts as any, 153 177 )) as string; 154 178 } 155 179 } ··· 180 204 post: (options) => request({ ...options, method: 'POST' }), 181 205 put: (options) => request({ ...options, method: 'PUT' }), 182 206 request, 207 + requestOptions: (options) => { 208 + if (options.security) { 209 + throw new Error('Security is not supported in requestOptions'); 210 + } 211 + 212 + if (options.requestValidator) { 213 + throw new Error( 214 + 'Request validation is not supported in requestOptions', 215 + ); 216 + } 217 + 218 + return requestOptions(options).req; 219 + }, 183 220 setConfig, 184 221 trace: (options) => request({ ...options, method: 'TRACE' }), 185 222 };
+43 -38
examples/openapi-ts-angular/src/client/client/types.gen.ts
··· 102 102 TError = unknown, 103 103 ThrowOnError extends boolean = boolean, 104 104 TResponseStyle extends ResponseStyle = 'fields', 105 - > = ThrowOnError extends true 106 - ? Promise< 107 - TResponseStyle extends 'data' 108 - ? TData extends Record<string, unknown> 109 - ? TData[keyof TData] 110 - : TData 111 - : { 112 - data: TData extends Record<string, unknown> 113 - ? TData[keyof TData] 114 - : TData; 115 - request: HttpRequest<unknown>; 116 - response: HttpResponse<TData>; 117 - } 118 - > 119 - : Promise< 120 - TResponseStyle extends 'data' 121 - ? 122 - | (TData extends Record<string, unknown> 105 + > = Promise< 106 + ThrowOnError extends true 107 + ? TResponseStyle extends 'data' 108 + ? TData extends Record<string, unknown> 109 + ? TData[keyof TData] 110 + : TData 111 + : { 112 + data: TData extends Record<string, unknown> 113 + ? TData[keyof TData] 114 + : TData; 115 + request: HttpRequest<unknown>; 116 + response: HttpResponse<TData>; 117 + } 118 + : TResponseStyle extends 'data' 119 + ? 120 + | (TData extends Record<string, unknown> ? TData[keyof TData] : TData) 121 + | undefined 122 + : 123 + | { 124 + data: TData extends Record<string, unknown> 123 125 ? TData[keyof TData] 124 - : TData) 125 - | undefined 126 - : 127 - | { 128 - data: TData extends Record<string, unknown> 129 - ? TData[keyof TData] 130 - : TData; 131 - error: undefined; 132 - request: HttpRequest<unknown>; 133 - response: HttpResponse<TData>; 134 - } 135 - | { 136 - data: undefined; 137 - error: TError[keyof TError]; 138 - request: HttpRequest<unknown>; 139 - response: HttpErrorResponse & { 140 - error: TError[keyof TError] | null; 141 - }; 142 - } 143 - >; 126 + : TData; 127 + error: undefined; 128 + request: HttpRequest<unknown>; 129 + response: HttpResponse<TData>; 130 + } 131 + | { 132 + data: undefined; 133 + error: TError[keyof TError]; 134 + request: HttpRequest<unknown>; 135 + response: HttpErrorResponse & { 136 + error: TError[keyof TError] | null; 137 + }; 138 + } 139 + >; 144 140 145 141 export interface ClientOptions { 146 142 baseUrl?: string; ··· 167 163 Pick<Required<RequestOptions<TResponseStyle, ThrowOnError>>, 'method'>, 168 164 ) => RequestResult<TData, TError, ThrowOnError, TResponseStyle>; 169 165 166 + type RequestOptionsFn = < 167 + ThrowOnError extends boolean = false, 168 + TResponseStyle extends ResponseStyle = 'fields', 169 + >( 170 + options: RequestOptions<TResponseStyle, ThrowOnError>, 171 + ) => HttpRequest<unknown>; 172 + 170 173 type BuildUrlFn = < 171 174 TData extends { 172 175 body?: unknown; ··· 185 188 unknown, 186 189 ResolvedRequestOptions 187 190 >; 191 + 192 + requestOptions: RequestOptionsFn; 188 193 }; 189 194 190 195 /**
+9 -13
examples/openapi-ts-angular/src/client/client/utils.gen.ts
··· 7 7 QuerySerializer, 8 8 QuerySerializerOptions, 9 9 } from '../core/bodySerializer.gen'; 10 - import { jsonBodySerializer } from '../core/bodySerializer.gen'; 11 10 import { 12 11 serializeArrayParam, 13 12 serializeObjectParam, ··· 195 194 return; 196 195 }; 197 196 198 - export const setAuthParams = async ({ 199 - security, 200 - ...options 201 - }: Pick<Required<RequestOptions>, 'security'> & 202 - Pick<RequestOptions, 'auth' | 'query'> & { 203 - headers: HttpHeaders; 204 - }) => { 205 - for (const auth of security) { 197 + export const setAuthParams = async ( 198 + options: Pick<Required<RequestOptions>, 'security'> & 199 + Pick<RequestOptions, 'auth' | 'query'> & { 200 + headers: HttpHeaders; 201 + }, 202 + ) => { 203 + for (const auth of options.security) { 206 204 const token = await getAuthToken(auth, options.auth); 207 205 208 206 if (!token) { ··· 219 217 options.query[name] = token; 220 218 break; 221 219 case 'cookie': 222 - options.headers.append('Cookie', `${name}=${token}`); 220 + options.headers = options.headers.append('Cookie', `${name}=${token}`); 223 221 break; 224 222 case 'header': 225 223 default: 226 - options.headers.set(name, token); 224 + options.headers = options.headers.set(name, token); 227 225 break; 228 226 } 229 227 ··· 432 430 export const createConfig = <T extends ClientOptions = ClientOptions>( 433 431 override: Config<Omit<ClientOptions, keyof T> & T> = {}, 434 432 ): Config<Omit<ClientOptions, keyof T> & T> => ({ 435 - ...jsonBodySerializer, 436 433 headers: defaultHeaders, 437 - // parseAs: 'auto', 438 434 querySerializer: defaultQuerySerializer, 439 435 ...override, 440 436 });
+405 -369
examples/openapi-ts-angular/src/client/sdk.gen.ts
··· 1 1 // This file is auto-generated by @hey-api/openapi-ts 2 2 3 + import { Injectable } from '@angular/core'; 4 + 3 5 import type { Client, Options as ClientOptions, TDataShape } from './client'; 4 6 import { client as _heyApiClient } from './client.gen'; 5 7 import type { ··· 79 81 meta?: Record<string, unknown>; 80 82 }; 81 83 82 - /** 83 - * Add a new pet to the store. 84 - * Add a new pet to the store. 85 - */ 86 - export const addPet = <ThrowOnError extends boolean = false>( 87 - options: Options<AddPetData, ThrowOnError>, 88 - ) => 89 - (options.client ?? _heyApiClient).post< 90 - AddPetResponses, 91 - AddPetErrors, 92 - ThrowOnError 93 - >({ 94 - security: [ 95 - { 96 - scheme: 'bearer', 97 - type: 'http', 84 + @Injectable({ 85 + providedIn: 'root', 86 + }) 87 + export class PetService { 88 + /** 89 + * Add a new pet to the store. 90 + * Add a new pet to the store. 91 + */ 92 + public addPet<ThrowOnError extends boolean = false>( 93 + options: Options<AddPetData, ThrowOnError>, 94 + ) { 95 + return (options.client ?? _heyApiClient).post< 96 + AddPetResponses, 97 + AddPetErrors, 98 + ThrowOnError 99 + >({ 100 + security: [ 101 + { 102 + scheme: 'bearer', 103 + type: 'http', 104 + }, 105 + ], 106 + url: '/pet', 107 + ...options, 108 + headers: { 109 + 'Content-Type': 'application/json', 110 + ...options.headers, 98 111 }, 99 - ], 100 - url: '/pet', 101 - ...options, 102 - headers: { 103 - 'Content-Type': 'application/json', 104 - ...options.headers, 105 - }, 106 - }); 112 + }); 113 + } 107 114 108 - /** 109 - * Update an existing pet. 110 - * Update an existing pet by Id. 111 - */ 112 - export const updatePet = <ThrowOnError extends boolean = false>( 113 - options: Options<UpdatePetData, ThrowOnError>, 114 - ) => 115 - (options.client ?? _heyApiClient).put< 116 - UpdatePetResponses, 117 - UpdatePetErrors, 118 - ThrowOnError 119 - >({ 120 - security: [ 121 - { 122 - scheme: 'bearer', 123 - type: 'http', 115 + /** 116 + * Update an existing pet. 117 + * Update an existing pet by Id. 118 + */ 119 + public updatePet<ThrowOnError extends boolean = false>( 120 + options: Options<UpdatePetData, ThrowOnError>, 121 + ) { 122 + return (options.client ?? _heyApiClient).put< 123 + UpdatePetResponses, 124 + UpdatePetErrors, 125 + ThrowOnError 126 + >({ 127 + security: [ 128 + { 129 + scheme: 'bearer', 130 + type: 'http', 131 + }, 132 + ], 133 + url: '/pet', 134 + ...options, 135 + headers: { 136 + 'Content-Type': 'application/json', 137 + ...options.headers, 124 138 }, 125 - ], 126 - url: '/pet', 127 - ...options, 128 - headers: { 129 - 'Content-Type': 'application/json', 130 - ...options.headers, 131 - }, 132 - }); 139 + }); 140 + } 133 141 134 - /** 135 - * Finds Pets by status. 136 - * Multiple status values can be provided with comma separated strings. 137 - */ 138 - export const findPetsByStatus = <ThrowOnError extends boolean = false>( 139 - options: Options<FindPetsByStatusData, ThrowOnError>, 140 - ) => 141 - (options.client ?? _heyApiClient).get< 142 - FindPetsByStatusResponses, 143 - FindPetsByStatusErrors, 144 - ThrowOnError 145 - >({ 146 - security: [ 147 - { 148 - scheme: 'bearer', 149 - type: 'http', 150 - }, 151 - ], 152 - url: '/pet/findByStatus', 153 - ...options, 154 - }); 142 + /** 143 + * Finds Pets by status. 144 + * Multiple status values can be provided with comma separated strings. 145 + */ 146 + public findPetsByStatus<ThrowOnError extends boolean = false>( 147 + options: Options<FindPetsByStatusData, ThrowOnError>, 148 + ) { 149 + return (options.client ?? _heyApiClient).get< 150 + FindPetsByStatusResponses, 151 + FindPetsByStatusErrors, 152 + ThrowOnError 153 + >({ 154 + security: [ 155 + { 156 + scheme: 'bearer', 157 + type: 'http', 158 + }, 159 + ], 160 + url: '/pet/findByStatus', 161 + ...options, 162 + }); 163 + } 155 164 156 - /** 157 - * Finds Pets by tags. 158 - * Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing. 159 - */ 160 - export const findPetsByTags = <ThrowOnError extends boolean = false>( 161 - options: Options<FindPetsByTagsData, ThrowOnError>, 162 - ) => 163 - (options.client ?? _heyApiClient).get< 164 - FindPetsByTagsResponses, 165 - FindPetsByTagsErrors, 166 - ThrowOnError 167 - >({ 168 - security: [ 169 - { 170 - scheme: 'bearer', 171 - type: 'http', 172 - }, 173 - ], 174 - url: '/pet/findByTags', 175 - ...options, 176 - }); 165 + /** 166 + * Finds Pets by tags. 167 + * Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing. 168 + */ 169 + public findPetsByTags<ThrowOnError extends boolean = false>( 170 + options: Options<FindPetsByTagsData, ThrowOnError>, 171 + ) { 172 + return (options.client ?? _heyApiClient).get< 173 + FindPetsByTagsResponses, 174 + FindPetsByTagsErrors, 175 + ThrowOnError 176 + >({ 177 + security: [ 178 + { 179 + scheme: 'bearer', 180 + type: 'http', 181 + }, 182 + ], 183 + url: '/pet/findByTags', 184 + ...options, 185 + }); 186 + } 177 187 178 - /** 179 - * Deletes a pet. 180 - * Delete a pet. 181 - */ 182 - export const deletePet = <ThrowOnError extends boolean = false>( 183 - options: Options<DeletePetData, ThrowOnError>, 184 - ) => 185 - (options.client ?? _heyApiClient).delete< 186 - DeletePetResponses, 187 - DeletePetErrors, 188 - ThrowOnError 189 - >({ 190 - security: [ 191 - { 192 - scheme: 'bearer', 193 - type: 'http', 194 - }, 195 - ], 196 - url: '/pet/{petId}', 197 - ...options, 198 - }); 188 + /** 189 + * Deletes a pet. 190 + * Delete a pet. 191 + */ 192 + public deletePet<ThrowOnError extends boolean = false>( 193 + options: Options<DeletePetData, ThrowOnError>, 194 + ) { 195 + return (options.client ?? _heyApiClient).delete< 196 + DeletePetResponses, 197 + DeletePetErrors, 198 + ThrowOnError 199 + >({ 200 + security: [ 201 + { 202 + scheme: 'bearer', 203 + type: 'http', 204 + }, 205 + ], 206 + url: '/pet/{petId}', 207 + ...options, 208 + }); 209 + } 199 210 200 - /** 201 - * Find pet by ID. 202 - * Returns a single pet. 203 - */ 204 - export const getPetById = <ThrowOnError extends boolean = false>( 205 - options: Options<GetPetByIdData, ThrowOnError>, 206 - ) => 207 - (options.client ?? _heyApiClient).get< 208 - GetPetByIdResponses, 209 - GetPetByIdErrors, 210 - ThrowOnError 211 - >({ 212 - security: [ 213 - { 214 - name: 'api_key', 215 - type: 'apiKey', 216 - }, 217 - { 218 - scheme: 'bearer', 219 - type: 'http', 220 - }, 221 - ], 222 - url: '/pet/{petId}', 223 - ...options, 224 - }); 211 + /** 212 + * Find pet by ID. 213 + * Returns a single pet. 214 + */ 215 + public getPetById<ThrowOnError extends boolean = false>( 216 + options: Options<GetPetByIdData, ThrowOnError>, 217 + ) { 218 + return (options.client ?? _heyApiClient).get< 219 + GetPetByIdResponses, 220 + GetPetByIdErrors, 221 + ThrowOnError 222 + >({ 223 + security: [ 224 + { 225 + name: 'api_key', 226 + type: 'apiKey', 227 + }, 228 + { 229 + scheme: 'bearer', 230 + type: 'http', 231 + }, 232 + ], 233 + url: '/pet/{petId}', 234 + ...options, 235 + }); 236 + } 225 237 226 - /** 227 - * Updates a pet in the store with form data. 228 - * Updates a pet resource based on the form data. 229 - */ 230 - export const updatePetWithForm = <ThrowOnError extends boolean = false>( 231 - options: Options<UpdatePetWithFormData, ThrowOnError>, 232 - ) => 233 - (options.client ?? _heyApiClient).post< 234 - UpdatePetWithFormResponses, 235 - UpdatePetWithFormErrors, 236 - ThrowOnError 237 - >({ 238 - security: [ 239 - { 240 - scheme: 'bearer', 241 - type: 'http', 242 - }, 243 - ], 244 - url: '/pet/{petId}', 245 - ...options, 246 - }); 238 + /** 239 + * Updates a pet in the store with form data. 240 + * Updates a pet resource based on the form data. 241 + */ 242 + public updatePetWithForm<ThrowOnError extends boolean = false>( 243 + options: Options<UpdatePetWithFormData, ThrowOnError>, 244 + ) { 245 + return (options.client ?? _heyApiClient).post< 246 + UpdatePetWithFormResponses, 247 + UpdatePetWithFormErrors, 248 + ThrowOnError 249 + >({ 250 + security: [ 251 + { 252 + scheme: 'bearer', 253 + type: 'http', 254 + }, 255 + ], 256 + url: '/pet/{petId}', 257 + ...options, 258 + }); 259 + } 247 260 248 - /** 249 - * Uploads an image. 250 - * Upload image of the pet. 251 - */ 252 - export const uploadFile = <ThrowOnError extends boolean = false>( 253 - options: Options<UploadFileData, ThrowOnError>, 254 - ) => 255 - (options.client ?? _heyApiClient).post< 256 - UploadFileResponses, 257 - UploadFileErrors, 258 - ThrowOnError 259 - >({ 260 - bodySerializer: null, 261 - security: [ 262 - { 263 - scheme: 'bearer', 264 - type: 'http', 261 + /** 262 + * Uploads an image. 263 + * Upload image of the pet. 264 + */ 265 + public uploadFile<ThrowOnError extends boolean = false>( 266 + options: Options<UploadFileData, ThrowOnError>, 267 + ) { 268 + return (options.client ?? _heyApiClient).post< 269 + UploadFileResponses, 270 + UploadFileErrors, 271 + ThrowOnError 272 + >({ 273 + bodySerializer: null, 274 + security: [ 275 + { 276 + scheme: 'bearer', 277 + type: 'http', 278 + }, 279 + ], 280 + url: '/pet/{petId}/uploadImage', 281 + ...options, 282 + headers: { 283 + 'Content-Type': 'application/octet-stream', 284 + ...options.headers, 265 285 }, 266 - ], 267 - url: '/pet/{petId}/uploadImage', 268 - ...options, 269 - headers: { 270 - 'Content-Type': 'application/octet-stream', 271 - ...options.headers, 272 - }, 273 - }); 286 + }); 287 + } 288 + } 274 289 275 - /** 276 - * Returns pet inventories by status. 277 - * Returns a map of status codes to quantities. 278 - */ 279 - export const getInventory = <ThrowOnError extends boolean = false>( 280 - options?: Options<GetInventoryData, ThrowOnError>, 281 - ) => 282 - (options?.client ?? _heyApiClient).get< 283 - GetInventoryResponses, 284 - GetInventoryErrors, 285 - ThrowOnError 286 - >({ 287 - security: [ 288 - { 289 - name: 'api_key', 290 - type: 'apiKey', 290 + @Injectable({ 291 + providedIn: 'root', 292 + }) 293 + export class StoreService { 294 + /** 295 + * Returns pet inventories by status. 296 + * Returns a map of status codes to quantities. 297 + */ 298 + public getInventory<ThrowOnError extends boolean = false>( 299 + options?: Options<GetInventoryData, ThrowOnError>, 300 + ) { 301 + return (options?.client ?? _heyApiClient).get< 302 + GetInventoryResponses, 303 + GetInventoryErrors, 304 + ThrowOnError 305 + >({ 306 + security: [ 307 + { 308 + name: 'api_key', 309 + type: 'apiKey', 310 + }, 311 + ], 312 + url: '/store/inventory', 313 + ...options, 314 + }); 315 + } 316 + 317 + /** 318 + * Place an order for a pet. 319 + * Place a new order in the store. 320 + */ 321 + public placeOrder<ThrowOnError extends boolean = false>( 322 + options?: Options<PlaceOrderData, ThrowOnError>, 323 + ) { 324 + return (options?.client ?? _heyApiClient).post< 325 + PlaceOrderResponses, 326 + PlaceOrderErrors, 327 + ThrowOnError 328 + >({ 329 + url: '/store/order', 330 + ...options, 331 + headers: { 332 + 'Content-Type': 'application/json', 333 + ...options?.headers, 291 334 }, 292 - ], 293 - url: '/store/inventory', 294 - ...options, 295 - }); 335 + }); 336 + } 296 337 297 - /** 298 - * Place an order for a pet. 299 - * Place a new order in the store. 300 - */ 301 - export const placeOrder = <ThrowOnError extends boolean = false>( 302 - options?: Options<PlaceOrderData, ThrowOnError>, 303 - ) => 304 - (options?.client ?? _heyApiClient).post< 305 - PlaceOrderResponses, 306 - PlaceOrderErrors, 307 - ThrowOnError 308 - >({ 309 - url: '/store/order', 310 - ...options, 311 - headers: { 312 - 'Content-Type': 'application/json', 313 - ...options?.headers, 314 - }, 315 - }); 338 + /** 339 + * Delete purchase order by identifier. 340 + * For valid response try integer IDs with value < 1000. Anything above 1000 or non-integers will generate API errors. 341 + */ 342 + public deleteOrder<ThrowOnError extends boolean = false>( 343 + options: Options<DeleteOrderData, ThrowOnError>, 344 + ) { 345 + return (options.client ?? _heyApiClient).delete< 346 + DeleteOrderResponses, 347 + DeleteOrderErrors, 348 + ThrowOnError 349 + >({ 350 + url: '/store/order/{orderId}', 351 + ...options, 352 + }); 353 + } 316 354 317 - /** 318 - * Delete purchase order by identifier. 319 - * For valid response try integer IDs with value < 1000. Anything above 1000 or non-integers will generate API errors. 320 - */ 321 - export const deleteOrder = <ThrowOnError extends boolean = false>( 322 - options: Options<DeleteOrderData, ThrowOnError>, 323 - ) => 324 - (options.client ?? _heyApiClient).delete< 325 - DeleteOrderResponses, 326 - DeleteOrderErrors, 327 - ThrowOnError 328 - >({ 329 - url: '/store/order/{orderId}', 330 - ...options, 331 - }); 355 + /** 356 + * Find purchase order by ID. 357 + * For valid response try integer IDs with value <= 5 or > 10. Other values will generate exceptions. 358 + */ 359 + public getOrderById<ThrowOnError extends boolean = false>( 360 + options: Options<GetOrderByIdData, ThrowOnError>, 361 + ) { 362 + return (options.client ?? _heyApiClient).get< 363 + GetOrderByIdResponses, 364 + GetOrderByIdErrors, 365 + ThrowOnError 366 + >({ 367 + url: '/store/order/{orderId}', 368 + ...options, 369 + }); 370 + } 371 + } 332 372 333 - /** 334 - * Find purchase order by ID. 335 - * For valid response try integer IDs with value <= 5 or > 10. Other values will generate exceptions. 336 - */ 337 - export const getOrderById = <ThrowOnError extends boolean = false>( 338 - options: Options<GetOrderByIdData, ThrowOnError>, 339 - ) => 340 - (options.client ?? _heyApiClient).get< 341 - GetOrderByIdResponses, 342 - GetOrderByIdErrors, 343 - ThrowOnError 344 - >({ 345 - url: '/store/order/{orderId}', 346 - ...options, 347 - }); 373 + @Injectable({ 374 + providedIn: 'root', 375 + }) 376 + export class UserService { 377 + /** 378 + * Create user. 379 + * This can only be done by the logged in user. 380 + */ 381 + public createUser<ThrowOnError extends boolean = false>( 382 + options?: Options<CreateUserData, ThrowOnError>, 383 + ) { 384 + return (options?.client ?? _heyApiClient).post< 385 + CreateUserResponses, 386 + CreateUserErrors, 387 + ThrowOnError 388 + >({ 389 + url: '/user', 390 + ...options, 391 + headers: { 392 + 'Content-Type': 'application/json', 393 + ...options?.headers, 394 + }, 395 + }); 396 + } 348 397 349 - /** 350 - * Create user. 351 - * This can only be done by the logged in user. 352 - */ 353 - export const createUser = <ThrowOnError extends boolean = false>( 354 - options?: Options<CreateUserData, ThrowOnError>, 355 - ) => 356 - (options?.client ?? _heyApiClient).post< 357 - CreateUserResponses, 358 - CreateUserErrors, 359 - ThrowOnError 360 - >({ 361 - url: '/user', 362 - ...options, 363 - headers: { 364 - 'Content-Type': 'application/json', 365 - ...options?.headers, 366 - }, 367 - }); 398 + /** 399 + * Creates list of users with given input array. 400 + * Creates list of users with given input array. 401 + */ 402 + public createUsersWithListInput<ThrowOnError extends boolean = false>( 403 + options?: Options<CreateUsersWithListInputData, ThrowOnError>, 404 + ) { 405 + return (options?.client ?? _heyApiClient).post< 406 + CreateUsersWithListInputResponses, 407 + CreateUsersWithListInputErrors, 408 + ThrowOnError 409 + >({ 410 + url: '/user/createWithList', 411 + ...options, 412 + headers: { 413 + 'Content-Type': 'application/json', 414 + ...options?.headers, 415 + }, 416 + }); 417 + } 368 418 369 - /** 370 - * Creates list of users with given input array. 371 - * Creates list of users with given input array. 372 - */ 373 - export const createUsersWithListInput = <ThrowOnError extends boolean = false>( 374 - options?: Options<CreateUsersWithListInputData, ThrowOnError>, 375 - ) => 376 - (options?.client ?? _heyApiClient).post< 377 - CreateUsersWithListInputResponses, 378 - CreateUsersWithListInputErrors, 379 - ThrowOnError 380 - >({ 381 - url: '/user/createWithList', 382 - ...options, 383 - headers: { 384 - 'Content-Type': 'application/json', 385 - ...options?.headers, 386 - }, 387 - }); 419 + /** 420 + * Logs user into the system. 421 + * Log into the system. 422 + */ 423 + public loginUser<ThrowOnError extends boolean = false>( 424 + options?: Options<LoginUserData, ThrowOnError>, 425 + ) { 426 + return (options?.client ?? _heyApiClient).get< 427 + LoginUserResponses, 428 + LoginUserErrors, 429 + ThrowOnError 430 + >({ 431 + url: '/user/login', 432 + ...options, 433 + }); 434 + } 388 435 389 - /** 390 - * Logs user into the system. 391 - * Log into the system. 392 - */ 393 - export const loginUser = <ThrowOnError extends boolean = false>( 394 - options?: Options<LoginUserData, ThrowOnError>, 395 - ) => 396 - (options?.client ?? _heyApiClient).get< 397 - LoginUserResponses, 398 - LoginUserErrors, 399 - ThrowOnError 400 - >({ 401 - url: '/user/login', 402 - ...options, 403 - }); 436 + /** 437 + * Logs out current logged in user session. 438 + * Log user out of the system. 439 + */ 440 + public logoutUser<ThrowOnError extends boolean = false>( 441 + options?: Options<LogoutUserData, ThrowOnError>, 442 + ) { 443 + return (options?.client ?? _heyApiClient).get< 444 + LogoutUserResponses, 445 + LogoutUserErrors, 446 + ThrowOnError 447 + >({ 448 + url: '/user/logout', 449 + ...options, 450 + }); 451 + } 404 452 405 - /** 406 - * Logs out current logged in user session. 407 - * Log user out of the system. 408 - */ 409 - export const logoutUser = <ThrowOnError extends boolean = false>( 410 - options?: Options<LogoutUserData, ThrowOnError>, 411 - ) => 412 - (options?.client ?? _heyApiClient).get< 413 - LogoutUserResponses, 414 - LogoutUserErrors, 415 - ThrowOnError 416 - >({ 417 - url: '/user/logout', 418 - ...options, 419 - }); 453 + /** 454 + * Delete user resource. 455 + * This can only be done by the logged in user. 456 + */ 457 + public deleteUser<ThrowOnError extends boolean = false>( 458 + options: Options<DeleteUserData, ThrowOnError>, 459 + ) { 460 + return (options.client ?? _heyApiClient).delete< 461 + DeleteUserResponses, 462 + DeleteUserErrors, 463 + ThrowOnError 464 + >({ 465 + url: '/user/{username}', 466 + ...options, 467 + }); 468 + } 420 469 421 - /** 422 - * Delete user resource. 423 - * This can only be done by the logged in user. 424 - */ 425 - export const deleteUser = <ThrowOnError extends boolean = false>( 426 - options: Options<DeleteUserData, ThrowOnError>, 427 - ) => 428 - (options.client ?? _heyApiClient).delete< 429 - DeleteUserResponses, 430 - DeleteUserErrors, 431 - ThrowOnError 432 - >({ 433 - url: '/user/{username}', 434 - ...options, 435 - }); 436 - 437 - /** 438 - * Get user by user name. 439 - * Get user detail based on username. 440 - */ 441 - export const getUserByName = <ThrowOnError extends boolean = false>( 442 - options: Options<GetUserByNameData, ThrowOnError>, 443 - ) => 444 - (options.client ?? _heyApiClient).get< 445 - GetUserByNameResponses, 446 - GetUserByNameErrors, 447 - ThrowOnError 448 - >({ 449 - url: '/user/{username}', 450 - ...options, 451 - }); 470 + /** 471 + * Get user by user name. 472 + * Get user detail based on username. 473 + */ 474 + public getUserByName<ThrowOnError extends boolean = false>( 475 + options: Options<GetUserByNameData, ThrowOnError>, 476 + ) { 477 + return (options.client ?? _heyApiClient).get< 478 + GetUserByNameResponses, 479 + GetUserByNameErrors, 480 + ThrowOnError 481 + >({ 482 + url: '/user/{username}', 483 + ...options, 484 + }); 485 + } 452 486 453 - /** 454 - * Update user resource. 455 - * This can only be done by the logged in user. 456 - */ 457 - export const updateUser = <ThrowOnError extends boolean = false>( 458 - options: Options<UpdateUserData, ThrowOnError>, 459 - ) => 460 - (options.client ?? _heyApiClient).put< 461 - UpdateUserResponses, 462 - UpdateUserErrors, 463 - ThrowOnError 464 - >({ 465 - url: '/user/{username}', 466 - ...options, 467 - headers: { 468 - 'Content-Type': 'application/json', 469 - ...options.headers, 470 - }, 471 - }); 487 + /** 488 + * Update user resource. 489 + * This can only be done by the logged in user. 490 + */ 491 + public updateUser<ThrowOnError extends boolean = false>( 492 + options: Options<UpdateUserData, ThrowOnError>, 493 + ) { 494 + return (options.client ?? _heyApiClient).put< 495 + UpdateUserResponses, 496 + UpdateUserErrors, 497 + ThrowOnError 498 + >({ 499 + url: '/user/{username}', 500 + ...options, 501 + headers: { 502 + 'Content-Type': 'application/json', 503 + ...options.headers, 504 + }, 505 + }); 506 + } 507 + }
+8
packages/openapi-ts/src/plugins/@hey-api/sdk/config.ts
··· 7 7 config: { 8 8 asClass: false, 9 9 auth: true, 10 + classNameBuilder: '{{name}}', 10 11 classStructure: 'auto', 11 12 client: true, 12 13 exportFromIndex: true, ··· 81 82 plugin.config.asClass = true; 82 83 } else { 83 84 plugin.config.instance = false; 85 + } 86 + 87 + // Set default classNameBuilder based on client type 88 + if (plugin.config.classNameBuilder === '{{name}}') { 89 + if (plugin.config.client === '@hey-api/client-angular') { 90 + plugin.config.classNameBuilder = '{{name}}Service'; 91 + } 84 92 } 85 93 }, 86 94 };
+1 -1
packages/openapi-ts/src/plugins/@hey-api/sdk/plugin.ts
··· 197 197 const functionNode = tsc.methodDeclaration({ 198 198 accessLevel: 'public', 199 199 comment: createOperationComment({ operation }), 200 - isStatic: !plugin.config.instance, 200 + isStatic: isAngularClient ? false : !plugin.config.instance, 201 201 name: entry.methodName, 202 202 parameters: opParameters.parameters, 203 203 returnType: undefined,