Skip to content

Commit fb6d48b

Browse files
committed
globalHeaders
1 parent 3a9fca2 commit fb6d48b

File tree

10 files changed

+36
-15
lines changed

10 files changed

+36
-15
lines changed

bin/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ program
1111
.option('-i, --input <value>', 'Path to swagger specification', './spec.json')
1212
.option('-o, --output <value>', 'Output directory', './generated')
1313
.option('-c, --client <value>', 'HTTP client to generate [fetch, xhr, node-fetch]', 'fetch')
14+
.option('--globalHeaders <value>', 'Common HTTP headers')
1415
.option('--useOptions', 'Use options vs arguments style functions')
1516
.option('--useUnionTypes', 'Use inclusive union types')
1617
.option('--exportCore <value>', 'Generate core', true)
@@ -26,6 +27,7 @@ if (OpenAPI) {
2627
input: program.input,
2728
output: program.output,
2829
httpClient: program.client,
30+
globalHeaders: program.globalHeaders ? JSON.parse(program.globalHeaders) : {},
2931
useOptions: program.useOptions,
3032
useUnionTypes: program.useUnionTypes,
3133
exportCore: JSON.parse(program.exportCore) === true,

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@
6363
"js-yaml": "3.14.0",
6464
"mkdirp": "1.0.4",
6565
"path": "0.12.7",
66-
"rimraf": "3.0.2"
66+
"rimraf": "3.0.2",
67+
"stringify-object": "^3.3.0"
6768
},
6869
"devDependencies": {
6970
"@babel/core": "7.11.0",
@@ -76,6 +77,7 @@
7677
"@types/mkdirp": "1.0.1",
7778
"@types/node": "14.0.27",
7879
"@types/rimraf": "3.0.0",
80+
"@types/stringify-object": "^3.3.0",
7981
"@typescript-eslint/eslint-plugin": "3.7.1",
8082
"@typescript-eslint/parser": "3.7.1",
8183
"codecov": "3.7.2",

src/index.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@ import { writeClient } from './utils/writeClient';
1010
export enum HttpClient {
1111
FETCH = 'fetch',
1212
XHR = 'xhr',
13-
NODE_FETCH = 'node-fetch'
13+
NODE_FETCH = 'node-fetch',
1414
}
1515

1616
export interface Options {
1717
input: string | Record<string, any>;
1818
output: string;
1919
httpClient?: HttpClient;
20+
globalHeaders?: { [key: string]: any };
2021
useOptions?: boolean;
2122
useUnionTypes?: boolean;
2223
exportCore?: boolean;
@@ -33,6 +34,7 @@ export interface Options {
3334
* @param input The relative ___location of the OpenAPI spec.
3435
* @param output The relative ___location of the output directory.
3536
* @param httpClient The selected httpClient (fetch or XHR).
37+
* @param globalHeaders Common HTTP headers
3638
* @param useOptions Use options or arguments functions.
3739
* @param useUnionTypes Use inclusive union types.
3840
* @param exportCore: Generate core client classes.
@@ -45,6 +47,7 @@ export async function generate({
4547
input,
4648
output,
4749
httpClient = HttpClient.FETCH,
50+
globalHeaders = {},
4851
useOptions = false,
4952
useUnionTypes = false,
5053
exportCore = true,
@@ -64,7 +67,7 @@ export async function generate({
6467
const client = parseV2(openApi);
6568
const clientFinal = postProcessClient(client, useUnionTypes);
6669
if (write) {
67-
await writeClient(clientFinal, templates, output, httpClient, useOptions, exportCore, exportServices, exportModels, exportSchemas);
70+
await writeClient(clientFinal, templates, output, httpClient, globalHeaders, useOptions, exportCore, exportServices, exportModels, exportSchemas);
6871
}
6972
break;
7073
}
@@ -73,7 +76,7 @@ export async function generate({
7376
const client = parseV3(openApi);
7477
const clientFinal = postProcessClient(client, useUnionTypes);
7578
if (write) {
76-
await writeClient(clientFinal, templates, output, httpClient, useOptions, exportCore, exportServices, exportModels, exportSchemas);
79+
await writeClient(clientFinal, templates, output, httpClient, globalHeaders, useOptions, exportCore, exportServices, exportModels, exportSchemas);
7780
}
7881
break;
7982
}

src/templates/core/OpenAPI.hbs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ interface Config {
99
CLIENT: 'fetch' | 'xhr' | 'node-fetch';
1010
WITH_CREDENTIALS: boolean;
1111
TOKEN: string;
12+
WITH_HEADERS: { [key: string]: any };
1213
}
1314

1415
export const OpenAPI: Config = {
@@ -17,4 +18,5 @@ export const OpenAPI: Config = {
1718
CLIENT: '{{{httpClient}}}',
1819
WITH_CREDENTIALS: false,
1920
TOKEN: '',
21+
WITH_HEADERS: {{{globalHeaders}}}
2022
};

src/templates/core/request.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export async function request(options: Readonly<RequestOptions>): Promise<Result
2828
// Create request headers
2929
const headers = new H({
3030
...options.headers,
31+
...OpenAPI.WITH_HEADERS,
3132
Accept: 'application/json',
3233
});
3334

src/utils/writeClient.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,15 @@ describe('writeClient', () => {
1616
};
1717

1818
const templates: Templates = {
19+
servicesIndex: () => 'dummy',
1920
index: () => 'dummy',
2021
model: () => 'dummy',
2122
schema: () => 'dummy',
2223
service: () => 'dummy',
2324
settings: () => 'dummy',
2425
};
2526

26-
await writeClient(client, templates, '/', HttpClient.FETCH, false, true, true, true, true);
27+
await writeClient(client, templates, '/', HttpClient.FETCH, {}, false, true, true, true, true);
2728

2829
expect(rmdir).toBeCalled();
2930
expect(mkdir).toBeCalled();

src/utils/writeClient.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export async function writeClient(
3232
templates: Templates,
3333
output: string,
3434
httpClient: HttpClient,
35+
globalHeaders: { [key: string]: any },
3536
useOptions: boolean,
3637
exportCore: boolean,
3738
exportServices: boolean,
@@ -64,9 +65,9 @@ export async function writeClient(
6465

6566
if (exportServices) {
6667
await mkdir(outputPathServices);
67-
await writeClientSettings(client, templates, outputPathCore, httpClient);
68+
await writeClientSettings(client, templates, outputPathCore, httpClient, globalHeaders);
6869
await writeClientServices(client.services, templates, outputPathServices, useOptions);
69-
await writeClientServicesIndex(client.services, templates, outputPathServices)
70+
await writeClientServicesIndex(client.services, templates, outputPathServices);
7071
}
7172

7273
if (exportSchemas) {

src/utils/writeClientServicesIndex.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,8 @@ import { Templates } from './registerHandlebarTemplates';
77

88
export async function writeClientServicesIndex(services: Service[], templates: Templates, outputPath: string): Promise<void> {
99
const templateResult = templates.servicesIndex({
10-
services
10+
services,
1111
});
12-
13-
await writeFile(
14-
path.resolve(outputPath, 'index.ts'),
15-
format(templateResult)
16-
);
12+
13+
await writeFile(path.resolve(outputPath, 'index.ts'), format(templateResult));
1714
}

src/utils/writeClientSettings.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,15 @@ describe('writeClientSettings', () => {
1616
};
1717

1818
const templates: Templates = {
19+
servicesIndex: () => 'dummy',
1920
index: () => 'dummy',
2021
model: () => 'dummy',
2122
schema: () => 'dummy',
2223
service: () => 'dummy',
2324
settings: () => 'dummy',
2425
};
2526

26-
await writeClientSettings(client, templates, '/', HttpClient.FETCH);
27+
await writeClientSettings(client, templates, '/', HttpClient.FETCH, {});
2728

2829
expect(writeFile).toBeCalledWith('/OpenAPI.ts', 'dummy');
2930
});

src/utils/writeClientSettings.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,33 @@
11
import * as path from 'path';
2+
import stringifyObject from 'stringify-object';
23

34
import { Client } from '../client/interfaces/Client';
45
import { HttpClient } from '../index';
56
import { writeFile } from './fileSystem';
67
import { Templates } from './registerHandlebarTemplates';
78

9+
const indent = ' ';
10+
811
/**
912
* Generate OpenAPI configuration file "OpenAPI.ts"
1013
* @param client Client object, containing, models, schemas and services.
1114
* @param templates The loaded handlebar templates.
1215
* @param outputPath Directory to write the generated files to.
1316
* @param httpClient The selected httpClient (fetch or XHR).
17+
* @param globalHeaders Common HTTP headers
1418
*/
15-
export async function writeClientSettings(client: Client, templates: Templates, outputPath: string, httpClient: HttpClient): Promise<void> {
19+
export async function writeClientSettings(client: Client, templates: Templates, outputPath: string, httpClient: HttpClient, globalHeaders: { [key: string]: any }): Promise<void> {
1620
await writeFile(
1721
path.resolve(outputPath, 'OpenAPI.ts'),
1822
templates.settings({
1923
httpClient,
24+
globalHeaders: stringifyObject(
25+
globalHeaders,
26+
{
27+
indent,
28+
},
29+
indent
30+
),
2031
server: client.server,
2132
version: client.version,
2233
})

0 commit comments

Comments
 (0)