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

26 lines
804 B
TypeScript
Raw Normal View History

2021-03-02 21:05:13 -06:00
import { Fetcher } from '@commerce/utils/types'
import { handleFetchResponse } from './utils'
2021-03-27 15:54:32 -06:00
import { swellConfig } from './index'
2021-03-02 21:05:13 -06:00
2021-03-27 15:54:32 -06:00
const fetcher: Fetcher = async ({ method = 'get', variables, query }) => {
const { swell } = swellConfig
async function callSwell() {
if (Array.isArray(variables)) {
const arg1 = variables[0]
const arg2 = variables[1]
2021-04-01 16:32:12 -06:00
// console.log('fetcher', query, method, variables);
2021-03-27 15:54:32 -06:00
const response = await swell[query][method](arg1, arg2)
return handleFetchResponse(response)
} else {
2021-04-01 16:32:12 -06:00
// console.log('fetcher', query, method, variables);
2021-03-27 15:54:32 -06:00
const response = await swell[query][method](variables)
return handleFetchResponse(response)
}
}
if (query in swell) {
2021-03-27 15:54:32 -06:00
return await callSwell()
}
2021-03-02 21:05:13 -06:00
}
export default fetcher