Skip to content

Commit 7a572b4

Browse files
authored
DX-1168: Change signature of SADD (#1250)
* feat: change type of SADD to give type error when no members are specified * chore: format
1 parent 1b5d028 commit 7a572b4

File tree

9 files changed

+37
-17
lines changed

9 files changed

+37
-17
lines changed

pkg/commands/sadd.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,9 @@ test("returns the number of added members", async () => {
1414
const res = await new SAddCommand([key, value1, value2]).exec(client);
1515
expect(res).toEqual(2);
1616
});
17+
18+
test("throws when there are no members", async () => {
19+
const key = newKey();
20+
// @ts-expect-error It should give type error when no members are given
21+
expect(async () => await new SAddCommand([key]).exec(client)).toThrow();
22+
});

pkg/commands/sadd.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ import { Command } from "./command";
55
* @see https://redis.io/commands/sadd
66
*/
77
export class SAddCommand<TData = string> extends Command<number, number> {
8-
constructor(cmd: [key: string, ...members: TData[]], opts?: CommandOptions<number, number>) {
8+
constructor(
9+
cmd: [key: string, member: TData, ...members: TData[]],
10+
opts?: CommandOptions<number, number>
11+
) {
912
super(["sadd", ...cmd], opts);
1013
}
1114
}

pkg/http.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export interface Requester {
3131
*/
3232
upstashSyncToken?: string;
3333
request: <TResult = unknown>(req: UpstashRequest) => Promise<UpstashResponse<TResult>>;
34-
};
34+
}
3535

3636
type ResultError = {
3737
result?: string | number | null | (string | number | null)[];
@@ -250,8 +250,7 @@ export class HttpClient implements Requester {
250250
this.upstashSyncToken = headers.get("upstash-sync-token") ?? "";
251251
}
252252

253-
254-
/**
253+
/**
255254
* We save the new `upstash-sync-token` in the response header to use it in the next request.
256255
*/
257256
if (this.readYourWrites) {

pkg/pipeline.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -841,8 +841,8 @@ export class Pipeline<TCommands extends Command<any, any>[] = []> {
841841
/**
842842
* @see https://redis.io/commands/sadd
843843
*/
844-
sadd = <TData>(key: string, ...members: TData[]) =>
845-
this.chain(new SAddCommand<TData>([key, ...members], this.commandOptions));
844+
sadd = <TData>(key: string, member: TData, ...members: TData[]) =>
845+
this.chain(new SAddCommand<TData>([key, member, ...members], this.commandOptions));
846846

847847
/**
848848
* @see https://redis.io/commands/scan

pkg/read-your-writes.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ describe("Read Your Writes Feature", () => {
4343
new SetCommand([`key${i}`, `value${i}`]).exec(client).then(() => {
4444
expect(client.upstashSyncToken).not.toEqual(currentSync);
4545
currentSync = client.upstashSyncToken;
46-
}),
46+
})
4747
);
4848

4949
await Promise.all(promises);

pkg/redis.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -943,8 +943,8 @@ export class Redis {
943943
/**
944944
* @see https://redis.io/commands/sadd
945945
*/
946-
sadd = <TData>(key: string, ...members: TData[]) =>
947-
new SAddCommand<TData>([key, ...members], this.opts).exec(this.client);
946+
sadd = <TData>(key: string, member: TData, ...members: TData[]) =>
947+
new SAddCommand<TData>([key, member, ...members], this.opts).exec(this.client);
948948

949949
/**
950950
* @see https://redis.io/commands/scan

platforms/cloudflare.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ export type RedisConfigCloudflare = {
3232
keepAlive?: boolean;
3333

3434
/**
35-
* When this flag is enabled, any subsequent commands issued by this client are guaranteed to observe the effects of all earlier writes submitted by the same client.
36-
*/
35+
* When this flag is enabled, any subsequent commands issued by this client are guaranteed to observe the effects of all earlier writes submitted by the same client.
36+
*/
3737
readYourWrites?: boolean;
3838
} & core.RedisOptions &
3939
RequesterConfig &
@@ -56,11 +56,15 @@ export class Redis extends core.Redis {
5656
*/
5757
constructor(config: RedisConfigCloudflare, env?: Env) {
5858
if (!config.url) {
59-
throw new Error(`[Upstash Redis] The 'url' property is missing or undefined in your Redis config.`)
59+
throw new Error(
60+
`[Upstash Redis] The 'url' property is missing or undefined in your Redis config.`
61+
);
6062
}
6163

6264
if (!config.token) {
63-
throw new Error(`[Upstash Redis] The 'token' property is missing or undefined in your Redis config.`)
65+
throw new Error(
66+
`[Upstash Redis] The 'token' property is missing or undefined in your Redis config.`
67+
);
6468
}
6569

6670
if (config.url.startsWith(" ") || config.url.endsWith(" ") || /\r|\n/.test(config.url)) {

platforms/fastly.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,15 @@ export class Redis extends core.Redis {
5353
*/
5454
constructor(config: RedisConfigFastly) {
5555
if (!config.url) {
56-
throw new Error(`[Upstash Redis] The 'url' property is missing or undefined in your Redis config.`)
56+
throw new Error(
57+
`[Upstash Redis] The 'url' property is missing or undefined in your Redis config.`
58+
);
5759
}
5860

5961
if (!config.token) {
60-
throw new Error(`[Upstash Redis] The 'token' property is missing or undefined in your Redis config.`)
62+
throw new Error(
63+
`[Upstash Redis] The 'token' property is missing or undefined in your Redis config.`
64+
);
6165
}
6266

6367
if (config.url.startsWith(" ") || config.url.endsWith(" ") || /\r|\n/.test(config.url)) {

platforms/nodejs.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,15 @@ export class Redis extends core.Redis {
102102
}
103103

104104
if (!configOrRequester.url) {
105-
throw new Error(`[Upstash Redis] The 'url' property is missing or undefined in your Redis config.`)
105+
throw new Error(
106+
`[Upstash Redis] The 'url' property is missing or undefined in your Redis config.`
107+
);
106108
}
107109

108110
if (!configOrRequester.token) {
109-
throw new Error(`[Upstash Redis] The 'token' property is missing or undefined in your Redis config.`)
111+
throw new Error(
112+
`[Upstash Redis] The 'token' property is missing or undefined in your Redis config.`
113+
);
110114
}
111115

112116
if (

0 commit comments

Comments
 (0)