Skip to content

Commit 949b2b0

Browse files
authored
Merge pull request #2605 from franworks/refactor/#2604-move-get-valid-request-body-to-core-utils
refactor(client): moves getValidRequestBody to core-body utils
2 parents 67e066f + aca5a7c commit 949b2b0

File tree

355 files changed

+5644
-3548
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

355 files changed

+5644
-3548
lines changed

.changeset/two-deers-pump.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@hey-api/openapi-ts": patch
3+
---
4+
5+
fix(client): move `getValidRequestBody()` function to `client-core`

packages/openapi-ts-tests/main/test/__snapshots__/2.0.x/body-response-text-plain/client/client.gen.ts

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import { createSseClient } from '../core/serverSentEvents.gen';
44
import type { HttpMethod } from '../core/types.gen';
5+
import { getValidRequestBody } from '../core/utils.gen';
56
import type {
67
Client,
78
Config,
@@ -212,26 +213,6 @@ export const createClient = (config: Config = {}): Client => {
212213
};
213214
};
214215

215-
function getValidRequestBody(options: ResolvedRequestOptions) {
216-
const hasBody = options.body !== undefined;
217-
const isSerializedBody = hasBody && options.bodySerializer;
218-
219-
if (isSerializedBody) {
220-
const hasSerializedBody =
221-
options.serializedBody !== undefined && options.serializedBody !== '';
222-
223-
return hasSerializedBody ? options.serializedBody : null;
224-
}
225-
226-
// plain/text body
227-
if (hasBody) {
228-
return options.body;
229-
}
230-
231-
// no body was provided
232-
return undefined;
233-
}
234-
235216
const makeMethodFn =
236217
(method: Uppercase<HttpMethod>) => (options: RequestOptions) =>
237218
request({ ...options, method });

packages/openapi-ts-tests/main/test/__snapshots__/2.0.x/body-response-text-plain/core/utils.gen.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// This file is auto-generated by @hey-api/openapi-ts
22

