Commit a88851a4 authored by tom's avatar tom

use api token from cookie

parent 840acb7f
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,
});
};
}
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);
......
......@@ -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 });
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment