Skip to content

Commit

Permalink
refactor!: rename ApiBuilder type to ApiClient
Browse files Browse the repository at this point in the history
  • Loading branch information
johannschopplich committed Oct 25, 2023
1 parent 25505a7 commit d0ed4fe
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
10 changes: 5 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ofetch } from 'ofetch'
import { joinURL } from 'ufo'
import type { FetchOptions } from 'ofetch'
import type { ApiBuilder, ApiMethodHandler, ResponseType } from './types'
import type { ApiClient, ApiMethodHandler, ResponseType } from './types'
import { mergeFetchOptions } from './utils'

const payloadMethods = ['POST', 'PUT', 'DELETE', 'PATCH']
Expand All @@ -11,11 +11,11 @@ const payloadMethods = ['POST', 'PUT', 'DELETE', 'PATCH']
*/
export function createClient<R extends ResponseType = 'json'>(
defaultOptions: Omit<FetchOptions<R>, 'method'> = {},
): ApiBuilder {
): ApiClient {
// Callable internal target required to use `apply` on it
const internalTarget = (() => {}) as ApiBuilder
const internalTarget = (() => {}) as ApiClient

function p(url: string): ApiBuilder {
function p(url: string): ApiClient {
return new Proxy(internalTarget, {
get(_target, key: string) {
const method = key.toUpperCase()
Expand Down Expand Up @@ -51,4 +51,4 @@ export function createClient<R extends ResponseType = 'json'>(
return p(defaultOptions.baseURL || '/')
}

export type { ApiBuilder } from './types'
export type { ApiClient } from './types'
6 changes: 3 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ export type ApiMethodHandler<Data = never> = <
opts?: Omit<FetchOptions<R>, 'baseURL' | 'method'>,
) => Promise<MappedType<R, T>>

export type ApiBuilder = {
[key: string]: ApiBuilder
(...segmentsOrIds: (string | number)[]): ApiBuilder
export type ApiClient = {
[key: string]: ApiClient
(...args: (string | number)[]): ApiClient
} & {
get: ApiMethodHandler<FetchOptions['query']>
post: ApiMethodHandler<FetchOptions['body']>
Expand Down
4 changes: 2 additions & 2 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { createApp, eventHandler, getQuery, getRequestHeaders, readBody, toNodeL
import { listen } from 'listhen'
import type { Listener } from 'listhen'
import { createClient } from '../src'
import type { ApiBuilder } from '../src'
import type { ApiClient } from '../src'

// Test TypeScript support
interface ApiGETResponse {
Expand All @@ -13,7 +13,7 @@ interface ApiGETResponse {

describe('unrested', () => {
let listener: Listener
let client: ApiBuilder
let client: ApiClient

beforeEach(async () => {
const app = createApp()
Expand Down

0 comments on commit d0ed4fe

Please sign in to comment.