3-
import type { QuerySerializer } from './bodySerializer.gen';
3+
import type { BodySerializer, QuerySerializer } from './bodySerializer.gen';
44
import {
55
type ArraySeparatorStyle,
66
serializeArrayParam,
@@ -112,3 +112,32 @@ export const getUrl = ({
112112
}
113113
return url;
114114
};
115+
116+
export function getValidRequestBody(options: {
117+
body?: unknown;
118+
bodySerializer?: BodySerializer | null;
119+
serializedBody?: unknown;
120+
}) {
121+
const hasBody = options.body !== undefined;
122+
const isSerializedBody = hasBody && options.bodySerializer;
123+
124+
if (isSerializedBody) {
125+
if ('serializedBody' in options) {
126+
const hasSerializedBody =
127+
options.serializedBody !== undefined && options.serializedBody !== '';
128+
129+
return hasSerializedBody ? options.serializedBody : null;
130+
}
131+
132+
// not all clients implement a serializedBody property (i.e. client-axios)
133+
return options.body !== '' ? options.body : null;
134+
}
135+
136+
// plain/text body
137+
if (hasBody) {
138+
return options.body;
139+
}
140+
141+
// no body was provided
142+
return undefined;
143+
}

packages/openapi-ts-tests/main/test/__snapshots__/2.0.x/form-data/client/client.gen.ts

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import { createSseClient } from '../core/serverSentEvents.gen';
44
import type { HttpMethod } from '../core/types.gen';
5+
import { getValidRequestBody } from '../core/utils.gen';
56
import type {
67
Client,
78
Config,
@@ -212,26 +213,6 @@ export const createClient = (config: Config = {}): Client => {
212213
};
213214
};
214215

215-
function getValidRequestBody(options: ResolvedRequestOptions) {
216-
const hasBody = options.body !== undefined;
217-
const isSerializedBody = hasBody && options.bodySerializer;
218-
219-
if (isSerializedBody) {
220-
const hasSerializedBody =
221-
options.serializedBody !== undefined && options.serializedBody !== '';
222-
223-
return hasSerializedBody ? options.serializedBody : null;
224-
}
225-
226-
// plain/text body
227-
if (hasBody) {
228-
return options.body;
229-
}
230-
231-
// no body was provided
232-
return undefined;
233-
}
234-
235216
const makeMethodFn =
236217
(method: Uppercase<HttpMethod>) => (options: RequestOptions) =>
237218
request({ ...options, method });

packages/openapi-ts-tests/main/test/__snapshots__/2.0.x/form-data/core/utils.gen.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// This file is auto-generated by @hey-api/openapi-ts
22

3-
import type { QuerySerializer } from './bodySerializer.gen';
3+
import type { BodySerializer, QuerySerializer } from './bodySerializer.gen';
44
import {
55
type ArraySeparatorStyle,
66
serializeArrayParam,
@@ -112,3 +112,32 @@ export const getUrl = ({
112112
}
113113
return url;
114114
};
115+
116+
export function getValidRequestBody(options: {
117+
body?: unknown;
118+
bodySerializer?: BodySerializer | null;
119+
serializedBody?: unknown;
120+
}) {
121+
const hasBody = options.body !== undefined;
122+
const isSerializedBody = hasBody && options.bodySerializer;
123+
124+
if (isSerializedBody) {
125+
if ('serializedBody' in options) {
126+
const hasSerializedBody =
127+
options.serializedBody !== undefined && options.serializedBody !== '';
128+
129+
return hasSerializedBody ? options.serializedBody : null;
130+
}
131+
132+
// not all clients implement a serializedBody property (i.e. client-axios)
133+
return options.body !== '' ? options.body : null;
134+
}
135+
136+
// plain/text body
137+
if (hasBody) {
138+
return options.body;
139+
}
140+
141+
// no body was provided
142+
return undefined;
143+
}

packages/openapi-ts-tests/main/test/__snapshots__/2.0.x/plugins/@hey-api/client-fetch/sdk-nested-classes-instance/client/client.gen.ts

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import { createSseClient } from '../core/serverSentEvents.gen';
44
import type { HttpMethod } from '../core/types.gen';
5+
import { getValidRequestBody } from '../core/utils.gen';
56
import type {
67
Client,
78
Config,
@@ -212,26 +213,6 @@ export const createClient = (config: Config = {}): Client => {
212213
};
213214
};
214215

215-
function getValidRequestBody(options: ResolvedRequestOptions) {
216-
const hasBody = options.body !== undefined;
217-
const isSerializedBody = hasBody && options.bodySerializer;
218-
219-
if (isSerializedBody) {
220-
const hasSerializedBody =
221-
options.serializedBody !== undefined && options.serializedBody !== '';
222-
223-
return hasSerializedBody ? options.serializedBody : null;
224-
}
225-
226-
// plain/text body
227-
if (hasBody) {
228-
return options.body;
229-
}
230-
231-
// no body was provided
232-
return undefined;
233-
}
234-
235216
const makeMethodFn =
236217
(method: Uppercase<HttpMethod>) => (options: RequestOptions) =>
237218
request({ ...options, method });

packages/openapi-ts-tests/main/test/__snapshots__/2.0.x/plugins/@hey-api/client-fetch/sdk-nested-classes-instance/core/utils.gen.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// This file is auto-generated by @hey-api/openapi-ts
22

3-
import type { QuerySerializer } from './bodySerializer.gen';
3+
import type { BodySerializer, QuerySerializer } from './bodySerializer.gen';
44
import {
55
type ArraySeparatorStyle,
66
serializeArrayParam,
@@ -112,3 +112,32 @@ export const getUrl = ({
112112
}
113113
return url;
114114
};
115+
116+
export function getValidRequestBody(options: {
117+
body?: unknown;
118+
bodySerializer?: BodySerializer | null;
119+
serializedBody?: unknown;
120+
}) {
121+
const hasBody = options.body !== undefined;
122+
const isSerializedBody = hasBody && options.bodySerializer;
123+
124+
if (isSerializedBody) {
125+
if ('serializedBody' in options) {
126+
const hasSerializedBody =
127+
options.serializedBody !== undefined && options.serializedBody !== '';
128+
129+
return hasSerializedBody ? options.serializedBody : null;
130+
}
131+
132+
// not all clients implement a serializedBody property (i.e. client-axios)
133+
return options.body !== '' ? options.body : null;
134+
}
135+
136+
// plain/text body
137+
if (hasBody) {
138+
return options.body;
139+
}
140+
141+
// no body was provided
142+
return undefined;
143+
}

packages/openapi-ts-tests/main/test/__snapshots__/2.0.x/plugins/@hey-api/client-fetch/sdk-nested-classes/client/client.gen.ts

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import { createSseClient } from '../core/serverSentEvents.gen';
44
import type { HttpMethod } from '../core/types.gen';
5+
import { getValidRequestBody } from '../core/utils.gen';
56
import type {
67
Client,
78
Config,
@@ -212,26 +213,6 @@ export const createClient = (config: Config = {}): Client => {
212213
};
213214
};
214215

215-
function getValidRequestBody(options: ResolvedRequestOptions) {
216-
const hasBody = options.body !== undefined;
217-
const isSerializedBody = hasBody && options.bodySerializer;
218-
219-
if (isSerializedBody) {
220-
const hasSerializedBody =
221-
options.serializedBody !== undefined && options.serializedBody !== '';
222-
223-
return hasSerializedBody ? options.serializedBody : null;
224-
}
225-
226-
// plain/text body
227-
if (hasBody) {
228-
return options.body;
229-
}
230-
231-
// no body was provided
232-
return undefined;
233-
}
234-
235216
const makeMethodFn =
236217
(method: Uppercase<HttpMethod>) => (options: RequestOptions) =>
237218
request({ ...options, method });

packages/openapi-ts-tests/main/test/__snapshots__/2.0.x/plugins/@hey-api/client-fetch/sdk-nested-classes/core/utils.gen.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// This file is auto-generated by @hey-api/openapi-ts
22

3-
import type { QuerySerializer } from './bodySerializer.gen';
3+
import type { BodySerializer, QuerySerializer } from './bodySerializer.gen';
44
import {
55
type ArraySeparatorStyle,
66
serializeArrayParam,
@@ -112,3 +112,32 @@ export const getUrl = ({
112112
}
113113
return url;
114114
};
115+
116+
export function getValidRequestBody(options: {
117+
body?: unknown;
118+
bodySerializer?: BodySerializer | null;
119+
serializedBody?: unknown;
120+
}) {
121+
const hasBody = options.body !== undefined;
122+
const isSerializedBody = hasBody && options.bodySerializer;
123+
124+
if (isSerializedBody) {
125+
if ('serializedBody' in options) {
126+
const hasSerializedBody =
127+
options.serializedBody !== undefined && options.serializedBody !== '';
128+
129+
return hasSerializedBody ? options.serializedBody : null;
130+
}
131+
132+
// not all clients implement a serializedBody property (i.e. client-axios)
133+
return options.body !== '' ? options.body : null;
134+
}
135+
136+
// plain/text body
137+
if (hasBody) {
138+
return options.body;
139+
}
140+
141+
// no body was provided
142+
return undefined;
143+
}

packages/openapi-ts-tests/main/test/__snapshots__/2.0.x/plugins/@hey-api/sdk/default/client/client.gen.ts

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import { createSseClient } from '../core/serverSentEvents.gen';
44
import type { HttpMethod } from '../core/types.gen';
5+
import { getValidRequestBody } from '../core/utils.gen';
56
import type {
67
Client,
78
Config,
@@ -212,26 +213,6 @@ export const createClient = (config: Config = {}): Client => {
212213
};
213214
};
214215

215-
function getValidRequestBody(options: ResolvedRequestOptions) {
216-
const hasBody = options.body !== undefined;
217-
const isSerializedBody = hasBody && options.bodySerializer;
218-
219-
if (isSerializedBody) {
220-
const hasSerializedBody =
221-
options.serializedBody !== undefined && options.serializedBody !== '';
222-
223-
return hasSerializedBody ? options.serializedBody : null;
224-
}
225-
226-
// plain/text body
227-
if (hasBody) {
228-
return options.body;
229-
}
230-
231-
// no body was provided
232-
return undefined;
233-
}
234-
235216
const makeMethodFn =
236217
(method: Uppercase<HttpMethod>) => (options: RequestOptions) =>
237218
request({ ...options, method });

0 commit comments

Comments
 (0)