mirror of
https://github.com/ferdikoomen/openapi-typescript-codegen.git
synced 2025-12-08 20:16:21 +00:00
- Latest dependencies
This commit is contained in:
parent
cf45ccd2e5
commit
5d52e3e51c
@ -90,7 +90,7 @@
|
||||
"@typescript-eslint/parser": "4.2.0",
|
||||
"codecov": "3.7.2",
|
||||
"eslint": "7.9.0",
|
||||
"eslint-config-prettier": "6.11.0",
|
||||
"eslint-config-prettier": "6.12.0",
|
||||
"eslint-plugin-prettier": "3.1.4",
|
||||
"eslint-plugin-simple-import-sort": "5.0.3",
|
||||
"express": "4.17.1",
|
||||
@ -101,7 +101,7 @@
|
||||
"puppeteer": "5.3.1",
|
||||
"rollup": "2.28.2",
|
||||
"rollup-plugin-terser": "7.0.2",
|
||||
"rollup-plugin-typescript2": "0.27.2",
|
||||
"rollup-plugin-typescript2": "0.27.3",
|
||||
"typescript": "4.0.3"
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,3 +1,5 @@
|
||||
{{>header}}
|
||||
|
||||
import { ApiError } from './ApiError';
|
||||
import { ApiRequestOptions } from './ApiRequestOptions';
|
||||
import { ApiResult } from './ApiResult';
|
||||
@ -5,28 +7,37 @@ import { OpenAPI } from './OpenAPI';
|
||||
|
||||
{{>functions/isDefined}}
|
||||
|
||||
|
||||
{{>functions/getQueryString}}
|
||||
|
||||
|
||||
{{>functions/getUrl}}
|
||||
|
||||
|
||||
{{>functions/getFormData}}
|
||||
|
||||
|
||||
{{>functions/getHeaders}}
|
||||
|
||||
|
||||
{{>fetch/getRequestBody}}
|
||||
|
||||
|
||||
{{>fetch/sendRequest}}
|
||||
|
||||
|
||||
{{>fetch/getResponseHeader}}
|
||||
|
||||
|
||||
{{>fetch/getResponseBody}}
|
||||
|
||||
|
||||
{{>functions/catchErrors}}
|
||||
|
||||
|
||||
/**
|
||||
* Request using fetch
|
||||
* @param options Request options
|
||||
* Request using fetch client
|
||||
* @param options The request options from the the service
|
||||
* @result ApiResult
|
||||
* @throws ApiError
|
||||
*/
|
||||
|
||||
@ -3,7 +3,7 @@ function getUrl(options: ApiRequestOptions): string {
|
||||
const url = `${OpenAPI.BASE}${path}`;
|
||||
|
||||
if (options.query) {
|
||||
return url + getQueryString(options.query);
|
||||
return `${url}${getQueryString(options.query)}`;
|
||||
}
|
||||
return url;
|
||||
}
|
||||
|
||||
@ -1,3 +1,5 @@
|
||||
{{>header}}
|
||||
|
||||
import { ApiError } from './ApiError';
|
||||
import { ApiRequestOptions } from './ApiRequestOptions';
|
||||
import { ApiResult } from './ApiResult';
|
||||
@ -7,31 +9,40 @@ import * as FormData from 'form-data';
|
||||
|
||||
{{>functions/isDefined}}
|
||||
|
||||
|
||||
{{>functions/getQueryString}}
|
||||
|
||||
|
||||
{{>functions/getUrl}}
|
||||
|
||||
|
||||
{{>functions/getFormData}}
|
||||
|
||||
|
||||
{{>functions/getHeaders}}
|
||||
|
||||
|
||||
{{>node/getRequestBody}}
|
||||
|
||||
|
||||
{{>node/sendRequest}}
|
||||
|
||||
|
||||
{{>node/getResponseHeader}}
|
||||
|
||||
|
||||
{{>node/getResponseBody}}
|
||||
|
||||
|
||||
{{>functions/catchErrors}}
|
||||
|
||||
|
||||
/**
|
||||
* Request using node-fetch
|
||||
* @param options Request options
|
||||
* @result ApiResult
|
||||
* @throws ApiError
|
||||
*/
|
||||
* 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);
|
||||
|
||||
@ -1,5 +1,3 @@
|
||||
{{>header}}
|
||||
|
||||
{{#equals httpClient 'fetch'}}
|
||||
{{>fetch/request}}
|
||||
{{/equals}}
|
||||
|
||||
@ -1,3 +1,5 @@
|
||||
{{>header}}
|
||||
|
||||
import { ApiError } from './ApiError';
|
||||
import { ApiRequestOptions } from './ApiRequestOptions';
|
||||
import { ApiResult } from './ApiResult';
|
||||
@ -5,29 +7,40 @@ import { OpenAPI } from './OpenAPI';
|
||||
|
||||
{{>functions/isDefined}}
|
||||
|
||||
|
||||
{{>functions/isSuccess}}
|
||||
|
||||
|
||||
{{>functions/getQueryString}}
|
||||
|
||||
|
||||
{{>functions/getUrl}}
|
||||
|
||||
|
||||
{{>functions/getFormData}}
|
||||
|
||||
|
||||
{{>functions/getHeaders}}
|
||||
|
||||
|
||||
{{>xhr/getRequestBody}}
|
||||
|
||||
|
||||
{{>xhr/sendRequest}}
|
||||
|
||||
|
||||
{{>xhr/getResponseHeader}}
|
||||
|
||||
|
||||
{{>xhr/getResponseBody}}
|
||||
|
||||
|
||||
{{>functions/catchErrors}}
|
||||
|
||||
|
||||
/**
|
||||
* Request using XHR
|
||||
* @param options Request options
|
||||
* Request using XHR client
|
||||
* @param options The request options from the the service
|
||||
* @result ApiResult
|
||||
* @throws ApiError
|
||||
*/
|
||||
|
||||
@ -86,6 +86,7 @@ import { OpenAPI } from './OpenAPI';
|
||||
function isDefined<T>(value: T | null | undefined): value is Exclude<T, null | undefined> {
|
||||
return value !== undefined && value !== null;
|
||||
}
|
||||
|
||||
function getQueryString(params: Record<string, any>): string {
|
||||
const qs: string[] = [];
|
||||
Object.keys(params).forEach(key => {
|
||||
@ -105,15 +106,17 @@ function getQueryString(params: Record<string, any>): string {
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
function getUrl(options: ApiRequestOptions): string {
|
||||
const path = options.path.replace(/[:]/g, '_');
|
||||
const url = \`\${OpenAPI.BASE}\${path}\`;
|
||||
|
||||
if (options.query) {
|
||||
return url + getQueryString(options.query);
|
||||
return \`\${url}\${getQueryString(options.query)}\`;
|
||||
}
|
||||
return url;
|
||||
}
|
||||
|
||||
function getFormData(params: Record<string, any>): FormData {
|
||||
const formData = new FormData();
|
||||
Object.keys(params).forEach(key => {
|
||||
@ -124,6 +127,7 @@ function getFormData(params: Record<string, any>): FormData {
|
||||
});
|
||||
return formData;
|
||||
}
|
||||
|
||||
function getHeaders(options: ApiRequestOptions): Headers {
|
||||
const headers = new Headers({
|
||||
Accept: 'application/json',
|
||||
@ -147,6 +151,7 @@ function getHeaders(options: ApiRequestOptions): Headers {
|
||||
}
|
||||
return headers;
|
||||
}
|
||||
|
||||
function getRequestBody(options: ApiRequestOptions): BodyInit | undefined {
|
||||
if (options.formData) {
|
||||
return getFormData(options.formData);
|
||||
@ -162,6 +167,7 @@ function getRequestBody(options: ApiRequestOptions): BodyInit | undefined {
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
async function sendRequest(options: ApiRequestOptions, url: string): Promise<Response> {
|
||||
const request: RequestInit = {
|
||||
method: options.method,
|
||||
@ -170,6 +176,7 @@ async function sendRequest(options: ApiRequestOptions, url: string): Promise<Res
|
||||
};
|
||||
return await fetch(url, request);
|
||||
}
|
||||
|
||||
function getResponseHeader(response: Response, responseHeader?: string): string | null {
|
||||
if (responseHeader) {
|
||||
const content = response.headers.get(responseHeader);
|
||||
@ -179,6 +186,7 @@ function getResponseHeader(response: Response, responseHeader?: string): string
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
async function getResponseBody(response: Response): Promise<any> {
|
||||
try {
|
||||
const contentType = response.headers.get('Content-Type');
|
||||
@ -196,6 +204,7 @@ async function getResponseBody(response: Response): Promise<any> {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function catchErrors(options: ApiRequestOptions, result: ApiResult): void {
|
||||
const errors: Record<number, string> = {
|
||||
400: 'Bad Request',
|
||||
@ -219,8 +228,8 @@ function catchErrors(options: ApiRequestOptions, result: ApiResult): void {
|
||||
}
|
||||
|
||||
/**
|
||||
* Request using fetch
|
||||
* @param options Request options
|
||||
* Request using fetch client
|
||||
* @param options The request options from the the service
|
||||
* @result ApiResult
|
||||
* @throws ApiError
|
||||
*/
|
||||
@ -2108,6 +2117,7 @@ import { OpenAPI } from './OpenAPI';
|
||||
function isDefined<T>(value: T | null | undefined): value is Exclude<T, null | undefined> {
|
||||
return value !== undefined && value !== null;
|
||||
}
|
||||
|
||||
function getQueryString(params: Record<string, any>): string {
|
||||
const qs: string[] = [];
|
||||
Object.keys(params).forEach(key => {
|
||||
@ -2127,15 +2137,17 @@ function getQueryString(params: Record<string, any>): string {
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
function getUrl(options: ApiRequestOptions): string {
|
||||
const path = options.path.replace(/[:]/g, '_');
|
||||
const url = \`\${OpenAPI.BASE}\${path}\`;
|
||||
|
||||
if (options.query) {
|
||||
return url + getQueryString(options.query);
|
||||
return \`\${url}\${getQueryString(options.query)}\`;
|
||||
}
|
||||
return url;
|
||||
}
|
||||
|
||||
function getFormData(params: Record<string, any>): FormData {
|
||||
const formData = new FormData();
|
||||
Object.keys(params).forEach(key => {
|
||||
@ -2146,6 +2158,7 @@ function getFormData(params: Record<string, any>): FormData {
|
||||
});
|
||||
return formData;
|
||||
}
|
||||
|
||||
function getHeaders(options: ApiRequestOptions): Headers {
|
||||
const headers = new Headers({
|
||||
Accept: 'application/json',
|
||||
@ -2169,6 +2182,7 @@ function getHeaders(options: ApiRequestOptions): Headers {
|
||||
}
|
||||
return headers;
|
||||
}
|
||||
|
||||
function getRequestBody(options: ApiRequestOptions): BodyInit | undefined {
|
||||
if (options.formData) {
|
||||
return getFormData(options.formData);
|
||||
@ -2184,6 +2198,7 @@ function getRequestBody(options: ApiRequestOptions): BodyInit | undefined {
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
async function sendRequest(options: ApiRequestOptions, url: string): Promise<Response> {
|
||||
const request: RequestInit = {
|
||||
method: options.method,
|
||||
@ -2192,6 +2207,7 @@ async function sendRequest(options: ApiRequestOptions, url: string): Promise<Res
|
||||
};
|
||||
return await fetch(url, request);
|
||||
}
|
||||
|
||||
function getResponseHeader(response: Response, responseHeader?: string): string | null {
|
||||
if (responseHeader) {
|
||||
const content = response.headers.get(responseHeader);
|
||||
@ -2201,6 +2217,7 @@ function getResponseHeader(response: Response, responseHeader?: string): string
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
async function getResponseBody(response: Response): Promise<any> {
|
||||
try {
|
||||
const contentType = response.headers.get('Content-Type');
|
||||
@ -2218,6 +2235,7 @@ async function getResponseBody(response: Response): Promise<any> {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function catchErrors(options: ApiRequestOptions, result: ApiResult): void {
|
||||
const errors: Record<number, string> = {
|
||||
400: 'Bad Request',
|
||||
@ -2241,8 +2259,8 @@ function catchErrors(options: ApiRequestOptions, result: ApiResult): void {
|
||||
}
|
||||
|
||||
/**
|
||||
* Request using fetch
|
||||
* @param options Request options
|
||||
* Request using fetch client
|
||||
* @param options The request options from the the service
|
||||
* @result ApiResult
|
||||
* @throws ApiError
|
||||
*/
|
||||
|
||||
34
yarn.lock
34
yarn.lock
@ -1178,9 +1178,9 @@
|
||||
"@types/babel__traverse" "*"
|
||||
|
||||
"@types/babel__generator@*":
|
||||
version "7.6.1"
|
||||
resolved "https://registry.yarnpkg.com/@types/babel__generator/-/babel__generator-7.6.1.tgz#4901767b397e8711aeb99df8d396d7ba7b7f0e04"
|
||||
integrity sha512-bBKm+2VPJcMRVwNhxKu8W+5/zT7pwNEqeokFOmbvVSqGzFneNxYcEBro9Ac7/N9tlsaPYnZLK8J1LWKkMsLAew==
|
||||
version "7.6.2"
|
||||
resolved "https://registry.yarnpkg.com/@types/babel__generator/-/babel__generator-7.6.2.tgz#f3d71178e187858f7c45e30380f8f1b7415a12d8"
|
||||
integrity sha512-MdSJnBjl+bdwkLskZ3NGFp9YcXGx5ggLpQQPqtgakVhsWK0hTtNYhjpZLlWQTviGTvF8at+Bvli3jV7faPdgeQ==
|
||||
dependencies:
|
||||
"@babel/types" "^7.0.0"
|
||||
|
||||
@ -1193,9 +1193,9 @@
|
||||
"@babel/types" "^7.0.0"
|
||||
|
||||
"@types/babel__traverse@*", "@types/babel__traverse@^7.0.6":
|
||||
version "7.0.14"
|
||||
resolved "https://registry.yarnpkg.com/@types/babel__traverse/-/babel__traverse-7.0.14.tgz#e99da8c075d4fb098c774ba65dabf7dc9954bd13"
|
||||
integrity sha512-8w9szzKs14ZtBVuP6Wn7nMLRJ0D6dfB0VEBEyRgxrZ/Ln49aNMykrghM2FaNn4FJRzNppCSa0Rv9pBRM5Xc3wg==
|
||||
version "7.0.15"
|
||||
resolved "https://registry.yarnpkg.com/@types/babel__traverse/-/babel__traverse-7.0.15.tgz#db9e4238931eb69ef8aab0ad6523d4d4caa39d03"
|
||||
integrity sha512-Pzh9O3sTK8V6I1olsXpCfj2k/ygO2q1X0vhhnDrEQyYLHZesWz+zMZMVcwXLCYf0U36EtmyYaFGPfXlTtDHe3A==
|
||||
dependencies:
|
||||
"@babel/types" "^7.3.0"
|
||||
|
||||
@ -2302,9 +2302,9 @@ ee-first@1.1.1:
|
||||
integrity sha1-WQxhFWsK4vTwJVcyoViyZrxWsh0=
|
||||
|
||||
electron-to-chromium@^1.3.571:
|
||||
version "1.3.572"
|
||||
resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.3.572.tgz#62d87dfe32ca1f6b9a0f76917d24f66e94e19c01"
|
||||
integrity sha512-TKqdEukCCl7JC20SwEoWTbtnGt4YjfHWAv4tcNky0a9qGo0WdM+Lrd60tps+nkaJCmktKBJjr99fLtEBU1ipWQ==
|
||||
version "1.3.573"
|
||||
resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.3.573.tgz#6a21e13ee894eb441677333d5fe9fa3a449689a1"
|
||||
integrity sha512-oypaNmexr8w0m2GX67fGLQ0Xgsd7uXz7GcwaHZ9eW3ZdQ8uA2+V/wXmLdMTk3gcacbqQGAN7CXWG3fOkfKYftw==
|
||||
|
||||
emittery@^0.7.1:
|
||||
version "0.7.1"
|
||||
@ -2423,10 +2423,10 @@ escodegen@^1.14.1:
|
||||
optionalDependencies:
|
||||
source-map "~0.6.1"
|
||||
|
||||
eslint-config-prettier@6.11.0:
|
||||
version "6.11.0"
|
||||
resolved "https://registry.yarnpkg.com/eslint-config-prettier/-/eslint-config-prettier-6.11.0.tgz#f6d2238c1290d01c859a8b5c1f7d352a0b0da8b1"
|
||||
integrity sha512-oB8cpLWSAjOVFEJhhyMZh6NOEOtBVziaqdDQ86+qhDHFbZXoRTM7pNSvFRfW/W/L/LrQ38C99J5CGuRBBzBsdA==
|
||||
eslint-config-prettier@6.12.0:
|
||||
version "6.12.0"
|
||||
resolved "https://registry.yarnpkg.com/eslint-config-prettier/-/eslint-config-prettier-6.12.0.tgz#9eb2bccff727db1c52104f0b49e87ea46605a0d2"
|
||||
integrity sha512-9jWPlFlgNwRUYVoujvWTQ1aMO8o6648r+K7qU7K5Jmkbyqav1fuEZC0COYpGBxyiAJb65Ra9hrmFx19xRGwXWw==
|
||||
dependencies:
|
||||
get-stdin "^6.0.0"
|
||||
|
||||
@ -4973,10 +4973,10 @@ rollup-plugin-terser@7.0.2:
|
||||
serialize-javascript "^4.0.0"
|
||||
terser "^5.0.0"
|
||||
|
||||
rollup-plugin-typescript2@0.27.2:
|
||||
version "0.27.2"
|
||||
resolved "https://registry.yarnpkg.com/rollup-plugin-typescript2/-/rollup-plugin-typescript2-0.27.2.tgz#871a7f5d2a774f9cef50d25da868eec72acc2ed8"
|
||||
integrity sha512-zarMH2F8oT/NO6p20gl/jkts+WxyzOlhOIUwUU/EDx5e6ewdDPS/flwLj5XFuijUCr64bZwqKuRVwCPdXXYefQ==
|
||||
rollup-plugin-typescript2@0.27.3:
|
||||
version "0.27.3"
|
||||
resolved "https://registry.yarnpkg.com/rollup-plugin-typescript2/-/rollup-plugin-typescript2-0.27.3.tgz#cd9455ac026d325b20c5728d2cc54a08a771b68b"
|
||||
integrity sha512-gmYPIFmALj9D3Ga1ZbTZAKTXq1JKlTQBtj299DXhqYz9cL3g/AQfUvbb2UhH+Nf++cCq941W2Mv7UcrcgLzJJg==
|
||||
dependencies:
|
||||
"@rollup/pluginutils" "^3.1.0"
|
||||
find-cache-dir "^3.3.1"
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user