mirror of
https://github.com/vercel/commerce.git
synced 2025-07-23 04:36:49 +00:00
initial provider (#2)
This commit is contained in:
committed by
GitHub
parent
5b4d1b57d4
commit
3342d9d1bb
44
framework/medusa/fetcher.ts
Normal file
44
framework/medusa/fetcher.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import { CommerceError } from '@commerce/utils/errors'
|
||||
import { Fetcher } from '@commerce/utils/types'
|
||||
import { callMedusa } from './utils/call-medusa'
|
||||
|
||||
enum Query {
|
||||
Auth = <any>'auth',
|
||||
Carts = <any>'carts',
|
||||
Customers = <any>'customers',
|
||||
Errors = <any>'errors',
|
||||
Orders = <any>'orders',
|
||||
Products = <any>'products',
|
||||
ReturnReasons = <any>'returnReasons',
|
||||
Returns = <any>'returns',
|
||||
ShippingOptions = <any>'shippingOptions',
|
||||
Swaps = <any>'swaps',
|
||||
}
|
||||
|
||||
export const fetcher: Fetcher = async ({ method, query, variables }) => {
|
||||
if (!query) {
|
||||
throw new CommerceError({ message: 'An argument for query is required' })
|
||||
}
|
||||
|
||||
if (!Object.values(Query).includes(query!)) {
|
||||
throw new CommerceError({
|
||||
message: `${query} is not a valid method argument. Available queries are ${Object.keys(
|
||||
Query
|
||||
)
|
||||
.map((k) => Query[k as any])
|
||||
.join(', ')}`,
|
||||
})
|
||||
}
|
||||
|
||||
if (!method) {
|
||||
throw new CommerceError({ message: 'An argument for method is required' })
|
||||
}
|
||||
|
||||
const response = await callMedusa(method, query, variables)
|
||||
|
||||
if (response.statusText === 'OK') {
|
||||
const { data } = response
|
||||
return data
|
||||
}
|
||||
throw response
|
||||
}
|
Reference in New Issue
Block a user