From 49429e8961fa50f8cb9b80b830d3822210065609 Mon Sep 17 00:00:00 2001 From: ogzhanolguncu Date: Mon, 30 Oct 2023 11:32:06 +0300 Subject: [PATCH 1/2] Allow users to import command types --- package.json | 4 ++-- tsup.config.js | 7 ++++++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 16cb6bb3..18df0a4f 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,7 @@ "./**" ], "scripts": { - "build": "tsup && cp README.md ./dist/ && cp package.json ./dist/ && cp LICENSE ./dist/", + "build": "tsup && cp README.md ./dist/ && cp package.json ./dist/ && cp LICENSE ./dist/ && rm -rf ./dist/pkg/commands/*.{js,mjs,d.mts}", "test": "bun test pkg --coverage", "fmt": "bunx @biomejs/biome check --apply ./pkg" }, @@ -51,4 +51,4 @@ "@biomejs/biome": "^1.3.0", "crypto-js": "^4.1.1" } -} \ No newline at end of file +} diff --git a/tsup.config.js b/tsup.config.js index 6d23dc5c..219fb1aa 100644 --- a/tsup.config.js +++ b/tsup.config.js @@ -1,7 +1,12 @@ import { defineConfig } from "tsup"; export default defineConfig({ - entry: ["platforms/nodejs.ts", "platforms/cloudflare.ts", "platforms/fastly.ts"], + entry: [ + "platforms/nodejs.ts", + "platforms/cloudflare.ts", + "platforms/fastly.ts", + "pkg/commands/**/!(*.test).ts", + ], format: ["cjs", "esm"], splitting: false, sourcemap: false, From f60ac8ef1fbefed14cf9bdc5e3a2c21b276d9d53 Mon Sep 17 00:00:00 2001 From: ogzhanolguncu Date: Mon, 30 Oct 2023 12:23:28 +0300 Subject: [PATCH 2/2] Add command types to platforms --- package.json | 2 +- pkg/commands/types.ts | 155 ++++++++++++++++++++++++++++++++++++++++ pkg/redis.ts | 20 +++--- platforms/cloudflare.ts | 8 ++- platforms/fastly.ts | 1 + platforms/nodejs.ts | 2 +- tsup.config.js | 7 +- 7 files changed, 174 insertions(+), 21 deletions(-) create mode 100644 pkg/commands/types.ts diff --git a/package.json b/package.json index 18df0a4f..6c1e7180 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,7 @@ "./**" ], "scripts": { - "build": "tsup && cp README.md ./dist/ && cp package.json ./dist/ && cp LICENSE ./dist/ && rm -rf ./dist/pkg/commands/*.{js,mjs,d.mts}", + "build": "tsup && cp README.md ./dist/ && cp package.json ./dist/ && cp LICENSE ./dist/", "test": "bun test pkg --coverage", "fmt": "bunx @biomejs/biome check --apply ./pkg" }, diff --git a/pkg/commands/types.ts b/pkg/commands/types.ts new file mode 100644 index 00000000..546c990a --- /dev/null +++ b/pkg/commands/types.ts @@ -0,0 +1,155 @@ +export { type AppendCommand } from "./append"; +export { type BitCountCommand } from "./bitcount"; +export { type BitOpCommand } from "./bitop"; +export { type BitPosCommand } from "./bitpos"; +export { type CopyCommand } from "./copy"; +export { type DBSizeCommand } from "./dbsize"; +export { type DecrCommand } from "./decr"; +export { type DecrByCommand } from "./decrby"; +export { type DelCommand } from "./del"; +export { type EchoCommand } from "./echo"; +export { type EvalCommand } from "./eval"; +export { type EvalshaCommand } from "./evalsha"; +export { type ExistsCommand } from "./exists"; +export { type ExpireCommand } from "./expire"; +export { type ExpireAtCommand } from "./expireat"; +export { type FlushAllCommand } from "./flushall"; +export { type FlushDBCommand } from "./flushdb"; +export { type GeoAddCommand, GeoAddCommandOptions, GeoMember } from "./geo_add"; +export { type GeoDistCommand } from "./geo_dist"; +export { type GeoHashCommand } from "./geo_hash"; +export { type GeoPosCommand } from "./geo_pos"; +export { type GeoSearchCommand } from "./geo_search"; +export { type GeoSearchStoreCommand } from "./geo_search_store"; +export { type GetCommand } from "./get"; +export { type GetBitCommand } from "./getbit"; +export { type GetDelCommand } from "./getdel"; +export { type GetRangeCommand } from "./getrange"; +export { type GetSetCommand } from "./getset"; +export { type HDelCommand } from "./hdel"; +export { type HExistsCommand } from "./hexists"; +export { type HGetCommand } from "./hget"; +export { type HGetAllCommand } from "./hgetall"; +export { type HIncrByCommand } from "./hincrby"; +export { type HIncrByFloatCommand } from "./hincrbyfloat"; +export { type HKeysCommand } from "./hkeys"; +export { type HLenCommand } from "./hlen"; +export { type HMGetCommand } from "./hmget"; +export { type HMSetCommand } from "./hmset"; +export { type HRandFieldCommand } from "./hrandfield"; +export { type HScanCommand } from "./hscan"; +export { type HSetCommand } from "./hset"; +export { type HSetNXCommand } from "./hsetnx"; +export { type HStrLenCommand } from "./hstrlen"; +export { type HValsCommand } from "./hvals"; +export { type IncrCommand } from "./incr"; +export { type IncrByCommand } from "./incrby"; +export { type IncrByFloatCommand } from "./incrbyfloat"; +export { type JsonArrAppendCommand } from "./json_arrappend"; +export { type JsonArrIndexCommand } from "./json_arrindex"; +export { type JsonArrInsertCommand } from "./json_arrinsert"; +export { type JsonArrLenCommand } from "./json_arrlen"; +export { type JsonArrPopCommand } from "./json_arrpop"; +export { type JsonArrTrimCommand } from "./json_arrtrim"; +export { type JsonClearCommand } from "./json_clear"; +export { type JsonDelCommand } from "./json_del"; +export { type JsonForgetCommand } from "./json_forget"; +export { type JsonGetCommand } from "./json_get"; +export { type JsonMGetCommand } from "./json_mget"; +export { type JsonNumIncrByCommand } from "./json_numincrby"; +export { type JsonNumMultByCommand } from "./json_nummultby"; +export { type JsonObjKeysCommand } from "./json_objkeys"; +export { type JsonObjLenCommand } from "./json_objlen"; +export { type JsonRespCommand } from "./json_resp"; +export { type JsonSetCommand } from "./json_set"; +export { type JsonStrAppendCommand } from "./json_strappend"; +export { type JsonStrLenCommand } from "./json_strlen"; +export { type JsonToggleCommand } from "./json_toggle"; +export { type JsonTypeCommand } from "./json_type"; +export { type KeysCommand } from "./keys"; +export { type LIndexCommand } from "./lindex"; +export { type LInsertCommand } from "./linsert"; +export { type LLenCommand } from "./llen"; +export { type LMoveCommand } from "./lmove"; +export { type LPopCommand } from "./lpop"; +export { type LPushCommand } from "./lpush"; +export { type LPushXCommand } from "./lpushx"; +export { type LRangeCommand } from "./lrange"; +export { type LRemCommand } from "./lrem"; +export { type LSetCommand } from "./lset"; +export { type LTrimCommand } from "./ltrim"; +export { type MGetCommand } from "./mget"; +export { type MSetCommand } from "./mset"; +export { type MSetNXCommand } from "./msetnx"; +export { type PersistCommand } from "./persist"; +export { type PExpireCommand } from "./pexpire"; +export { type PExpireAtCommand } from "./pexpireat"; +export { type PingCommand } from "./ping"; +export { type PSetEXCommand } from "./psetex"; +export { type PTtlCommand } from "./pttl"; +export { type PublishCommand } from "./publish"; +export { type RandomKeyCommand } from "./randomkey"; +export { type RenameCommand } from "./rename"; +export { type RenameNXCommand } from "./renamenx"; +export { type RPopCommand } from "./rpop"; +export { type RPushCommand } from "./rpush"; +export { type RPushXCommand } from "./rpushx"; +export { type SAddCommand } from "./sadd"; +export { type ScanCommand, ScanCommandOptions } from "./scan"; +export { type SCardCommand } from "./scard"; +export { type ScriptExistsCommand } from "./script_exists"; +export { type ScriptFlushCommand } from "./script_flush"; +export { type ScriptLoadCommand } from "./script_load"; +export { type SDiffCommand } from "./sdiff"; +export { type SDiffStoreCommand } from "./sdiffstore"; +export { type SetCommand, SetCommandOptions } from "./set"; +export { type SetBitCommand } from "./setbit"; +export { type SetExCommand } from "./setex"; +export { type SetNxCommand } from "./setnx"; +export { type SetRangeCommand } from "./setrange"; +export { type SInterCommand } from "./sinter"; +export { type SInterStoreCommand } from "./sinterstore"; +export { type SIsMemberCommand } from "./sismember"; +export { type SMembersCommand } from "./smembers"; +export { type SMIsMemberCommand } from "./smismember"; +export { type SMoveCommand } from "./smove"; +export { type SPopCommand } from "./spop"; +export { type SRandMemberCommand } from "./srandmember"; +export { type SRemCommand } from "./srem"; +export { type SScanCommand } from "./sscan"; +export { type StrLenCommand } from "./strlen"; +export { type SUnionCommand } from "./sunion"; +export { type SUnionStoreCommand } from "./sunionstore"; +export { type TimeCommand } from "./time"; +export { type TouchCommand } from "./touch"; +export { type TtlCommand } from "./ttl"; +export { Type, type TypeCommand } from "./type"; +export { type UnlinkCommand } from "./unlink"; +export { type XAddCommand } from "./xadd"; +export { type XRangeCommand } from "./xrange"; +export { + ScoreMember, + ZAddCommandOptions, + ZAddCommandOptionsWithIncr, + type ZAddCommand, +} from "./zadd"; +export { type ZCardCommand } from "./zcard"; +export { type ZCountCommand } from "./zcount"; +export { type ZDiffStoreCommand } from "./zdiffstore"; +export { type ZIncrByCommand } from "./zincrby"; +export { type ZInterStoreCommand, ZInterStoreCommandOptions } from "./zinterstore"; +export { type ZLexCountCommand } from "./zlexcount"; +export { type ZMScoreCommand } from "./zmscore"; +export { type ZPopMaxCommand } from "./zpopmax"; +export { type ZPopMinCommand } from "./zpopmin"; +export { type ZRangeCommand, ZRangeCommandOptions } from "./zrange"; +export { type ZRankCommand } from "./zrank"; +export { type ZRemCommand } from "./zrem"; +export { type ZRemRangeByLexCommand } from "./zremrangebylex"; +export { type ZRemRangeByRankCommand } from "./zremrangebyrank"; +export { type ZRemRangeByScoreCommand } from "./zremrangebyscore"; +export { type ZRevRankCommand } from "./zrevrank"; +export { type ZScanCommand } from "./zscan"; +export { type ZScoreCommand } from "./zscore"; +export { type ZUnionCommand, ZUnionCommandOptions } from "./zunion"; +export { type ZUnionStoreCommand, ZUnionStoreCommandOptions } from "./zunionstore"; diff --git a/pkg/redis.ts b/pkg/redis.ts index e70b5bd9..62e6331b 100644 --- a/pkg/redis.ts +++ b/pkg/redis.ts @@ -363,8 +363,8 @@ export class Redis { use = ( middleware: ( r: UpstashRequest, - next: (req: UpstashRequest) => Promise>, - ) => Promise>, + next: (req: UpstashRequest) => Promise> + ) => Promise> ) => { const makeRequest = this.client.request.bind(this.client); this.client.request = (req: UpstashRequest) => middleware(req, makeRequest) as any; @@ -447,7 +447,7 @@ export class Redis { ...sourceKeys: string[] ) => new BitOpCommand([op as any, destinationKey, sourceKey, ...sourceKeys], this.opts).exec( - this.client, + this.client ); /** @@ -633,12 +633,12 @@ export class Redis { >( key: string, count: number, - withValues: boolean, + withValues: boolean ): Promise>; } = >( key: string, count?: number, - withValues?: boolean, + withValues?: boolean ) => new HRandFieldCommand([key, count, withValues] as any, this.opts).exec(this.client); /** @@ -1067,19 +1067,19 @@ export class Redis { | [ key: string, opts: ZAddCommandOptions | ZAddCommandOptionsWithIncr, - ...scoreMemberPairs: [ScoreMember, ...ScoreMember[]], + ...scoreMemberPairs: [ScoreMember, ...ScoreMember[]] ] ) => { if ("score" in args[1]) { return new ZAddCommand( [args[0], args[1] as ScoreMember, ...(args.slice(2) as any)], - this.opts, + this.opts ).exec(this.client); } return new ZAddCommand( [args[0], args[1] as any, ...(args.slice(2) as any)], - this.opts, + this.opts ).exec(this.client); }; /** @@ -1146,13 +1146,13 @@ export class Redis { key: string, min: `(${string}` | `[${string}` | "-" | "+", max: `(${string}` | `[${string}` | "-" | "+", - opts: { byLex: true } & ZRangeCommandOptions, + opts: { byLex: true } & ZRangeCommandOptions ] | [ key: string, min: number | `(${number}` | "-inf" | "+inf", max: number | `(${number}` | "-inf" | "+inf", - opts: { byScore: true } & ZRangeCommandOptions, + opts: { byScore: true } & ZRangeCommandOptions ] ) => new ZRangeCommand(args as any, this.opts).exec(this.client); diff --git a/platforms/cloudflare.ts b/platforms/cloudflare.ts index 7df414df..ff158a91 100644 --- a/platforms/cloudflare.ts +++ b/platforms/cloudflare.ts @@ -6,6 +6,8 @@ import { VERSION } from "../version"; type Env = { UPSTASH_DISABLE_TELEMETRY?: string; }; + +export type * from "../pkg/commands/types"; export type { Requester, UpstashRequest, UpstashResponse }; /** * Connection credentials for upstash redis. @@ -82,7 +84,7 @@ export class Redis extends core.Redis { UPSTASH_REDIS_REST_TOKEN: string; UPSTASH_DISABLE_TELEMETRY?: string; }, - opts?: Omit, + opts?: Omit ): Redis { // @ts-ignore These will be defined by cloudflare const url = env?.UPSTASH_REDIS_REST_URL ?? UPSTASH_REDIS_REST_URL; @@ -92,12 +94,12 @@ export class Redis extends core.Redis { if (!url) { throw new Error( - "Unable to find environment variable: `UPSTASH_REDIS_REST_URL`. Please add it via `wrangler secret put UPSTASH_REDIS_REST_URL`", + "Unable to find environment variable: `UPSTASH_REDIS_REST_URL`. Please add it via `wrangler secret put UPSTASH_REDIS_REST_URL`" ); } if (!token) { throw new Error( - "Unable to find environment variable: `UPSTASH_REDIS_REST_TOKEN`. Please add it via `wrangler secret put UPSTASH_REDIS_REST_TOKEN`", + "Unable to find environment variable: `UPSTASH_REDIS_REST_TOKEN`. Please add it via `wrangler secret put UPSTASH_REDIS_REST_TOKEN`" ); } return new Redis({ ...opts, url, token }, env); diff --git a/platforms/fastly.ts b/platforms/fastly.ts index 7e15c95e..b68722c0 100644 --- a/platforms/fastly.ts +++ b/platforms/fastly.ts @@ -3,6 +3,7 @@ import { HttpClient } from "../pkg/http"; import * as core from "../pkg/redis"; import { VERSION } from "../version"; +export type * from "../pkg/commands/types"; export type { Requester, UpstashRequest, UpstashResponse }; /** diff --git a/platforms/nodejs.ts b/platforms/nodejs.ts index c2115a1c..91d52e99 100644 --- a/platforms/nodejs.ts +++ b/platforms/nodejs.ts @@ -18,7 +18,7 @@ if (typeof atob === "undefined") { return Buffer.from(b64, "base64").toString("utf-8"); }; } - +export type * from "../pkg/commands/types"; export type { Requester, UpstashRequest, UpstashResponse }; /** diff --git a/tsup.config.js b/tsup.config.js index 219fb1aa..6d23dc5c 100644 --- a/tsup.config.js +++ b/tsup.config.js @@ -1,12 +1,7 @@ import { defineConfig } from "tsup"; export default defineConfig({ - entry: [ - "platforms/nodejs.ts", - "platforms/cloudflare.ts", - "platforms/fastly.ts", - "pkg/commands/**/!(*.test).ts", - ], + entry: ["platforms/nodejs.ts", "platforms/cloudflare.ts", "platforms/fastly.ts"], format: ["cjs", "esm"], splitting: false, sourcemap: false,