rtk-incubator / rtk-query

Data fetching and caching addon for Redux Toolkit
https://redux-toolkit.js.org/rtk-query/overview
MIT License
626 stars 31 forks source link

Erro no cors RateLimit #181

Closed fera765 closed 3 years ago

fera765 commented 3 years ago

RTK query problem

Inside the api I have the cors imported from the state with app.use(cors ()), but still giving this same error ..

I also made the configuration inside the RTK header

prepareHeaders: (headers, { getState }) => {
      headers.set('Access-Control-Allow-Origin', '*');
      headers.set('Access-Control-Allow-Methods', 'GET, POST, PUT,PATCH, DELETE, OPTIONS');
    return headers;
  },

Access to fetch at 'http://localhost:3333/post/view/5c42f13a-2456-4b1a-9209-d87778303c22' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled. fetchBaseQuery.ts:30 GET http://localhost:3333/post/view/5c42f13a-2456-4b1a-9209-d87778303c22 net::ERR_FAILED

msutkowski commented 3 years ago

Hi there,

Those headers shouldn't be inside of prepareHeaders. You should only need to include credentials for authenticated endpoints from a client perspective.

Double check your server configuration and make sure you can hit your api with a tool like insomnia.rest or postman.

Additional reading: https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS

fera765 commented 3 years ago

Hi there,

Those headers shouldn't be inside of prepareHeaders. You should only need to include credentials for authenticated endpoints from a client perspective.

Double check your server configuration and make sure you can hit your api with a tool like insomnia.rest or postman.

Additional reading: https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS

I was able to debug the application, and I was able to identify that the error is because of RateLimit

fera765 commented 3 years ago

Too many requests per second are blocking, and my server is blocking... These settings are inside my API

import { RateLimiterRedis } from 'rate-limiter-flexible';

const redisClient = redis.createClient({
  host: process.env.REDIS_HOST,
  port: Number(process.env.REDIS_PORT),
  password: process.env.REDIS_PASS,
});

const rate = new RateLimiterRedis({
  storeClient: redisClient,
  keyPrefix: 'middleware',
  points: 5,
  duration: 1,
});

is it wrong configuration, inside RTK-Query?