Skip to content

Commit c0a3187

Browse files
committed
chore(types): use StaticEncode type from TypeBox
This might fix the type recursion issue.
1 parent 68f452d commit c0a3187

File tree

11 files changed

+68
-57
lines changed

11 files changed

+68
-57
lines changed

src/commands/hash.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { Static } from "@sinclair/typebox";
1+
import { Static, StaticEncode } from "@sinclair/typebox";
22
import { RedisCommand, RedisValue } from "../command";
3-
import { RedisField, RedisHash, TRedisHash, Value } from "../key";
3+
import { RedisField, RedisHash, TRedisHash } from "../key";
44

55
/**
66
* Get the value of a field in a hash.
@@ -29,15 +29,15 @@ export function HSET<T extends TRedisHash, TField extends RedisField<T>>(
2929
export function HSET<T extends TRedisHash, TField extends RedisField<T>>(
3030
hash: RedisHash<T>,
3131
field: TField,
32-
value: Value<T[TField]>,
32+
value: StaticEncode<T[TField]>,
3333
): RedisCommand<number>;
3434

3535
/**
3636
* Set the values of multiple fields in a hash.
3737
*/
3838
export function HSET<T extends TRedisHash>(
3939
hash: RedisHash<T>,
40-
values: { [K in RedisField<T>]?: Value<T[K]> },
40+
values: { [K in RedisField<T>]?: StaticEncode<T[K]> },
4141
): RedisCommand<number>;
4242

