WIP OrderCloud provider

This commit is contained in:
goncy
2021-08-06 16:23:33 -03:00
parent 0e7e7b7d5f
commit f765590484
50 changed files with 691 additions and 4 deletions

View File

@@ -0,0 +1,43 @@
import { FetcherError } from '@commerce/utils/errors'
import type { OrdercloudConfig } from '../index'
import fetch from './fetch'
const fetchRestApi: (
getConfig: () => OrdercloudConfig
) => <T>(
method: string,
resource: string,
body?: Record<string, unknown>,
fetchOptions?: Record<string, any>
) => Promise<T> =
(getConfig) =>
async <T>(
method: string,
resource: string,
body?: Record<string, unknown>,
fetchOptions?: Record<string, any>
) => {
const { commerceUrl } = getConfig()
const res = await fetch(`${commerceUrl}${resource}`, {
...fetchOptions,
method,
headers: {
...fetchOptions?.headers,
accept: 'application/json, text/plain, */*',
'accept-language': 'es,en;q=0.9,es-ES;q=0.8,fr;q=0.7',
authorization: 'Bearer <your token>',
},
body: body ? JSON.stringify(body) : undefined,
})
if (!res.ok) {
throw new FetcherError({
errors: [{ message: res.statusText }],
status: res.status,
})
}
return (await res.json()) as T
}
export default fetchRestApi