Commit e6906e3d authored by tom's avatar tom

node-fetch wrapper

parent 0127bc42
import type { RequestInit, Response } from 'node-fetch';
import nodeFetch from 'node-fetch';
// first arg can be only a string
// FIXME migrate to RequestInfo later
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://${ process.env.API_HOST }${ process.env.API_BASE_PATH }${ path }`;
return nodeFetch(url, {
headers,
...init,
})
}
export default function getDefaultHeaders() {
return {
accept: 'application/json',
authorization: `Bearer ${ process.env.API_AUTHORIZATION_TOKEN }`,
'content-type': 'application/json',
}
}
export default function getUrl(path: string) {
return `https://${ process.env.API_HOST }${ process.env.API_BASE_PATH }${ path }`
}
import type { NextApiRequest, NextApiResponse } from 'next'
import fetch from 'node-fetch';
import getDefaultHeaders from 'pages/api-helpers/getDefaultHeaders';
import getUrl from 'pages/api-helpers/getUrl';
import fetch from 'api/utils/fetch';
export default async function handler(_req: NextApiRequest, res: NextApiResponse) {
const url = getUrl('/account/v1/user/tags/address');
const url = '/account/v1/user/tags/address';
switch (_req.method) {
case 'GET': {
const response = await fetch(url, {
method: 'GET',
headers: getDefaultHeaders(),
})
const response = await fetch(url)
const data = await response.json();
res.status(200).json(data)
......@@ -22,7 +17,6 @@ export default async function handler(_req: NextApiRequest, res: NextApiResponse
case 'POST': {
const response = await fetch(url, {
method: 'POST',
headers: getDefaultHeaders(),
body: _req.body,
})
const data = await response.json();
......
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