4343
export function HSET<T extends TRedisHash>(

src/commands/json.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { TSchema } from "@sinclair/typebox";
1+
import { StaticEncode, TSchema } from "@sinclair/typebox";
22
import { RedisCommand } from "../command";
3-
import { RedisKey, Value } from "../key";
3+
import { RedisKey } from "../key";
44
import { encodeModifiers, Modifiers } from "../modifier";
55
import { NX, XX } from "../modifiers";
66

@@ -11,7 +11,7 @@ import { NX, XX } from "../modifiers";
1111
export function SET<T extends TSchema>(
1212
key: RedisKey<T>,
1313
path: string,
14-
value: Value<T>,
14+
value: StaticEncode<T>,
1515
...modifiers: Modifiers<[NX | XX]>
1616
) {
1717
return new RedisCommand<"OK" | null>([
@@ -28,7 +28,7 @@ export function SET<T extends TSchema>(
2828
* @see https://redis.io/commands/json.get/
2929
*/
3030
export function GET<T extends TSchema>(key: RedisKey<T>, paths: string[]) {
31-
return new RedisCommand<Value<T> | undefined>(
31+
return new RedisCommand<StaticEncode<T> | undefined>(
3232
["JSON.GET", key.name, ...paths],
3333
(result) => (result !== null ? key.decode(result) : undefined),
3434
);

src/commands/pubsub.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
import { TSchema } from "@sinclair/typebox";
1+
import { StaticEncode, TSchema } from "@sinclair/typebox";
22
import { RedisChannel } from "../channel";
33
import { RedisCommand } from "../command";
4-
import { Value } from "../key";
54

65
/**
76
* Publish a message to a channel.
87
*/
98
export function PUBLISH<T extends TSchema>(
109
channel: RedisChannel<T>,
11-
message: Value<T>,
10+
message: StaticEncode<T>,
1211
) {
1312
return new RedisCommand<number>([
1413
"PUBLISH",

src/commands/read.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { TSchema } from "@sinclair/typebox";
1+
import { StaticEncode, TSchema } from "@sinclair/typebox";
22
import { RedisCommand } from "../command";
3-
import { RedisKey, Value } from "../key";
3+
import { RedisKey } from "../key";
44
import { RedisModifier } from "../modifier";
55

66
/** Return the previous value stored at this key */
@@ -9,14 +9,14 @@ export type GET = RedisModifier<"GET">;
99
/** Get the value of a key */
1010
export function GET<T extends TSchema>(
1111
key: RedisKey<T>,
12-
): RedisCommand<Value<T> | undefined>;
12+
): RedisCommand<StaticEncode<T> | undefined>;
1313

1414
/** Return the previous value stored at this key */
1515
export function GET(): GET;
1616

1717
export function GET<T extends TSchema>(
1818
key?: RedisKey<T>,
19-
): GET | RedisCommand<Value<T> | undefined> {
19+
): GET | RedisCommand<StaticEncode<T> | undefined> {
2020
if (!key) {
2121
return new RedisModifier("GET");
2222
}

src/commands/set.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import { TSchema } from "@sinclair/typebox";
1+
import { StaticEncode, TSchema } from "@sinclair/typebox";
22
import { RedisCommand } from "../command";
3-
import { RedisSet, Value } from "../key";
3+
import { RedisSet } from "../key";
44

55
/**
66
* Add one or more members to a set
77
*/
88
export function SADD<T extends TSchema>(
99
key: RedisSet<T>,
10-
...members: [Value<T>, ...Value<T>[]]
10+
...members: [StaticEncode<T>, ...StaticEncode<T>[]]
1111
) {
1212
return new RedisCommand<number>([
1313
"SADD",
@@ -29,7 +29,7 @@ export function SCARD<T extends TSchema>(key: RedisSet<T>) {
2929
export function SDIFF<T extends TSchema>(
3030
...keys: [RedisSet<T>, ...RedisSet<T>[]]
3131
) {
32-
return new RedisCommand<Value<T>[]>(
32+
return new RedisCommand<StaticEncode<T>[]>(
3333
["SDIFF", ...keys.map((key) => key.name)],
3434
(reply: unknown[]) => reply.map((value) => keys[0].decode(value)),
3535
);
@@ -41,7 +41,7 @@ export function SDIFF<T extends TSchema>(
4141
export function SINTER<T extends TSchema>(
4242
...keys: [RedisSet<T>, ...RedisSet<T>[]]
4343
) {
44-
return new RedisCommand<Value<T>[]>(
44+
return new RedisCommand<StaticEncode<T>[]>(
4545
["SINTER", ...keys.map((key) => key.name)],
4646
(reply: unknown[]) => reply.map((value) => keys[0].decode(value)),
4747
);
@@ -52,7 +52,7 @@ export function SINTER<T extends TSchema>(
5252
*/
5353
export function SISMEMBER<T extends TSchema>(
5454
key: RedisSet<T>,
55-
member: Value<T>,
55+
member: StaticEncode<T>,
5656
) {
5757
return new RedisCommand<boolean>(
5858
["SISMEMBER", key.name, key.encode(member)],
@@ -64,7 +64,7 @@ export function SISMEMBER<T extends TSchema>(
6464
* Get all members in a set
6565
*/
6666
export function SMEMBERS<T extends TSchema>(key: RedisSet<T>) {
67-
return new RedisCommand<Value<T>[]>(
67+
return new RedisCommand<StaticEncode<T>[]>(
6868
["SMEMBERS", key.name],
6969
(reply: unknown[]) => reply.map((value) => key.decode(value)),
7070
);
@@ -75,18 +75,18 @@ export function SMEMBERS<T extends TSchema>(key: RedisSet<T>) {
7575
*/
7676
export function SPOP<T extends TSchema>(
7777
key: RedisSet<T>,
78-
): RedisCommand<Value<T> | undefined>;
78+
): RedisCommand<StaticEncode<T> | undefined>;
7979

8080
/**
8181
* Remove and return one or multiple random members from a set
8282
*/
8383
export function SPOP<T extends TSchema>(
8484
key: RedisSet<T>,
8585
count: number,
86-
): RedisCommand<Value<T>[]>;
86+
): RedisCommand<StaticEncode<T>[]>;
8787

8888
export function SPOP<T extends TSchema>(key: RedisSet<T>, count?: number) {
89-
return new RedisCommand<Value<T> | Value<T>[] | undefined>(
89+
return new RedisCommand<StaticEncode<T> | StaticEncode<T>[] | undefined>(
9090
count ? ["SPOP", key.name, count.toString()] : ["SPOP", key.name],
9191
(reply) => {
9292
if (reply === null) return undefined;
@@ -102,7 +102,7 @@ export function SPOP<T extends TSchema>(key: RedisSet<T>, count?: number) {
102102
*/
103103
export function SREM<T extends TSchema>(
104104
key: RedisSet<T>,
105-
...members: Value<T>[]
105+
...members: StaticEncode<T>[]
106106
) {
107107
return new RedisCommand<number>([
108108
"SREM",
@@ -115,7 +115,7 @@ export function SREM<T extends TSchema>(
115115
* Return the union of multiple sets
116116
*/
117117
export function SUNION<T extends TSchema>(...keys: RedisSet<T>[]) {
118-
return new RedisCommand<Value<T>[]>(
118+
return new RedisCommand<StaticEncode<T>[]>(
119119
["SUNION", ...keys.map((key) => key.name)],
120120
(reply: unknown[]) => reply.map((value) => keys[0].decode(value)),
121121
);

src/commands/stream.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
import { StaticEncode } from "@sinclair/typebox";
12
import { castArray, isArray } from "radashi";
23
import { RedisCommand, RedisValue } from "../command";
3-
import { Value } from "../key";
44
import { encodeModifiers, Modifiers, Require } from "../modifier";
55
import { BLOCK, COUNT, MAXLEN, MINID, NOACK, NOMKSTREAM } from "../modifiers";
66
import {
@@ -37,7 +37,7 @@ export function XACK(
3737
export function XADD<T extends TRedisStreamEntry>(
3838
stream: RedisStream<T>,
3939
id: "*" | (string & {}),
40-
data: Value<T>,
40+
data: StaticEncode<T>,
4141
...modifiers: Modifiers<[MAXLEN | MINID]>
4242
): RedisCommand<string>;
4343

@@ -50,14 +50,14 @@ export function XADD<T extends TRedisStreamEntry>(
5050
export function XADD<T extends TRedisStreamEntry>(
5151
stream: RedisStream<T>,
5252
id: "*" | (string & {}),
53-
data: Value<T>,
53+
data: StaticEncode<T>,
5454
...modifiers: Modifiers<[Require<NOMKSTREAM>, MAXLEN | MINID]>
5555
): RedisCommand<null>;
5656

5757
export function XADD<T extends TRedisStreamEntry>(
5858
stream: RedisStream<T>,
5959
id: "*" | (string & {}),
60-
data: Value<T>,
60+
data: StaticEncode<T>,
6161
...modifiers: Modifiers
6262
): RedisCommand<any> {
6363
return new RedisCommand([

src/commands/write.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { TNumber, TSchema } from "@sinclair/typebox";
1+
import { StaticEncode, TNumber, TSchema } from "@sinclair/typebox";
22
import { RedisCommand } from "../command";
3-
import { RedisKey, Value } from "../key";
3+
import { RedisKey } from "../key";
44
import { encodeModifiers, Modifiers, Require } from "../modifier";
55
import {
66
EX,
@@ -49,7 +49,7 @@ export function GETEX<T extends TSchema>(
4949
key: RedisKey<T>,
5050
...modifiers: Modifiers<[EX | PX | EXAT | PXAT | PERSIST]>
5151
) {
52-
return new RedisCommand<Value<T> | undefined>(
52+
return new RedisCommand<StaticEncode<T> | undefined>(
5353
["GETEX", key.name, ...encodeModifiers(modifiers)],
5454
(result) => (result !== null ? key.decode(result) : undefined),
5555
);
@@ -60,21 +60,21 @@ export function GETEX<T extends TSchema>(
6060
*/
6161
export function SET<T extends TSchema>(
6262
key: RedisKey<T>,
63-
value: Value<T>,
63+
value: StaticEncode<T>,
6464
...modifiers: Modifiers<
6565
[NX | XX, Require<GET>, EX | PX | EXAT | PXAT | KEEPTTL]
6666
>
67-
): RedisCommand<Value<T> | undefined>;
67+
): RedisCommand<StaticEncode<T> | undefined>;
6868

6969
export function SET<T extends TSchema>(
7070
key: RedisKey<T>,
71-
value: Value<T>,
71+
value: StaticEncode<T>,
7272
...modifiers: Modifiers<[NX | XX, EX | PX | EXAT | PXAT | KEEPTTL]>
7373
): RedisCommand<boolean>;
7474

7575
export function SET(
7676
key: RedisKey<any>,
77-
value: Value<any>,
77+
value: StaticEncode<any>,
7878
...modifiers: Modifiers
7979
): RedisCommand<any> {
8080
return new RedisCommand(

src/key.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Static, StaticDecode, TSchema } from "@sinclair/typebox";
1+
import { StaticEncode, TSchema } from "@sinclair/typebox";
22
import { Decode, Encode } from "@sinclair/typebox/value";
33
import { RedisValue } from "./command";
44
import { RedisTransform } from "./transform";
@@ -8,12 +8,9 @@ export type TRedisHash<TValue extends TSchema = TSchema> = Record<
88
TValue
99
>;
1010

11-
/**
12-
* The value type of a Redis key that points to a primitive value or a hash map.
13-
*/
14-
export type Value<T extends TSchema | TRedisHash> = T extends TSchema
15-
? StaticDecode<T>
16-
: { [K in RedisField<T>]?: StaticDecode<T[K]> };
11+
export type StaticHash<T extends TRedisHash> = {
12+
[K in RedisField<T>]: StaticEncode<T[K]>;
13+
};
1714

1815
/**
1916
* A field name of a Redis key that points to a hash map.
@@ -66,7 +63,7 @@ export class RedisHash<T extends TRedisHash = TRedisHash> extends RedisKey<T> {
6663
*/
6764
encodeField<TField extends RedisField<T>>(
6865
field: TField,
69-
value: Static<T[TField]>,
66+
value: StaticEncode<T[TField]>,
7067
): RedisValue {
7168
// The schema is defined for JS, not Redis, so a "decoded" value
7269
// represents a Redis value.
@@ -79,7 +76,7 @@ export class RedisHash<T extends TRedisHash = TRedisHash> extends RedisKey<T> {
7976
decodeField<TField extends RedisField<T>>(
8077
field: TField,
8178
value: unknown,
82-
): T extends TSchema ? never : Static<T[TField]> {
79+
): T extends TSchema ? never : StaticEncode<T[TField]> {
8380
// The schema is defined for JS, not Redis, so an "encoded" value
8481
// represents a JS value.
8582
return Encode(this.schema[field], value);

src/stream.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1-
import { TObject, TRecord, TSchema, TString } from "@sinclair/typebox";
1+
import {
2+
StaticEncode,
3+
TObject,
4+
TRecord,
5+
TSchema,
6+
TString,
7+
} from "@sinclair/typebox";
28
import { RedisValue } from "./command";
3-
import { Value } from "./key";
49
import { RedisTransform } from "./transform";
510

611
/** Any valid schema for a Redis stream entry. */
@@ -51,7 +56,7 @@ export class RedisConsumerGroup {
5156

5257
export class RedisStreamEntry<T extends TRedisStreamEntry = TRedisStreamEntry> {
5358
declare $$typeof: "RedisStreamEntry";
54-
readonly data: Value<T>;
59+
readonly data: StaticEncode<T>;
5560
constructor(
5661
/** The stream this entry belongs to */
5762
readonly stream: RedisStream<T>,

src/subscriber.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
import { TSchema } from "@sinclair/typebox";
1+
import { StaticEncode, TSchema } from "@sinclair/typebox";
22
import { castArray } from "radashi";
33
import { RedisChannel, RedisChannelPattern } from "./channel";
44
import { RedisClient } from "./client";
55
import { RedisCommand, RedisValue } from "./command";
6-
import { Value } from "./key";
76
import { RedisClientOptions } from "./type";
87
import { stringifyResult } from "./utils/stringify-result";
98

109
export interface SubscribeCallback<T extends TSchema = TSchema> {
1110
(
12-
message: Value<T>,
11+
message: StaticEncode<T>,
1312
channel: string,
1413
pattern: RedisChannel<T> | RedisChannelPattern<T>,
1514
): void;
@@ -181,7 +180,7 @@ export class MessageEvent<T extends TSchema = TSchema> {
181180
#stream: TransformStream;
182181

183182
constructor(
184-
readonly data: Value<T>,
183+
readonly data: StaticEncode<T>,
185184
readonly channel: string,
186185
readonly key: SubscriptionKey<T>,
187186
stream: TransformStream,

0 commit comments

Comments
 (0)