61 lines
1.4 KiB
TypeScript
61 lines
1.4 KiB
TypeScript
import merge from "deepmerge"
|
|
|
|
import { env } from "@/env/server"
|
|
|
|
import type {
|
|
RequestOptionsWithJSONBody,
|
|
RequestOptionsWithOutBody,
|
|
} from "@/types/fetch"
|
|
import type { Endpoint } from "./endpoints"
|
|
|
|
export { endpoints } from "./endpoints"
|
|
|
|
const defaultOptions: RequestInit = {
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
},
|
|
mode: "cors",
|
|
}
|
|
|
|
export async function get(
|
|
endpoint: Endpoint,
|
|
options: RequestOptionsWithOutBody
|
|
) {
|
|
defaultOptions.method = "GET"
|
|
return fetch(`${env.API_BASEURL}/${endpoint}`, merge(defaultOptions, options))
|
|
}
|
|
|
|
export async function patch(
|
|
endpoint: Endpoint,
|
|
options: RequestOptionsWithJSONBody
|
|
) {
|
|
const { body, ...requestOptions } = options
|
|
defaultOptions.body = JSON.stringify(body)
|
|
defaultOptions.method = "PATCH"
|
|
return fetch(
|
|
`${env.API_BASEURL}/${endpoint}`,
|
|
merge(defaultOptions, requestOptions)
|
|
)
|
|
}
|
|
|
|
export async function post(
|
|
endpoint: Endpoint,
|
|
options: RequestOptionsWithJSONBody
|
|
) {
|
|
const { body, ...requestOptions } = options
|
|
defaultOptions.body = JSON.stringify(body)
|
|
defaultOptions.method = "POST"
|
|
return fetch(
|
|
`${env.API_BASEURL}/${endpoint}`,
|
|
merge(defaultOptions, requestOptions)
|
|
)
|
|
}
|
|
|
|
export async function remove(
|
|
endpoint: Endpoint,
|
|
options: RequestOptionsWithOutBody
|
|
) {
|
|
defaultOptions.method = "DELETE"
|
|
return fetch(`${env.API_BASEURL}/${endpoint}`, merge(defaultOptions, options))
|
|
}
|