Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(types): Optional JSON params where undefined is not valid #134

Merged
merged 4 commits into from
Aug 31, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 10 additions & 12 deletions src/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
nullable,
number,
object,
omit,
optional,
record,
string,
Expand Down Expand Up @@ -178,36 +177,35 @@ export type JsonRpcError = OptionalField<
>;

export const JsonRpcParamsStruct: Struct<Json[] | Record<string, Json>, null> =
optional(union([record(string(), JsonStruct), array(JsonStruct)])) as any;
union([record(string(), JsonStruct), array(JsonStruct)]);

export type JsonRpcParams = Json[] | Record<string, Json>;

export const JsonRpcRequestStruct = object({
id: JsonRpcIdStruct,
jsonrpc: JsonRpcVersionStruct,
method: string(),
params: JsonRpcParamsStruct,
params: optional(JsonRpcParamsStruct),
});

export type InferWithParams<
Type extends Struct<any>,
Params extends JsonRpcParams,
> = Omit<Infer<Type>, 'params'> &
(keyof Params extends undefined
? {
params?: Params;
}
: {
params: Params;
});
> = Omit<Infer<Type>, 'params'> & {
params?: Exclude<Params, undefined>;
};

/**
* A JSON-RPC request object.
*/
export type JsonRpcRequest<Params extends JsonRpcParams = JsonRpcParams> =
InferWithParams<typeof JsonRpcRequestStruct, Params>;

export const JsonRpcNotificationStruct = omit(JsonRpcRequestStruct, ['id']);
export const JsonRpcNotificationStruct = object({
jsonrpc: JsonRpcVersionStruct,
method: string(),
params: optional(JsonRpcParamsStruct),
});

/**
* A JSON-RPC notification object.
Expand Down