File tree Expand file tree Collapse file tree 17 files changed +193
-39
lines changed Expand file tree Collapse file tree 17 files changed +193
-39
lines changed Original file line number Diff line number Diff line change 1
1
{{> header }}
2
2
3
+ type Resolver<T > = () => Promise<T >;
4
+ type Headers = Record<string , string>;
5
+
3
6
interface Config {
4
7
BASE: string;
5
8
VERSION: string;
6
9
WITH_CREDENTIALS: boolean;
7
- TOKEN: string | (() => Promise<string >);
10
+ TOKEN?: string | Resolver<string >;
11
+ USERNAME?: string | Resolver<string >;
12
+ PASSWORD?: string | Resolver<string >;
13
+ HEADERS?: Headers | Resolver<Headers >;
8
14
}
9
15
10
16
export const OpenAPI: Config = {
11
17
BASE: '{{{ server }}} ',
12
18
VERSION: '{{{ version }}} ',
13
19
WITH_CREDENTIALS: false,
14
- TOKEN: '',
20
+ TOKEN: undefined,
21
+ USERNAME: undefined,
22
+ PASSWORD: undefined,
23
+ HEADERS: undefined,
15
24
};
Original file line number Diff line number Diff line change 1
1
async function getHeaders(options: ApiRequestOptions): Promise<Headers > {
2
2
const headers = new Headers({
3
3
Accept: 'application/json',
4
+ ...OpenAPI.HEADERS,
4
5
...options.headers,
5
6
});
6
7
7
- const token = await getToken();
8
- if (isDefined(token) && token !== '') {
8
+ const token = await resolve(OpenAPI.TOKEN);
9
+ const username = await resolve(OpenAPI.USERNAME);
10
+ const password = await resolve(OpenAPI.PASSWORD);
11
+
12
+ if (isStringWithValue(token)) {
9
13
headers.append('Authorization', `Bearer ${token}`);
10
14
}
11
15
16
+ if (isStringWithValue(username) && isStringWithValue(password)) {
17
+ const credentials = btoa(`${username}:${password}`);
18
+ headers.append('Authorization', `Basic ${credentials}`);
19
+ }
20
+
12
21
if (options.body) {
13
22
if (isBlob(options.body)) {
14
23
headers.append('Content-Type', options.body.type || 'application/octet-stream');
Original file line number Diff line number Diff line change @@ -11,6 +11,9 @@ import { OpenAPI } from './OpenAPI';
11
11
{{> functions/isString }}
12
12
13
13
14
+ {{> functions/isStringWithValue }}
15
+
16
+
14
17
{{> functions/isBlob }}
15
18
16
19
@@ -23,7 +26,7 @@ import { OpenAPI } from './OpenAPI';
23
26
{{> functions/getFormData }}
24
27
25
28
26
- {{> functions/getToken }}
29
+ {{> functions/resolve }}
27
30
28
31
29
32
{{> fetch/getHeaders }}
Load Diff This file was deleted.
Original file line number Diff line number Diff line change
1
+ function isStringWithValue(value: any): value is string {
2
+ return isString(value) && value !== '';
3
+ }
Original file line number Diff line number Diff line change
1
+ type Resolver<T > = () => Promise<T >;
2
+
3
+ async function resolve<T >(resolver?: T | Resolver<T >): Promise<T | undefined> {
4
+ if (typeof resolver === 'function') {
5
+ return (resolver as Resolver<T >)();
6
+ }
7
+ return resolver;
8
+ }
Original file line number Diff line number Diff line change 1
1
async function getHeaders(options: ApiRequestOptions): Promise<Headers > {
2
2
const headers = new Headers({
3
3
Accept: 'application/json',
4
+ ...OpenAPI.HEADERS,
4
5
...options.headers,
5
6
});
6
7
7
- const token = await getToken();
8
- if (isDefined(token) && token !== '') {
8
+ const token = await resolve(OpenAPI.TOKEN);
9
+ const username = await resolve(OpenAPI.USERNAME);
10
+ const password = await resolve(OpenAPI.PASSWORD);
11
+
12
+ if (isStringWithValue(token)) {
9
13
headers.append('Authorization', `Bearer ${token}`);
10
14
}
11
15
16
+ if (isStringWithValue(username) && isStringWithValue(password)) {
17
+ const credentials = Buffer.from(`${username}:${password}`).toString('base64');
18
+ headers.append('Authorization', `Basic ${credentials}`);
19
+ }
20
+
12
21
if (options.body) {
13
22
if (isBinary(options.body)) {
14
23
headers.append('Content-Type', 'application/octet-stream');
Original file line number Diff line number Diff line change @@ -15,6 +15,9 @@ import { OpenAPI } from './OpenAPI';
15
15
{{> functions/isString }}
16
16
17
17
18
+ {{> functions/isStringWithValue }}
19
+
20
+
18
21
{{> functions/isBinary }}
19
22
20
23
@@ -27,7 +30,7 @@ import { OpenAPI } from './OpenAPI';
27
30
{{> functions/getFormData }}
28
31
29
32
30
- {{> functions/getToken }}
33
+ {{> functions/resolve }}
31
34
32
35
33
36
{{> node/getHeaders }}
Original file line number Diff line number Diff line change 1
1
async function getHeaders(options: ApiRequestOptions): Promise<Headers > {
2
2
const headers = new Headers({
3
3
Accept: 'application/json',
4
+ ...OpenAPI.HEADERS,
4
5
...options.headers,
5
6
});
6
7
7
- const token = await getToken();
8
- if (isDefined(token) && token !== '') {
8
+ const token = await resolve(OpenAPI.TOKEN);
9
+ const username = await resolve(OpenAPI.USERNAME);
10
+ const password = await resolve(OpenAPI.PASSWORD);
11
+
12
+ if (isStringWithValue(token)) {
9
13
headers.append('Authorization', `Bearer ${token}`);
10
14
}
11
15
16
+ if (isStringWithValue(username) && isStringWithValue(password)) {
17
+ const credentials = btoa(`${username}:${password}`);
18
+ headers.append('Authorization', `Basic ${credentials}`);
19
+ }
20
+
12
21
if (options.body) {
13
22
if (isBlob(options.body)) {
14
23
headers.append('Content-Type', options.body.type || 'application/octet-stream');
Original file line number Diff line number Diff line change @@ -11,6 +11,9 @@ import { OpenAPI } from './OpenAPI';
11
11
{{> functions/isString }}
12
12
13
13
14
+ {{> functions/isStringWithValue }}
15
+
16
+
14
17
{{> functions/isBlob }}
15
18
16
19
@@ -26,7 +29,7 @@ import { OpenAPI } from './OpenAPI';
26
29
{{> functions/getFormData }}
27
30
28
31
29
- {{> functions/getToken }}
32
+ {{> functions/resolve }}
30
33
31
34
32
35
{{> fetch/getHeaders }}
You can’t perform that action at this time.
0 commit comments