Skip to content

Commit

Permalink
fix: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mattmess1221 committed Nov 11, 2024
1 parent 01b63e4 commit de36968
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions src/runtime/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@
import type { FetchHooks } from 'ofetch'

type Arrayify<T> = { [P in keyof T]-?: Extract<T[P], unknown[]> }
type Hooks = Arrayify<Pick<FetchHooks, 'onRequest' | 'onResponse'>>

export function mergeFetchHooks(...hooks: FetchHooks[]): Hooks {
const result: Hooks = {
function maybePush<T>(array: T[], values: T | T[] | undefined): void {
if (values) {
if (Array.isArray(values)) {
array.push(...values)
}
else {
array.push(values)
}
}
}

export function mergeFetchHooks(...hooks: FetchHooks[]): FetchHooks {
const result = {
onRequest: [],
onResponse: [],
}
onRequestError: [],
onResponseError: [],
} as Arrayify<FetchHooks>

hooks.forEach((hook) => {
for (const name of Object.keys(result) as (keyof Hooks)[]) {
if (hook) {
result[name].push(...(Array.isArray(hook) ? hook : [hook]))
}
}
Object.entries(hook).forEach(([key, value]) => {
maybePush(result[key as keyof typeof result], value)
})
})

return result
Expand Down

0 comments on commit de36968

Please sign in to comment.