Skip to content

Commit 5126685

Browse files
committed
feat: make base URL more easily overridable in the generated reference client
1 parent 8211946 commit 5126685

5 files changed

Lines changed: 25 additions & 16 deletions

File tree

__tests__/fixtures/openai/main.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* WARN: Do not edit directly.
55
*
6-
* Generated on 2023-04-28T12:24:57.414Z
6+
* Generated on 2023-04-29T03:20:30.391Z
77
*
88
*/
99
import {
@@ -122,9 +122,10 @@ export class OpenAiApiRestClient extends RestServiceClient<
122122
AllOutputs
123123
> {
124124
constructor(
125+
baseUrl = new URL('https://api.openai.com/v1/'),
125126
fetcher = createIsomorphicFetcher(),
126127
config?: RestServiceClientConfig,
127128
) {
128-
super(new URL('https://api.openai.com/v1/'), fetcher, config);
129+
super(baseUrl, fetcher, config);
129130
}
130131
}

__tests__/fixtures/petstore/main.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* WARN: Do not edit directly.
55
*
6-
* Generated on 2023-04-28T12:24:51.108Z
6+
* Generated on 2023-04-29T03:20:23.373Z
77
*
88
*/
99
import {
@@ -31,9 +31,10 @@ export class SwaggerPetstoreRestClient extends RestServiceClient<
3131
AllOutputs
3232
> {
3333
constructor(
34+
baseUrl = new URL('http://petstore.swagger.io/api/'),
3435
fetcher = createIsomorphicFetcher(),
3536
config?: RestServiceClientConfig,
3637
) {
37-
super(new URL('http://petstore.swagger.io/api/'), fetcher, config);
38+
super(baseUrl, fetcher, config);
3839
}
3940
}

__tests__/fixtures/test1/main.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* WARN: Do not edit directly.
55
*
6-
* Generated on 2023-04-28T12:24:53.479Z
6+
* Generated on 2023-04-29T03:20:25.967Z
77
*
88
*/
99
import {
@@ -78,9 +78,10 @@ export class BillingServiceRestApiRestClient extends RestServiceClient<
7878
AllOutputs
7979
> {
8080
constructor(
81+
baseUrl = new URL('https://api.example.com/'),
8182
fetcher = createIsomorphicFetcher(),
8283
config?: RestServiceClientConfig,
8384
) {
84-
super(new URL('https://api.example.com/'), fetcher, config);
85+
super(baseUrl, fetcher, config);
8586
}
8687
}

__tests__/index.test.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,29 @@ import { CreateModerationCommand } from './fixtures/openai/commands.js';
33
import { OpenAiApiRestClient } from './fixtures/openai/main.js';
44
import { FindPetsCommand } from './fixtures/petstore/commands.js';
55
import { SwaggerPetstoreRestClient } from './fixtures/petstore/main.js';
6-
import { logger } from './logger.js';
7-
8-
export const petStoreClient = new SwaggerPetstoreRestClient();
9-
export const openAiClient = new OpenAiApiRestClient();
106

117
test('PetStore FindPets', async () => {
8+
const petStoreClient = new SwaggerPetstoreRestClient(
9+
new URL('http://invalid'),
10+
);
1211
const command = new FindPetsCommand({
1312
limit: '10',
1413
tags: ['tag1', 'tag2'],
1514
});
1615

17-
const result = await petStoreClient.send(command).catch(logger.error);
16+
const result = await petStoreClient.json(command).catch((err) => err);
1817

1918
expect(result).toBeTruthy();
2019
});
2120

22-
test('PetStore FindPets', async () => {
21+
test('OpenAI CreateModeration', async () => {
22+
const openAiClient = new OpenAiApiRestClient(new URL('http://invalid'));
23+
2324
const command = new CreateModerationCommand({
2425
input: 'This is a test',
2526
});
2627

27-
const result = await openAiClient.send(command).catch(logger.error);
28+
const result = await openAiClient.json(command).catch((err) => err);
2829

2930
expect(result).toBeTruthy();
3031
});

lib/process-document.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,13 @@ export async function processOpenApiDocument(
675675

676676
const ctor = clientClassDeclaration.addConstructor();
677677

678+
const baseUrl = ctor.addParameter({
679+
name: 'baseUrl',
680+
initializer: `new URL('${new URL(
681+
`${schema.servers?.[0]?.url || 'https://api.example.com'}/`,
682+
)}')`,
683+
});
684+
678685
const fetcherParam = ctor.addParameter({
679686
name: 'fetcher',
680687
// type: fetcherMethodType,
@@ -694,9 +701,7 @@ export async function processOpenApiDocument(
694701
SyntaxKind.CallExpression,
695702
);
696703
callExpr?.addArguments([
697-
`new URL('${new URL(
698-
`${schema.servers?.[0]?.url || 'https://api.example.com'}/`,
699-
)}')`,
704+
baseUrl.getName(),
700705
fetcherParam.getName(),
701706
configParam.getName(),
702707
]);

0 commit comments

Comments
 (0)