|
1 | | -// The following is an example file generated to demonstrate how to use your newly generated types |
2 | | -import { paths } from './types'; |
3 | | -import createClient from 'openapi-fetch'; |
| 1 | +/** |
| 2 | + * Example client demonstrating how to use the generated API types. |
| 3 | + * Adjust middlewares usage according to your need. |
| 4 | + * |
| 5 | + * - Check out Aligent's middlewares: https://github.com/aligent/microservice-development-utilities |
| 6 | + * - For more information about openapi-fetch middlewares: https://openapi-ts.dev/openapi-fetch/middleware-auth#middleware |
| 7 | + */ |
| 8 | +import { |
| 9 | + apiKeyAuthMiddleware, |
| 10 | + fetchSsmParams, |
| 11 | + retryMiddleware, |
| 12 | +} from '@aligent/microservice-util-lib'; |
| 13 | +import createClient, { Client, ClientOptions } from 'openapi-fetch'; |
| 14 | +import { paths } from './generated-types'; |
4 | 15 |
|
5 | | -// This type is an example of what is initialised using the types generated in the paths interface which is created when generation occurs. |
6 | | -// Its worth looking into that paths interface, to see the the types that were generated for your client. |
7 | | -type ExampleResponse = |
8 | | - paths['/customers']['get']['responses']['200']['content']['application/json']; |
| 16 | +export class <%= className %> { |
| 17 | + private credential: string | null = null; |
| 18 | + public readonly client: Client<paths, `${string}/${string}`>; |
9 | 19 |
|
10 | | -// Using openapi-fetch we can create a fully typed REST client by passing in paths as a generic. |
11 | | -// If you wish however, you can use any api client you want (axios, basic fetch etc.) and use the paths separately to maintain type safety in your client. |
12 | | -// Keep this variable exported. It will need to be to make sure all other services in your application can access and use the client. |
13 | | -export const client = createClient<paths>({ |
14 | | - baseUrl: '', |
15 | | - signal: AbortSignal.timeout(10000), |
16 | | -}); |
| 20 | + constructor(options: ClientOptions, credentialPath: string) { |
| 21 | + this.client = createClient<paths>(options); |
17 | 22 |
|
18 | | -// Client getters are then fully typed. Try deleting '/customers' and seeing what routes you can use! |
19 | | -const response = client.GET('/customers', { |
20 | | - params: { |
21 | | - query: {}, |
22 | | - }, |
23 | | -}); |
| 23 | + /** |
| 24 | + * The order in which middleware are registered matters. |
| 25 | + * - For requests, onRequest() will be called in the order registered |
| 26 | + * - For responses, onResponse() will be called in reverse order. |
| 27 | + */ |
| 28 | + this.client.use( |
| 29 | + apiKeyAuthMiddleware({ |
| 30 | + header: 'Authorization', |
| 31 | + value: async () => { |
| 32 | + if (!this.credential) { |
| 33 | + const param = await fetchSsmParams(credentialPath); |
| 34 | + if (!param?.Value) { |
| 35 | + throw new Error('Unable to fetch API client credential'); |
| 36 | + } |
| 37 | + |
| 38 | + this.credential = param.Value; |
| 39 | + } |
| 40 | + |
| 41 | + return `Bearer ${this.credential}`; |
| 42 | + }, |
| 43 | + }) |
| 44 | + ); |
| 45 | + |
| 46 | + this.client.use( |
| 47 | + retryMiddleware({ |
| 48 | + onRetry: ({ attempt, error }) => |
| 49 | + console.log(`Retrying...${attempt} due to ${String(error)}`), |
| 50 | + }) |
| 51 | + ); |
| 52 | + } |
| 53 | +} |
0 commit comments