-
-
Notifications
You must be signed in to change notification settings - Fork 377
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Feature]
--union-enums
CLI option (#61)
* feat: add --union-enums cli option which generate all enums as union types * docs: update README, CHANGELOG * chore(fix): manual test for union enums option
- Loading branch information
Showing
11 changed files
with
212 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
{ | ||
"components": { | ||
"examples": {}, | ||
"headers": {}, | ||
"parameters": {}, | ||
"requestBodies": {}, | ||
"responses": {}, | ||
"schemas": { | ||
"StringEnum": { | ||
"enum": ["String1", "String2", "String3", "String4"], | ||
"type": "string" | ||
}, | ||
"NumberEnum": { | ||
"enum": [1, 2, 3, 4], | ||
"type": "number" | ||
} | ||
}, | ||
"securitySchemes": {} | ||
}, | ||
"info": { | ||
"title": "" | ||
}, | ||
"openapi": "3.0.0", | ||
"paths": {}, | ||
"servers": [ | ||
{ | ||
"url": "http://localhost:8080/api/v1" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* tslint:disable */ | ||
/* eslint-disable */ | ||
|
||
/* | ||
* --------------------------------------------------------------- | ||
* ## THIS FILE WAS GENERATED VIA SWAGGER-TYPESCRIPT-API ## | ||
* ## ## | ||
* ## AUTHOR: acacode ## | ||
* ## SOURCE: https://github.com/acacode/swagger-typescript-api ## | ||
* --------------------------------------------------------------- | ||
*/ | ||
|
||
export type StringEnum = "String1" | "String2" | "String3" | "String4"; | ||
|
||
export type NumberEnum = 1 | 2 | 3 | 4; | ||
|
||
export type RequestParams = Omit<RequestInit, "body" | "method"> & { | ||
secure?: boolean; | ||
}; | ||
|
||
type ApiConfig<SecurityDataType> = { | ||
baseUrl?: string; | ||
baseApiParams?: RequestParams; | ||
securityWorker?: (securityData: SecurityDataType) => RequestParams; | ||
}; | ||
|
||
const enum BodyType { | ||
Json, | ||
} | ||
|
||
class HttpClient<SecurityDataType> { | ||
public baseUrl: string = "http://localhost:8080/api/v1"; | ||
private securityData: SecurityDataType = null as any; | ||
private securityWorker: ApiConfig<SecurityDataType>["securityWorker"] = (() => {}) as any; | ||
|
||
private baseApiParams: RequestParams = { | ||
credentials: "same-origin", | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
redirect: "follow", | ||
referrerPolicy: "no-referrer", | ||
}; | ||
|
||
constructor({ baseUrl, baseApiParams, securityWorker }: ApiConfig<SecurityDataType> = {}) { | ||
this.baseUrl = baseUrl || this.baseUrl; | ||
this.baseApiParams = baseApiParams || this.baseApiParams; | ||
this.securityWorker = securityWorker || this.securityWorker; | ||
} | ||
|
||
public setSecurityData = (data: SecurityDataType) => { | ||
this.securityData = data; | ||
}; | ||
|
||
private bodyFormatters: Record<BodyType, (input: any) => any> = { | ||
[BodyType.Json]: JSON.stringify, | ||
}; | ||
|
||
private mergeRequestOptions(params: RequestParams, securityParams?: RequestParams): RequestParams { | ||
return { | ||
...this.baseApiParams, | ||
...params, | ||
...(securityParams || {}), | ||
headers: { | ||
...(this.baseApiParams.headers || {}), | ||
...(params.headers || {}), | ||
...((securityParams && securityParams.headers) || {}), | ||
}, | ||
}; | ||
} | ||
|
||
private safeParseResponse = <T = any, E = any>(response: Response): Promise<T> => | ||
response | ||
.json() | ||
.then((data) => data) | ||
.catch((e) => response.text); | ||
|
||
public request = <T = any, E = any>( | ||
path: string, | ||
method: string, | ||
{ secure, ...params }: RequestParams = {}, | ||
body?: any, | ||
bodyType?: BodyType, | ||
secureByDefault?: boolean, | ||
): Promise<T> => | ||
fetch(`${this.baseUrl}${path}`, { | ||
// @ts-ignore | ||
...this.mergeRequestOptions(params, (secureByDefault || secure) && this.securityWorker(this.securityData)), | ||
method, | ||
body: body ? this.bodyFormatters[bodyType || BodyType.Json](body) : null, | ||
}).then(async (response) => { | ||
const data = await this.safeParseResponse<T, E>(response); | ||
if (!response.ok) throw data; | ||
return data; | ||
}); | ||
} | ||
|
||
/** | ||
* @title Api | ||
* @baseUrl http://localhost:8080/api/v1 | ||
*/ | ||
export class Api<SecurityDataType = any> extends HttpClient<SecurityDataType> {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
const { generateApi } = require("../../../src"); | ||
const { resolve } = require("path"); | ||
const validateGeneratedModule = require("../../helpers/validateGeneratedModule"); | ||
const createSchemasInfos = require("../../helpers/createSchemaInfos"); | ||
|
||
const schemas = createSchemasInfos({ absolutePathToSchemas: resolve(__dirname, "./") }); | ||
|
||
schemas.forEach(({ absolutePath, apiFileName }) => { | ||
generateApi({ | ||
name: apiFileName, | ||
input: absolutePath, | ||
output: resolve(__dirname, "./"), | ||
generateUnionEnums: true, | ||
}) | ||
.then(() => { | ||
const diagnostics = validateGeneratedModule({ | ||
pathToFile: resolve(__dirname, `./${apiFileName}`), | ||
}); | ||
if (diagnostics.length) throw "Failed"; | ||
}) | ||
.catch((e) => { | ||
console.error("unionEnums option test failed."); | ||
throw e; | ||
}); | ||
}); |