Commit 1dc819a6 authored by tom goriunov's avatar tom goriunov Committed by GitHub

Merge pull request #115 from blockscout/merge-fix

fix watchlist request
parents ceb2d373 dc810c3e
import type { NextApiRequest } from 'next';
import * as cookies from 'lib/cookies';
export default function getUrlWithNetwork(_req: NextApiRequest, path: string) {
const networkType = _req.cookies[cookies.NAMES.NETWORK_TYPE];
const networkSubType = _req.cookies[cookies.NAMES.NETWORK_SUB_TYPE];
if (!networkType || !networkSubType) {
// eslint-disable-next-line no-console
console.error(`Incorrect network: NETWORK_TYPE=${ networkType } NETWORK_SUB_TYPE=${ networkSubType }`);
}
return `/${ networkType }/${ networkSubType }/${ path }`;
}
import type { NextApiRequest, NextApiResponse } from 'next';
import fetch from 'lib/api/fetch';
import * as cookies from 'lib/cookies';
import getUrlWithNetwork from 'lib/api/getUrlWithNetwork';
type Methods = 'GET' | 'POST' | 'PUT' | 'DELETE';
......@@ -9,15 +9,8 @@ export default function handler<TRes, TErrRes>(getUrl: (_req: NextApiRequest) =>
return async(_req: NextApiRequest, res: NextApiResponse<TRes | TErrRes>) => {
if (_req.method && allowedMethods.includes(_req.method as Methods)) {
const isBodyDisallowed = _req.method === 'GET' || _req.method === 'HEAD';
const networkType = _req.cookies[cookies.NAMES.NETWORK_TYPE];
const networkSubType = _req.cookies[cookies.NAMES.NETWORK_SUB_TYPE];
if (!networkType || !networkSubType) {
// eslint-disable-next-line no-console
console.error(`Incorrect network: NETWORK_TYPE=${ networkType } NETWORK_SUB_TYPE=${ networkSubType }`);
}
const url = `/${ networkType }/${ networkSubType }/api${ getUrl(_req) }`;
const url = getUrlWithNetwork(_req, `/api${ getUrl(_req) }`);
const response = await fetch(url, {
method: _req.method,
body: isBodyDisallowed ? undefined : _req.body,
......
......@@ -5,9 +5,11 @@ import type { Tokenlist } from 'types/api/tokenlist';
import type { TWatchlistItem } from 'types/client/account';
import fetch from 'lib/api/fetch';
import getUrlWithNetwork from 'lib/api/getUrlWithNetwork';
const watchlistWithTokensHandler = async(_req: NextApiRequest, res: NextApiResponse<Array<TWatchlistItem>>) => {
const watchlistResponse = await fetch('/account/v1/user/watchlist', { method: 'GET' });
const url = getUrlWithNetwork(_req, 'api/account/v1/user/watchlist');
const watchlistResponse = await fetch(url, { method: 'GET' });
const watchlistData = await watchlistResponse.json() as WatchlistAddresses;
......
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