fix: remove corsFetch
This commit is contained in:
parent
038fa3b301
commit
eebc334e02
|
@ -1,43 +0,0 @@
|
||||||
import { NextRequest, NextResponse } from "next/server";
|
|
||||||
|
|
||||||
async function handle(
|
|
||||||
req: NextRequest,
|
|
||||||
{ params }: { params: { path: string[] } },
|
|
||||||
) {
|
|
||||||
if (req.method === "OPTIONS") {
|
|
||||||
return NextResponse.json({ body: "OK" }, { status: 200 });
|
|
||||||
}
|
|
||||||
|
|
||||||
const [protocol, ...subpath] = params.path;
|
|
||||||
const targetUrl = `${protocol}://${subpath.join("/")}`;
|
|
||||||
|
|
||||||
const method = req.headers.get("method") ?? undefined;
|
|
||||||
const shouldNotHaveBody = ["get", "head"].includes(
|
|
||||||
method?.toLowerCase() ?? "",
|
|
||||||
);
|
|
||||||
|
|
||||||
const fetchOptions: RequestInit = {
|
|
||||||
headers: {
|
|
||||||
authorization: req.headers.get("authorization") ?? "",
|
|
||||||
},
|
|
||||||
body: shouldNotHaveBody ? null : req.body,
|
|
||||||
method,
|
|
||||||
// @ts-ignore
|
|
||||||
duplex: "half",
|
|
||||||
};
|
|
||||||
|
|
||||||
const fetchResult = await fetch(targetUrl, fetchOptions);
|
|
||||||
|
|
||||||
console.log("[Any Proxy]", targetUrl, {
|
|
||||||
status: fetchResult.status,
|
|
||||||
statusText: fetchResult.statusText,
|
|
||||||
});
|
|
||||||
|
|
||||||
return fetchResult;
|
|
||||||
}
|
|
||||||
|
|
||||||
export const POST = handle;
|
|
||||||
export const GET = handle;
|
|
||||||
export const OPTIONS = handle;
|
|
||||||
|
|
||||||
export const runtime = "edge";
|
|
|
@ -1,6 +1,5 @@
|
||||||
import { STORAGE_KEY } from "@/app/constant";
|
import { STORAGE_KEY } from "@/app/constant";
|
||||||
import { SyncStore } from "@/app/store/sync";
|
import { SyncStore } from "@/app/store/sync";
|
||||||
import { corsFetch } from "../cors";
|
|
||||||
import { chunks } from "../format";
|
import { chunks } from "../format";
|
||||||
|
|
||||||
export type UpstashConfig = SyncStore["upstash"];
|
export type UpstashConfig = SyncStore["upstash"];
|
||||||
|
@ -18,10 +17,9 @@ export function createUpstashClient(store: SyncStore) {
|
||||||
return {
|
return {
|
||||||
async check() {
|
async check() {
|
||||||
try {
|
try {
|
||||||
const res = await corsFetch(this.path(`get/${storeKey}`), {
|
const res = await fetch(this.path(`get/${storeKey}`, proxyUrl), {
|
||||||
method: "GET",
|
method: "GET",
|
||||||
headers: this.headers(),
|
headers: this.headers(),
|
||||||
proxyUrl,
|
|
||||||
});
|
});
|
||||||
console.log("[Upstash] check", res.status, res.statusText);
|
console.log("[Upstash] check", res.status, res.statusText);
|
||||||
return [200].includes(res.status);
|
return [200].includes(res.status);
|
||||||
|
@ -32,10 +30,9 @@ export function createUpstashClient(store: SyncStore) {
|
||||||
},
|
},
|
||||||
|
|
||||||
async redisGet(key: string) {
|
async redisGet(key: string) {
|
||||||
const res = await corsFetch(this.path(`get/${key}`), {
|
const res = await fetch(this.path(`get/${key}`, proxyUrl), {
|
||||||
method: "GET",
|
method: "GET",
|
||||||
headers: this.headers(),
|
headers: this.headers(),
|
||||||
proxyUrl,
|
|
||||||
});
|
});
|
||||||
|
|
||||||
console.log("[Upstash] get key = ", key, res.status, res.statusText);
|
console.log("[Upstash] get key = ", key, res.status, res.statusText);
|
||||||
|
@ -45,11 +42,10 @@ export function createUpstashClient(store: SyncStore) {
|
||||||
},
|
},
|
||||||
|
|
||||||
async redisSet(key: string, value: string) {
|
async redisSet(key: string, value: string) {
|
||||||
const res = await corsFetch(this.path(`set/${key}`), {
|
const res = await fetch(this.path(`set/${key}`, proxyUrl), {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: this.headers(),
|
headers: this.headers(),
|
||||||
body: value,
|
body: value,
|
||||||
proxyUrl,
|
|
||||||
});
|
});
|
||||||
|
|
||||||
console.log("[Upstash] set key = ", key, res.status, res.statusText);
|
console.log("[Upstash] set key = ", key, res.status, res.statusText);
|
||||||
|
@ -84,7 +80,7 @@ export function createUpstashClient(store: SyncStore) {
|
||||||
Authorization: `Bearer ${config.apiKey}`,
|
Authorization: `Bearer ${config.apiKey}`,
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
path(path: string) {
|
path(path: string, proxyUrl: string = "") {
|
||||||
// let url = config.endpoint;
|
// let url = config.endpoint;
|
||||||
|
|
||||||
if (!path.endsWith("/")) {
|
if (!path.endsWith("/")) {
|
||||||
|
@ -94,7 +90,11 @@ export function createUpstashClient(store: SyncStore) {
|
||||||
path = path.slice(1);
|
path = path.slice(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
let url = new URL("/api/upstash/" + path);
|
if (proxyUrl.length > 0 && !proxyUrl.endsWith("/")) {
|
||||||
|
proxyUrl += "/";
|
||||||
|
}
|
||||||
|
|
||||||
|
let url = new URL(proxyUrl + "/api/upstash/" + path);
|
||||||
|
|
||||||
// add query params
|
// add query params
|
||||||
url.searchParams.append("endpoint", config.endpoint);
|
url.searchParams.append("endpoint", config.endpoint);
|
||||||
|
|
|
@ -15,10 +15,9 @@ export function createWebDavClient(store: SyncStore) {
|
||||||
return {
|
return {
|
||||||
async check() {
|
async check() {
|
||||||
try {
|
try {
|
||||||
const res = await corsFetch(this.path(folder), {
|
const res = await fetch(this.path(folder, proxyUrl), {
|
||||||
method: "MKCOL",
|
method: "MKCOL",
|
||||||
headers: this.headers(),
|
headers: this.headers(),
|
||||||
proxyUrl,
|
|
||||||
});
|
});
|
||||||
console.log("[WebDav] check", res.status, res.statusText);
|
console.log("[WebDav] check", res.status, res.statusText);
|
||||||
return [201, 200, 404, 301, 302, 307, 308].includes(res.status);
|
return [201, 200, 404, 301, 302, 307, 308].includes(res.status);
|
||||||
|
@ -30,10 +29,9 @@ export function createWebDavClient(store: SyncStore) {
|
||||||
},
|
},
|
||||||
|
|
||||||
async get(key: string) {
|
async get(key: string) {
|
||||||
const res = await corsFetch(this.path(fileName), {
|
const res = await fetch(this.path(fileName, proxyUrl), {
|
||||||
method: "GET",
|
method: "GET",
|
||||||
headers: this.headers(),
|
headers: this.headers(),
|
||||||
proxyUrl,
|
|
||||||
});
|
});
|
||||||
|
|
||||||
console.log("[WebDav] get key = ", key, res.status, res.statusText);
|
console.log("[WebDav] get key = ", key, res.status, res.statusText);
|
||||||
|
@ -42,11 +40,10 @@ export function createWebDavClient(store: SyncStore) {
|
||||||
},
|
},
|
||||||
|
|
||||||
async set(key: string, value: string) {
|
async set(key: string, value: string) {
|
||||||
const res = await corsFetch(this.path(fileName), {
|
const res = await fetch(this.path(fileName, proxyUrl), {
|
||||||
method: "PUT",
|
method: "PUT",
|
||||||
headers: this.headers(),
|
headers: this.headers(),
|
||||||
body: value,
|
body: value,
|
||||||
proxyUrl,
|
|
||||||
});
|
});
|
||||||
|
|
||||||
console.log("[WebDav] set key = ", key, res.status, res.statusText);
|
console.log("[WebDav] set key = ", key, res.status, res.statusText);
|
||||||
|
@ -59,7 +56,7 @@ export function createWebDavClient(store: SyncStore) {
|
||||||
authorization: `Basic ${auth}`,
|
authorization: `Basic ${auth}`,
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
path(path: string) {
|
path(path: string, proxyUrl: string = "") {
|
||||||
if (!path.endsWith("/")) {
|
if (!path.endsWith("/")) {
|
||||||
path += "/";
|
path += "/";
|
||||||
}
|
}
|
||||||
|
@ -67,7 +64,11 @@ export function createWebDavClient(store: SyncStore) {
|
||||||
path = path.slice(1);
|
path = path.slice(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
let url = new URL("/api/webdav/" + path);
|
if (proxyUrl.length > 0 && !proxyUrl.endsWith("/")) {
|
||||||
|
proxyUrl += "/";
|
||||||
|
}
|
||||||
|
|
||||||
|
let url = new URL(proxyUrl + "/api/webdav/" + path);
|
||||||
|
|
||||||
// add query params
|
// add query params
|
||||||
url.searchParams.append("endpoint", config.endpoint);
|
url.searchParams.append("endpoint", config.endpoint);
|
||||||
|
|
|
@ -14,37 +14,3 @@ export function corsPath(path: string) {
|
||||||
|
|
||||||
return `${baseUrl}${path}`;
|
return `${baseUrl}${path}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function corsFetch(
|
|
||||||
url: string,
|
|
||||||
options: RequestInit & {
|
|
||||||
proxyUrl?: string;
|
|
||||||
},
|
|
||||||
) {
|
|
||||||
if (!url.startsWith("http")) {
|
|
||||||
throw Error("[CORS Fetch] url must starts with http/https");
|
|
||||||
}
|
|
||||||
|
|
||||||
let proxyUrl = options.proxyUrl ?? corsPath(ApiPath.Cors);
|
|
||||||
if (!proxyUrl.endsWith("/")) {
|
|
||||||
proxyUrl += "/";
|
|
||||||
}
|
|
||||||
|
|
||||||
url = url.replace("://", "/");
|
|
||||||
|
|
||||||
const corsOptions = {
|
|
||||||
...options,
|
|
||||||
method: "POST",
|
|
||||||
headers: options.method
|
|
||||||
? {
|
|
||||||
...options.headers,
|
|
||||||
method: options.method,
|
|
||||||
}
|
|
||||||
: options.headers,
|
|
||||||
};
|
|
||||||
|
|
||||||
const corsUrl = proxyUrl + url;
|
|
||||||
console.info("[CORS] target = ", corsUrl);
|
|
||||||
|
|
||||||
return fetch(corsUrl, corsOptions);
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in New Issue