Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@trpc/server

Package Overview
Dependencies
Maintainers
3
Versions
1072
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@trpc/server - npm Package Compare versions

Comparing version 3.0.0-alpha.0 to 3.0.0-alpha.1

dist/procedure.d.ts

1

dist/index.d.ts

@@ -7,2 +7,3 @@ export * from './assertNotBrowser';

export * from './transformer';
export * from './procedure';
export * from './adapters/standalone';

@@ -10,2 +10,3 @@ "use strict";

tslib_1.__exportStar(require("./transformer"), exports);
tslib_1.__exportStar(require("./procedure"), exports);
tslib_1.__exportStar(require("./adapters/standalone"), exports);

53

dist/router.d.ts

@@ -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 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 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> = 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 AnyRouter<TContext = any> = Router<TContext, any, any, any, any>;

@@ -51,11 +28,9 @@ 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>;
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, TOutput extends Subscription<unknown>>(path: TPath, procedure: CreateProcedureWithoutInput<TContext, TOutput>): Router<TContext, TQueries, TMutations, TSubscriptions & Record<TPath, inferProcedureFromOptions<typeof procedure>>, TMiddleware>;
/**
* ⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️
* **Experimental.** API might change without major version bump
* ⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠
*/
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>;
/**
* Merge router with other router

@@ -71,4 +46,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: {

@@ -82,3 +61,3 @@ target: 'queries' | 'subscriptions' | 'mutations';

/**
* Function to be called before any route is invoked
* Function to be called before any procedure is invoked
* Can be async or sync

@@ -85,0 +64,0 @@ */

@@ -9,2 +9,3 @@ "use strict";

var errors_1 = require("./errors");
var procedure_1 = require("./procedure");
assertNotBrowser_1.assertNotBrowser();

@@ -20,14 +21,14 @@ var Router = /** @class */ (function () {

}
Router.prefixProcedures = function (routes, prefix) {
Router.prefixProcedures = function (procedures, prefix) {
var eps = {};
for (var key in routes) {
eps[prefix + key] = routes[key];
for (var key in procedures) {
eps[prefix + key] = procedures[key];
}
return eps;
};
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>>(
// routes: TProcedures,
// ): Router<TContext, TQueries & TProcedures, TMutations, TSubscriptions> {
// const router = new Router<TContext, any, {}, {}>({
// queries: routes,
// 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,68 +92,49 @@ 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 (routes) {
var _a;
var newRoutes = {};
for (var key in routes) {
var route = routes[key];
newRoutes[key] = tslib_1.__assign(tslib_1.__assign({}, route), { _middlewares: tslib_1.__spreadArrays(this._def.middlewares, ((_a = route._middlewares) !== null && _a !== void 0 ? _a : [])) });
}
return newRoutes;
};
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:
if (!this.has(opts.target, opts.path)) {
throw new errors_1.RouteNotFoundError("No such route \"" + opts.path + "\"");
throw new errors_1.RouteNotFoundError("No such procedure \"" + 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 })];
}

@@ -184,3 +148,3 @@ });

/**
* Function to be called before any route is invoked
* Function to be called before any procedure is invoked
* Can be async or sync

@@ -187,0 +151,0 @@ */

{
"name": "@trpc/server",
"version": "3.0.0-alpha.0",
"version": "3.0.0-alpha.1",
"description": "TRPC Server",

@@ -52,3 +52,3 @@ "author": "KATT",

},
"gitHead": "a98d84732e3d81a2fbdb5255886a64e70c78ddec"
"gitHead": "4c1e5f04df3e7b5122b40af9c3c18f75d01a28e4"
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc