Skip to content

Commit 1388213

Browse files
implementing request body serialization
1 parent 7f2e77d commit 1388213

File tree

4 files changed

+74
-14
lines changed

4 files changed

+74
-14
lines changed

src/internal/utils/pathparams.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export function GetSimplePathParams(
3030
paramName: string,
3131
paramValue: any
3232
): Map<string, string> {
33-
const pathParams: Map<string, string> = new Map<string, string>();
33+
let pathParams: Map<string, string> = new Map<string, string>();
3434
if (typeof paramValue === "string") pathParams.set(paramName, paramValue);
3535
else pathParams.set(paramName, JSON.stringify(paramValue));
3636
return pathParams;

src/internal/utils/queryparams.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export function GetQueryParamSerializer(
3636
encode: FormSerializer,
3737
};
3838
}
39-
case "default":
39+
default:
4040
// go to next query parameter field, assume first implemented serializer will serialize all query parameters for this request
4141
return;
4242
}

src/internal/utils/requestbody.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
const requestMetadataKey = "request";
2+
3+
export function SerializeRequestBody(request: any): [string, unknown] {
4+
const fieldNames: string[] = Object.getOwnPropertyNames(request);
5+
let [mediaType, requestBody]: [string, unknown] = ["", ""];
6+
fieldNames.forEach((fname) => {
7+
const requestAnn: string = Reflect.getMetadata(
8+
requestMetadataKey,
9+
request,
10+
fname
11+
);
12+
if (requestAnn == null) return;
13+
const requestDecorator: RequestDecorator =
14+
ParseRequestDecorator(requestAnn);
15+
switch (requestDecorator.MediaType) {
16+
case "multipart/form-data":
17+
case "multipart/mixed":
18+
[mediaType, requestBody] = [
19+
requestDecorator.MediaType,
20+
EncodeMultipartFormData(request[fname]),
21+
];
22+
break;
23+
case "application/json":
24+
case "text/json":
25+
default:
26+
[mediaType, requestBody] = [requestDecorator.MediaType, request[fname]];
27+
}
28+
});
29+
return [mediaType, requestBody];
30+
}
31+
32+
function ParseRequestDecorator(requestAnn: string): RequestDecorator {
33+
// example "media_type=multipart/form-data"
34+
const requestDecorator: RequestDecorator = new RequestDecorator(
35+
"application/octet-stream"
36+
);
37+
const [mediaTypeKey, mediaTypeVal]: string[] = requestAnn.split("=");
38+
if (mediaTypeKey === "media_type") requestDecorator.MediaType = mediaTypeVal;
39+
return requestDecorator;
40+
}
41+
42+
class RequestDecorator {
43+
MediaType: string;
44+
constructor(MediaType: string) {
45+
this.MediaType = MediaType;
46+
}
47+
}

src/sdk/sdk.ts

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import * as shared from "./models/shared";
66
import * as operations from "./models/operations";
77
import { ParamsSerializerOptions } from "axios";
88
import { GetQueryParamSerializer } from "internal/utils/queryparams";
9+
import { SerializeRequestBody } from "../internal/utils/requestbody";
910

1011
const Servers = ["http://api.prod.speakeasyapi.dev"] as const;
1112

@@ -1468,9 +1469,12 @@ export class SDK {
14681469
props.PathParams
14691470
);
14701471

1471-
let reqContentType = "application/json";
1472+
let [reqContentType, reqBody]: [string, unknown] = [
1473+
"application/json",
1474+
props.Request,
1475+
];
14721476
try {
1473-
reqContentType = utils.SerializeRequestBody(props);
1477+
[reqContentType, reqBody] = SerializeRequestBody(props);
14741478
} catch (e: unknown) {
14751479
if (e instanceof Error) {
14761480
return new Error("Error serializing request body", {
@@ -1495,7 +1499,7 @@ export class SDK {
14951499
operations.InsertVersionMetadataResponse,
14961500
AxiosResponse<operations.InsertVersionMetadataResponse>,
14971501
unknown
1498-
>(url, {
1502+
>(url, reqBody, {
14991503
headers: headers,
15001504
...config,
15011505
});
@@ -1602,9 +1606,12 @@ export class SDK {
16021606
props.PathParams
16031607
);
16041608

1605-
let reqContentType = "application/json";
1609+
let [reqContentType, reqBody]: [string, unknown] = [
1610+
"application/json",
1611+
props.Request,
1612+
];
16061613
try {
1607-
reqContentType = utils.SerializeRequestBody(props);
1614+
[reqContentType, reqBody] = SerializeRequestBody(props);
16081615
} catch (e: unknown) {
16091616
if (e instanceof Error) {
16101617
return new Error("Error serializing request body", {
@@ -1628,7 +1635,7 @@ export class SDK {
16281635
operations.RegisterSchemaResponse,
16291636
AxiosResponse<operations.RegisterSchemaResponse>,
16301637
unknown
1631-
>(url, {
1638+
>(url, reqBody, {
16321639
headers: headers,
16331640
...config,
16341641
});
@@ -1719,9 +1726,12 @@ export class SDK {
17191726
props.PathParams
17201727
);
17211728

1722-
let reqContentType = "application/json";
1729+
let [reqContentType, reqBody]: [string, unknown] = [
1730+
"application/json",
1731+
props.Request,
1732+
];
17231733
try {
1724-
reqContentType = utils.SerializeRequestBody(props);
1734+
[reqContentType, reqBody] = SerializeRequestBody(props);
17251735
} catch (e: unknown) {
17261736
if (e instanceof Error) {
17271737
return new Error("Error serializing request body", {
@@ -1745,7 +1755,7 @@ export class SDK {
17451755
operations.UpsertApiResponse,
17461756
AxiosResponse<operations.UpsertApiResponse>,
17471757
unknown
1748-
>(url, {
1758+
>(url, reqBody, {
17491759
headers: headers,
17501760
...config,
17511761
});
@@ -1791,9 +1801,12 @@ export class SDK {
17911801
props.PathParams
17921802
);
17931803

1794-
let reqContentType = "application/json";
1804+
let [reqContentType, reqBody]: [string, unknown] = [
1805+
"application/json",
1806+
props.Request,
1807+
];
17951808
try {
1796-
reqContentType = utils.SerializeRequestBody(props);
1809+
[reqContentType, reqBody] = SerializeRequestBody(props);
17971810
} catch (e: unknown) {
17981811
if (e instanceof Error) {
17991812
return new Error("Error serializing request body", {
@@ -1818,7 +1831,7 @@ export class SDK {
18181831
operations.UpsertApiEndpointResponse,
18191832
AxiosResponse<operations.UpsertApiEndpointResponse>,
18201833
unknown
1821-
>(url, {
1834+
>(url, reqBody, {
18221835
headers: headers,
18231836
...config,
18241837
});

0 commit comments

Comments
 (0)