upstash / ratelimit-js

Rate limiting library for serverless runtimes
https://ratelimit-with-upstash.vercel.app/
MIT License
1.72k stars 34 forks source link

Redis object got conflicted #125

Open hckhanh opened 1 week ago

hckhanh commented 1 week ago

I get this error when try to use this package

src/lib/upstash.ts:15:3 - error ts(2322): Type 'import("/Users/khanh/RustRoverProjects/sshare/node_modules/@upstash/redis/nodejs").Redis' is not assignable to type 'Redis'.
  The types returned by 'multi()' are incompatible between these types.

15   redis: ratelimitRedis,
     ~~~~~
import { Ratelimit } from '@upstash/ratelimit'
import { Redis } from '@upstash/redis/cloudflare'
import {
  UPSTASH_RATELIMIT_REDIS_TOKEN,
  UPSTASH_RATELIMIT_REDIS_URL,
} from 'astro:env/server'

export const ratelimitRedis = new Redis({
  token: UPSTASH_RATELIMIT_REDIS_TOKEN,
  url: UPSTASH_RATELIMIT_REDIS_URL,
  enableTelemetry: false,
})

export const verifyPasswordRatelimit = new Ratelimit({
  redis: ratelimitRedis,
  limiter: Ratelimit.slidingWindow(10, '10 s'),
  prefix: 'verify-password',
  analytics: true,
})

package.json

    "@upstash/ratelimit": "2.0.3",
    "@upstash/redis": "1.34.3",
CahidArda commented 5 days ago

Hi @hckhanh,

Can you try using the latest Upstash Redis release? we bumped Upstash Redis which should fix the issue.

hckhanh commented 5 days ago

Hi @CahidArda, thank you.

It's fixed now.

hckhanh commented 4 days ago

@CahidArda I got another error:

src/lib/upstash.ts:19:3 - error ts(2322): Type 'import("/home/runner/work/sshare/sshare/node_modules/@upstash/redis/cloudflare").Redis' is not assignable to type 'Redis'.
  The types returned by 'multi()' are incompatible between these types.

19   redis: ratelimitRedis,
     ~~~~~
hckhanh commented 4 days ago

For now, I have to cast to never to make the compilation go through

hckhanh commented 4 days ago

Do you think that @upstash/redis should be declared as a peer dependency?

ytkimirti commented 4 days ago

Hey @hckhanh, you are right, it should be a peer dependency but it should not cause any problems.

Can you share your ratelimit and redis versions? I couldn't reproduce it with their latest releases

hckhanh commented 4 days ago

This is my config @ytkimirti:

package.json

    "@upstash/ratelimit": "2.0.4",
    "@upstash/redis": "1.34.3",

tsconfig.json

{
  "extends": "astro/tsconfigs/strict",
  "compilerOptions": {
    "jsx": "react-jsx",
    "jsxImportSource": "react",
    "baseUrl": ".",
    "paths": {
      "@/*": ["./src/*"]
    }
  }
}
ytkimirti commented 4 days ago

@hckhanh I still can't reproduce it. Here is the code i tested with. Can you share a codesanbox link? I am not sure what we are doing differently here

import { Ratelimit } from "@upstash/ratelimit";
import { Redis } from "@upstash/redis/cloudflare";

const redis = Redis.fromEnv();

const ratelimit = new Ratelimit({
  limiter: Ratelimit.fixedWindow(10, "10s"),
  redis: redis,
});

console.log(ratelimit);
dependencies:
@upstash/ratelimit 2.0.4
@upstash/redis 1.34.3
astro 4.16.7

devDependencies:
typescript 5.6.2
hckhanh commented 4 days ago

@ytkimirti Did you initialize the application with astro and run astro check?

This is my reproduction: https://stackblitz.com/edit/withastro-astro-gdk7od?file=src%2Flib%2Fupstash.ts

ytkimirti commented 9 hours ago

Yes you are right, thanks a lot for reproduction repo.

It's weird that it only happens with astro-check. I will look into it today