Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion api-client/deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"@preact/signals": "npm:@preact/signals@^2.5.1"
},
"name": "@01edu/api-client",
"version": "0.1.3",
"version": "0.1.4",
"license": "MIT",
"exports": { ".": "./mod.ts" },
"compilerOptions": {
Expand Down
20 changes: 15 additions & 5 deletions api-client/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,12 @@ type RequestState<T> =
at: number
}

type ReplacerType = (key: string, value: unknown) => unknown

type Options = {
headers?: HeadersInit
signal?: AbortSignal
replacer?: ReplacerType
}

const withoutBody = new Set([
Expand Down Expand Up @@ -155,7 +158,9 @@ export const makeClient = <T extends GenericRoutes>(baseUrl = ''): {
input?: HandlerIO<T, K>[0] | undefined,
options?: Options | undefined,
) => Promise<HandlerIO<T, K>[1]>
signal: () => RequestState<HandlerIO<T, K>[1]> & {
signal: (
options?: { replacer?: ReplacerType },
) => RequestState<HandlerIO<T, K>[1]> & {
$: Signal<RequestState<HandlerIO<T, K>[1]>>
reset: () => void
fetch: (
Expand All @@ -177,6 +182,7 @@ export const makeClient = <T extends GenericRoutes>(baseUrl = ''): {
const defaultHeaders = { 'Content-Type': 'application/json' }

async function fetcher(input?: Input, options?: Options | undefined) {
const { replacer, ...fetchOptions } = options || {}
let url = `${baseUrl}${path}`
let headers = options?.headers
if (!headers) {
Expand All @@ -197,13 +203,16 @@ export const makeClient = <T extends GenericRoutes>(baseUrl = ''): {

const response = await fetch(
url,
{ ...options, method, headers, body: bodyInput },
{ ...fetchOptions, method, headers, body: bodyInput },
)
if (withoutBody.has(response.status)) return null as unknown as Output
const body = await response.text()
let payload
const contentType = response.headers.get('content-type')
try {
payload = JSON.parse(body)
payload = contentType?.includes('application/json')
? JSON.parse(body, replacer)
: body
if (response.ok) return payload as Output
} catch {
throw new ErrorWithBody(body, { response })
Expand All @@ -212,7 +221,7 @@ export const makeClient = <T extends GenericRoutes>(baseUrl = ''): {
throw new ErrorWithData(message, data)
}

const signal = () => {
const signal = (options: { replacer?: ReplacerType } = {}) => {
const $ = new Signal<RequestState<Output>>({ pending: 0 })
return {
$,
Expand All @@ -226,9 +235,10 @@ export const makeClient = <T extends GenericRoutes>(baseUrl = ''): {
const controller = new AbortController()
prev.controller?.abort()
$.value = { pending: Date.now(), controller, data: prev.data }
const { replacer } = options
const { signal } = controller
$.value = {
data: await fetcher(input, { signal, headers }),
data: await fetcher(input, { replacer, signal, headers }),
at: Date.now(),
}
} catch (err) {
Expand Down