Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/shy-humans-pay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"swagger-typescript-api": patch
---

Add a configuration parameter for the generated ContentType enum to avoid name clashing
2 changes: 2 additions & 0 deletions src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ export class CodeGenConfig {

successResponseStatusRange = [200, 299];

contentTypeEnumName = "ContentType";

extractingOptions: Partial<ExtractingOptions> = {
requestBodySuffix: ["Payload", "Body", "Input"],
requestParamsSuffix: ["Params"],
Expand Down
8 changes: 4 additions & 4 deletions templates/base/http-clients/axios-http-client.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export interface FullRequestParams extends Omit<AxiosRequestConfig, "data" | "pa
/** request path */
path: string;
/** content type of request body */
type?: ContentType;
type?: <%~ config.contentTypeEnumName %>;
/** query params */
query?: QueryParamsType;
/** format of response (i.e. response.json() -> format: "json") */
Expand All @@ -30,7 +30,7 @@ export interface ApiConfig<SecurityDataType = unknown> extends Omit<AxiosRequest
format?: ResponseType;
}

export enum ContentType {
export enum <%~ config.contentTypeEnumName %> {
Json = "application/json",
JsonApi = "application/vnd.api+json",
FormData = "multipart/form-data",
Expand Down Expand Up @@ -116,11 +116,11 @@ export class HttpClient<SecurityDataType = unknown> {
const requestParams = this.mergeRequestParams(params, secureParams);
const responseFormat = (format || this.format) || undefined;

if (type === ContentType.FormData && body && body !== null && typeof body === "object") {
if (type === <%~ config.contentTypeEnumName %>.FormData && body && body !== null && typeof body === "object") {
body = this.createFormData(body as Record<string, unknown>);
}

if (type === ContentType.Text && body && body !== null && typeof body !== "string") {
if (type === <%~ config.contentTypeEnumName %>.Text && body && body !== null && typeof body !== "string") {
body = JSON.stringify(body);
}

Expand Down
20 changes: 10 additions & 10 deletions templates/base/http-clients/fetch-http-client.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export interface FullRequestParams extends Omit<RequestInit, "body"> {
/** request path */
path: string;
/** content type of request body */
type?: ContentType;
type?: <%~ config.contentTypeEnumName %>;
/** query params */
query?: QueryParamsType;
/** format of response (i.e. response.json() -> format: "json") */
Expand Down Expand Up @@ -41,7 +41,7 @@ export interface HttpResponse<D extends unknown, E extends unknown = unknown> ex

type CancelToken = Symbol | string | number;

export enum ContentType {
export enum <%~ config.contentTypeEnumName %> {
Json = "application/json",
JsonApi = "application/vnd.api+json",
FormData = "multipart/form-data",
Expand Down Expand Up @@ -102,11 +102,11 @@ export class HttpClient<SecurityDataType = unknown> {
return queryString ? `?${queryString}` : "";
}

private contentFormatters: Record<ContentType, (input: any) => any> = {
[ContentType.Json]: (input:any) => input !== null && (typeof input === "object" || typeof input === "string") ? JSON.stringify(input) : input,
[ContentType.JsonApi]: (input:any) => input !== null && (typeof input === "object" || typeof input === "string") ? JSON.stringify(input) : input,
[ContentType.Text]: (input:any) => input !== null && typeof input !== "string" ? JSON.stringify(input) : input,
[ContentType.FormData]: (input: any) => {
private contentFormatters: Record<<%~ config.contentTypeEnumName %>, (input: any) => any> = {
[<%~ config.contentTypeEnumName %>.Json]: (input:any) => input !== null && (typeof input === "object" || typeof input === "string") ? JSON.stringify(input) : input,
[<%~ config.contentTypeEnumName %>.JsonApi]: (input:any) => input !== null && (typeof input === "object" || typeof input === "string") ? JSON.stringify(input) : input,
[<%~ config.contentTypeEnumName %>.Text]: (input:any) => input !== null && typeof input !== "string" ? JSON.stringify(input) : input,
[<%~ config.contentTypeEnumName %>.FormData]: (input: any) => {
if (input instanceof FormData) {
return input;
}
Expand All @@ -124,7 +124,7 @@ export class HttpClient<SecurityDataType = unknown> {
return formData;
}, new FormData());
},
[ContentType.UrlEncoded]: (input: any) => this.toQueryString(input),
[<%~ config.contentTypeEnumName %>.UrlEncoded]: (input: any) => this.toQueryString(input),
}

protected mergeRequestParams(params1: RequestParams, params2?: RequestParams): RequestParams {
Expand Down Expand Up @@ -181,7 +181,7 @@ export class HttpClient<SecurityDataType = unknown> {
const secureParams = ((typeof secure === 'boolean' ? secure : this.baseApiParams.secure) && this.securityWorker && await this.securityWorker(this.securityData)) || {};
const requestParams = this.mergeRequestParams(params, secureParams);
const queryString = query && this.toQueryString(query);
const payloadFormatter = this.contentFormatters[type || ContentType.Json];
const payloadFormatter = this.contentFormatters[type || <%~ config.contentTypeEnumName %>.Json];
const responseFormat = format || requestParams.format;

return this.customFetch(
Expand All @@ -190,7 +190,7 @@ export class HttpClient<SecurityDataType = unknown> {
...requestParams,
headers: {
...(requestParams.headers || {}),
...(type && type !== ContentType.FormData ? { "Content-Type": type } : {}),
...(type && type !== <%~ config.contentTypeEnumName %>.FormData ? { "Content-Type": type } : {}),
},
signal: (cancelToken ? this.createAbortSignal(cancelToken) : requestParams.signal) || null,
body: typeof body === "undefined" || body === null ? null : payloadFormatter(body),
Expand Down
15 changes: 9 additions & 6 deletions templates/default/procedure-call.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,17 @@ const wrapperArgs = _
.map(argToTmpl)
.join(', ')

const enumName = config.contentTypeEnumName;

// RequestParams["type"]
const requestContentKind = {
"JSON": "ContentType.Json",
"JSON_API": "ContentType.JsonApi",
"URL_ENCODED": "ContentType.UrlEncoded",
"FORM_DATA": "ContentType.FormData",
"TEXT": "ContentType.Text",
}
JSON: `${enumName}.Json`,
JSON_API: `${enumName}.JsonApi`,
URL_ENCODED: `${enumName}.UrlEncoded`,
FORM_DATA: `${enumName}.FormData`,
TEXT: `${enumName}.Text`,
};

// RequestParams["format"]
const responseContentKind = {
"JSON": '"json"',
Expand Down
15 changes: 9 additions & 6 deletions templates/modular/procedure-call.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,17 @@ const wrapperArgs = _
.map(argToTmpl)
.join(', ')

const enumName = config.contentTypeEnumName;

// RequestParams["type"]
const requestContentKind = {
"JSON": "ContentType.Json",
"JSON_API": "ContentType.JsonApi",
"URL_ENCODED": "ContentType.UrlEncoded",
"FORM_DATA": "ContentType.FormData",
"TEXT": "ContentType.Text",
}
JSON: `${enumName}.Json`,
JSON_API: `${enumName}.JsonApi`,
URL_ENCODED: `${enumName}.UrlEncoded`,
FORM_DATA: `${enumName}.FormData`,
TEXT: `${enumName}.Text`,
};

// RequestParams["format"]
const responseContentKind = {
"JSON": '"json"',
Expand Down
4 changes: 4 additions & 0 deletions types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,10 @@ export interface GenerateApiConfiguration {
extractingOptions: Partial<ExtractingOptions>;
/** update configuration object during generation */
update: (update: Partial<GenerateApiConfiguration["config"]>) => void;
/** name for the generated ContentType enum
* @default ContentType
*/
contentTypeEnumName: string;
};
modelTypes: ModelType[];
hasFormDataRoutes: boolean;
Expand Down
Loading