mirror of
https://github.com/luxfi/explore.git
synced 2026-09-24 00:28:18 +00:00
use api token from cookie
This commit is contained in:
+14
-11
@@ -1,18 +1,21 @@
|
||||
import type { NextApiRequest } from 'next';
|
||||
import type { RequestInit, Response } from 'node-fetch';
|
||||
import nodeFetch from 'node-fetch';
|
||||
|
||||
// first arg can be only a string
|
||||
// FIXME migrate to RequestInfo later if needed
|
||||
export default function fetch(path: string, init?: RequestInit): Promise<Response> {
|
||||
const headers = {
|
||||
accept: 'application/json',
|
||||
authorization: `Bearer ${ process.env.API_AUTHORIZATION_TOKEN }`,
|
||||
'content-type': 'application/json',
|
||||
};
|
||||
const url = `https://blockscout.com${ path }`;
|
||||
export default function fetchFactory(_req: NextApiRequest) {
|
||||
return function fetch(path: string, init?: RequestInit): Promise<Response> {
|
||||
const headers = {
|
||||
accept: 'application/json',
|
||||
'content-type': 'application/json',
|
||||
cookie: `_explorer_key=${ _req.cookies._explorer_key }`,
|
||||
};
|
||||
const url = `https://blockscout.com${ path }`;
|
||||
|
||||
return nodeFetch(url, {
|
||||
headers,
|
||||
...init,
|
||||
});
|
||||
return nodeFetch(url, {
|
||||
headers,
|
||||
...init,
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
+6
-4
@@ -1,7 +1,7 @@
|
||||
import { withSentry } from '@sentry/nextjs';
|
||||
import type { NextApiRequest, NextApiResponse } from 'next';
|
||||
|
||||
import fetch from 'lib/api/fetch';
|
||||
import fetchFactory from 'lib/api/fetch';
|
||||
import getUrlWithNetwork from 'lib/api/getUrlWithNetwork';
|
||||
|
||||
type Methods = 'GET' | 'POST' | 'PUT' | 'DELETE';
|
||||
@@ -16,7 +16,8 @@ export default function createHandler(getUrl: (_req: NextApiRequest) => string,
|
||||
|
||||
const isBodyDisallowed = _req.method === 'GET' || _req.method === 'HEAD';
|
||||
|
||||
const url = getUrlWithNetwork(_req, `/api${ getUrl(_req) }`);
|
||||
const url = getUrlWithNetwork(_req, `api${ getUrl(_req) }`);
|
||||
const fetch = fetchFactory(_req);
|
||||
const response = await fetch(url, {
|
||||
method: _req.method,
|
||||
body: isBodyDisallowed ? undefined : _req.body,
|
||||
@@ -29,12 +30,13 @@ export default function createHandler(getUrl: (_req: NextApiRequest) => string,
|
||||
}
|
||||
|
||||
let responseError;
|
||||
const defaultError = { statusText: response.statusText, status: response.status };
|
||||
|
||||
try {
|
||||
const error = await response.json() as { errors: unknown };
|
||||
responseError = error?.errors || {};
|
||||
responseError = error?.errors || defaultError;
|
||||
} catch (error) {
|
||||
responseError = { statusText: response.statusText, status: response.status };
|
||||
responseError = defaultError;
|
||||
}
|
||||
|
||||
res.status(500).json(responseError);
|
||||
|
||||
+1
-1
@@ -22,7 +22,7 @@ const moduleExports = {
|
||||
return [
|
||||
{
|
||||
source: '/',
|
||||
destination: '/xdai/testnet',
|
||||
destination: '/poa/core',
|
||||
permanent: true,
|
||||
},
|
||||
];
|
||||
|
||||
@@ -4,10 +4,11 @@ import type { WatchlistAddresses } from 'types/api/account';
|
||||
import type { Tokenlist } from 'types/api/tokenlist';
|
||||
import type { TWatchlistItem } from 'types/client/account';
|
||||
|
||||
import fetch from 'lib/api/fetch';
|
||||
import fetchFactory from 'lib/api/fetch';
|
||||
import getUrlWithNetwork from 'lib/api/getUrlWithNetwork';
|
||||
|
||||
const watchlistWithTokensHandler = async(_req: NextApiRequest, res: NextApiResponse<Array<TWatchlistItem>>) => {
|
||||
const fetch = fetchFactory(_req);
|
||||
const url = getUrlWithNetwork(_req, 'api/account/v1/user/watchlist');
|
||||
const watchlistResponse = await fetch(url, { method: 'GET' });
|
||||
|
||||
|
||||
@@ -33,7 +33,7 @@ const NetworkMenuLink = ({ name, type, subType, icon, isActive, isMobile, routeN
|
||||
const pathName = `/${ type }${ subType ? '/' + subType : '' }${ localPath }`;
|
||||
|
||||
// will fix later after we agree on CI/CD workflow
|
||||
const href = type === 'xdai' && subType === 'testnet' ? pathName : 'https://blockscout.com' + pathName;
|
||||
const href = type === 'poa' && subType === 'core' ? pathName : 'https://blockscout.com' + pathName;
|
||||
const hasIcon = Boolean(icon);
|
||||
const colors = useColors({ hasIcon });
|
||||
|
||||
|
||||
Reference in New Issue
Block a user