Skip to content

Commit 34d598a

Browse files
committed
- Woking on httpntlm
1 parent 711d835 commit 34d598a

16 files changed

+200
-3
lines changed

bin/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ program
1212
.version(pkg.version)
1313
.requiredOption('-i, --input <value>', 'OpenAPI specification, can be a path, url or string content (required)')
1414
.requiredOption('-o, --output <value>', 'Output directory (required)')
15-
.option('-c, --client <value>', 'HTTP client to generate [fetch, xhr, node]', 'fetch')
15+
.option('-c, --client <value>', 'HTTP client to generate [fetch, xhr, node, httpntlm]', 'fetch')
1616
.option('--useOptions', 'Use options instead of arguments')
1717
.option('--useUnionTypes', 'Use union types instead of enums')
1818
.option('--exportCore <value>', 'Write core files to disk', true)

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@
9090
"express": "4.17.1",
9191
"form-data": "3.0.0",
9292
"glob": "7.1.6",
93+
"httpntlm": "1.7.6",
9394
"jest": "26.6.3",
9495
"jest-cli": "26.6.3",
9596
"node-fetch": "2.6.1",

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export enum HttpClient {
1111
FETCH = 'fetch',
1212
XHR = 'xhr',
1313
NODE = 'node',
14+
HTTPNTLM = 'httpntlm',
1415
}
1516

1617
export type Options = {

src/templates/core/OpenAPI.hbs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ type Config = {
1010
TOKEN?: string | Resolver<string>;
1111
USERNAME?: string | Resolver<string>;
1212
PASSWORD?: string | Resolver<string>;
13+
DOMAIN?: string | Resolver<string>;
1314
HEADERS?: Headers | Resolver<Headers>;
1415
}
1516

@@ -20,5 +21,6 @@ export const OpenAPI: Config = {
2021
TOKEN: undefined,
2122
USERNAME: undefined,
2223
PASSWORD: undefined,
24+
DOMAIN: undefined,
2325
HEADERS: undefined,
2426
};
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
async function getHeaders(options: ApiRequestOptions): Promise<Headers> {
2+
const headers = new Headers({
3+
Accept: 'application/json',
4+
...OpenAPI.HEADERS,
5+
...options.headers,
6+
});
7+
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)) {
13+
headers.append('Authorization', `Bearer ${token}`);
14+
}
15+
16+
if (isStringWithValue(username) && isStringWithValue(password)) {
17+
const credentials = Buffer.from(`${username}:${password}`).toString('base64');
18+
headers.append('Authorization', `Basic ${credentials}`);
19+
}
20+
21+
if (options.body) {
22+
if (isBinary(options.body)) {
23+
headers.append('Content-Type', 'application/octet-stream');
24+
} else if (isString(options.body)) {
25+
headers.append('Content-Type', 'text/plain');
26+
} else {
27+
headers.append('Content-Type', 'application/json');
28+
}
29+
}
30+
return headers;
31+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function getRequestBody(options: ApiRequestOptions): BodyInit | undefined {
2+
if (options.formData) {
3+
return getFormData(options.formData);
4+
}
5+
if (options.body) {
6+
if (isString(options.body) || isBinary(options.body)) {
7+
return options.body;
8+
} else {
9+
return JSON.stringify(options.body);
10+
}
11+
}
12+
return undefined;
13+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
async function getResponseBody(response: Response): Promise<any> {
2+
try {
3+
const contentType = response.headers.get('Content-Type');
4+
if (contentType) {
5+
const isJSON = contentType.toLowerCase().startsWith('application/json');
6+
if (isJSON) {
7+
return await response.json();
8+
} else {
9+
return await response.text();
10+
}
11+
}
12+
} catch (error) {
13+
console.error(error);
14+
}
15+
return null;
16+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
function getResponseHeader(response: Response, responseHeader?: string): string | null {
2+
if (responseHeader) {
3+
const content = response.headers.get(responseHeader);
4+
if (isString(content)) {
5+
return content;
6+
}
7+
}
8+
return null;
9+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
{{>header}}
2+
3+
import FormData from 'form-data';
4+
import fetch, { BodyInit, Headers, RequestInit, Response } from 'node-fetch';
5+
import { types } from 'util';
6+
7+
import { ApiError } from './ApiError';
8+
import type { ApiRequestOptions } from './ApiRequestOptions';
9+
import type { ApiResult } from './ApiResult';
10+
import { OpenAPI } from './OpenAPI';
11+
12+
{{>functions/isDefined}}
13+
14+
15+
{{>functions/isString}}
16+
17+
18+
{{>functions/isStringWithValue}}
19+
20+
21+
{{>functions/isBinary}}
22+
23+
24+
{{>functions/getQueryString}}
25+
26+
27+
{{>functions/getUrl}}
28+
29+
30+
{{>functions/getFormData}}
31+
32+
33+
{{>functions/resolve}}
34+
35+
36+
{{>httpntlm/getHeaders}}
37+
38+
39+
{{>httpntlm/getRequestBody}}
40+
41+
42+
{{>httpntlm/sendRequest}}
43+
44+
45+
{{>httpntlm/getResponseHeader}}
46+
47+
48+
{{>httpntlm/getResponseBody}}
49+
50+
51+
{{>functions/catchErrors}}
52+
53+
54+
/**
55+
* Request using node-fetch client
56+
* @param options The request options from the the service
57+
* @result ApiResult
58+
* @throws ApiError
59+
*/
60+
export async function request(options: ApiRequestOptions): Promise<ApiResult> {
61+
const url = getUrl(options);
62+
const response = await sendRequest(options, url);
63+
const responseBody = await getResponseBody(response);
64+
const responseHeader = getResponseHeader(response, options.responseHeader);
65+
66+
const result: ApiResult = {
67+
url,
68+
ok: response.ok,
69+
status: response.status,
70+
statusText: response.statusText,
71+
body: responseHeader || responseBody,
72+
};
73+
74+
catchErrors(options, result);
75+
return result;
76+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
async function sendRequest(options: ApiRequestOptions, url: string): Promise<Response> {
2+
const request: RequestInit = {
3+
method: options.method,
4+
headers: await getHeaders(options),
5+
body: getRequestBody(options),
6+
};
7+
return await fetch(url, request);
8+
}

0 commit comments

Comments
 (0)