mirror of
https://github.com/movie-web/simple-proxy.git
synced 2025-09-13 16:43:26 +00:00
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "simple-proxy",
|
||||
"private": true,
|
||||
"version": "2.0.0",
|
||||
"version": "2.0.1",
|
||||
"scripts": {
|
||||
"prepare": "nitropack prepare",
|
||||
"dev": "nitropack dev",
|
||||
|
@@ -1,3 +1,4 @@
|
||||
import { getBodyBuffer } from '@/utils/body';
|
||||
import {
|
||||
getProxyHeaders,
|
||||
getAfterResponseHeaders,
|
||||
@@ -11,7 +12,7 @@ export default defineEventHandler(async (event) => {
|
||||
// parse destination URL
|
||||
const destination = getQuery<{ destination?: string }>(event).destination;
|
||||
if (!destination)
|
||||
return sendJson({
|
||||
return await sendJson({
|
||||
event,
|
||||
status: 400,
|
||||
data: {
|
||||
@@ -19,12 +20,16 @@ export default defineEventHandler(async (event) => {
|
||||
},
|
||||
});
|
||||
|
||||
// read body
|
||||
const body = await getBodyBuffer(event);
|
||||
|
||||
// proxy
|
||||
cleanupHeadersBeforeProxy(event);
|
||||
await proxyRequest(event, destination, {
|
||||
fetchOptions: {
|
||||
redirect: 'follow',
|
||||
headers: getProxyHeaders(event.headers),
|
||||
body,
|
||||
},
|
||||
onResponse(outputEvent, response) {
|
||||
const headers = getAfterResponseHeaders(response.headers, response.url);
|
||||
|
13
src/utils/body.ts
Normal file
13
src/utils/body.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import { H3Event } from 'h3';
|
||||
|
||||
export function hasBody(event: H3Event) {
|
||||
const method = event.method.toUpperCase();
|
||||
return ['PUT', 'POST', 'PATCH', 'DELETE'].includes(method);
|
||||
}
|
||||
|
||||
export async function getBodyBuffer(
|
||||
event: H3Event,
|
||||
): Promise<Buffer | undefined> {
|
||||
if (!hasBody(event)) return;
|
||||
return await readRawBody(event, false);
|
||||
}
|
@@ -1,11 +1,10 @@
|
||||
import { H3Event, EventHandlerRequest } from 'h3';
|
||||
|
||||
export function sendJson(ops: {
|
||||
export async function sendJson(ops: {
|
||||
event: H3Event<EventHandlerRequest>;
|
||||
data: Record<string, any>;
|
||||
status?: number;
|
||||
}) {
|
||||
setResponseStatus(ops.event, ops.status ?? 200);
|
||||
appendResponseHeader(ops.event, 'content-type', 'application/json');
|
||||
send(ops.event, JSON.stringify(ops.data, null, 2));
|
||||
await send(ops.event, JSON.stringify(ops.data, null, 2), 'application/json');
|
||||
}
|
||||
|
Reference in New Issue
Block a user