feat(SW-160): update profile

This commit is contained in:
Simon Emanuelsson
2024-07-17 16:12:18 +02:00
committed by Michael Zetterberg
parent b6e22d51a5
commit 2337d37f1a
32 changed files with 459 additions and 244 deletions

View File

@@ -14,6 +14,7 @@ export { endpoints } from "./endpoints"
const defaultOptions: RequestInit = {
cache: "no-store",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
},
mode: "cors",
@@ -25,25 +26,40 @@ const fetch = fetchRetry(global.fetch, {
return Math.pow(2, attempt) * 150 // 150, 300, 600
},
})
const url = new URL(env.API_BASEURL)
export async function get(
endpoint: Endpoint | `${Endpoint}/${string}`,
options: RequestOptionsWithOutBody,
params?: URLSearchParams
params = {}
) {
const url = new URL(
`${env.API_BASEURL}/${endpoint}${params ? `?${params.toString()}` : ""}`
)
url.pathname = endpoint
const searchParams = new URLSearchParams(params)
if (searchParams.size) {
searchParams.forEach((value, key) => {
url.searchParams.set(key, value)
})
url.searchParams.sort()
}
return fetch(url, merge.all([defaultOptions, { method: "GET" }, options]))
}
export async function patch(
endpoint: Endpoint | `${Endpoint}/${string}`,
options: RequestOptionsWithJSONBody
options: RequestOptionsWithJSONBody,
params = {}
) {
const { body, ...requestOptions } = options
url.pathname = endpoint
const searchParams = new URLSearchParams(params)
if (searchParams.size) {
searchParams.forEach((value, key) => {
url.searchParams.set(key, value)
})
url.searchParams.sort()
}
return fetch(
`${env.API_BASEURL}/${endpoint}`,
url,
merge.all([
defaultOptions,
{ body: JSON.stringify(body), method: "PATCH" },
@@ -55,11 +71,19 @@ export async function patch(
export async function post(
endpoint: Endpoint | `${Endpoint}/${string}`,
options: RequestOptionsWithJSONBody,
params?: URLSearchParams
params = {},
) {
const { body, ...requestOptions } = options
url.pathname = endpoint
const searchParams = new URLSearchParams(params)
if (searchParams.size) {
searchParams.forEach((value, key) => {
url.searchParams.set(key, value)
})
url.searchParams.sort()
}
return fetch(
`${env.API_BASEURL}/${endpoint}${params ? `?${params.toString()}` : ""}`,
url,
merge.all([
defaultOptions,
{ body: JSON.stringify(body), method: "POST" },
@@ -71,10 +95,15 @@ export async function post(
export async function remove(
endpoint: Endpoint | `${Endpoint}/${string}`,
options: RequestOptionsWithOutBody,
params?: URLSearchParams
params = {},
) {
return fetch(
`${env.API_BASEURL}/${endpoint}${params ? `?${params.toString()}` : ""}`,
merge.all([defaultOptions, { method: "DELETE" }, options])
)
url.pathname = endpoint
const searchParams = new URLSearchParams(params)
if (searchParams.size) {
searchParams.forEach((value, key) => {
url.searchParams.set(key, value)
})
url.searchParams.sort()
}
return fetch(url, merge.all([defaultOptions, { method: "DELETE" }, options]))
}

View File

@@ -4,6 +4,7 @@ import "dayjs/locale/fi"
import "dayjs/locale/sv"
import d from "dayjs"
import advancedFormat from "dayjs/plugin/advancedFormat"
import isToday from "dayjs/plugin/isToday"
import relativeTime from "dayjs/plugin/relativeTime"
import utc from "dayjs/plugin/utc"
@@ -55,6 +56,7 @@ d.locale("no", {
/**
* If more plugins are needed https://day.js.org/docs/en/plugin/plugin
*/
d.extend(advancedFormat)
d.extend(isToday)
d.extend(relativeTime)
d.extend(utc)