This repository has been archived by the owner on Dec 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make the RPC call as property access on a
Proxy
obj (#21)
- Loading branch information
Showing
16 changed files
with
5,810 additions
and
5,876 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,20 @@ | ||
import type { appRouter } from './server'; | ||
import { createClient } from './trpc/client'; | ||
import { createRouterProxy } from './trpc/client/createRouterProxy'; | ||
|
||
const client = createClient<typeof appRouter>(); | ||
const { queries } = createRouterProxy<typeof appRouter>(); | ||
|
||
async function main() { | ||
{ | ||
// you can CMD+click `greeting` below to get to the definition | ||
const greeting = await client.query(queries.greeting, { hello: 'world' }); | ||
// you can CMD+click `post.all` below to get to the definition | ||
const posts = await client.query(queries['post.all']); | ||
// you can CMD+click `postAll` / `postById` here | ||
const greeting = await client.query.postList(); | ||
const byId = await client.query.postById({ id: '1' }); | ||
|
||
console.log({ greeting, posts }); | ||
if (byId.ok) { | ||
console.log('data', byId.data); | ||
} else { | ||
console.log(byId.error.code); | ||
} | ||
|
||
{ | ||
// we can also use string based path if we prefer that | ||
// (but then you can't click your way to the backend) | ||
const greeting = await client.query('greeting', { hello: 'string' }); | ||
const posts = await client.query('post.all'); | ||
|
||
console.log({ greeting, posts }); | ||
} | ||
console.log({ greeting, byId }); | ||
} | ||
|
||
main(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,27 @@ | ||
import type { inferProcedure, Procedure, ProceduresByType } from '../server'; | ||
import type { ProceduresByType } from '../server'; | ||
|
||
type ValueOf<T> = T[keyof T]; | ||
function createRouterProxy<TRouter extends ProceduresByType<any>>() { | ||
return new Proxy({} as any, { | ||
get(_, type: string) { | ||
return new Proxy({} as any, { | ||
get(_, path: string) { | ||
return type + '.' + path; | ||
}, | ||
}); | ||
}, | ||
}) as any as TRouter; | ||
} | ||
|
||
type inferProcedureArgs<TProcedure extends Procedure<any, any>> = | ||
undefined extends inferProcedure<TProcedure>['_input_in'] | ||
? [inferProcedure<TProcedure>['_input_in']?] | ||
: [inferProcedure<TProcedure>['_input_in']]; | ||
export interface TRPCClient<TRouter extends ProceduresByType<any>> { | ||
query: NonNullable<TRouter['queries']>; | ||
mutation: NonNullable<TRouter['mutations']>; | ||
$query: never; // Observable version of query? | ||
} | ||
|
||
type ExpectProcedure<T> = T extends Procedure<any, any> ? T : never; | ||
export function createClient<TRouter extends ProceduresByType<any>>() { | ||
function query<TPath extends keyof TRouter['queries'] & string>( | ||
path: TPath, | ||
...args: inferProcedureArgs<ExpectProcedure<TRouter['queries'][TPath]>> | ||
): Promise< | ||
inferProcedure<ExpectProcedure<TRouter['queries'][TPath]>>['result'] | ||
>; | ||
function query< | ||
TProcedure extends ValueOf<TRouter['queries']> & Procedure<any, any>, | ||
>( | ||
path: TProcedure, | ||
...args: inferProcedureArgs<TProcedure> | ||
): Promise<inferProcedure<TProcedure>['result']>; | ||
function query(..._args: any[]): any { | ||
throw new Error('Unimplemented'); | ||
} | ||
function mutation< | ||
TPath extends keyof TRouter['mutations'] & string, | ||
TProcedure extends TRouter['mutations'][TPath] & Procedure<any, any>, | ||
>( | ||
path: TPath, | ||
...args: inferProcedureArgs<TProcedure> | ||
): Promise<inferProcedure<TProcedure>['result']>; | ||
function mutation< | ||
TProcedure extends ValueOf<TRouter['mutations']> & Procedure<any, any>, | ||
>( | ||
path: TProcedure, | ||
...args: inferProcedureArgs<TProcedure> | ||
): Promise<inferProcedure<TProcedure>['result']>; | ||
function mutation(..._args: any[]): any { | ||
throw new Error('Unimplemented'); | ||
} | ||
export function createClient< | ||
TRouter extends ProceduresByType<any>, | ||
>(): TRPCClient<TRouter> { | ||
const proxy = createRouterProxy<TRouter>(); | ||
|
||
return { | ||
query, | ||
mutation, | ||
}; | ||
return proxy as any; | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { ProcedureResultError, MaybePromise } from '../procedure'; | ||
const middlewareMarker = Symbol('middlewareMarker'); | ||
|
||
///////// middleware implementation /////////// | ||
interface MiddlewareNextResultBase<TParams> { | ||
/** | ||
* If the resolver is a middleware, it should pass through their `next()`'s output. | ||
*/ | ||
readonly _next: typeof middlewareMarker; | ||
TParams: TParams; | ||
} | ||
interface MiddlewareReturnResultBase<_TParams> { | ||
/** | ||
* If the resolver is a middleware, it should pass through their `next()`'s output. | ||
*/ | ||
readonly _return: typeof middlewareMarker; | ||
} | ||
export interface MiddlewareNextOKResult<TParams> | ||
extends MiddlewareNextResultBase<TParams> {} | ||
export interface MiddlewareNextErrorResult<TParams> | ||
extends MiddlewareNextResultBase<TParams>, | ||
ProcedureResultError<any> {} | ||
|
||
export interface MiddlewareReturnOKResult<TParams> | ||
extends MiddlewareReturnResultBase<TParams> {} | ||
export interface MiddlewareReturnErrorResult<TParams> | ||
extends MiddlewareReturnResultBase<TParams>, | ||
ProcedureResultError<any> {} | ||
|
||
export type MiddlewareNextResult<TParams> = | ||
| MiddlewareNextOKResult<TParams> | ||
| MiddlewareNextErrorResult<TParams>; | ||
|
||
export type MiddlewareReturnResult<TParams> = | ||
| MiddlewareReturnOKResult<TParams> | ||
| MiddlewareReturnErrorResult<TParams>; | ||
|
||
export type MiddlewareParams<TInputParams> = TInputParams & { | ||
next: { | ||
(): Promise<MiddlewareNextResult<TInputParams>>; | ||
<T>(params: T): Promise<MiddlewareNextResult<T>>; | ||
}; | ||
}; | ||
|
||
export type Middleware<TInputParams, TNextParams, TResult = never> = ( | ||
params: MiddlewareParams<TInputParams>, | ||
) => MaybePromise< | ||
MiddlewareNextResult<TNextParams> | MiddlewareReturnResult<TResult> | ||
>; | ||
|
||
type ExcludeMiddlewareReturnResult<T> = T extends MiddlewareReturnResult<any> | ||
? never | ||
: T; | ||
|
||
export type ExcludeMiddlewareNextResult<T> = T extends MiddlewareNextResult<any> | ||
? never | ||
: T; | ||
|
||
export type ExcludeMiddlewareResult<T> = ExcludeMiddlewareReturnResult< | ||
ExcludeMiddlewareNextResult<T> | ||
>; | ||
|
||
export function middlewareResult<T>(_result: T): MiddlewareReturnResult<T> { | ||
throw new Error('Unimplemented'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.