mihawk
Version:
A tiny & simple mock server tool, support json,js,cjs,ts(typescript).
20 lines (19 loc) • 579 B
JavaScript
import nodeFetch from 'node-fetch';
export async function jsonRequest(url, options) {
const apiRes = await nodeFetch(url, {
method: 'GET',
...options,
headers: {
...options?.headers,
'Content-Type': 'application/json',
},
});
if (!apiRes.ok || apiRes.status !== 200) {
throw new Error(`HTTP Error: ${apiRes.status}`);
}
if (apiRes.headers.get('content-type') !== 'application/json') {
throw new Error('Invalid content-type');
}
const res = await apiRes.json();
return res;
}