Skip to content

Commit d7d50ce

Browse files
committed
- Simplified the header checks
- Names the bodyMediaType > mediaType, to align with swagger naming convention
1 parent 22f4d28 commit d7d50ce

File tree

10 files changed

+51
-62
lines changed

10 files changed

+51
-62
lines changed

src/openApi/v3/parser/getOperationRequestBody.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,8 @@ export function getOperationRequestBody(openApi: OpenApi, parameter: OpenApiRequ
3333

3434
if (parameter.content) {
3535
const schema = getContent(openApi, parameter.content);
36-
const mediaType = getMediaType(openApi, parameter.content);
3736
if (schema) {
38-
requestBody.mediaType = mediaType;
37+
requestBody.mediaType = getMediaType(openApi, parameter.content);
3938
if (schema?.$ref) {
4039
const model = getType(schema.$ref);
4140
requestBody.export = 'reference';

src/templates/core/ApiRequestOptions.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export type ApiRequestOptions = {
88
readonly query?: Record<string, any>;
99
readonly formData?: Record<string, any>;
1010
readonly body?: any;
11-
readonly bodyMediaType?: string;
11+
readonly mediaType?: string;
1212
readonly responseHeader?: string;
1313
readonly errors?: Record<number, string>;
1414
}

src/templates/core/fetch/getHeaders.hbs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,14 @@ async function getHeaders(options: ApiRequestOptions): Promise<Headers> {
2020
}
2121

2222
if (options.body) {
23-
if (options.bodyMediaType) {
24-
headers.append('Content-Type', options.bodyMediaType);
23+
if (options.mediaType) {
24+
headers.append('Content-Type', options.mediaType);
25+
} else if (isBlob(options.body)) {
26+
headers.append('Content-Type', options.body.type || 'application/octet-stream');
27+
} else if (isString(options.body)) {
28+
headers.append('Content-Type', 'text/plain');
2529
} else {
26-
if (isBlob(options.body)) {
27-
headers.append('Content-Type', options.body.type || 'application/octet-stream');
28-
} else if (isString(options.body)) {
29-
headers.append('Content-Type', 'text/plain');
30-
} else {
31-
headers.append('Content-Type', 'application/json');
32-
}
30+
headers.append('Content-Type', 'application/json');
3331
}
3432
}
3533
return headers;

src/templates/core/fetch/getRequestBody.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ function getRequestBody(options: ApiRequestOptions): BodyInit | undefined {
33
return getFormData(options.formData);
44
}
55
if (options.body) {
6-
if (options.bodyMediaType?.includes('/json')) {
6+
if (options.mediaType?.includes('/json')) {
77
return JSON.stringify(options.body)
88
} else if (isString(options.body) || isBlob(options.body)) {
99
return options.body;

src/templates/core/node/getHeaders.hbs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,14 @@ async function getHeaders(options: ApiRequestOptions): Promise<Headers> {
2020
}
2121

2222
if (options.body) {
23-
if (options.bodyMediaType) {
24-
headers.append('Content-Type', options.bodyMediaType);
23+
if (options.mediaType) {
24+
headers.append('Content-Type', options.mediaType);
25+
} else if (isBinary(options.body)) {
26+
headers.append('Content-Type', 'application/octet-stream');
27+
} else if (isString(options.body)) {
28+
headers.append('Content-Type', 'text/plain');
2529
} else {
26-
if (isBinary(options.body)) {
27-
headers.append('Content-Type', 'application/octet-stream');
28-
} else if (isString(options.body)) {
29-
headers.append('Content-Type', 'text/plain');
30-
} else {
31-
headers.append('Content-Type', 'application/json');
32-
}
30+
headers.append('Content-Type', 'application/json');
3331
}
3432
}
3533
return headers;

src/templates/core/node/getRequestBody.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ function getRequestBody(options: ApiRequestOptions): BodyInit | undefined {
33
return getFormData(options.formData);
44
}
55
if (options.body) {
6-
if (options.bodyMediaType?.includes('/json')) {
6+
if (options.mediaType?.includes('/json')) {
77
return JSON.stringify(options.body)
88
} else if (isString(options.body) || isBinary(options.body)) {
99
return options.body;

src/templates/core/xhr/getHeaders.hbs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,14 @@ async function getHeaders(options: ApiRequestOptions): Promise<Headers> {
2020
}
2121

2222
if (options.body) {
23-
if (options.bodyMediaType) {
24-
headers.append('Content-Type', options.bodyMediaType);
23+
if (options.mediaType) {
24+
headers.append('Content-Type', options.mediaType);
25+
} else if (isBlob(options.body)) {
26+
headers.append('Content-Type', options.body.type || 'application/octet-stream');
27+
} else if (isString(options.body)) {
28+
headers.append('Content-Type', 'text/plain');
2529
} else {
26-
if (isBlob(options.body)) {
27-
headers.append('Content-Type', options.body.type || 'application/octet-stream');
28-
} else if (isString(options.body)) {
29-
headers.append('Content-Type', 'text/plain');
30-
} else {
31-
headers.append('Content-Type', 'application/json');
32-
}
30+
headers.append('Content-Type', 'application/json');
3331
}
3432
}
3533
return headers;

src/templates/core/xhr/getRequestBody.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ function getRequestBody(options: ApiRequestOptions): any {
33
return getFormData(options.formData);
44
}
55
if (options.body) {
6-
if (options.bodyMediaType?.includes('/json')) {
6+
if (options.mediaType?.includes('/json')) {
77
return JSON.stringify(options.body)
88
} else if (isString(options.body) || isBlob(options.body)) {
99
return options.body;

src/templates/exportService.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export class {{{name}}} {
7070
{{#if parametersBody}}
7171
body: {{{parametersBody.name}}},
7272
{{#if parametersBody.mediaType}}
73-
bodyMediaType: '{{{parametersBody.mediaType}}}',
73+
mediaType: '{{{parametersBody.mediaType}}}',
7474
{{/if}}
7575
{{/if}}
7676
{{#if responseHeader}}

test/__snapshots__/index.spec.js.snap

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export type ApiRequestOptions = {
3535
readonly query?: Record<string, any>;
3636
readonly formData?: Record<string, any>;
3737
readonly body?: any;
38-
readonly bodyMediaType?: string;
38+
readonly mediaType?: string;
3939
readonly responseHeader?: string;
4040
readonly errors?: Record<number, string>;
4141
}"
@@ -181,16 +181,14 @@ async function getHeaders(options: ApiRequestOptions): Promise<Headers> {
181181
}
182182

183183
if (options.body) {
184-
if (options.bodyMediaType) {
185-
headers.append('Content-Type', options.bodyMediaType);
184+
if (options.mediaType) {
185+
headers.append('Content-Type', options.mediaType);
186+
} else if (isBlob(options.body)) {
187+
headers.append('Content-Type', options.body.type || 'application/octet-stream');
188+
} else if (isString(options.body)) {
189+
headers.append('Content-Type', 'text/plain');
186190
} else {
187-
if (isBlob(options.body)) {
188-
headers.append('Content-Type', options.body.type || 'application/octet-stream');
189-
} else if (isString(options.body)) {
190-
headers.append('Content-Type', 'text/plain');
191-
} else {
192-
headers.append('Content-Type', 'application/json');
193-
}
191+
headers.append('Content-Type', 'application/json');
194192
}
195193
}
196194
return headers;
@@ -201,7 +199,7 @@ function getRequestBody(options: ApiRequestOptions): BodyInit | undefined {
201199
return getFormData(options.formData);
202200
}
203201
if (options.body) {
204-
if (options.bodyMediaType?.includes('/json')) {
202+
if (options.mediaType?.includes('/json')) {
205203
return JSON.stringify(options.body)
206204
} else if (isString(options.body) || isBlob(options.body)) {
207205
return options.body;
@@ -2390,7 +2388,7 @@ export type ApiRequestOptions = {
23902388
readonly query?: Record<string, any>;
23912389
readonly formData?: Record<string, any>;
23922390
readonly body?: any;
2393-
readonly bodyMediaType?: string;
2391+
readonly mediaType?: string;
23942392
readonly responseHeader?: string;
23952393
readonly errors?: Record<number, string>;
23962394
}"
@@ -2536,16 +2534,14 @@ async function getHeaders(options: ApiRequestOptions): Promise<Headers> {
25362534
}
25372535

25382536
if (options.body) {
2539-
if (options.bodyMediaType) {
2540-
headers.append('Content-Type', options.bodyMediaType);
2537+
if (options.mediaType) {
2538+
headers.append('Content-Type', options.mediaType);
2539+
} else if (isBlob(options.body)) {
2540+
headers.append('Content-Type', options.body.type || 'application/octet-stream');
2541+
} else if (isString(options.body)) {
2542+
headers.append('Content-Type', 'text/plain');
25412543
} else {
2542-
if (isBlob(options.body)) {
2543-
headers.append('Content-Type', options.body.type || 'application/octet-stream');
2544-
} else if (isString(options.body)) {
2545-
headers.append('Content-Type', 'text/plain');
2546-
} else {
2547-
headers.append('Content-Type', 'application/json');
2548-
}
2544+
headers.append('Content-Type', 'application/json');
25492545
}
25502546
}
25512547
return headers;
@@ -2556,7 +2552,7 @@ function getRequestBody(options: ApiRequestOptions): BodyInit | undefined {
25562552
return getFormData(options.formData);
25572553
}
25582554
if (options.body) {
2559-
if (options.bodyMediaType?.includes('/json')) {
2555+
if (options.mediaType?.includes('/json')) {
25602556
return JSON.stringify(options.body)
25612557
} else if (isString(options.body) || isBlob(options.body)) {
25622558
return options.body;
@@ -4495,7 +4491,7 @@ export class ComplexService {
44954491
method: 'PUT',
44964492
path: \`/api/v\${OpenAPI.VERSION}/complex/\${id}\`,
44974493
body: requestBody,
4498-
bodyMediaType: 'application/json-patch+json',
4494+
mediaType: 'application/json-patch+json',
44994495
});
45004496
return result.body;
45014497
}
@@ -4792,7 +4788,7 @@ export class ParametersService {
47924788
'parameterForm': parameterForm,
47934789
},
47944790
body: requestBody,
4795-
bodyMediaType: 'application/json',
4791+
mediaType: 'application/json',
47964792
});
47974793
return result.body;
47984794
}
@@ -4837,7 +4833,7 @@ export class ParametersService {
48374833
'parameter_form': parameterForm,
48384834
},
48394835
body: requestBody,
4840-
bodyMediaType: 'application/json',
4836+
mediaType: 'application/json',
48414837
});
48424838
return result.body;
48434839
}
@@ -4858,7 +4854,7 @@ export class ParametersService {
48584854
'parameter': parameter,
48594855
},
48604856
body: requestBody,
4861-
bodyMediaType: 'application/json',
4857+
mediaType: 'application/json',
48624858
});
48634859
return result.body;
48644860
}
@@ -4879,7 +4875,7 @@ export class ParametersService {
48794875
'parameter': parameter,
48804876
},
48814877
body: requestBody,
4882-
bodyMediaType: 'application/json',
4878+
mediaType: 'application/json',
48834879
});
48844880
return result.body;
48854881
}
@@ -4908,7 +4904,7 @@ export class RequestBodyService {
49084904
method: 'POST',
49094905
path: \`/api/v\${OpenAPI.VERSION}/requestBody/\`,
49104906
body: requestBody,
4911-
bodyMediaType: 'application/json',
4907+
mediaType: 'application/json',
49124908
});
49134909
return result.body;
49144910
}

0 commit comments

Comments
 (0)