fix: do not mutate api default options

This commit is contained in:
Michael Zetterberg
2024-05-11 02:09:24 +02:00
parent 21488fec2e
commit 01587d7fd5

View File

@@ -23,11 +23,10 @@ export async function get(
options: RequestOptionsWithOutBody, options: RequestOptionsWithOutBody,
params?: URLSearchParams params?: URLSearchParams
) { ) {
defaultOptions.method = "GET"
const url = new URL( const url = new URL(
`${env.API_BASEURL}/${endpoint}${params ? `?${params.toString()}` : ""}` `${env.API_BASEURL}/${endpoint}${params ? `?${params.toString()}` : ""}`
) )
return fetch(url, merge(defaultOptions, options)) return fetch(url, merge.all([defaultOptions, { method: "GET" }, options]))
} }
export async function patch( export async function patch(
@@ -35,11 +34,13 @@ export async function patch(
options: RequestOptionsWithJSONBody options: RequestOptionsWithJSONBody
) { ) {
const { body, ...requestOptions } = options const { body, ...requestOptions } = options
defaultOptions.body = JSON.stringify(body)
defaultOptions.method = "PATCH"
return fetch( return fetch(
`${env.API_BASEURL}/${endpoint}`, `${env.API_BASEURL}/${endpoint}`,
merge(defaultOptions, requestOptions) merge.all([
defaultOptions,
{ body: JSON.stringify(body), method: "PATCH" },
requestOptions,
])
) )
} }
@@ -48,11 +49,13 @@ export async function post(
options: RequestOptionsWithJSONBody options: RequestOptionsWithJSONBody
) { ) {
const { body, ...requestOptions } = options const { body, ...requestOptions } = options
defaultOptions.body = JSON.stringify(body)
defaultOptions.method = "POST"
return fetch( return fetch(
`${env.API_BASEURL}/${endpoint}`, `${env.API_BASEURL}/${endpoint}`,
merge(defaultOptions, requestOptions) merge.all([
defaultOptions,
{ body: JSON.stringify(body), method: "POST" },
requestOptions,
])
) )
} }
@@ -60,6 +63,8 @@ export async function remove(
endpoint: Endpoint, endpoint: Endpoint,
options: RequestOptionsWithOutBody options: RequestOptionsWithOutBody
) { ) {
defaultOptions.method = "DELETE" return fetch(
return fetch(`${env.API_BASEURL}/${endpoint}`, merge(defaultOptions, options)) `${env.API_BASEURL}/${endpoint}`,
merge.all([defaultOptions, { method: "DELETE" }, options])
)
} }