@trpc/server
Advanced tools
Comparing version 3.0.0-alpha.5 to 3.0.0-alpha.6
@@ -26,3 +26,3 @@ import { MiddlewareFunction } from './router'; | ||
protected resolver: ProcedureResolver<TContext, TInput, TOutput>; | ||
private inputParser; | ||
protected inputParser: ProcedureInputParser<TInput>; | ||
constructor(opts: ProcedureOptions<TContext, TInput, TOutput>); | ||
@@ -46,6 +46,4 @@ /** | ||
export declare class ProcedureWithoutInput<TContext, TOutput> extends Procedure<TContext, undefined, TOutput> { | ||
constructor(opts: ProcedureOptions<TContext, undefined, TOutput>); | ||
} | ||
export declare class ProcedureWithInput<TContext, TInput, TOutput> extends Procedure<TContext, TInput, TOutput> { | ||
constructor(opts: ProcedureOptions<TContext, TInput, TOutput>); | ||
} | ||
@@ -52,0 +50,0 @@ export declare type CreateProcedureWithInput<TContext, TInput, TOutput> = { |
@@ -76,4 +76,4 @@ "use strict"; | ||
tslib_1.__extends(ProcedureWithoutInput, _super); | ||
function ProcedureWithoutInput(opts) { | ||
return _super.call(this, opts) || this; | ||
function ProcedureWithoutInput() { | ||
return _super !== null && _super.apply(this, arguments) || this; | ||
} | ||
@@ -85,4 +85,4 @@ return ProcedureWithoutInput; | ||
tslib_1.__extends(ProcedureWithInput, _super); | ||
function ProcedureWithInput(opts) { | ||
return _super.call(this, opts) || this; | ||
function ProcedureWithInput() { | ||
return _super !== null && _super.apply(this, arguments) || this; | ||
} | ||
@@ -89,0 +89,0 @@ return ProcedureWithInput; |
@@ -0,33 +1,10 @@ | ||
import { CreateProcedureWithInput, CreateProcedureWithoutInput, inferProcedureFromOptions, Procedure, ProcedureWithInput } from './procedure'; | ||
import { Subscription } from './subscription'; | ||
import { format, Prefixer, ThenArg } from './types'; | ||
export declare type ProcedureInputParserZodEsque<TInput = unknown> = { | ||
parse: (input: any) => TInput; | ||
}; | ||
export declare type ProcedureInputParserCustomValidatorEsque<TInput = unknown> = (input: unknown) => TInput; | ||
export declare type ProcedureInputParserYupEsque<TInput = unknown> = { | ||
validateSync: (input: unknown) => TInput; | ||
}; | ||
export declare type ProcedureInputParser<TInput = unknown> = ProcedureInputParserZodEsque<TInput> | ProcedureInputParserYupEsque<TInput> | ProcedureInputParserCustomValidatorEsque<TInput>; | ||
export declare type ProcedureResolver<TContext = unknown, TInput = unknown, TOutput = unknown> = (opts: { | ||
ctx: TContext; | ||
input: TInput; | ||
}) => Promise<TOutput> | TOutput; | ||
export declare type ProcedureWithInput<TContext = unknown, TInput = unknown, TOutput = unknown> = { | ||
input: ProcedureInputParser<TInput>; | ||
resolve: ProcedureResolver<TContext, TInput, TOutput>; | ||
}; | ||
export declare type ProcedureWithoutInput<TContext = unknown, TInput = unknown, TOutput = unknown> = { | ||
input?: undefined | null; | ||
resolve: ProcedureResolver<TContext, TInput, TOutput>; | ||
}; | ||
export declare type Procedure<TContext = unknown, TInput = unknown, TOutput = unknown> = (ProcedureWithInput<TContext, TInput, TOutput> | ProcedureWithoutInput<TContext, TInput, TOutput>) & { | ||
_middlewares?: MiddlewareFunction<TContext>[]; | ||
}; | ||
export declare type ProcedureRecord<TContext = unknown, TInput = unknown, TOutput = unknown> = Record<string, Procedure<TContext, TInput, TOutput>>; | ||
export declare type inferProcedureInput<TProcedure extends Procedure<any, any, any>> = TProcedure extends ProcedureWithInput<any, infer Input, any> ? Input : never; | ||
export declare type inferProcedureInput<TProcedure extends Procedure<any, any, any>> = TProcedure extends ProcedureWithInput<any, infer Input, any> ? Input : undefined; | ||
export declare type inferAsyncReturnType<TFunction extends (...args: any) => any> = ThenArg<ReturnType<TFunction>>; | ||
export declare type inferProcedureOutput<TProcedure extends Procedure> = inferAsyncReturnType<TProcedure['resolve']>; | ||
export declare type inferSubscriptionOutput<TRouter extends AnyRouter, TPath extends keyof TRouter['_def']['subscriptions']> = ReturnType<inferAsyncReturnType<TRouter['_def']['subscriptions'][TPath]['resolve']>['output']>; | ||
export declare type inferHandlerInput<TProcedure extends Procedure> = TProcedure extends ProcedureWithInput<any, any, any> ? [inferProcedureInput<TProcedure>] : [undefined?]; | ||
export declare type inferHandlerFn<TProcedures extends ProcedureRecord<any, any, any>> = <TProcedure extends TProcedures[TPath], TPath extends keyof TProcedures & string>(path: TPath, ...args: TProcedure extends ProcedureWithInput<any, any, any> ? [inferProcedureInput<TProcedure>] : [undefined?]) => Promise<inferProcedureOutput<TProcedures[TPath]>>; | ||
export declare type inferProcedureOutput<TProcedure extends Procedure> = inferAsyncReturnType<TProcedure['call']>; | ||
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 AnyRouter<TContext = any> = Router<TContext, any, any, any, any>; | ||
@@ -51,4 +28,6 @@ export declare type MiddlewareFunction<TContext> = (opts: { | ||
private static prefixProcedures; | ||
query<TPath extends string, TInput, TOutput>(path: TPath, route: Procedure<TContext, TInput, TOutput>): Router<TContext, format<TQueries & Record<TPath, typeof route>>, TMutations, TSubscriptions, TMiddleware>; | ||
mutation<TPath extends string, TInput, TOutput>(path: TPath, route: Procedure<TContext, TInput, TOutput>): Router<TContext, TQueries, format<TMutations & Record<TPath, typeof route>>, TSubscriptions, TMiddleware>; | ||
query<TPath extends string, TInput, TOutput>(path: TPath, procedure: CreateProcedureWithInput<TContext, TInput, TOutput>): Router<TContext, TQueries & Record<TPath, inferProcedureFromOptions<typeof procedure>>, TMutations, TSubscriptions, TMiddleware>; | ||
query<TPath extends string, TOutput>(path: TPath, procedure: CreateProcedureWithoutInput<TContext, TOutput>): Router<TContext, 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, TMutations & Record<TPath, inferProcedureFromOptions<typeof procedure>>, TSubscriptions, TMiddleware>; | ||
mutation<TPath extends string, TOutput>(path: TPath, procedure: CreateProcedureWithoutInput<TContext, TOutput>): Router<TContext, TQueries, TMutations & Record<TPath, inferProcedureFromOptions<typeof procedure>>, TSubscriptions, TMiddleware>; | ||
/** | ||
@@ -59,4 +38,10 @@ * ⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️ | ||
*/ | ||
subscription<TPath extends string, TInput, TOutput extends Subscription>(path: TPath, route: Procedure<TContext, TInput, TOutput>): Router<TContext, TQueries, TMutations, format<TSubscriptions & Record<TPath, typeof route>>, 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>>, TMiddleware>; | ||
/** | ||
* ⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️ | ||
* **Experimental.** API might change without major version bump | ||
* ⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠ | ||
*/ | ||
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>; | ||
/** | ||
* Merge router with other router | ||
@@ -72,4 +57,8 @@ * @param router | ||
merge<TPath extends string, TChildRouter extends AnyRouter<TContext>>(prefix: TPath, router: TChildRouter): Router<TContext, TQueries & Prefixer<TChildRouter['_def']['queries'], `${TPath}`>, TMutations & Prefixer<TChildRouter['_def']['mutations'], `${TPath}`>, TSubscriptions & Prefixer<TChildRouter['_def']['subscriptions'], `${TPath}`>, TMiddleware>; | ||
private inheritMiddlewares; | ||
private static getInput; | ||
/** | ||
* Invoke procedure. Only for internal use within library. | ||
* | ||
* @throws RouteNotFoundError | ||
* @throws InputValidationError | ||
*/ | ||
invoke(opts: { | ||
@@ -76,0 +65,0 @@ target: 'queries' | 'subscriptions' | 'mutations'; |
@@ -9,2 +9,3 @@ "use strict"; | ||
var errors_1 = require("./errors"); | ||
var procedure_1 = require("./procedure"); | ||
assertNotBrowser_1.assertNotBrowser(); | ||
@@ -27,7 +28,7 @@ var Router = /** @class */ (function () { | ||
}; | ||
Router.prototype.query = function (path, route) { | ||
Router.prototype.query = function (path, procedure) { | ||
var _a; | ||
var router = new Router({ | ||
queries: (_a = {}, | ||
_a[path] = route, | ||
_a[path] = procedure_1.createProcedure(procedure), | ||
_a), | ||
@@ -40,14 +41,3 @@ mutations: {}, | ||
}; | ||
// TODO / help: https://github.com/trpc/trpc/pull/37 | ||
// public queries<TProcedures extends ProcedureRecord<TContext, any, any>>( | ||
// procedures: TProcedures, | ||
// ): Router<TContext, TQueries & TProcedures, TMutations, TSubscriptions> { | ||
// const router = new Router<TContext, any, {}, {}>({ | ||
// queries: procedures, | ||
// mutations: {}, | ||
// subscriptions: {}, | ||
// }); | ||
// return this.merge(router) as any; | ||
// } | ||
Router.prototype.mutation = function (path, route) { | ||
Router.prototype.mutation = function (path, procedure) { | ||
var _a; | ||
@@ -57,3 +47,3 @@ var router = new Router({ | ||
mutations: (_a = {}, | ||
_a[path] = route, | ||
_a[path] = procedure_1.createProcedure(procedure), | ||
_a), | ||
@@ -65,8 +55,3 @@ subscriptions: {}, | ||
}; | ||
/** | ||
* ⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️ | ||
* **Experimental.** API might change without major version bump | ||
* ⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠ | ||
*/ | ||
Router.prototype.subscription = function (path, route) { | ||
Router.prototype.subscription = function (path, procedure) { | ||
var _a; | ||
@@ -77,3 +62,3 @@ var router = new Router({ | ||
subscriptions: (_a = {}, | ||
_a[path] = route, | ||
_a[path] = procedure_1.createProcedure(procedure), | ||
_a), | ||
@@ -87,9 +72,9 @@ middlewares: [], | ||
var prefix = ''; | ||
var router; | ||
var childRouter; | ||
if (typeof prefixOrRouter === 'string' && maybeRouter instanceof Router) { | ||
prefix = prefixOrRouter; | ||
router = maybeRouter; | ||
childRouter = maybeRouter; | ||
} | ||
else if (prefixOrRouter instanceof Router) { | ||
router = prefixOrRouter; | ||
childRouter = prefixOrRouter; | ||
} | ||
@@ -99,7 +84,5 @@ else { | ||
} | ||
var duplicateQueries = Object.keys(router._def.queries).filter(function (key) { | ||
return _this.has('queries', prefix + key); | ||
}); | ||
var duplicateMutations = Object.keys(router._def.mutations).filter(function (key) { return _this.has('mutations', prefix + key); }); | ||
var duplicateSubscriptions = Object.keys(router._def.subscriptions).filter(function (key) { return _this.has('subscriptions', prefix + key); }); | ||
var duplicateQueries = Object.keys(childRouter._def.queries).filter(function (key) { return _this.has('queries', prefix + key); }); | ||
var duplicateMutations = Object.keys(childRouter._def.mutations).filter(function (key) { return _this.has('mutations', prefix + key); }); | ||
var duplicateSubscriptions = Object.keys(childRouter._def.subscriptions).filter(function (key) { return _this.has('subscriptions', prefix + key); }); | ||
var duplicates = tslib_1.__spreadArrays(duplicateQueries, duplicateMutations, duplicateSubscriptions); | ||
@@ -109,46 +92,29 @@ if (duplicates.length) { | ||
} | ||
var mergeProcedures = function (defs) { | ||
var newDefs = {}; | ||
for (var key in defs) { | ||
var procedure = defs[key]; | ||
var newProcedure = procedure.inheritMiddlewares(_this._def.middlewares); | ||
newDefs[key] = newProcedure; | ||
} | ||
return Router.prefixProcedures(newDefs, prefix); | ||
}; | ||
return new Router({ | ||
queries: tslib_1.__assign(tslib_1.__assign({}, this._def.queries), this.inheritMiddlewares(Router.prefixProcedures(router._def.queries, prefix))), | ||
mutations: tslib_1.__assign(tslib_1.__assign({}, this._def.mutations), this.inheritMiddlewares(Router.prefixProcedures(router._def.mutations, prefix))), | ||
subscriptions: tslib_1.__assign(tslib_1.__assign({}, this._def.subscriptions), this.inheritMiddlewares(Router.prefixProcedures(router._def.subscriptions, prefix))), | ||
queries: tslib_1.__assign(tslib_1.__assign({}, this._def.queries), mergeProcedures(childRouter._def.queries)), | ||
mutations: tslib_1.__assign(tslib_1.__assign({}, this._def.mutations), mergeProcedures(childRouter._def.mutations)), | ||
subscriptions: tslib_1.__assign(tslib_1.__assign({}, this._def.subscriptions), mergeProcedures(childRouter._def.subscriptions)), | ||
middlewares: this._def.middlewares, | ||
}); | ||
}; | ||
Router.prototype.inheritMiddlewares = function (procedures) { | ||
var _a; | ||
var newProcedures = {}; | ||
for (var key in procedures) { | ||
var procedure = procedures[key]; | ||
newProcedures[key] = tslib_1.__assign(tslib_1.__assign({}, procedure), { _middlewares: tslib_1.__spreadArrays(this._def.middlewares, ((_a = procedure._middlewares) !== null && _a !== void 0 ? _a : [])) }); | ||
} | ||
return newProcedures; | ||
}; | ||
Router.getInput = function (route, rawInput) { | ||
if (!route.input) { | ||
return undefined; | ||
} | ||
try { | ||
var anyInput = route.input; | ||
if (typeof anyInput.parse === 'function') { | ||
return anyInput.parse(rawInput); | ||
} | ||
if (typeof anyInput === 'function') { | ||
return anyInput(rawInput); | ||
} | ||
if (typeof anyInput.validateSync === 'function') { | ||
return anyInput.validateSync(rawInput); | ||
} | ||
throw new Error('Could not find a validator fn'); | ||
} | ||
catch (_err) { | ||
var err = new errors_1.InputValidationError(_err); | ||
throw err; | ||
} | ||
}; | ||
/** | ||
* Invoke procedure. Only for internal use within library. | ||
* | ||
* @throws RouteNotFoundError | ||
* @throws InputValidationError | ||
*/ | ||
Router.prototype.invoke = function (opts) { | ||
var _a; | ||
return tslib_1.__awaiter(this, void 0, void 0, function () { | ||
var target, route, ctx, _i, _b, fn, input; | ||
return tslib_1.__generator(this, function (_c) { | ||
switch (_c.label) { | ||
var target, procedure, ctx, input, _i, _a, fn; | ||
return tslib_1.__generator(this, function (_b) { | ||
switch (_b.label) { | ||
case 0: | ||
@@ -159,19 +125,17 @@ if (!this.has(opts.target, opts.path)) { | ||
target = this._def[opts.target]; | ||
route = target[opts.path]; | ||
ctx = opts.ctx; | ||
_i = 0, _b = (_a = route._middlewares) !== null && _a !== void 0 ? _a : []; | ||
_c.label = 1; | ||
procedure = target[opts.path]; | ||
ctx = opts.ctx, input = opts.input; | ||
_i = 0, _a = procedure.middlewares; | ||
_b.label = 1; | ||
case 1: | ||
if (!(_i < _b.length)) return [3 /*break*/, 4]; | ||
fn = _b[_i]; | ||
if (!(_i < _a.length)) return [3 /*break*/, 4]; | ||
fn = _a[_i]; | ||
return [4 /*yield*/, fn({ ctx: ctx })]; | ||
case 2: | ||
_c.sent(); | ||
_c.label = 3; | ||
_b.sent(); | ||
_b.label = 3; | ||
case 3: | ||
_i++; | ||
return [3 /*break*/, 1]; | ||
case 4: | ||
input = Router.getInput(route, opts.input); | ||
return [2 /*return*/, route.resolve({ ctx: ctx, input: input })]; | ||
case 4: return [2 /*return*/, procedure.call({ ctx: ctx, input: input })]; | ||
} | ||
@@ -178,0 +142,0 @@ }); |
{ | ||
"name": "@trpc/server", | ||
"version": "3.0.0-alpha.5", | ||
"version": "3.0.0-alpha.6", | ||
"description": "TRPC Server", | ||
@@ -52,3 +52,3 @@ "author": "KATT", | ||
}, | ||
"gitHead": "c0d6b0e05f9ca3d307545d7f347165e7f12c4133" | ||
"gitHead": "f7e2da6e4d67ad340d206aff2dac778f7f367d7b" | ||
} |
54504
1148