Skip to content

Commit

Permalink
DX-1168: Change signature of SADD (#1250)
Browse files Browse the repository at this point in the history
* feat: change type of SADD to give type error when no members are specified

* chore: format
  • Loading branch information
ytkimirti authored Aug 22, 2024
1 parent 1b5d028 commit 7a572b4
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 17 deletions.
6 changes: 6 additions & 0 deletions pkg/commands/sadd.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,9 @@ test("returns the number of added members", async () => {
const res = await new SAddCommand([key, value1, value2]).exec(client);
expect(res).toEqual(2);
});

test("throws when there are no members", async () => {
const key = newKey();
// @ts-expect-error It should give type error when no members are given
expect(async () => await new SAddCommand([key]).exec(client)).toThrow();
});
5 changes: 4 additions & 1 deletion pkg/commands/sadd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import { Command } from "./command";
* @see https://redis.io/commands/sadd
*/
export class SAddCommand<TData = string> extends Command<number, number> {
constructor(cmd: [key: string, ...members: TData[]], opts?: CommandOptions<number, number>) {
constructor(
cmd: [key: string, member: TData, ...members: TData[]],
opts?: CommandOptions<number, number>
) {
super(["sadd", ...cmd], opts);
}
}
5 changes: 2 additions & 3 deletions pkg/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export interface Requester {
*/
upstashSyncToken?: string;
request: <TResult = unknown>(req: UpstashRequest) => Promise<UpstashResponse<TResult>>;
};
}

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


/**
/**
* We save the new `upstash-sync-token` in the response header to use it in the next request.
*/
if (this.readYourWrites) {
Expand Down
4 changes: 2 additions & 2 deletions pkg/pipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -841,8 +841,8 @@ export class Pipeline<TCommands extends Command<any, any>[] = []> {
/**
* @see https://redis.io/commands/sadd
*/
sadd = <TData>(key: string, ...members: TData[]) =>
this.chain(new SAddCommand<TData>([key, ...members], this.commandOptions));
sadd = <TData>(key: string, member: TData, ...members: TData[]) =>
this.chain(new SAddCommand<TData>([key, member, ...members], this.commandOptions));

/**
* @see https://redis.io/commands/scan
Expand Down
2 changes: 1 addition & 1 deletion pkg/read-your-writes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ describe("Read Your Writes Feature", () => {
new SetCommand([`key${i}`, `value${i}`]).exec(client).then(() => {
expect(client.upstashSyncToken).not.toEqual(currentSync);
currentSync = client.upstashSyncToken;
}),
})
);

await Promise.all(promises);
Expand Down
4 changes: 2 additions & 2 deletions pkg/redis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -943,8 +943,8 @@ export class Redis {
/**
* @see https://redis.io/commands/sadd
*/
sadd = <TData>(key: string, ...members: TData[]) =>
new SAddCommand<TData>([key, ...members], this.opts).exec(this.client);
sadd = <TData>(key: string, member: TData, ...members: TData[]) =>
new SAddCommand<TData>([key, member, ...members], this.opts).exec(this.client);

/**
* @see https://redis.io/commands/scan
Expand Down
12 changes: 8 additions & 4 deletions platforms/cloudflare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ export type RedisConfigCloudflare = {
keepAlive?: boolean;

/**
* 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.
*/
* 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.
*/
readYourWrites?: boolean;
} & core.RedisOptions &
RequesterConfig &
Expand All @@ -56,11 +56,15 @@ export class Redis extends core.Redis {
*/
constructor(config: RedisConfigCloudflare, env?: Env) {
if (!config.url) {
throw new Error(`[Upstash Redis] The 'url' property is missing or undefined in your Redis config.`)
throw new Error(
`[Upstash Redis] The 'url' property is missing or undefined in your Redis config.`
);
}

if (!config.token) {
throw new Error(`[Upstash Redis] The 'token' property is missing or undefined in your Redis config.`)
throw new Error(
`[Upstash Redis] The 'token' property is missing or undefined in your Redis config.`
);
}

if (config.url.startsWith(" ") || config.url.endsWith(" ") || /\r|\n/.test(config.url)) {
Expand Down
8 changes: 6 additions & 2 deletions platforms/fastly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,15 @@ export class Redis extends core.Redis {
*/
constructor(config: RedisConfigFastly) {
if (!config.url) {
throw new Error(`[Upstash Redis] The 'url' property is missing or undefined in your Redis config.`)
throw new Error(
`[Upstash Redis] The 'url' property is missing or undefined in your Redis config.`
);
}

if (!config.token) {
throw new Error(`[Upstash Redis] The 'token' property is missing or undefined in your Redis config.`)
throw new Error(
`[Upstash Redis] The 'token' property is missing or undefined in your Redis config.`
);
}

if (config.url.startsWith(" ") || config.url.endsWith(" ") || /\r|\n/.test(config.url)) {
Expand Down
8 changes: 6 additions & 2 deletions platforms/nodejs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,15 @@ export class Redis extends core.Redis {
}

if (!configOrRequester.url) {
throw new Error(`[Upstash Redis] The 'url' property is missing or undefined in your Redis config.`)
throw new Error(
`[Upstash Redis] The 'url' property is missing or undefined in your Redis config.`
);
}

if (!configOrRequester.token) {
throw new Error(`[Upstash Redis] The 'token' property is missing or undefined in your Redis config.`)
throw new Error(
`[Upstash Redis] The 'token' property is missing or undefined in your Redis config.`
);
}

if (
Expand Down

0 comments on commit 7a572b4

Please sign in to comment.