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