Skip to content

Commit

Permalink
perf: remove mergeFetchOptions util
Browse files Browse the repository at this point in the history
  • Loading branch information
johannschopplich committed Oct 31, 2023
1 parent b5028b6 commit 4a086e4
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 55 deletions.
11 changes: 4 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { ofetch } from 'ofetch'
import { joinURL } from 'ufo'
import type { FetchOptions } from 'ofetch'
import type { ApiClient, ApiClientFetcher, ResponseType } from './types'
import { mergeFetchOptions } from './utils'
import type { $Fetch, ApiClient, ResponseType } from './types'

const payloadMethods = ['POST', 'PUT', 'DELETE', 'PATCH']

Expand All @@ -20,7 +19,7 @@ export function createClient<R extends ResponseType = 'json'>(
if (!['GET', ...payloadMethods].includes(method))
return p(joinURL(url, key))

const handler: ApiClientFetcher = <T = any, R extends ResponseType = 'json'>(
const handler: $Fetch = <T = any, R extends ResponseType = 'json'>(
data?: any,
opts: FetchOptions<R> = {},
) => {
Expand All @@ -31,10 +30,8 @@ export function createClient<R extends ResponseType = 'json'>(

opts.method = method

return ofetch<T, R>(
url,
mergeFetchOptions(opts, defaultOptions) as FetchOptions<R>,
)
const fetcher = ofetch.create(defaultOptions)
return fetcher<T, R>(url, opts)
}

return handler
Expand Down
16 changes: 8 additions & 8 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,26 @@ export interface ResponseMap {
}

export type ResponseType = keyof ResponseMap | 'json'
export type MappedType<
export type MappedResponseType<
R extends ResponseType,
JsonType = any,
> = R extends keyof ResponseMap ? ResponseMap[R] : JsonType

export type ApiClientFetcher<Data = unknown> = <
export type $Fetch<Data = unknown> = <
T = any,
R extends ResponseType = 'json',
>(
data?: Data,
opts?: Omit<FetchOptions<R>, 'baseURL' | 'method'>,
) => Promise<MappedType<R, T>>
) => Promise<MappedResponseType<R, T>>

export type ApiClient = {
[key: string]: ApiClient
(...args: (string | number)[]): ApiClient
} & {
get: ApiClientFetcher<FetchOptions['query']>
post: ApiClientFetcher<FetchOptions['body']>
put: ApiClientFetcher<FetchOptions['body']>
delete: ApiClientFetcher<FetchOptions['body']>
patch: ApiClientFetcher<FetchOptions['body']>
get: $Fetch<FetchOptions['query']>
post: $Fetch<FetchOptions['body']>
put: $Fetch<FetchOptions['body']>
delete: $Fetch<FetchOptions['body']>
patch: $Fetch<FetchOptions['body']>
}
34 changes: 0 additions & 34 deletions src/utils.ts

This file was deleted.

12 changes: 6 additions & 6 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import type { Listener } from 'listhen'
import { createClient } from '../src'
import type { ApiClient } from '../src'

// Test TypeScript support
interface ApiGETResponse {
// Test TypeScript generic
interface FooResponse {
foo: string
}

Expand Down Expand Up @@ -94,23 +94,23 @@ describe('unrested', () => {
})

it('bracket syntax for path segment', async () => {
const response = await client.foo['1'].get<ApiGETResponse>()
const response = await client.foo['1'].get<FooResponse>()
expect(response).to.deep.equal({ foo: '1' })
})

it('chain syntax for path segment', async () => {
const response = await client.foo(1).get<ApiGETResponse>()
const response = await client.foo(1).get<FooResponse>()
expect(response).to.deep.equal({ foo: '1' })
})

it('multiple path segments', async () => {
const response = await client('foo', '1').get<ApiGETResponse>()
const response = await client('foo', '1').get<FooResponse>()
expect(response).to.deep.equal({ foo: '1' })
})

it('invalid api endpoint', () => {
expect(async () => {
await client.baz.get<ApiGETResponse>()
await client.baz.get<FooResponse>()
}).rejects.toThrow(/404/)
})
})

0 comments on commit 4a086e4

Please sign in to comment.