Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redis object got conflicted #125

Closed
hckhanh opened this issue Oct 17, 2024 · 21 comments
Closed

Redis object got conflicted #125

hckhanh opened this issue Oct 17, 2024 · 21 comments

Comments

@hckhanh
Copy link

hckhanh commented Oct 17, 2024

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
Copy link
Contributor

Hi @hckhanh,

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

@hckhanh
Copy link
Author

hckhanh commented Oct 23, 2024

Hi @CahidArda, thank you.

It's fixed now.

@hckhanh hckhanh closed this as completed Oct 23, 2024
@hckhanh
Copy link
Author

hckhanh commented Oct 24, 2024

@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 hckhanh reopened this Oct 24, 2024
@hckhanh
Copy link
Author

hckhanh commented Oct 24, 2024

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

@hckhanh
Copy link
Author

hckhanh commented Oct 24, 2024

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

@ytkimirti
Copy link
Contributor

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
Copy link
Author

hckhanh commented Oct 24, 2024

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
Copy link
Contributor

@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
Copy link
Author

hckhanh commented Oct 24, 2024

@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
Copy link
Contributor

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

@hckhanh
Copy link
Author

hckhanh commented Oct 29, 2024

Thanks @ytkimirti

@ytkimirti
Copy link
Contributor

Hey @hckhanh, I tried a few stuff and I couldn't find anything wrong with how we use peer dependencies. I opened an issue in the astro repo.

Meanwhile, a simple // @ts-ignore should be enough

export const ratelimit = new Ratelimit({
  // @ts-ignore
  redis: redis,
  limiter: Ratelimit.slidingWindow(10, "1 m"),
  enableProtection: true,
  analytics: true,
});

@hckhanh
Copy link
Author

hckhanh commented Nov 1, 2024

Thank you @ytkimirti

@rafalzawadzki
Copy link

The same happened to me in a NextJS project.

@upstash/ratelimit 2.0.4
@upstash/redis 1.34.3
lib/utils/rate-limiter.ts:11:3 - error TS2322: Type 'import("---/node_modules/.pnpm/@[email protected]/node_modules/@upstash/redis/nodejs").Redis' is not assignable to type 'Redis'.
  The types returned by 'multi()' are incompatible between these types.
    Type 'import("---/node_modules/.pnpm/@[email protected]/node_modules/@upstash/redis/zmscore-Dc6Llqgr").P<[]>' is not assignable to type 'Pipeline<[]>'.
      Types have separate declarations of a private property 'client'.

11   redis: Redis.fromEnv(),
     ~~~~~

  node_modules/.pnpm/@[email protected]/node_modules/@upstash/ratelimit/dist/index.d.ts:516:5
    516     redis: Redis;
            ~~~~~
    The expected type comes from property 'redis' which is declared here on type 'RegionRatelimitConfig'

lib/utils/rate-limiter.ts:17:3 - error TS2322: Type 'import("---/node_modules/.pnpm/@[email protected]/node_modules/@upstash/redis/nodejs").Redis' is not assignable to type 'Redis'.
  The types returned by 'multi()' are incompatible between these types.
    Type 'import("---/node_modules/.pnpm/@[email protected]/node_modules/@upstash/redis/zmscore-Dc6Llqgr").P<[]>' is not assignable to type 'Pipeline<[]>'.
      Types have separate declarations of a private property 'client'.

17   redis,
     ~~~~~

  node_modules/.pnpm/@[email protected]/node_modules/@upstash/ratelimit/dist/index.d.ts:516:5
    516     redis: Redis;
            ~~~~~
    The expected type comes from property 'redis' which is declared here on type 'RegionRatelimitConfig'

@ytkimirti
Copy link
Contributor

Hey @rafalzawadzki, can you share your nextjs and typescript version with us? I couldn't reproduce it

pnpm list next typescript

@rafalzawadzki
Copy link

Hey @rafalzawadzki, can you share your nextjs and typescript version with us? I couldn't reproduce it

pnpm list next typescript

after some prodding it turned out my project was still using the old package version in node_modules/.pnpm. After I deleted it, it magically resolved to the new one.

this never happened to me before so I wonder if it still may be in any way related to your package?

@ytkimirti
Copy link
Contributor

Hmm interesting, but in the error you shared it seems the versions are correct from the file paths. I am still unsure why this happens.

I will probably make a cast inside our sdk to prevent this from happening again

@rafalzawadzki
Copy link

true! maybe it was just my IDE resolving to the wrong directory upon CMD+click in the terminal.

Nevertheless, here are the versions:

next 14.0.4
typescript 5.6.3

@CahidArda
Copy link
Contributor

Hi @rafalzawadzki and @hckhanh,

do you still experience the issue in ratelimi version 2.0.5?

@hckhanh
Copy link
Author

hckhanh commented Dec 6, 2024

Let me check again

@hckhanh
Copy link
Author

hckhanh commented Dec 6, 2024

@CahidArda finally, it is fixed. Thank you for your support.

@hckhanh hckhanh closed this as completed Dec 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants