Skip to content

Commit 71d6492

Browse files
authored
chore: centralize openrpc wrapped requests (#2650)
1 parent a434834 commit 71d6492

File tree

8 files changed

+81
-40
lines changed

8 files changed

+81
-40
lines changed

packages/fdr-sdk/src/api-definition/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ export * from "./types";
1414
export * from "./unwrap";
1515
export * from "./url";
1616
export * from "./snippets/backfill";
17+
export * from "./wrapOpenRPCRequest";

packages/fdr-sdk/src/api-definition/snippets/curl.ts

+2-10
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
} from "@fern-api/ui-core-utils";
1010

1111
import type * as Latest from "../latest";
12+
import { wrapOpenRPCRequest } from "../wrapOpenRPCRequest";
1213
import {
1314
SnippetHttpRequest,
1415
SnippetHttpRequestBodyFormValue,
@@ -91,16 +92,7 @@ function getBodyJsonString(
9192
protocol?: Latest.Protocol
9293
): string[] {
9394
if (protocol?.type === "openrpc") {
94-
let params = [];
95-
if (value && typeof value === "object") {
96-
params = Object.values(value);
97-
}
98-
const payload = {
99-
id: 1,
100-
jsonrpc: "2.0",
101-
method: protocol.methodName,
102-
params: params,
103-
};
95+
const payload = wrapOpenRPCRequest(value, protocol.methodName);
10496
const stringifiedValue = JSON.stringify(payload, null, 2).replace(
10597
/'/g,
10698
"\\'"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Wraps a request body in the OpenRPC format.
3+
*
4+
* @param body - The request body to wrap
5+
* @param methodName - The OpenRPC method name
6+
* @returns The wrapped request body in OpenRPC format
7+
*/
8+
export function wrapOpenRPCRequest(
9+
body: unknown,
10+
methodName: string
11+
): {
12+
jsonrpc: string;
13+
method: string;
14+
params: unknown[];
15+
id: number;
16+
} {
17+
let params: unknown[] = [];
18+
if (body && typeof body === "object") {
19+
params = Object.values(body);
20+
}
21+
return {
22+
jsonrpc: "2.0",
23+
method: methodName,
24+
params,
25+
id: 1,
26+
};
27+
}

packages/fern-docs/bundle/src/components/playground/code-snippets/builders/types.ts

+8-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import type { EndpointContext } from "@fern-api/fdr-sdk/api-definition";
2-
import { buildEndpointUrl } from "@fern-api/fdr-sdk/api-definition";
2+
import {
3+
buildEndpointUrl,
4+
wrapOpenRPCRequest,
5+
} from "@fern-api/fdr-sdk/api-definition";
36

47
import {
58
PlaygroundAuthState,
@@ -26,16 +29,10 @@ export abstract class PlaygroundCodeSnippetBuilder {
2629

2730
protected maybeWrapJsonBody(body: unknown): unknown {
2831
if (this.context.endpoint.protocol?.type === "openrpc") {
29-
let params = [];
30-
if (body && typeof body === "object") {
31-
params = Object.values(body);
32-
}
33-
return {
34-
jsonrpc: "2.0",
35-
method: this.context.endpoint.protocol.methodName,
36-
params,
37-
id: 1,
38-
};
32+
return wrapOpenRPCRequest(
33+
body,
34+
this.context.endpoint.protocol.methodName
35+
);
3936
}
4037
return body;
4138
}

packages/fern-docs/bundle/src/components/playground/endpoint/PlaygroundEndpoint.tsx

+5-5
Original file line numberDiff line numberDiff line change
@@ -137,11 +137,11 @@ export const PlaygroundEndpoint = ({
137137
}),
138138
method: endpoint.method,
139139
headers,
140-
body: await serializeFormStateBody(
141-
endpoint.requests?.[0]?.body,
142-
formState.body,
143-
usesApplicationJsonInFormDataValue
144-
),
140+
body: await serializeFormStateBody({
141+
shape: endpoint.requests?.[0]?.body,
142+
body: formState.body,
143+
usesApplicationJsonInFormDataValue,
144+
}),
145145
};
146146
if (endpoint.responses?.[0]?.body.type === "stream") {
147147
const [res, stream] = await executeProxyStream(req, isProxyDisabled);

packages/fern-docs/bundle/src/components/playground/utils/oauth.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,11 @@ export const oAuthClientCredentialReferencedEndpointLoginFlow = async ({
5858
}),
5959
method: endpoint.method,
6060
headers,
61-
body: await serializeFormStateBody(
62-
endpoint.requests?.[0]?.body,
63-
formState.body,
64-
false
65-
),
61+
body: await serializeFormStateBody({
62+
shape: endpoint.requests?.[0]?.body,
63+
body: formState.body,
64+
usesApplicationJsonInFormDataValue: false,
65+
}),
6666
};
6767
const res = await executeProxyRest(req);
6868

packages/fern-docs/bundle/src/components/playground/utils/serialize.test.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,11 @@ describe("serializeFormStateBody", () => {
5151
},
5252
};
5353

54-
const result = await serializeFormStateBody(shape, body, true);
54+
const result = await serializeFormStateBody({
55+
shape,
56+
body,
57+
usesApplicationJsonInFormDataValue: true,
58+
});
5559

5660
expect(result).toEqual({
5761
type: "form-data",
@@ -93,7 +97,11 @@ describe("serializeFormStateBody", () => {
9397
},
9498
};
9599

96-
const result = await serializeFormStateBody(shape, body, true);
100+
const result = await serializeFormStateBody({
101+
shape,
102+
body,
103+
usesApplicationJsonInFormDataValue: true,
104+
});
97105

98106
expect(result).toEqual({
99107
type: "form-data",

packages/fern-docs/bundle/src/components/playground/utils/serialize.ts

+23-7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { compact, flatten } from "es-toolkit/array";
33
import {
44
FormDataField,
55
HttpRequestBodyShape,
6+
Protocol,
7+
wrapOpenRPCRequest,
68
} from "@fern-api/fdr-sdk/api-definition";
79
import { assertNever, isNonNullish } from "@fern-api/ui-core-utils";
810

@@ -14,18 +16,32 @@ import {
1416
SerializableFormDataEntryValue,
1517
} from "../types";
1618

17-
export async function serializeFormStateBody(
18-
shape: HttpRequestBodyShape | undefined,
19-
body: PlaygroundFormStateBody | undefined,
20-
usesApplicationJsonInFormDataValue: boolean
21-
): Promise<ProxyRequest.SerializableBody | undefined> {
19+
export const serializeFormStateBody = async ({
20+
shape,
21+
body,
22+
usesApplicationJsonInFormDataValue,
23+
protocol,
24+
}: {
25+
shape: HttpRequestBodyShape | undefined;
26+
body: PlaygroundFormStateBody | undefined;
27+
usesApplicationJsonInFormDataValue: boolean;
28+
protocol?: Protocol;
29+
}): Promise<ProxyRequest.SerializableBody | undefined> => {
2230
if (shape == null || body == null) {
2331
return undefined;
2432
}
2533

2634
switch (body.type) {
27-
case "json":
35+
case "json": {
36+
if (protocol?.type === "openrpc") {
37+
// Wrap the request body in OpenRPC format
38+
return {
39+
type: "json",
40+
value: wrapOpenRPCRequest(body.value, protocol.methodName),
41+
};
42+
}
2843
return { type: "json", value: body.value };
44+
}
2945
case "form-data": {
3046
const formDataValue: Record<string, SerializableFormDataEntryValue> = {};
3147
for (const [key, value] of Object.entries(body.value)) {
@@ -109,7 +125,7 @@ export async function serializeFormStateBody(
109125
default:
110126
assertNever(body);
111127
}
112-
}
128+
};
113129

114130
async function serializeFile(
115131
file: File | undefined

0 commit comments

Comments
 (0)