mirror of
https://github.com/ferdikoomen/openapi-typescript-codegen.git
synced 2025-12-08 20:16:21 +00:00
- Fixes
This commit is contained in:
parent
34d598ac25
commit
4c1e74eb93
@ -90,7 +90,6 @@
|
||||
"express": "4.17.1",
|
||||
"form-data": "3.0.0",
|
||||
"glob": "7.1.6",
|
||||
"httpntlm": "1.7.6",
|
||||
"jest": "26.6.3",
|
||||
"jest-cli": "26.6.3",
|
||||
"node-fetch": "2.6.1",
|
||||
|
||||
@ -11,7 +11,6 @@ export enum HttpClient {
|
||||
FETCH = 'fetch',
|
||||
XHR = 'xhr',
|
||||
NODE = 'node',
|
||||
HTTPNTLM = 'httpntlm',
|
||||
}
|
||||
|
||||
export type Options = {
|
||||
|
||||
@ -50,7 +50,7 @@ import { OpenAPI } from './OpenAPI';
|
||||
/**
|
||||
* Request using fetch client
|
||||
* @param options The request options from the the service
|
||||
* @result ApiResult
|
||||
* @returns ApiResult
|
||||
* @throws ApiError
|
||||
*/
|
||||
export async function request(options: ApiRequestOptions): Promise<ApiResult> {
|
||||
|
||||
@ -4,5 +4,8 @@ async function sendRequest(options: ApiRequestOptions, url: string): Promise<Res
|
||||
headers: await getHeaders(options),
|
||||
body: getRequestBody(options),
|
||||
};
|
||||
if (OpenAPI.WITH_CREDENTIALS) {
|
||||
request.credentials = 'include';
|
||||
}
|
||||
return await fetch(url, request);
|
||||
}
|
||||
|
||||
@ -1,31 +0,0 @@
|
||||
async function getHeaders(options: ApiRequestOptions): Promise<Headers> {
|
||||
const headers = new Headers({
|
||||
Accept: 'application/json',
|
||||
...OpenAPI.HEADERS,
|
||||
...options.headers,
|
||||
});
|
||||
|
||||
const token = await resolve(OpenAPI.TOKEN);
|
||||
const username = await resolve(OpenAPI.USERNAME);
|
||||
const password = await resolve(OpenAPI.PASSWORD);
|
||||
|
||||
if (isStringWithValue(token)) {
|
||||
headers.append('Authorization', `Bearer ${token}`);
|
||||
}
|
||||
|
||||
if (isStringWithValue(username) && isStringWithValue(password)) {
|
||||
const credentials = Buffer.from(`${username}:${password}`).toString('base64');
|
||||
headers.append('Authorization', `Basic ${credentials}`);
|
||||
}
|
||||
|
||||
if (options.body) {
|
||||
if (isBinary(options.body)) {
|
||||
headers.append('Content-Type', 'application/octet-stream');
|
||||
} else if (isString(options.body)) {
|
||||
headers.append('Content-Type', 'text/plain');
|
||||
} else {
|
||||
headers.append('Content-Type', 'application/json');
|
||||
}
|
||||
}
|
||||
return headers;
|
||||
}
|
||||
@ -1,13 +0,0 @@
|
||||
function getRequestBody(options: ApiRequestOptions): BodyInit | undefined {
|
||||
if (options.formData) {
|
||||
return getFormData(options.formData);
|
||||
}
|
||||
if (options.body) {
|
||||
if (isString(options.body) || isBinary(options.body)) {
|
||||
return options.body;
|
||||
} else {
|
||||
return JSON.stringify(options.body);
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
@ -1,16 +0,0 @@
|
||||
async function getResponseBody(response: Response): Promise<any> {
|
||||
try {
|
||||
const contentType = response.headers.get('Content-Type');
|
||||
if (contentType) {
|
||||
const isJSON = contentType.toLowerCase().startsWith('application/json');
|
||||
if (isJSON) {
|
||||
return await response.json();
|
||||
} else {
|
||||
return await response.text();
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@ -1,9 +0,0 @@
|
||||
function getResponseHeader(response: Response, responseHeader?: string): string | null {
|
||||
if (responseHeader) {
|
||||
const content = response.headers.get(responseHeader);
|
||||
if (isString(content)) {
|
||||
return content;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@ -1,76 +0,0 @@
|
||||
{{>header}}
|
||||
|
||||
import FormData from 'form-data';
|
||||
import fetch, { BodyInit, Headers, RequestInit, Response } from 'node-fetch';
|
||||
import { types } from 'util';
|
||||
|
||||
import { ApiError } from './ApiError';
|
||||
import type { ApiRequestOptions } from './ApiRequestOptions';
|
||||
import type { ApiResult } from './ApiResult';
|
||||
import { OpenAPI } from './OpenAPI';
|
||||
|
||||
{{>functions/isDefined}}
|
||||
|
||||
|
||||
{{>functions/isString}}
|
||||
|
||||
|
||||
{{>functions/isStringWithValue}}
|
||||
|
||||
|
||||
{{>functions/isBinary}}
|
||||
|
||||
|
||||
{{>functions/getQueryString}}
|
||||
|
||||
|
||||
{{>functions/getUrl}}
|
||||
|
||||
|
||||
{{>functions/getFormData}}
|
||||
|
||||
|
||||
{{>functions/resolve}}
|
||||
|
||||
|
||||
{{>httpntlm/getHeaders}}
|
||||
|
||||
|
||||
{{>httpntlm/getRequestBody}}
|
||||
|
||||
|
||||
{{>httpntlm/sendRequest}}
|
||||
|
||||
|
||||
{{>httpntlm/getResponseHeader}}
|
||||
|
||||
|
||||
{{>httpntlm/getResponseBody}}
|
||||
|
||||
|
||||
{{>functions/catchErrors}}
|
||||
|
||||
|
||||
/**
|
||||
* Request using node-fetch client
|
||||
* @param options The request options from the the service
|
||||
* @result ApiResult
|
||||
* @throws ApiError
|
||||
*/
|
||||
export async function request(options: ApiRequestOptions): Promise<ApiResult> {
|
||||
const url = getUrl(options);
|
||||
const response = await sendRequest(options, url);
|
||||
const responseBody = await getResponseBody(response);
|
||||
const responseHeader = getResponseHeader(response, options.responseHeader);
|
||||
|
||||
const result: ApiResult = {
|
||||
url,
|
||||
ok: response.ok,
|
||||
status: response.status,
|
||||
statusText: response.statusText,
|
||||
body: responseHeader || responseBody,
|
||||
};
|
||||
|
||||
catchErrors(options, result);
|
||||
return result;
|
||||
}
|
||||
@ -1,8 +0,0 @@
|
||||
async function sendRequest(options: ApiRequestOptions, url: string): Promise<Response> {
|
||||
const request: RequestInit = {
|
||||
method: options.method,
|
||||
headers: await getHeaders(options),
|
||||
body: getRequestBody(options),
|
||||
};
|
||||
return await fetch(url, request);
|
||||
}
|
||||
@ -52,11 +52,11 @@ import { OpenAPI } from './OpenAPI';
|
||||
|
||||
|
||||
/**
|
||||
* Request using node-fetch client
|
||||
* @param options The request options from the the service
|
||||
* @result ApiResult
|
||||
* @throws ApiError
|
||||
*/
|
||||
* Request using node-fetch client
|
||||
* @param options The request options from the the service
|
||||
* @returns ApiResult
|
||||
* @throws ApiError
|
||||
*/
|
||||
export async function request(options: ApiRequestOptions): Promise<ApiResult> {
|
||||
const url = getUrl(options);
|
||||
const response = await sendRequest(options, url);
|
||||
|
||||
@ -4,5 +4,8 @@ async function sendRequest(options: ApiRequestOptions, url: string): Promise<Res
|
||||
headers: await getHeaders(options),
|
||||
body: getRequestBody(options),
|
||||
};
|
||||
if (OpenAPI.WITH_CREDENTIALS) {
|
||||
request.credentials = 'include';
|
||||
}
|
||||
return await fetch(url, request);
|
||||
}
|
||||
|
||||
@ -1,4 +1,3 @@
|
||||
{{#equals @root.httpClient 'fetch'}}{{>fetch/request}}{{/equals}}
|
||||
{{#equals @root.httpClient 'xhr'}}{{>xhr/request}}{{/equals}}
|
||||
{{#equals @root.httpClient 'node'}}{{>node/request}}{{/equals}}
|
||||
{{#equals @root.httpClient 'httpntml'}}{{>httpntml/request}}{{/equals}}
|
||||
{{~#equals @root.httpClient 'fetch'}}{{>fetch/request}}{{/equals~}}
|
||||
{{~#equals @root.httpClient 'xhr'}}{{>xhr/request}}{{/equals~}}
|
||||
{{~#equals @root.httpClient 'node'}}{{>node/request}}{{/equals~}}
|
||||
|
||||
@ -53,7 +53,7 @@ import { OpenAPI } from './OpenAPI';
|
||||
/**
|
||||
* Request using XHR client
|
||||
* @param options The request options from the the service
|
||||
* @result ApiResult
|
||||
* @returns ApiResult
|
||||
* @throws ApiError
|
||||
*/
|
||||
export async function request(options: ApiRequestOptions): Promise<ApiResult> {
|
||||
|
||||
@ -31,7 +31,7 @@ export class {{{name}}} {
|
||||
{{/if}}
|
||||
{{/unless}}
|
||||
{{#each results}}
|
||||
* @result {{{type}}} {{{description}}}
|
||||
* @returns {{{type}}} {{{description}}}
|
||||
{{/each}}
|
||||
* @throws ApiError
|
||||
*/
|
||||
|
||||
@ -26,12 +26,6 @@ import nodeGetResponseBody from '../templates/core/node/getResponseBody.hbs';
|
||||
import nodeGetResponseHeader from '../templates/core/node/getResponseHeader.hbs';
|
||||
import nodeRequest from '../templates/core/node/request.hbs';
|
||||
import nodeSendRequest from '../templates/core/node/sendRequest.hbs';
|
||||
import httpntlmGetHeaders from '../templates/core/httpntlm/getHeaders.hbs';
|
||||
import httpntlmGetRequestBody from '../templates/core/httpntlm/getRequestBody.hbs';
|
||||
import httpntlmGetResponseBody from '../templates/core/httpntlm/getResponseBody.hbs';
|
||||
import httpntlmGetResponseHeader from '../templates/core/httpntlm/getResponseHeader.hbs';
|
||||
import httpntlmRequest from '../templates/core/httpntlm/request.hbs';
|
||||
import httpntlmSendRequest from '../templates/core/httpntlm/sendRequest.hbs';
|
||||
import templateCoreSettings from '../templates/core/OpenAPI.hbs';
|
||||
import templateCoreRequest from '../templates/core/request.hbs';
|
||||
import xhrGetHeaders from '../templates/core/xhr/getHeaders.hbs';
|
||||
@ -179,13 +173,5 @@ export function registerHandlebarTemplates(): Templates {
|
||||
Handlebars.registerPartial('node/sendRequest', Handlebars.template(nodeSendRequest));
|
||||
Handlebars.registerPartial('node/request', Handlebars.template(nodeRequest));
|
||||
|
||||
// Specific files for the node client implementation
|
||||
Handlebars.registerPartial('httpntlm/getHeaders', Handlebars.template(httpntlmGetHeaders));
|
||||
Handlebars.registerPartial('httpntlm/getRequestBody', Handlebars.template(httpntlmGetRequestBody));
|
||||
Handlebars.registerPartial('httpntlm/getResponseBody', Handlebars.template(httpntlmGetResponseBody));
|
||||
Handlebars.registerPartial('httpntlm/getResponseHeader', Handlebars.template(httpntlmGetResponseHeader));
|
||||
Handlebars.registerPartial('httpntlm/sendRequest', Handlebars.template(httpntlmSendRequest));
|
||||
Handlebars.registerPartial('httpntlm/request', Handlebars.template(httpntlmRequest));
|
||||
|
||||
return templates;
|
||||
}
|
||||
|
||||
@ -6,7 +6,7 @@ async function generateV2() {
|
||||
await OpenAPI.generate({
|
||||
input: './test/spec/v2.json',
|
||||
output: './test/generated/v2/',
|
||||
httpClient: OpenAPI.HttpClient.HTTPNTLM,
|
||||
httpClient: OpenAPI.HttpClient.FETCH,
|
||||
useOptions: false,
|
||||
useUnionTypes: false,
|
||||
exportCore: true,
|
||||
@ -20,7 +20,7 @@ async function generateV3() {
|
||||
await OpenAPI.generate({
|
||||
input: './test/spec/v3.json',
|
||||
output: './test/generated/v3/',
|
||||
httpClient: OpenAPI.HttpClient.HTTPNTLM,
|
||||
httpClient: OpenAPI.HttpClient.FETCH,
|
||||
useOptions: false,
|
||||
useUnionTypes: false,
|
||||
exportCore: true,
|
||||
|
||||
1
types/index.d.ts
vendored
1
types/index.d.ts
vendored
@ -2,7 +2,6 @@ export declare enum HttpClient {
|
||||
FETCH = 'fetch',
|
||||
XHR = 'xhr',
|
||||
NODE = 'node',
|
||||
HTTPNTLM = 'httpntlm',
|
||||
}
|
||||
|
||||
export type Options = {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user