mirror of
https://github.com/luxfi/explore.git
synced 2026-09-24 00:28:18 +00:00
* get client key on 429 error * set key to cookie and pass it in query params * move csrf-token header to useApiFetch * pin host for preview * actual layout * support new changes in API * proxy headers from API * add header to request * remove next.js proxy flag * fix ts * refactor * add tests
31 lines
951 B
TypeScript
31 lines
951 B
TypeScript
import _pick from 'lodash/pick';
|
|
import _pickBy from 'lodash/pickBy';
|
|
import type { NextApiRequest, NextApiResponse } from 'next';
|
|
|
|
import appConfig from 'configs/app/config';
|
|
import fetchFactory from 'lib/api/nodeFetch';
|
|
|
|
const handler = async(nextReq: NextApiRequest, nextRes: NextApiResponse) => {
|
|
if (!nextReq.url) {
|
|
nextRes.status(500).json({ error: 'no url provided' });
|
|
return;
|
|
}
|
|
|
|
const url = new URL(
|
|
nextReq.url.replace(/^\/node-api\/proxy/, ''),
|
|
nextReq.headers['x-endpoint']?.toString() || appConfig.api.endpoint,
|
|
);
|
|
const apiRes = await fetchFactory(nextReq)(
|
|
url.toString(),
|
|
_pickBy(_pick(nextReq, [ 'body', 'method' ]), Boolean),
|
|
);
|
|
|
|
// proxy some headers from API
|
|
nextRes.setHeader('x-request-id', apiRes.headers.get('x-request-id') || '');
|
|
nextRes.setHeader('set-cookie', apiRes.headers.get('set-cookie') || '');
|
|
|
|
nextRes.status(apiRes.status).send(apiRes.body);
|
|
};
|
|
|
|
export default handler;
|