Skip to content

Commit

Permalink
5.1.2 Release (#165)
Browse files Browse the repository at this point in the history
* fix: linter disable rules is not working

* bump: up project version to 5.1.2
  • Loading branch information
js2me authored Feb 15, 2021
1 parent 798c8e9 commit 5535462
Show file tree
Hide file tree
Showing 64 changed files with 527 additions and 120 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# next release

# 5.1.2

Fixes:
- Linter disable rules is not working (issue #164, thanks @Haritaso)

# 5.1.1

Fixes:
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "swagger-typescript-api",
"version": "5.1.1",
"version": "5.1.2",
"description": "Create typescript api module from swagger schema",
"scripts": {
"cli:json": "node index.js -r -d -p ./swagger-test-cli.json -n swagger-test-cli.ts --extract-request-params --enum-names-as-values",
Expand Down
4 changes: 2 additions & 2 deletions src/filePrefix.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module.exports = {
filePrefix: `/* eslint:disable */
/* tslint-disable */
filePrefix: `/* eslint-disable */
/* tslint:disable */
/*
* ---------------------------------------------------------------
* ## THIS FILE WAS GENERATED VIA SWAGGER-TYPESCRIPT-API ##
Expand Down
4 changes: 2 additions & 2 deletions tests/generated/v2.0/adafruit.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint:disable */
/* tslint-disable */
/* eslint-disable */
/* tslint:disable */
/*
* ---------------------------------------------------------------
* ## THIS FILE WAS GENERATED VIA SWAGGER-TYPESCRIPT-API ##
Expand Down
4 changes: 2 additions & 2 deletions tests/generated/v2.0/another-example.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint:disable */
/* tslint-disable */
/* eslint-disable */
/* tslint:disable */
/*
* ---------------------------------------------------------------
* ## THIS FILE WAS GENERATED VIA SWAGGER-TYPESCRIPT-API ##
Expand Down
277 changes: 277 additions & 0 deletions tests/generated/v2.0/another-schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,277 @@
/* eslint-disable */
/* tslint:disable */
/*
* ---------------------------------------------------------------
* ## THIS FILE WAS GENERATED VIA SWAGGER-TYPESCRIPT-API ##
* ## ##
* ## AUTHOR: acacode ##
* ## SOURCE: https://github.com/acacode/swagger-typescript-api ##
* ---------------------------------------------------------------
*/

export interface Bar {
A?: string;

/** @format int32 */
B: number;

/** @format date-time */
C: string;
Baz?: Baz;
}

export interface Baz {
/** @format decimal */
D: number;
Color: Color;
}

export enum Color {
RED = 0,
GREEN = 1,
BLUE = 2,
}

export type QueryParamsType = Record<string | number, any>;
export type ResponseFormat = keyof Omit<Body, "body" | "bodyUsed">;

export interface FullRequestParams extends Omit<RequestInit, "body"> {
/** set parameter to `true` for call `securityWorker` for this request */
secure?: boolean;
/** request path */
path: string;
/** content type of request body */
type?: ContentType;
/** query params */
query?: QueryParamsType;
/** format of response (i.e. response.json() -> format: "json") */
format?: keyof Omit<Body, "body" | "bodyUsed">;
/** request body */
body?: unknown;
/** base url */
baseUrl?: string;
/** request cancellation token */
cancelToken?: CancelToken;
}

export type RequestParams = Omit<FullRequestParams, "body" | "method" | "query" | "path">;

export interface ApiConfig<SecurityDataType = unknown> {
baseUrl?: string;
baseApiParams?: Omit<RequestParams, "baseUrl" | "cancelToken" | "signal">;
securityWorker?: (securityData: SecurityDataType) => RequestParams | void;
}

export interface HttpResponse<D extends unknown, E extends unknown = unknown> extends Response {
data: D;
error: E;
}

type CancelToken = Symbol | string | number;

export enum ContentType {
Json = "application/json",
FormData = "multipart/form-data",
UrlEncoded = "application/x-www-form-urlencoded",
}

export class HttpClient<SecurityDataType = unknown> {
public baseUrl: string = "";
private securityData: SecurityDataType = null as any;
private securityWorker: null | ApiConfig<SecurityDataType>["securityWorker"] = null;
private abortControllers = new Map<CancelToken, AbortController>();

private baseApiParams: RequestParams = {
credentials: "same-origin",
headers: {},
redirect: "follow",
referrerPolicy: "no-referrer",
};

constructor(apiConfig: ApiConfig<SecurityDataType> = {}) {
Object.assign(this, apiConfig);
}

public setSecurityData = (data: SecurityDataType) => {
this.securityData = data;
};

private addQueryParam(query: QueryParamsType, key: string) {
const value = query[key];

return (
encodeURIComponent(key) +
"=" +
encodeURIComponent(Array.isArray(value) ? value.join(",") : typeof value === "number" ? value : `${value}`)
);
}

protected toQueryString(rawQuery?: QueryParamsType): string {
const query = rawQuery || {};
const keys = Object.keys(query).filter((key) => "undefined" !== typeof query[key]);
return keys
.map((key) =>
typeof query[key] === "object" && !Array.isArray(query[key])
? this.toQueryString(query[key] as QueryParamsType)
: this.addQueryParam(query, key),
)
.join("&");
}

protected addQueryParams(rawQuery?: QueryParamsType): string {
const queryString = this.toQueryString(rawQuery);
return queryString ? `?${queryString}` : "";
}

private contentFormatters: Record<ContentType, (input: any) => any> = {
[ContentType.Json]: (input: any) => (input !== null && typeof input === "object" ? JSON.stringify(input) : input),
[ContentType.FormData]: (input: any) =>
Object.keys(input || {}).reduce((data, key) => {
data.append(key, input[key]);
return data;
}, new FormData()),
[ContentType.UrlEncoded]: (input: any) => this.toQueryString(input),
};

private mergeRequestParams(params1: RequestParams, params2?: RequestParams): RequestParams {
return {
...this.baseApiParams,
...params1,
...(params2 || {}),
headers: {
...(this.baseApiParams.headers || {}),
...(params1.headers || {}),
...((params2 && params2.headers) || {}),
},
};
}

private createAbortSignal = (cancelToken: CancelToken): AbortSignal | undefined => {
if (this.abortControllers.has(cancelToken)) {
const abortController = this.abortControllers.get(cancelToken);
if (abortController) {
return abortController.signal;
}
return void 0;
}

const abortController = new AbortController();
this.abortControllers.set(cancelToken, abortController);
return abortController.signal;
};

public abortRequest = (cancelToken: CancelToken) => {
const abortController = this.abortControllers.get(cancelToken);

if (abortController) {
abortController.abort();
this.abortControllers.delete(cancelToken);
}
};

public request = <T = any, E = any>({
body,
secure,
path,
type,
query,
format = "json",
baseUrl,
cancelToken,
...params
}: FullRequestParams): Promise<HttpResponse<T, E>> => {
const secureParams = (secure && this.securityWorker && this.securityWorker(this.securityData)) || {};
const requestParams = this.mergeRequestParams(params, secureParams);
const queryString = query && this.toQueryString(query);
const payloadFormatter = this.contentFormatters[type || ContentType.Json];

return fetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, {
headers: {
...(type ? { "Content-Type": type } : {}),
...(requestParams.headers || {}),
},
...requestParams,
signal: cancelToken ? this.createAbortSignal(cancelToken) : void 0,
body: typeof body === "undefined" || body === null ? null : payloadFormatter(body),
}).then(async (response) => {
const r = response as HttpResponse<T, E>;
r.data = (null as unknown) as T;
r.error = (null as unknown) as E;

const data = await response[format]()
.then((data) => {
if (r.ok) {
r.data = data;
} else {
r.error = data;
}
return r;
})
.catch((e) => {
r.error = e;
return r;
});

if (cancelToken) {
this.abortControllers.delete(cancelToken);
}

if (!response.ok) throw data;
return data;
});
};
}

/**
* @title No title
*/
export class Api<SecurityDataType extends unknown> extends HttpClient<SecurityDataType> {
api = {
/**
* No description
*
* @tags Foo
* @name FooGetBarDescriptions
* @request GET:/api/Foo/GetBarDescriptions
*/
fooGetBarDescriptions: (params: RequestParams = {}) =>
this.request<string[], any>({
path: `/api/Foo/GetBarDescriptions`,
method: "GET",
format: "json",
...params,
}),

/**
* No description
*
* @tags Foo
* @name FooGetBar
* @request GET:/api/Foo/GetBar
*/
fooGetBar: (query: { id: number }, params: RequestParams = {}) =>
this.request<Bar, any>({
path: `/api/Foo/GetBar`,
method: "GET",
query: query,
format: "json",
...params,
}),

/**
* No description
*
* @tags Foo
* @name FooSetBar
* @request POST:/api/Foo/SetBar
*/
fooSetBar: (value: Bar, params: RequestParams = {}) =>
this.request<void, any>({
path: `/api/Foo/SetBar`,
method: "POST",
body: value,
type: ContentType.Json,
...params,
}),
};
}
4 changes: 2 additions & 2 deletions tests/generated/v2.0/api-with-examples.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint:disable */
/* tslint-disable */
/* eslint-disable */
/* tslint:disable */
/*
* ---------------------------------------------------------------
* ## THIS FILE WAS GENERATED VIA SWAGGER-TYPESCRIPT-API ##
Expand Down
4 changes: 2 additions & 2 deletions tests/generated/v2.0/authentiq.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint:disable */
/* tslint-disable */
/* eslint-disable */
/* tslint:disable */
/*
* ---------------------------------------------------------------
* ## THIS FILE WAS GENERATED VIA SWAGGER-TYPESCRIPT-API ##
Expand Down
4 changes: 2 additions & 2 deletions tests/generated/v2.0/example1.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint:disable */
/* tslint-disable */
/* eslint-disable */
/* tslint:disable */
/*
* ---------------------------------------------------------------
* ## THIS FILE WAS GENERATED VIA SWAGGER-TYPESCRIPT-API ##
Expand Down
4 changes: 2 additions & 2 deletions tests/generated/v2.0/file-formdata-example.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint:disable */
/* tslint-disable */
/* eslint-disable */
/* tslint:disable */
/*
* ---------------------------------------------------------------
* ## THIS FILE WAS GENERATED VIA SWAGGER-TYPESCRIPT-API ##
Expand Down
4 changes: 2 additions & 2 deletions tests/generated/v2.0/furkot-example.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint:disable */
/* tslint-disable */
/* eslint-disable */
/* tslint:disable */
/*
* ---------------------------------------------------------------
* ## THIS FILE WAS GENERATED VIA SWAGGER-TYPESCRIPT-API ##
Expand Down
4 changes: 2 additions & 2 deletions tests/generated/v2.0/giphy.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint:disable */
/* tslint-disable */
/* eslint-disable */
/* tslint:disable */
/*
* ---------------------------------------------------------------
* ## THIS FILE WAS GENERATED VIA SWAGGER-TYPESCRIPT-API ##
Expand Down
4 changes: 2 additions & 2 deletions tests/generated/v2.0/github-swagger.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint:disable */
/* tslint-disable */
/* eslint-disable */
/* tslint:disable */
/*
* ---------------------------------------------------------------
* ## THIS FILE WAS GENERATED VIA SWAGGER-TYPESCRIPT-API ##
Expand Down
4 changes: 2 additions & 2 deletions tests/generated/v2.0/path-args.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint:disable */
/* tslint-disable */
/* eslint-disable */
/* tslint:disable */
/*
* ---------------------------------------------------------------
* ## THIS FILE WAS GENERATED VIA SWAGGER-TYPESCRIPT-API ##
Expand Down
Loading

0 comments on commit 5535462

Please sign in to comment.