mirror of
https://github.com/vercel/commerce.git
synced 2025-05-18 15:36:58 +00:00
40 lines
850 B
TypeScript
40 lines
850 B
TypeScript
import { Fetcher } from './utils/types'
|
|
import { FetcherError } from '@commerce/utils/errors'
|
|
import { gateway as MoltinGateway, Moltin } from '@moltin/sdk';
|
|
import epClient from './utils/ep-client'
|
|
|
|
export const fetcher: Fetcher = async ({
|
|
url,
|
|
method = 'POST',
|
|
variables:{params}
|
|
}) => {
|
|
if(url && method) {
|
|
try {
|
|
|
|
|
|
let res = await epClient[url][method](...params);
|
|
|
|
if(res.errors && res.errors > 0) {
|
|
let error = res.errors[0];
|
|
throw new FetcherError({
|
|
message: error.detail,
|
|
status: error.status
|
|
});
|
|
}
|
|
return res;
|
|
} catch(err) {
|
|
|
|
console.log(err);
|
|
|
|
throw new FetcherError({
|
|
message: "Malformed request",
|
|
status: 400
|
|
});
|
|
}
|
|
}
|
|
throw new FetcherError({
|
|
message: "Malformed request",
|
|
status: 400
|
|
});
|
|
}
|