Skip to content
Merged
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
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ export enum RequestContentKind {
FORM_DATA = "FORM_DATA",
IMAGE = "IMAGE",
OTHER = "OTHER",
TEXT = "TEXT",
}

export interface RequestResponseInfo {
Expand Down
7 changes: 7 additions & 0 deletions src/schema-parser/schema-routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const CONTENT_KIND = {
FORM_DATA: "FORM_DATA",
IMAGE: "IMAGE",
OTHER: "OTHER",
TEXT: "TEXT",
};

class SchemaRoutes {
Expand Down Expand Up @@ -265,6 +266,12 @@ class SchemaRoutes {
return CONTENT_KIND.IMAGE;
}

if (
_.some(contentTypes, (contentType) => _.startsWith(contentType, "text/"))
) {
return CONTENT_KIND.TEXT;
}

return CONTENT_KIND.OTHER;
};

Expand Down
5 changes: 5 additions & 0 deletions templates/base/http-clients/axios-http-client.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export enum ContentType {
Json = "application/json",
FormData = "multipart/form-data",
UrlEncoded = "application/x-www-form-urlencoded",
Text = "text/plain",
}

export class HttpClient<SecurityDataType = unknown> {
Expand Down Expand Up @@ -114,6 +115,10 @@ export class HttpClient<SecurityDataType = unknown> {
body = this.createFormData(body as Record<string, unknown>);
}

if (type === ContentType.Text && body && body !== null && typeof body !== "string") {
body = JSON.stringify(body);
}

return this.instance.request({
...requestParams,
headers: {
Expand Down
2 changes: 2 additions & 0 deletions templates/base/http-clients/fetch-http-client.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export enum ContentType {
Json = "application/json",
FormData = "multipart/form-data",
UrlEncoded = "application/x-www-form-urlencoded",
Text = "text/plain",
}

export class HttpClient<SecurityDataType = unknown> {
Expand Down Expand Up @@ -102,6 +103,7 @@ export class HttpClient<SecurityDataType = unknown> {

private contentFormatters: Record<ContentType, (input: any) => any> = {
[ContentType.Json]: (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) =>
Object.keys(input || {}).reduce((formData, key) => {
const property = input[key];
Expand Down
1 change: 1 addition & 0 deletions templates/default/procedure-call.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ const requestContentKind = {
"JSON": "ContentType.Json",
"URL_ENCODED": "ContentType.UrlEncoded",
"FORM_DATA": "ContentType.FormData",
"TEXT": "ContentType.Text",
}
// RequestParams["format"]
const responseContentKind = {
Expand Down
1 change: 1 addition & 0 deletions templates/modular/procedure-call.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ const requestContentKind = {
"JSON": "ContentType.Json",
"URL_ENCODED": "ContentType.UrlEncoded",
"FORM_DATA": "ContentType.FormData",
"TEXT": "ContentType.Text",
}
// RequestParams["format"]
const responseContentKind = {
Expand Down
2 changes: 2 additions & 0 deletions tests/generated/v2.0/adafruit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ export enum ContentType {
Json = "application/json",
FormData = "multipart/form-data",
UrlEncoded = "application/x-www-form-urlencoded",
Text = "text/plain",
}

export class HttpClient<SecurityDataType = unknown> {
Expand Down Expand Up @@ -262,6 +263,7 @@ export class HttpClient<SecurityDataType = unknown> {
private contentFormatters: Record<ContentType, (input: any) => any> = {
[ContentType.Json]: (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) =>
Object.keys(input || {}).reduce((formData, key) => {
const property = input[key];
Expand Down
2 changes: 2 additions & 0 deletions tests/generated/v2.0/another-example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ export enum ContentType {
Json = "application/json",
FormData = "multipart/form-data",
UrlEncoded = "application/x-www-form-urlencoded",
Text = "text/plain",
}

export class HttpClient<SecurityDataType = unknown> {
Expand Down Expand Up @@ -228,6 +229,7 @@ export class HttpClient<SecurityDataType = unknown> {
private contentFormatters: Record<ContentType, (input: any) => any> = {
[ContentType.Json]: (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) =>
Object.keys(input || {}).reduce((formData, key) => {
const property = input[key];
Expand Down
2 changes: 2 additions & 0 deletions tests/generated/v2.0/another-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export enum ContentType {
Json = "application/json",
FormData = "multipart/form-data",
UrlEncoded = "application/x-www-form-urlencoded",
Text = "text/plain",
}

export class HttpClient<SecurityDataType = unknown> {
Expand Down Expand Up @@ -126,6 +127,7 @@ export class HttpClient<SecurityDataType = unknown> {
private contentFormatters: Record<ContentType, (input: any) => any> = {
[ContentType.Json]: (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) =>
Object.keys(input || {}).reduce((formData, key) => {
const property = input[key];
Expand Down
19 changes: 19 additions & 0 deletions tests/generated/v2.0/api-with-examples.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export enum ContentType {
Json = "application/json",
FormData = "multipart/form-data",
UrlEncoded = "application/x-www-form-urlencoded",
Text = "text/plain",
}

export class HttpClient<SecurityDataType = unknown> {
Expand Down Expand Up @@ -105,6 +106,7 @@ export class HttpClient<SecurityDataType = unknown> {
private contentFormatters: Record<ContentType, (input: any) => any> = {
[ContentType.Json]: (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) =>
Object.keys(input || {}).reduce((formData, key) => {
const property = input[key];
Expand Down Expand Up @@ -270,4 +272,21 @@ export class Api<SecurityDataType extends unknown> extends HttpClient<SecurityDa
...params,
}),
};
consumesPlainText = {
/**
* @description consumes plain text
*
* @name ConsumesPlainText
* @summary consumes plain text
* @request POST:/consumes-plain-text/
*/
consumesPlainText: (someParm: string, params: RequestParams = {}) =>
this.request<any, void>({
path: `/consumes-plain-text/`,
method: "POST",
body: someParm,
type: ContentType.Text,
...params,
}),
};
}
2 changes: 2 additions & 0 deletions tests/generated/v2.0/authentiq.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ export enum ContentType {
Json = "application/json",
FormData = "multipart/form-data",
UrlEncoded = "application/x-www-form-urlencoded",
Text = "text/plain",
}

export class HttpClient<SecurityDataType = unknown> {
Expand Down Expand Up @@ -145,6 +146,7 @@ export class HttpClient<SecurityDataType = unknown> {
private contentFormatters: Record<ContentType, (input: any) => any> = {
[ContentType.Json]: (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) =>
Object.keys(input || {}).reduce((formData, key) => {
const property = input[key];
Expand Down
2 changes: 2 additions & 0 deletions tests/generated/v2.0/enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ export enum ContentType {
Json = "application/json",
FormData = "multipart/form-data",
UrlEncoded = "application/x-www-form-urlencoded",
Text = "text/plain",
}

export class HttpClient<SecurityDataType = unknown> {
Expand Down Expand Up @@ -164,6 +165,7 @@ export class HttpClient<SecurityDataType = unknown> {
private contentFormatters: Record<ContentType, (input: any) => any> = {
[ContentType.Json]: (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) =>
Object.keys(input || {}).reduce((formData, key) => {
const property = input[key];
Expand Down
2 changes: 2 additions & 0 deletions tests/generated/v2.0/example1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ export enum ContentType {
Json = "application/json",
FormData = "multipart/form-data",
UrlEncoded = "application/x-www-form-urlencoded",
Text = "text/plain",
}

export class HttpClient<SecurityDataType = unknown> {
Expand Down Expand Up @@ -142,6 +143,7 @@ export class HttpClient<SecurityDataType = unknown> {
private contentFormatters: Record<ContentType, (input: any) => any> = {
[ContentType.Json]: (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) =>
Object.keys(input || {}).reduce((formData, key) => {
const property = input[key];
Expand Down
2 changes: 2 additions & 0 deletions tests/generated/v2.0/file-formdata-example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export enum ContentType {
Json = "application/json",
FormData = "multipart/form-data",
UrlEncoded = "application/x-www-form-urlencoded",
Text = "text/plain",
}

export class HttpClient<SecurityDataType = unknown> {
Expand Down Expand Up @@ -105,6 +106,7 @@ export class HttpClient<SecurityDataType = unknown> {
private contentFormatters: Record<ContentType, (input: any) => any> = {
[ContentType.Json]: (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) =>
Object.keys(input || {}).reduce((formData, key) => {
const property = input[key];
Expand Down
2 changes: 2 additions & 0 deletions tests/generated/v2.0/furkot-example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ export enum ContentType {
Json = "application/json",
FormData = "multipart/form-data",
UrlEncoded = "application/x-www-form-urlencoded",
Text = "text/plain",
}

export class HttpClient<SecurityDataType = unknown> {
Expand Down Expand Up @@ -178,6 +179,7 @@ export class HttpClient<SecurityDataType = unknown> {
private contentFormatters: Record<ContentType, (input: any) => any> = {
[ContentType.Json]: (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) =>
Object.keys(input || {}).reduce((formData, key) => {
const property = input[key];
Expand Down
2 changes: 2 additions & 0 deletions tests/generated/v2.0/giphy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ export enum ContentType {
Json = "application/json",
FormData = "multipart/form-data",
UrlEncoded = "application/x-www-form-urlencoded",
Text = "text/plain",
}

export class HttpClient<SecurityDataType = unknown> {
Expand Down Expand Up @@ -362,6 +363,7 @@ export class HttpClient<SecurityDataType = unknown> {
private contentFormatters: Record<ContentType, (input: any) => any> = {
[ContentType.Json]: (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) =>
Object.keys(input || {}).reduce((formData, key) => {
const property = input[key];
Expand Down
3 changes: 3 additions & 0 deletions tests/generated/v2.0/github-swagger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1949,6 +1949,7 @@ export enum ContentType {
Json = "application/json",
FormData = "multipart/form-data",
UrlEncoded = "application/x-www-form-urlencoded",
Text = "text/plain",
}

export class HttpClient<SecurityDataType = unknown> {
Expand Down Expand Up @@ -2003,6 +2004,7 @@ export class HttpClient<SecurityDataType = unknown> {
private contentFormatters: Record<ContentType, (input: any) => any> = {
[ContentType.Json]: (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) =>
Object.keys(input || {}).reduce((formData, key) => {
const property = input[key];
Expand Down Expand Up @@ -2684,6 +2686,7 @@ export class Api<SecurityDataType extends unknown> extends HttpClient<SecurityDa
this.request<void, void>({
path: `/markdown/raw`,
method: "POST",
type: ContentType.Text,
...params,
}),
};
Expand Down
2 changes: 2 additions & 0 deletions tests/generated/v2.0/path-args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export enum ContentType {
Json = "application/json",
FormData = "multipart/form-data",
UrlEncoded = "application/x-www-form-urlencoded",
Text = "text/plain",
}

export class HttpClient<SecurityDataType = unknown> {
Expand Down Expand Up @@ -105,6 +106,7 @@ export class HttpClient<SecurityDataType = unknown> {
private contentFormatters: Record<ContentType, (input: any) => any> = {
[ContentType.Json]: (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) =>
Object.keys(input || {}).reduce((formData, key) => {
const property = input[key];
Expand Down
2 changes: 2 additions & 0 deletions tests/generated/v2.0/petstore-expanded.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export enum ContentType {
Json = "application/json",
FormData = "multipart/form-data",
UrlEncoded = "application/x-www-form-urlencoded",
Text = "text/plain",
}

export class HttpClient<SecurityDataType = unknown> {
Expand Down Expand Up @@ -141,6 +142,7 @@ export class HttpClient<SecurityDataType = unknown> {
private contentFormatters: Record<ContentType, (input: any) => any> = {
[ContentType.Json]: (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) =>
Object.keys(input || {}).reduce((formData, key) => {
const property = input[key];
Expand Down
2 changes: 2 additions & 0 deletions tests/generated/v2.0/petstore-minimal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export enum ContentType {
Json = "application/json",
FormData = "multipart/form-data",
UrlEncoded = "application/x-www-form-urlencoded",
Text = "text/plain",
}

export class HttpClient<SecurityDataType = unknown> {
Expand Down Expand Up @@ -113,6 +114,7 @@ export class HttpClient<SecurityDataType = unknown> {
private contentFormatters: Record<ContentType, (input: any) => any> = {
[ContentType.Json]: (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) =>
Object.keys(input || {}).reduce((formData, key) => {
const property = input[key];
Expand Down
2 changes: 2 additions & 0 deletions tests/generated/v2.0/petstore-simple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ export enum ContentType {
Json = "application/json",
FormData = "multipart/form-data",
UrlEncoded = "application/x-www-form-urlencoded",
Text = "text/plain",
}

export class HttpClient<SecurityDataType = unknown> {
Expand Down Expand Up @@ -129,6 +130,7 @@ export class HttpClient<SecurityDataType = unknown> {
private contentFormatters: Record<ContentType, (input: any) => any> = {
[ContentType.Json]: (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) =>
Object.keys(input || {}).reduce((formData, key) => {
const property = input[key];
Expand Down
2 changes: 2 additions & 0 deletions tests/generated/v2.0/petstore-swagger-io.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ export enum ContentType {
Json = "application/json",
FormData = "multipart/form-data",
UrlEncoded = "application/x-www-form-urlencoded",
Text = "text/plain",
}

export class HttpClient<SecurityDataType = unknown> {
Expand Down Expand Up @@ -166,6 +167,7 @@ export class HttpClient<SecurityDataType = unknown> {
private contentFormatters: Record<ContentType, (input: any) => any> = {
[ContentType.Json]: (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) =>
Object.keys(input || {}).reduce((formData, key) => {
const property = input[key];
Expand Down
2 changes: 2 additions & 0 deletions tests/generated/v2.0/petstore-with-external-docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export enum ContentType {
Json = "application/json",
FormData = "multipart/form-data",
UrlEncoded = "application/x-www-form-urlencoded",
Text = "text/plain",
}

export class HttpClient<SecurityDataType = unknown> {
Expand Down Expand Up @@ -121,6 +122,7 @@ export class HttpClient<SecurityDataType = unknown> {
private contentFormatters: Record<ContentType, (input: any) => any> = {
[ContentType.Json]: (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) =>
Object.keys(input || {}).reduce((formData, key) => {
const property = input[key];
Expand Down
2 changes: 2 additions & 0 deletions tests/generated/v2.0/petstore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export enum ContentType {
Json = "application/json",
FormData = "multipart/form-data",
UrlEncoded = "application/x-www-form-urlencoded",
Text = "text/plain",
}

export class HttpClient<SecurityDataType = unknown> {
Expand Down Expand Up @@ -120,6 +121,7 @@ export class HttpClient<SecurityDataType = unknown> {
private contentFormatters: Record<ContentType, (input: any) => any> = {
[ContentType.Json]: (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) =>
Object.keys(input || {}).reduce((formData, key) => {
const property = input[key];
Expand Down
Loading