fix(frontend): proxy authenticated non-GET requests through rate limiter
This commit is contained in:
parent
567e794154
commit
a4ca0902a3
2 changed files with 10 additions and 6 deletions
|
@ -1,5 +1,5 @@
|
||||||
import { parse as parseCookie, serialize as serializeCookie } from "cookie";
|
import { parse as parseCookie, serialize as serializeCookie } from "cookie";
|
||||||
import { INTERNAL_API_BASE } from "~/env.server";
|
import { API_BASE, INTERNAL_API_BASE } from "~/env.server";
|
||||||
import { ApiError, ErrorCode } from "./api/error";
|
import { ApiError, ErrorCode } from "./api/error";
|
||||||
import { tokenCookieName } from "~/lib/utils";
|
import { tokenCookieName } from "~/lib/utils";
|
||||||
|
|
||||||
|
@ -11,12 +11,17 @@ export type RequestParams = {
|
||||||
isInternal?: boolean;
|
isInternal?: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export type RequestMethod = "GET" | "POST" | "PATCH" | "DELETE";
|
||||||
|
|
||||||
export async function baseRequest(
|
export async function baseRequest(
|
||||||
method: string,
|
method: RequestMethod,
|
||||||
path: string,
|
path: string,
|
||||||
params: RequestParams = {},
|
params: RequestParams = {},
|
||||||
): Promise<Response> {
|
): Promise<Response> {
|
||||||
const base = params.isInternal ? INTERNAL_API_BASE + "/internal" : INTERNAL_API_BASE + "/v2";
|
// Internal requests, unauthenticated requests, and GET requests bypass the rate limiting proxy.
|
||||||
|
// All other requests go through the proxy, and are rate limited.
|
||||||
|
let base = params.isInternal || !params.token || method === "GET" ? INTERNAL_API_BASE : API_BASE;
|
||||||
|
base += params.isInternal ? "/internal" : "/v2";
|
||||||
|
|
||||||
const url = `${base}${path}`;
|
const url = `${base}${path}`;
|
||||||
const resp = await fetch(url, {
|
const resp = await fetch(url, {
|
||||||
|
@ -43,12 +48,12 @@ export async function baseRequest(
|
||||||
return resp;
|
return resp;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function fastRequest(method: string, path: string, params: RequestParams = {}) {
|
export async function fastRequest(method: RequestMethod, path: string, params: RequestParams = {}) {
|
||||||
await baseRequest(method, path, params);
|
await baseRequest(method, path, params);
|
||||||
}
|
}
|
||||||
|
|
||||||
export default async function serverRequest<T>(
|
export default async function serverRequest<T>(
|
||||||
method: string,
|
method: RequestMethod,
|
||||||
path: string,
|
path: string,
|
||||||
params: RequestParams = {},
|
params: RequestParams = {},
|
||||||
) {
|
) {
|
||||||
|
|
|
@ -5,7 +5,6 @@ import {
|
||||||
Link,
|
Link,
|
||||||
Outlet,
|
Outlet,
|
||||||
useActionData,
|
useActionData,
|
||||||
useFetcher,
|
|
||||||
useRouteLoaderData,
|
useRouteLoaderData,
|
||||||
} from "@remix-run/react";
|
} from "@remix-run/react";
|
||||||
import { loader as settingsLoader } from "../settings/route";
|
import { loader as settingsLoader } from "../settings/route";
|
||||||
|
|
Loading…
Reference in a new issue