Skip to content

Commit

Permalink
WIP: fix JsonRpcRequest to be valid json
Browse files Browse the repository at this point in the history
fix/types: JsonRpcParams should be valid JSON values

`undefined` is not valid JSON.

https://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf
(Section 5):

    A JSON value can be an object, array, number, string, true, false, or null.
  • Loading branch information
legobeat committed Aug 24, 2023
1 parent 20bb059 commit 0018d5b
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,14 +180,28 @@ export type JsonRpcError = OptionalField<
export const JsonRpcParamsStruct = optional(
union([record(string(), JsonStruct), array(JsonStruct)]),
);
export type JsonRpcParams = Infer<typeof JsonRpcParamsStruct>;

export const JsonRpcRequestStruct = object({
export type JsonRpcParams = Json[] | Record<string, Json>;

export const JsonRpcRequestStruct: Struct<
{
id: number | null | string;
method: string;
jsonrpc: '2.0';
params?: Json[] | Record<string, Json>;
},
{
id: Struct<string | number | null, null>;
jsonrpc: Struct<'2.0', '2.0'>;
method: Struct<string, null>;
params?: any;
}
> = object({
id: JsonRpcIdStruct,
jsonrpc: JsonRpcVersionStruct,
method: string(),
params: JsonRpcParamsStruct,
});
}) as any;

export type InferWithParams<
Type extends Struct<any>,
Expand Down

0 comments on commit 0018d5b

Please sign in to comment.