4
0
forked from crowetic/commerce
commerce/framework/swell/fetcher.ts

27 lines
790 B
TypeScript
Raw Normal View History

2021-03-02 21:05:13 -06:00
import { Fetcher } from '@commerce/utils/types'
2021-05-13 16:10:09 +03:00
import { CommerceError } from '@commerce/utils/errors'
import { handleFetchResponse } from './utils'
import swell from './swell'
2021-03-02 21:05:13 -06:00
2021-03-27 15:54:32 -06:00
const fetcher: Fetcher = async ({ method = 'get', variables, query }) => {
async function callSwell() {
if (Array.isArray(variables)) {
const arg1 = variables[0]
const arg2 = variables[1]
2021-05-13 16:10:09 +03:00
const response = await swell[query!][method](arg1, arg2)
2021-03-27 15:54:32 -06:00
return handleFetchResponse(response)
} else {
2021-05-13 16:10:09 +03:00
const response = await swell[query!][method](variables)
2021-03-27 15:54:32 -06:00
return handleFetchResponse(response)
}
}
2021-05-13 16:10:09 +03:00
if (query && query in swell) {
2021-03-27 15:54:32 -06:00
return await callSwell()
2021-05-13 16:10:09 +03:00
} else {
throw new CommerceError({ message: 'Invalid query argument!' })
2021-03-27 15:54:32 -06:00
}
2021-03-02 21:05:13 -06:00
}
export default fetcher