feat(WEB-169): get profile data from API
This commit is contained in:
60
lib/api/index.ts
Normal file
60
lib/api/index.ts
Normal file
@@ -0,0 +1,60 @@
|
||||
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))
|
||||
}
|
||||
Reference in New Issue
Block a user