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
5 changes: 5 additions & 0 deletions .changeset/brave-seahorses-dream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hey-api/openapi-ts": patch
---

fix(client): improve handling of plain text, falsy, and unserialized request bodies
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ export const createClient = (config: Config = {}): Client => {
await opts.requestValidator(opts);
}

if (opts.body && opts.bodySerializer) {
if (opts.body !== undefined && opts.bodySerializer) {
opts.serializedBody = opts.bodySerializer(opts.body);
}

// remove Content-Type header if body is empty to avoid sending invalid requests
if (opts.serializedBody === undefined || opts.serializedBody === '') {
if (opts.body === undefined || opts.serializedBody === '') {
opts.headers.delete('Content-Type');
}

Expand All @@ -80,7 +80,7 @@ export const createClient = (config: Config = {}): Client => {
const requestInit: ReqInit = {
redirect: 'follow',
...opts,
body: opts.serializedBody,
body: getValidRequestBody(opts),
};

let request = new Request(url, requestInit);
Expand Down Expand Up @@ -212,6 +212,26 @@ export const createClient = (config: Config = {}): Client => {
};
};

function getValidRequestBody(options: ResolvedRequestOptions) {
const hasBody = options.body !== undefined;
const isSerializedBody = hasBody && options.bodySerializer;

if (isSerializedBody) {
const hasSerializedBody =
options.serializedBody !== undefined && options.serializedBody !== '';

return hasSerializedBody ? options.serializedBody : null;
}

// plain/text body
if (hasBody) {
return options.body;
}

// no body was provided
return undefined;
}

const makeMethodFn =
(method: Uppercase<HttpMethod>) => (options: RequestOptions) =>
request({ ...options, method });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ export const createClient = (config: Config = {}): Client => {
await opts.requestValidator(opts);
}

if (opts.body && opts.bodySerializer) {
if (opts.body !== undefined && opts.bodySerializer) {
opts.serializedBody = opts.bodySerializer(opts.body);
}

// remove Content-Type header if body is empty to avoid sending invalid requests
if (opts.serializedBody === undefined || opts.serializedBody === '') {
if (opts.body === undefined || opts.serializedBody === '') {
opts.headers.delete('Content-Type');
}

Expand All @@ -80,7 +80,7 @@ export const createClient = (config: Config = {}): Client => {
const requestInit: ReqInit = {
redirect: 'follow',
...opts,
body: opts.serializedBody,
body: getValidRequestBody(opts),
};

let request = new Request(url, requestInit);
Expand Down Expand Up @@ -212,6 +212,26 @@ export const createClient = (config: Config = {}): Client => {
};
};

function getValidRequestBody(options: ResolvedRequestOptions) {
const hasBody = options.body !== undefined;
const isSerializedBody = hasBody && options.bodySerializer;

if (isSerializedBody) {
const hasSerializedBody =
options.serializedBody !== undefined && options.serializedBody !== '';

return hasSerializedBody ? options.serializedBody : null;
}

// plain/text body
if (hasBody) {
return options.body;
}

// no body was provided
return undefined;
}

const makeMethodFn =
(method: Uppercase<HttpMethod>) => (options: RequestOptions) =>
request({ ...options, method });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ export const createClient = (config: Config = {}): Client => {
await opts.requestValidator(opts);
}

if (opts.body && opts.bodySerializer) {
if (opts.body !== undefined && opts.bodySerializer) {
opts.serializedBody = opts.bodySerializer(opts.body);
}

// remove Content-Type header if body is empty to avoid sending invalid requests
if (opts.serializedBody === undefined || opts.serializedBody === '') {
if (opts.body === undefined || opts.serializedBody === '') {
opts.headers.delete('Content-Type');
}

Expand All @@ -80,7 +80,7 @@ export const createClient = (config: Config = {}): Client => {
const requestInit: ReqInit = {
redirect: 'follow',
...opts,
body: opts.serializedBody,
body: getValidRequestBody(opts),
};

let request = new Request(url, requestInit);
Expand Down Expand Up @@ -212,6 +212,26 @@ export const createClient = (config: Config = {}): Client => {
};
};

function getValidRequestBody(options: ResolvedRequestOptions) {
const hasBody = options.body !== undefined;
const isSerializedBody = hasBody && options.bodySerializer;

if (isSerializedBody) {
const hasSerializedBody =
options.serializedBody !== undefined && options.serializedBody !== '';

return hasSerializedBody ? options.serializedBody : null;
}

// plain/text body
if (hasBody) {
return options.body;
}

// no body was provided
return undefined;
}

const makeMethodFn =
(method: Uppercase<HttpMethod>) => (options: RequestOptions) =>
request({ ...options, method });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ export const createClient = (config: Config = {}): Client => {
await opts.requestValidator(opts);
}

if (opts.body && opts.bodySerializer) {
if (opts.body !== undefined && opts.bodySerializer) {
opts.serializedBody = opts.bodySerializer(opts.body);
}

// remove Content-Type header if body is empty to avoid sending invalid requests
if (opts.serializedBody === undefined || opts.serializedBody === '') {
if (opts.body === undefined || opts.serializedBody === '') {
opts.headers.delete('Content-Type');
}

Expand All @@ -80,7 +80,7 @@ export const createClient = (config: Config = {}): Client => {
const requestInit: ReqInit = {
redirect: 'follow',
...opts,
body: opts.serializedBody,
body: getValidRequestBody(opts),
};

let request = new Request(url, requestInit);
Expand Down Expand Up @@ -212,6 +212,26 @@ export const createClient = (config: Config = {}): Client => {
};
};

function getValidRequestBody(options: ResolvedRequestOptions) {
const hasBody = options.body !== undefined;
const isSerializedBody = hasBody && options.bodySerializer;

if (isSerializedBody) {
const hasSerializedBody =
options.serializedBody !== undefined && options.serializedBody !== '';

return hasSerializedBody ? options.serializedBody : null;
}

// plain/text body
if (hasBody) {
return options.body;
}

// no body was provided
return undefined;
}

const makeMethodFn =
(method: Uppercase<HttpMethod>) => (options: RequestOptions) =>
request({ ...options, method });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ export const createClient = (config: Config = {}): Client => {
await opts.requestValidator(opts);
}

if (opts.body && opts.bodySerializer) {
if (opts.body !== undefined && opts.bodySerializer) {
opts.serializedBody = opts.bodySerializer(opts.body);
}

// remove Content-Type header if body is empty to avoid sending invalid requests
if (opts.serializedBody === undefined || opts.serializedBody === '') {
if (opts.body === undefined || opts.serializedBody === '') {
opts.headers.delete('Content-Type');
}

Expand All @@ -80,7 +80,7 @@ export const createClient = (config: Config = {}): Client => {
const requestInit: ReqInit = {
redirect: 'follow',
...opts,
body: opts.serializedBody,
body: getValidRequestBody(opts),
};

let request = new Request(url, requestInit);
Expand Down Expand Up @@ -212,6 +212,26 @@ export const createClient = (config: Config = {}): Client => {
};
};

function getValidRequestBody(options: ResolvedRequestOptions) {
const hasBody = options.body !== undefined;
const isSerializedBody = hasBody && options.bodySerializer;

if (isSerializedBody) {
const hasSerializedBody =
options.serializedBody !== undefined && options.serializedBody !== '';

return hasSerializedBody ? options.serializedBody : null;
}

// plain/text body
if (hasBody) {
return options.body;
}

// no body was provided
return undefined;
}

const makeMethodFn =
(method: Uppercase<HttpMethod>) => (options: RequestOptions) =>
request({ ...options, method });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ export const createClient = (config: Config = {}): Client => {
await opts.requestValidator(opts);
}

if (opts.body && opts.bodySerializer) {
if (opts.body !== undefined && opts.bodySerializer) {
opts.serializedBody = opts.bodySerializer(opts.body);
}

// remove Content-Type header if body is empty to avoid sending invalid requests
if (opts.serializedBody === undefined || opts.serializedBody === '') {
if (opts.body === undefined || opts.serializedBody === '') {
opts.headers.delete('Content-Type');
}

Expand All @@ -80,7 +80,7 @@ export const createClient = (config: Config = {}): Client => {
const requestInit: ReqInit = {
redirect: 'follow',
...opts,
body: opts.serializedBody,
body: getValidRequestBody(opts),
};

let request = new Request(url, requestInit);
Expand Down Expand Up @@ -212,6 +212,26 @@ export const createClient = (config: Config = {}): Client => {
};
};

function getValidRequestBody(options: ResolvedRequestOptions) {
const hasBody = options.body !== undefined;
const isSerializedBody = hasBody && options.bodySerializer;

if (isSerializedBody) {
const hasSerializedBody =
options.serializedBody !== undefined && options.serializedBody !== '';

return hasSerializedBody ? options.serializedBody : null;
}

// plain/text body
if (hasBody) {
return options.body;
}

// no body was provided
return undefined;
}

const makeMethodFn =
(method: Uppercase<HttpMethod>) => (options: RequestOptions) =>
request({ ...options, method });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ export const createClient = (config: Config = {}): Client => {
await opts.requestValidator(opts);
}

if (opts.body && opts.bodySerializer) {
if (opts.body !== undefined && opts.bodySerializer) {
opts.serializedBody = opts.bodySerializer(opts.body);
}

// remove Content-Type header if body is empty to avoid sending invalid requests
if (opts.serializedBody === undefined || opts.serializedBody === '') {
if (opts.body === undefined || opts.serializedBody === '') {
opts.headers.delete('Content-Type');
}

Expand All @@ -80,7 +80,7 @@ export const createClient = (config: Config = {}): Client => {
const requestInit: ReqInit = {
redirect: 'follow',
...opts,
body: opts.serializedBody,
body: getValidRequestBody(opts),
};

let request = new Request(url, requestInit);
Expand Down Expand Up @@ -212,6 +212,26 @@ export const createClient = (config: Config = {}): Client => {
};
};

function getValidRequestBody(options: ResolvedRequestOptions) {
const hasBody = options.body !== undefined;
const isSerializedBody = hasBody && options.bodySerializer;

if (isSerializedBody) {
const hasSerializedBody =
options.serializedBody !== undefined && options.serializedBody !== '';

return hasSerializedBody ? options.serializedBody : null;
}

// plain/text body
if (hasBody) {
return options.body;
}

// no body was provided
return undefined;
}

const makeMethodFn =
(method: Uppercase<HttpMethod>) => (options: RequestOptions) =>
request({ ...options, method });
Expand Down
Loading
Loading