@trpc/server
Advanced tools
Comparing version
@@ -110,3 +110,3 @@ "use strict"; | ||
return tslib_1.__awaiter(this, void 0, void 0, function () { | ||
var output, ctx, _e, method, deserializeInput, body, input, query, input, body, input, sub_1, json, err_1, json, _f, err_2; | ||
var output, ctx, _e, method, deserializeInput, caller, body, input, query, input, body, input, sub_1, json, err_1, json, _f, err_2; | ||
return tslib_1.__generator(this, function (_g) { | ||
@@ -134,2 +134,3 @@ switch (_g.label) { | ||
} | ||
caller = router.createCaller(ctx); | ||
if (!(method === 'POST')) return [3 /*break*/, 5]; | ||
@@ -140,8 +141,3 @@ return [4 /*yield*/, getPostBody({ req: req, maxBodySize: maxBodySize })]; | ||
input = deserializeInput(body.input); | ||
return [4 /*yield*/, router.invoke({ | ||
target: 'mutations', | ||
input: input, | ||
ctx: ctx, | ||
path: path, | ||
})]; | ||
return [4 /*yield*/, caller.mutation(path, input)]; | ||
case 4: | ||
@@ -154,8 +150,3 @@ output = _g.sent(); | ||
input = deserializeInput(getQueryInput(query)); | ||
return [4 /*yield*/, router.invoke({ | ||
target: 'queries', | ||
input: input, | ||
ctx: ctx, | ||
path: path, | ||
})]; | ||
return [4 /*yield*/, caller.query(path, input)]; | ||
case 6: | ||
@@ -170,10 +161,5 @@ output = _g.sent(); | ||
input = deserializeInput(body.input); | ||
return [4 /*yield*/, router.invoke({ | ||
target: 'subscriptions', | ||
input: input, | ||
ctx: ctx, | ||
path: path, | ||
})]; | ||
return [4 /*yield*/, caller.subscription(path, input)]; | ||
case 9: | ||
sub_1 = (_g.sent()); | ||
sub_1 = (output = _g.sent()); | ||
return [4 /*yield*/, new Promise(function (resolve, reject) { | ||
@@ -180,0 +166,0 @@ var _a, _b; |
@@ -9,3 +9,4 @@ import { CreateProcedureWithInput, CreateProcedureWithoutInput, inferProcedureFromOptions, Procedure, ProcedureWithInput } from './procedure'; | ||
export declare type inferSubscriptionOutput<TRouter extends AnyRouter, TPath extends keyof TRouter['_def']['subscriptions']> = ReturnType<inferAsyncReturnType<TRouter['_def']['subscriptions'][TPath]['call']>['output']>; | ||
export declare type inferHandlerInput<TProcedure extends Procedure<any, any, any>> = TProcedure extends ProcedureWithInput<any, infer TInput, any> ? undefined extends TInput ? [TInput?] : [TInput] : [undefined?]; | ||
export declare type inferHandlerInput<TProcedure extends Procedure> = TProcedure extends ProcedureWithInput<any, infer TInput, any> ? undefined extends TInput ? [TInput?] : [TInput] : [undefined?]; | ||
declare type inferHandlerFn<TProcedures extends ProcedureRecord> = <TProcedure extends TProcedures[TPath], TPath extends keyof TProcedures & string>(path: TPath, ...args: inferHandlerInput<TProcedure>) => Promise<inferProcedureOutput<TProcedures[TPath]>>; | ||
export declare type AnyRouter<TContext = any> = Router<TContext, any, any, any, any>; | ||
@@ -15,3 +16,3 @@ export declare type MiddlewareFunction<TContext> = (opts: { | ||
}) => Promise<void> | void; | ||
export declare class Router<TContext, TQueries extends ProcedureRecord<TContext>, TMutations extends ProcedureRecord<TContext>, TSubscriptions extends ProcedureRecord<TContext, unknown, Subscription<unknown>>, TMiddleware extends MiddlewareFunction<TContext>> { | ||
export declare class Router<TContext, TQueries extends ProcedureRecord<TContext>, TMutations extends ProcedureRecord<TContext>, TSubscriptions extends ProcedureRecord<TContext, unknown, Subscription<unknown>>, TMiddlewareFn extends MiddlewareFunction<TContext>> { | ||
readonly _def: Readonly<{ | ||
@@ -21,3 +22,3 @@ queries: Readonly<TQueries>; | ||
subscriptions: Readonly<TSubscriptions>; | ||
middlewares: TMiddleware[]; | ||
middlewares: TMiddlewareFn[]; | ||
}>; | ||
@@ -28,9 +29,9 @@ constructor(def?: { | ||
subscriptions: TSubscriptions; | ||
middlewares: TMiddleware[]; | ||
middlewares: TMiddlewareFn[]; | ||
}); | ||
private static prefixProcedures; | ||
query<TPath extends string, TInput, TOutput>(path: TPath, procedure: CreateProcedureWithInput<TContext, TInput, TOutput>): Router<TContext, flatten<TQueries, Record<TPath, inferProcedureFromOptions<typeof procedure>>>, TMutations, TSubscriptions, TMiddleware>; | ||
query<TPath extends string, TOutput>(path: TPath, procedure: CreateProcedureWithoutInput<TContext, TOutput>): Router<TContext, flatten<TQueries, Record<TPath, inferProcedureFromOptions<typeof procedure>>>, TMutations, TSubscriptions, TMiddleware>; | ||
mutation<TPath extends string, TInput, TOutput>(path: TPath, procedure: CreateProcedureWithInput<TContext, TInput, TOutput>): Router<TContext, TQueries, flatten<TMutations, Record<TPath, inferProcedureFromOptions<typeof procedure>>>, TSubscriptions, TMiddleware>; | ||
mutation<TPath extends string, TOutput>(path: TPath, procedure: CreateProcedureWithoutInput<TContext, TOutput>): Router<TContext, TQueries, flatten<TMutations, Record<TPath, inferProcedureFromOptions<typeof procedure>>>, TSubscriptions, TMiddleware>; | ||
query<TPath extends string, TInput, TOutput>(path: TPath, procedure: CreateProcedureWithInput<TContext, TInput, TOutput>): Router<TContext, flatten<TQueries, Record<TPath, inferProcedureFromOptions<typeof procedure>>>, TMutations, TSubscriptions, TMiddlewareFn>; | ||
query<TPath extends string, TOutput>(path: TPath, procedure: CreateProcedureWithoutInput<TContext, TOutput>): Router<TContext, flatten<TQueries, Record<TPath, inferProcedureFromOptions<typeof procedure>>>, TMutations, TSubscriptions, TMiddlewareFn>; | ||
mutation<TPath extends string, TInput, TOutput>(path: TPath, procedure: CreateProcedureWithInput<TContext, TInput, TOutput>): Router<TContext, TQueries, flatten<TMutations, Record<TPath, inferProcedureFromOptions<typeof procedure>>>, TSubscriptions, TMiddlewareFn>; | ||
mutation<TPath extends string, TOutput>(path: TPath, procedure: CreateProcedureWithoutInput<TContext, TOutput>): Router<TContext, TQueries, flatten<TMutations, Record<TPath, inferProcedureFromOptions<typeof procedure>>>, TSubscriptions, TMiddlewareFn>; | ||
/** | ||
@@ -41,3 +42,3 @@ * ⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️ | ||
*/ | ||
subscription<TPath extends string, TInput, TOutput extends Subscription<unknown>>(path: TPath, procedure: CreateProcedureWithInput<TContext, TInput, TOutput>): Router<TContext, TQueries, TMutations, TSubscriptions & Record<TPath, inferProcedureFromOptions<typeof procedure>>, TMiddleware>; | ||
subscription<TPath extends string, TInput, TOutput extends Subscription<unknown>>(path: TPath, procedure: CreateProcedureWithInput<TContext, TInput, TOutput>): Router<TContext, TQueries, TMutations, TSubscriptions & Record<TPath, inferProcedureFromOptions<typeof procedure>>, TMiddlewareFn>; | ||
/** | ||
@@ -48,3 +49,3 @@ * ⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️ | ||
*/ | ||
subscription<TPath extends string, TOutput extends Subscription<unknown>>(path: TPath, procedure: CreateProcedureWithoutInput<TContext, TOutput>): Router<TContext, TQueries, TMutations, TSubscriptions & Record<TPath, inferProcedureFromOptions<typeof procedure>>, TMiddleware>; | ||
subscription<TPath extends string, TOutput extends Subscription<unknown>>(path: TPath, procedure: CreateProcedureWithoutInput<TContext, TOutput>): Router<TContext, TQueries, TMutations, TSubscriptions & Record<TPath, inferProcedureFromOptions<typeof procedure>>, TMiddlewareFn>; | ||
/** | ||
@@ -54,3 +55,3 @@ * Merge router with other router | ||
*/ | ||
merge<TChildRouter extends AnyRouter<TContext>>(router: TChildRouter): Router<TContext, flatten<TQueries, TChildRouter['_def']['queries']>, flatten<TMutations, TChildRouter['_def']['mutations']>, flatten<TSubscriptions, TChildRouter['_def']['subscriptions']>, TMiddleware>; | ||
merge<TChildRouter extends AnyRouter<TContext>>(router: TChildRouter): Router<TContext, flatten<TQueries, TChildRouter['_def']['queries']>, flatten<TMutations, TChildRouter['_def']['mutations']>, flatten<TSubscriptions, TChildRouter['_def']['subscriptions']>, TMiddlewareFn>; | ||
/** | ||
@@ -61,3 +62,3 @@ * Merge router with other router | ||
*/ | ||
merge<TPath extends string, TChildRouter extends AnyRouter<TContext>>(prefix: TPath, router: TChildRouter): Router<TContext, flatten<TQueries, Prefixer<TChildRouter['_def']['queries'], `${TPath}`>>, flatten<TMutations, Prefixer<TChildRouter['_def']['mutations'], `${TPath}`>>, flatten<TSubscriptions, Prefixer<TChildRouter['_def']['subscriptions'], `${TPath}`>>, TMiddleware>; | ||
merge<TPath extends string, TChildRouter extends AnyRouter<TContext>>(prefix: TPath, router: TChildRouter): Router<TContext, flatten<TQueries, Prefixer<TChildRouter['_def']['queries'], `${TPath}`>>, flatten<TMutations, Prefixer<TChildRouter['_def']['mutations'], `${TPath}`>>, flatten<TSubscriptions, Prefixer<TChildRouter['_def']['subscriptions'], `${TPath}`>>, TMiddlewareFn>; | ||
/** | ||
@@ -69,9 +70,9 @@ * Invoke procedure. Only for internal use within library. | ||
*/ | ||
invoke(opts: { | ||
target: 'queries' | 'subscriptions' | 'mutations'; | ||
ctx: TContext; | ||
path: string; | ||
input?: unknown; | ||
}): Promise<unknown>; | ||
private invoke; | ||
private has; | ||
createCaller(ctx: TContext): { | ||
query: inferHandlerFn<TQueries>; | ||
mutation: inferHandlerFn<TMutations>; | ||
subscription: inferHandlerFn<TSubscriptions>; | ||
}; | ||
/** | ||
@@ -81,4 +82,5 @@ * Function to be called before any procedure is invoked | ||
*/ | ||
middleware(fn: TMiddleware): this; | ||
middleware(middleware: TMiddlewareFn): Router<TContext, TQueries, TMutations, TSubscriptions, TMiddlewareFn>; | ||
} | ||
export declare function router<TContext>(): Router<TContext, {}, {}, {}, MiddlewareFunction<TContext>>; | ||
export {}; |
@@ -123,2 +123,44 @@ "use strict"; | ||
}; | ||
Router.prototype.createCaller = function (ctx) { | ||
// eslint-disable-next-line @typescript-eslint/no-this-alias | ||
var self = this; | ||
return { | ||
query: function (path) { | ||
var args = []; | ||
for (var _i = 1; _i < arguments.length; _i++) { | ||
args[_i - 1] = arguments[_i]; | ||
} | ||
return self.invoke({ | ||
target: 'queries', | ||
ctx: ctx, | ||
path: path, | ||
input: args[0], | ||
}); | ||
}, | ||
mutation: function (path) { | ||
var args = []; | ||
for (var _i = 1; _i < arguments.length; _i++) { | ||
args[_i - 1] = arguments[_i]; | ||
} | ||
return self.invoke({ | ||
target: 'mutations', | ||
ctx: ctx, | ||
path: path, | ||
input: args[0], | ||
}); | ||
}, | ||
subscription: function (path) { | ||
var args = []; | ||
for (var _i = 1; _i < arguments.length; _i++) { | ||
args[_i - 1] = arguments[_i]; | ||
} | ||
return self.invoke({ | ||
target: 'subscriptions', | ||
ctx: ctx, | ||
path: path, | ||
input: args[0], | ||
}); | ||
}, | ||
}; | ||
}; | ||
/** | ||
@@ -128,5 +170,4 @@ * Function to be called before any procedure is invoked | ||
*/ | ||
Router.prototype.middleware = function (fn) { | ||
this._def.middlewares.push(fn); | ||
return this; | ||
Router.prototype.middleware = function (middleware) { | ||
return new Router(tslib_1.__assign(tslib_1.__assign({}, this._def), { middlewares: tslib_1.__spreadArrays(this._def.middlewares, [middleware]) })); | ||
}; | ||
@@ -133,0 +174,0 @@ return Router; |
{ | ||
"name": "@trpc/server", | ||
"version": "3.0.0-alpha.7", | ||
"version": "3.0.0-alpha.8", | ||
"description": "TRPC Server", | ||
@@ -51,3 +51,3 @@ "author": "KATT", | ||
}, | ||
"gitHead": "1ee95351c9d19eebdc173592ccf5b6907ecd8b89" | ||
"gitHead": "98cfba3ea7de88972306697b3975b70721841a36" | ||
} |
55729
2.63%1175
2.53%