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
12 changes: 6 additions & 6 deletions packages/hey-api/src/to-orpc-client.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,23 @@ describe('toORPCClient', () => {
somethingElse: 123,
})

const c = sdk.listPlanets()
const c = sdk.planetList()

it('satisfies nested client', () => {
const _b: NestedClient<Record<never, never>> = client
})

it('inputs', async () => {
client.listPlanets()
client.planetList()

client.listPlanets({
client.planetList({
query: {
limit: 10,
offset: 0,
},
})

client.listPlanets({
client.planetList({
query: {
// @ts-expect-error - invalid type
limit: 'invalid',
Expand All @@ -40,8 +40,8 @@ describe('toORPCClient', () => {
})

it('outputs', async () => {
expectTypeOf(await client.listPlanets()).toEqualTypeOf<{ body: Planet[], request: Request, response: Response }>()
expectTypeOf(await client.planetList()).toEqualTypeOf<{ body: Planet[], request: Request, response: Response }>()
expectTypeOf(await client.getPlanet({ path: { planetId: 'earth' } })).toEqualTypeOf<{ body: Planet, request: Request, response: Response }>()
expectTypeOf(await client.createPlanet({ body: { name: 'Earth' } })).toEqualTypeOf<{ body: Planet, request: Request, response: Response }>()
expectTypeOf(await client.planetCreate({ body: { name: 'Earth' } })).toEqualTypeOf<{ body: Planet | { id: string }, request: Request, response: Response }>()
})
})
18 changes: 9 additions & 9 deletions packages/hey-api/src/to-orpc-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ describe('toORPCClient', () => {
})

it('works', async () => {
const result = await client.listPlanets()
const result = await client.planetList()
expect(result).toEqual({
body: [{ id: 'earth', name: 'Earth' }],
request: expect.any(Request),
Expand All @@ -56,7 +56,7 @@ describe('toORPCClient', () => {
})

it('with lastEventId', async () => {
const result = await client.listPlanets({
const result = await client.planetList({
headers: {
'x-something': 'value',
'last-event-id': '123',
Expand All @@ -69,7 +69,7 @@ describe('toORPCClient', () => {
})

it('with query', async () => {
const result = await client.listPlanets({
const result = await client.planetList({
query: {
limit: 10,
offset: 0,
Expand All @@ -90,7 +90,7 @@ describe('toORPCClient', () => {
})

it('with body', async () => {
const result = await client.createPlanet({
const result = await client.planetCreate({
body: { name: 'Bob' },
})

Expand All @@ -102,7 +102,7 @@ describe('toORPCClient', () => {
const controller1 = new AbortController()
const controller2 = new AbortController()

const result = await client.createPlanet({
const result = await client.planetCreate({
body: { name: 'Bob' },
signal: controller1.signal,
}, { signal: controller2.signal })
Expand All @@ -116,7 +116,7 @@ describe('toORPCClient', () => {
const controller1 = new AbortController()
const controller2 = new AbortController()

const result = await client.createPlanet({
const result = await client.planetCreate({
body: { name: 'Bob' },
signal: controller1.signal,
}, { signal: controller2.signal })
Expand All @@ -132,7 +132,7 @@ describe('toORPCClient', () => {
controller1.abort()

await expect(
client.createPlanet({
client.planetCreate({
body: { name: 'Bob' },
signal: controller1.signal,
}, { signal: controller2.signal }),
Expand All @@ -145,7 +145,7 @@ describe('toORPCClient', () => {
controller2.abort()

await expect(
client.createPlanet({
client.planetCreate({
body: { name: 'Bob' },
signal: controller1.signal,
}, { signal: controller2.signal }),
Expand All @@ -154,6 +154,6 @@ describe('toORPCClient', () => {
})

it('throws on error', async () => {
await expect(client.listPlanets({ query: { throwOnError: 1 } as any })).rejects.toThrowError()
await expect(client.planetList({ query: { throwOnError: 1 } as any })).rejects.toThrowError()
})
})
6 changes: 4 additions & 2 deletions packages/hey-api/src/to-orpc-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ import type { Client, ThrowableError } from '@orpc/client'

export type experimental_ToORPCClientResult<T extends Record<string, any>> = {
[K in keyof T]:
T[K] extends (options: infer UInput extends Record<any, any> | undefined)
T[K] extends (options: infer UInput)
=> Promise<infer UResult>
? Client<Record<never, never>, UInput, {
body: UResult extends { data: infer USuccess } ? Exclude<USuccess, undefined> : never
request: Request
response: Response
}, ThrowableError>
: never
: T[K] extends Record<string, any>
? experimental_ToORPCClientResult<T[K]>
: never
}

/**
Expand Down
10 changes: 5 additions & 5 deletions packages/hey-api/tests/client/sdk.gen.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// This file is auto-generated by @hey-api/openapi-ts

import type { Options as ClientOptions, TDataShape, Client } from '@hey-api/client-fetch';
import type { ListPlanetsData, ListPlanetsResponses, CreatePlanetData, CreatePlanetResponses, GetPlanetData, GetPlanetResponses, GetPlanetErrors } from './types.gen';
import type { PlanetListData, PlanetListResponses, PlanetCreateData, PlanetCreateResponses, GetPlanetData, GetPlanetResponses, GetPlanetErrors } from './types.gen';
import { client as _heyApiClient } from './client.gen';

export type Options<TData extends TDataShape = TDataShape, ThrowOnError extends boolean = boolean> = ClientOptions<TData, ThrowOnError> & {
Expand All @@ -18,15 +18,15 @@ export type Options<TData extends TDataShape = TDataShape, ThrowOnError extends
meta?: Record<string, unknown>;
};

export const listPlanets = <ThrowOnError extends boolean = false>(options?: Options<ListPlanetsData, ThrowOnError>) => {
return (options?.client ?? _heyApiClient).get<ListPlanetsResponses, unknown, ThrowOnError>({
export const planetList = <ThrowOnError extends boolean = false>(options?: Options<PlanetListData, ThrowOnError>) => {
return (options?.client ?? _heyApiClient).get<PlanetListResponses, unknown, ThrowOnError>({
url: '/planets',
...options
});
};

export const createPlanet = <ThrowOnError extends boolean = false>(options: Options<CreatePlanetData, ThrowOnError>) => {
return (options.client ?? _heyApiClient).post<CreatePlanetResponses, unknown, ThrowOnError>({
export const planetCreate = <ThrowOnError extends boolean = false>(options: Options<PlanetCreateData, ThrowOnError>) => {
return (options.client ?? _heyApiClient).post<PlanetCreateResponses, unknown, ThrowOnError>({
url: '/planets',
...options,
headers: {
Expand Down
20 changes: 14 additions & 6 deletions packages/hey-api/tests/client/types.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ export type NewPlanet = {
name: string;
};

export type ListPlanetsData = {
export type UpdatedPlanet = {
id: string;
};

export type PlanetListData = {
body?: never;
path?: never;
query?: {
Expand All @@ -19,30 +23,34 @@ export type ListPlanetsData = {
url: '/planets';
};

export type ListPlanetsResponses = {
export type PlanetListResponses = {
/**
* A list of planets
*/
200: Array<Planet>;
};

export type ListPlanetsResponse = ListPlanetsResponses[keyof ListPlanetsResponses];
export type PlanetListResponse = PlanetListResponses[keyof PlanetListResponses];

export type CreatePlanetData = {
export type PlanetCreateData = {
body: NewPlanet;
path?: never;
query?: never;
url: '/planets';
};

export type CreatePlanetResponses = {
export type PlanetCreateResponses = {
/**
* A updated planet
*/
200: UpdatedPlanet;
/**
* A created planet
*/
201: Planet;
};

export type CreatePlanetResponse = CreatePlanetResponses[keyof CreatePlanetResponses];
export type PlanetCreateResponse = PlanetCreateResponses[keyof PlanetCreateResponses];

export type GetPlanetData = {
body?: never;
Expand Down
23 changes: 21 additions & 2 deletions packages/hey-api/tests/spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"paths": {
"/planets": {
"get": {
"operationId": "listPlanets",
"operationId": "planet/list",
"parameters": [
{
"name": "limit",
Expand Down Expand Up @@ -52,7 +52,7 @@
}
},
"post": {
"operationId": "createPlanet",
"operationId": "planet.create",
"requestBody": {
"content": {
"application/json": {
Expand All @@ -73,6 +73,16 @@
}
}
}
},
"200": {
"description": "An updated planet",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/UpdatedPlanet"
}
}
}
}
}
}
Expand Down Expand Up @@ -137,6 +147,15 @@
}
},
"required": ["name"]
},
"UpdatedPlanet": {
"type": "object",
"properties": {
"id": {
"type": "string"
}
},
"required": ["id"]
}
}
}
Expand Down