From 2d3deaa90370dc1935ef52c426cc1de38807efe6 Mon Sep 17 00:00:00 2001 From: legobt <6wbvkn0j@anonaddy.me> Date: Tue, 29 Aug 2023 01:22:51 +0000 Subject: [PATCH 1/4] fix(types): Optional JSON params where undefined is not valid --- src/json.ts | 61 ++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 56 insertions(+), 5 deletions(-) diff --git a/src/json.ts b/src/json.ts index 9f10ef71d..da0587d09 100644 --- a/src/json.ts +++ b/src/json.ts @@ -13,7 +13,6 @@ import { nullable, number, object, - omit, optional, record, string, @@ -178,16 +177,42 @@ export type JsonRpcError = OptionalField< >; export const JsonRpcParamsStruct: Struct, null> = - optional(union([record(string(), JsonStruct), array(JsonStruct)])) as any; + union([record(string(), JsonStruct), array(JsonStruct)]); export type JsonRpcParams = Json[] | Record; -export const JsonRpcRequestStruct = object({ +export const JsonRpcRequestStruct: Struct< + { + id: string | number | null; + jsonrpc: '2.0'; + method: string; + params?: Json[] | Record; + }, + { + id: Struct; + jsonrpc: Struct<'2.0', '2.0'>; + method: Struct; + params?: Struct, null>; + } +> = object({ id: JsonRpcIdStruct, jsonrpc: JsonRpcVersionStruct, method: string(), params: JsonRpcParamsStruct, -}); +}) as Struct< + { + id: string | number | null; + jsonrpc: '2.0'; + method: string; + params?: Json[] | Record; + }, + { + id: Struct; + jsonrpc: Struct<'2.0', '2.0'>; + method: Struct; + params?: Struct, null>; + } +>; export type InferWithParams< Type extends Struct, @@ -207,7 +232,33 @@ export type InferWithParams< export type JsonRpcRequest = InferWithParams; -export const JsonRpcNotificationStruct = omit(JsonRpcRequestStruct, ['id']); +export const JsonRpcNotificationStruct: Struct< + { + jsonrpc: '2.0'; + method: string; + params?: Json[] | Record; + }, + { + jsonrpc: Struct<'2.0', '2.0'>; + method: Struct; + params?: Struct, null>; + } +> = object({ + jsonrpc: JsonRpcVersionStruct, + method: string(), + params: JsonRpcParamsStruct, +}) as Struct< + { + jsonrpc: '2.0'; + method: string; + params?: Json[] | Record; + }, + { + jsonrpc: Struct<'2.0', '2.0'>; + method: Struct; + params?: Struct, null>; + } +>; /** * A JSON-RPC notification object. From 728519a3df8f847c98c38fcf1d6521a15414fddc Mon Sep 17 00:00:00 2001 From: legobt <6wbvkn0j@anonaddy.me> Date: Tue, 29 Aug 2023 01:34:17 +0000 Subject: [PATCH 2/4] superstruct-optional in value only; preserve type --- src/json.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/json.ts b/src/json.ts index da0587d09..2e9b4cd18 100644 --- a/src/json.ts +++ b/src/json.ts @@ -198,7 +198,7 @@ export const JsonRpcRequestStruct: Struct< id: JsonRpcIdStruct, jsonrpc: JsonRpcVersionStruct, method: string(), - params: JsonRpcParamsStruct, + params: optional(JsonRpcParamsStruct), }) as Struct< { id: string | number | null; @@ -246,7 +246,7 @@ export const JsonRpcNotificationStruct: Struct< > = object({ jsonrpc: JsonRpcVersionStruct, method: string(), - params: JsonRpcParamsStruct, + params: optional(JsonRpcParamsStruct), }) as Struct< { jsonrpc: '2.0'; From c8306e56f6c11c7f18323868f7995a95ab8202e1 Mon Sep 17 00:00:00 2001 From: legobt <6wbvkn0j@anonaddy.me> Date: Tue, 29 Aug 2023 21:16:02 +0000 Subject: [PATCH 3/4] types: Params always optional in InferWithParams --- src/json.ts | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/src/json.ts b/src/json.ts index 2e9b4cd18..55b4651fb 100644 --- a/src/json.ts +++ b/src/json.ts @@ -217,14 +217,9 @@ export const JsonRpcRequestStruct: Struct< export type InferWithParams< Type extends Struct, Params extends JsonRpcParams, -> = Omit, 'params'> & - (keyof Params extends undefined - ? { - params?: Params; - } - : { - params: Params; - }); +> = Omit, 'params'> & { + params?: Params; +}; /** * A JSON-RPC request object. From 45c2236b291489abd593f49825b7969f20de63e0 Mon Sep 17 00:00:00 2001 From: Maarten Zuidhoorn Date: Wed, 30 Aug 2023 10:18:23 +0200 Subject: [PATCH 4/4] Reduce type duplication by excluding `undefined` --- src/json.ts | 58 +++++------------------------------------------------ 1 file changed, 5 insertions(+), 53 deletions(-) diff --git a/src/json.ts b/src/json.ts index 55b4651fb..3bd142f75 100644 --- a/src/json.ts +++ b/src/json.ts @@ -181,44 +181,18 @@ export const JsonRpcParamsStruct: Struct, null> = export type JsonRpcParams = Json[] | Record; -export const JsonRpcRequestStruct: Struct< - { - id: string | number | null; - jsonrpc: '2.0'; - method: string; - params?: Json[] | Record; - }, - { - id: Struct; - jsonrpc: Struct<'2.0', '2.0'>; - method: Struct; - params?: Struct, null>; - } -> = object({ +export const JsonRpcRequestStruct = object({ id: JsonRpcIdStruct, jsonrpc: JsonRpcVersionStruct, method: string(), params: optional(JsonRpcParamsStruct), -}) as Struct< - { - id: string | number | null; - jsonrpc: '2.0'; - method: string; - params?: Json[] | Record; - }, - { - id: Struct; - jsonrpc: Struct<'2.0', '2.0'>; - method: Struct; - params?: Struct, null>; - } ->; +}); export type InferWithParams< Type extends Struct, Params extends JsonRpcParams, > = Omit, 'params'> & { - params?: Params; + params?: Exclude; }; /** @@ -227,33 +201,11 @@ export type InferWithParams< export type JsonRpcRequest = InferWithParams; -export const JsonRpcNotificationStruct: Struct< - { - jsonrpc: '2.0'; - method: string; - params?: Json[] | Record; - }, - { - jsonrpc: Struct<'2.0', '2.0'>; - method: Struct; - params?: Struct, null>; - } -> = object({ +export const JsonRpcNotificationStruct = object({ jsonrpc: JsonRpcVersionStruct, method: string(), params: optional(JsonRpcParamsStruct), -}) as Struct< - { - jsonrpc: '2.0'; - method: string; - params?: Json[] | Record; - }, - { - jsonrpc: Struct<'2.0', '2.0'>; - method: Struct; - params?: Struct, null>; - } ->; +}); /** * A JSON-RPC notification object.