Skip to content

Commit 06893f0

Browse files
authored
fix(spop): include string[] in return type (#231)
* fix(spop): include string[] in return type * _doesn't_
1 parent d982970 commit 06893f0

File tree

5 files changed

+34
-2
lines changed

5 files changed

+34
-2
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ Note: `multi` results are strongly-typed only when using typescript 4.0 and abov
113113

114114
The client no longer has an `execMulti` function. Use the `.exec()` method on the multi instance.
115115

116+
## Wrong types? Raise an issue!
117+
118+
Since the types are autogenerated from [redis-doc](https://github.com/redis/redis-doc), which doesn't adhere to a formal schema, they might be incomplete, or in some cases, wrong. The errors are easy enough to fix, but not easy to find, so if you think you've come across a case like this, please [raise an issue](https://github.com/mmkal/handy-redis/issues).
119+
116120
___
117121

118122
## Development

codegen/patches/schema.ts

+20
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export const fixupSchema = (schema: Record<string, JsonSchemaCommand>) => {
77

88
fixSetEnum(clone);
99
fixArrayRepliesManually(clone);
10+
fixBulkStringRepliesManually(clone);
1011

1112
return clone;
1213
};
@@ -40,6 +41,9 @@ function fixSetEnum(schema: Record<string, JsonSchemaCommand>) {
4041
* types to a few of the @array-reply commands, which often end up as `unknown`.
4142
*/
4243
function fixArrayRepliesManually(schema: Record<string, JsonSchemaCommand>) {
44+
/**
45+
* Dictionary of manual "array" schemas. Will likely be added/edited to over time.
46+
*/
4347
const manuallyFixedUp: Record<string, jsonSchema.JSONSchema7 & { type: "array" }> = {
4448
GEOHASH: { type: "array", items: { type: "string" } },
4549
KEYS: { type: "array", items: { type: "string" } },
@@ -70,3 +74,19 @@ function fixArrayRepliesManually(schema: Record<string, JsonSchemaCommand>) {
7074
}
7175
});
7276
}
77+
78+
function fixBulkStringRepliesManually(schema: Record<string, JsonSchemaCommand>) {
79+
/**
80+
* Catch-all bucket for patches to the generated schema. A lot of commands specify their return type
81+
* as `@bulk-string-reply` so a few fixes for those might end up here.
82+
*/
83+
const manuallyFixedUp: Record<string, jsonSchema.JSONSchema7> = {
84+
SPOP: { anyOf: [{ type: "null" }, { type: "string" }, { type: "array", items: { type: "string" } }] },
85+
};
86+
87+
Object.entries(schema).forEach(([name, command]) => {
88+
if (name in manuallyFixedUp) {
89+
command.return = manuallyFixedUp[name] || command.return;
90+
}
91+
});
92+
}

codegen/schema.json

+7-1
Original file line numberDiff line numberDiff line change
@@ -4853,11 +4853,17 @@
48534853
"group": "set",
48544854
"return": {
48554855
"anyOf": [
4856+
{
4857+
"type": "null"
4858+
},
48564859
{
48574860
"type": "string"
48584861
},
48594862
{
4860-
"type": "null"
4863+
"type": "array",
4864+
"items": {
4865+
"type": "string"
4866+
}
48614867
}
48624868
]
48634869
}

src/generated/interface.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -6547,7 +6547,7 @@ export interface Commands {
65476547
*
65486548
* [Full docs](https://redis.io/commands/spop)
65496549
*/
6550-
spop(key: string, count?: number): Promise<string | null>;
6550+
spop(key: string, count?: number): Promise<null | string | Array<string>>;
65516551

65526552
/**
65536553
* Get one or multiple random members from a set

test/types.test.ts

+2
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,6 @@ test("client has promisified redis methods", () => {
2727
expectTypeOf(client.quit).returns.resolves.toBeString();
2828

2929
expectTypeOf(client.end).returns.toEqualTypeOf<void>();
30+
31+
expectTypeOf(client.spop).returns.resolves.toEqualTypeOf<null | string | string[]>();
3032
});

0 commit comments

Comments
 (0)