@trpc/server
Advanced tools
Comparing version 2.1.0 to 2.2.0
import type * as express from 'express'; | ||
import { BaseOptions, CreateContextFn, CreateContextFnOptions } from '../http'; | ||
import { Router } from '../router'; | ||
import { AnyRouter } from '../router'; | ||
export declare type CreateExpressContextOptions = CreateContextFnOptions<express.Request, express.Response>; | ||
export declare type CreateExpressContextFn<TContext> = CreateContextFn<TContext, express.Request, express.Response>; | ||
export declare function createExpressMiddleware<TContext, TRouter extends Router<TContext, any, any, any>>(opts: { | ||
export declare function createExpressMiddleware<TContext, TRouter extends AnyRouter<TContext>>(opts: { | ||
router: TRouter; | ||
createContext: CreateExpressContextFn<TContext>; | ||
} & BaseOptions): express.Handler; |
import type { NextApiHandler, NextApiRequest, NextApiResponse } from 'next'; | ||
import { BaseOptions, CreateContextFn, CreateContextFnOptions } from '../http'; | ||
import { Router } from '../router'; | ||
import { AnyRouter } from '../router'; | ||
export declare type CreateNextContextOptions = CreateContextFnOptions<NextApiRequest, NextApiResponse>; | ||
export declare type CreateNextContextFn<TContext> = CreateContextFn<TContext, NextApiRequest, NextApiResponse>; | ||
export declare function createNextApiHandler<TContext, TRouter extends Router<TContext, any, any, any>>(opts: { | ||
export declare function createNextApiHandler<TContext, TRouter extends AnyRouter<TContext>>(opts: { | ||
router: TRouter; | ||
createContext: CreateNextContextFn<TContext>; | ||
} & BaseOptions): NextApiHandler; |
@@ -5,3 +5,3 @@ /// <reference types="node" /> | ||
import { InputValidationError } from './errors'; | ||
import { Router } from './router'; | ||
import { AnyRouter } from './router'; | ||
import { DataTransformer } from './transformer'; | ||
@@ -63,3 +63,3 @@ export declare class HTTPError extends Error { | ||
} | ||
export declare function requestHandler<TContext, TRouter extends Router<TContext, any, any, any>, TCreateContextFn extends CreateContextFn<TContext, TRequest, TResponse>, TRequest extends BaseRequest, TResponse extends BaseResponse>({ req, res, router, path, subscriptions, createContext, teardown, transformer, maxBodySize, }: { | ||
export declare function requestHandler<TContext, TRouter extends AnyRouter<TContext>, TCreateContextFn extends CreateContextFn<TContext, TRequest, TResponse>, TRequest extends BaseRequest, TResponse extends BaseResponse>({ req, res, router, path, subscriptions, createContext, teardown, transformer, maxBodySize, }: { | ||
req: TRequest; | ||
@@ -66,0 +66,0 @@ res: TResponse; |
@@ -128,2 +128,7 @@ "use strict"; | ||
}; | ||
if (method === 'HEAD') { | ||
res.statusCode = 204; | ||
res.end(); | ||
return [2 /*return*/]; | ||
} | ||
if (!(method === 'POST')) return [3 /*break*/, 5]; | ||
@@ -130,0 +135,0 @@ return [4 /*yield*/, getPostBody({ req: req, maxBodySize: maxBodySize })]; |
import { Subscription } from './subscription'; | ||
import { Prefixer, ThenArg } from './types'; | ||
import { format, Prefixer, ThenArg } from './types'; | ||
export declare type RouteInputParserZodEsque<TInput = unknown> = { | ||
@@ -23,3 +23,5 @@ parse: (input: any) => TInput; | ||
}; | ||
export declare type Route<TContext = unknown, TInput = unknown, TOutput = unknown> = RouteWithInput<TContext, TInput, TOutput> | RouteWithoutInput<TContext, TInput, TOutput>; | ||
export declare type Route<TContext = unknown, TInput = unknown, TOutput = unknown> = (RouteWithInput<TContext, TInput, TOutput> | RouteWithoutInput<TContext, TInput, TOutput>) & { | ||
_middlewares?: MiddlewareFunction<TContext>[]; | ||
}; | ||
export declare type RouteRecord<TContext = unknown, TInput = unknown, TOutput = unknown> = Record<string, Route<TContext, TInput, TOutput>>; | ||
@@ -32,4 +34,7 @@ export declare type inferRouteInput<TRoute extends Route<any, any, any>> = TRoute extends RouteWithInput<any, infer Input, any> ? Input : never; | ||
export declare type inferHandlerFn<TRoutes extends RouteRecord<any, any, any>> = <TRoute extends TRoutes[TPath], TPath extends keyof TRoutes & string>(path: TPath, ...args: TRoute extends RouteWithInput<any, any, any> ? [inferRouteInput<TRoute>] : [undefined?]) => Promise<inferRouteOutput<TRoutes[TPath]>>; | ||
export declare type AnyRouter<TContext = any> = Router<TContext, any, any, any>; | ||
export declare class Router<TContext, TQueries extends RouteRecord<TContext>, TMutations extends RouteRecord<TContext>, TSubscriptions extends RouteRecord<TContext, unknown, Subscription<unknown>>> { | ||
export declare type AnyRouter<TContext = any> = Router<TContext, any, any, any, any>; | ||
export declare type MiddlewareFunction<TContext> = (opts: { | ||
ctx: TContext; | ||
}) => Promise<void> | void; | ||
export declare class Router<TContext, TQueries extends RouteRecord<TContext>, TMutations extends RouteRecord<TContext>, TSubscriptions extends RouteRecord<TContext, unknown, Subscription<unknown>>, TMiddleware extends MiddlewareFunction<TContext>> { | ||
readonly _def: Readonly<{ | ||
@@ -39,2 +44,3 @@ queries: Readonly<TQueries>; | ||
subscriptions: Readonly<TSubscriptions>; | ||
middlewares: TMiddleware[]; | ||
}>; | ||
@@ -45,6 +51,7 @@ constructor(def?: { | ||
subscriptions: TSubscriptions; | ||
middlewares: TMiddleware[]; | ||
}); | ||
private static prefixRoutes; | ||
query<TPath extends string, TInput, TOutput>(path: TPath, route: Route<TContext, TInput, TOutput>): Router<TContext, TQueries & Record<TPath, typeof route>, TMutations, TSubscriptions>; | ||
mutation<TPath extends string, TInput, TOutput>(path: TPath, route: Route<TContext, TInput, TOutput>): Router<TContext, TQueries, TMutations & Record<TPath, typeof route>, TSubscriptions>; | ||
query<TPath extends string, TInput, TOutput>(path: TPath, route: Route<TContext, TInput, TOutput>): Router<TContext, format<TQueries & Record<TPath, typeof route>>, TMutations, TSubscriptions, TMiddleware>; | ||
mutation<TPath extends string, TInput, TOutput>(path: TPath, route: Route<TContext, TInput, TOutput>): Router<TContext, TQueries, format<TMutations & Record<TPath, typeof route>>, TSubscriptions, TMiddleware>; | ||
/** | ||
@@ -55,3 +62,3 @@ * ⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️ | ||
*/ | ||
subscription<TPath extends string, TInput, TOutput extends Subscription>(path: TPath, route: Route<TContext, TInput, TOutput>): Router<TContext, TQueries, TMutations, TSubscriptions & Record<TPath, typeof route>>; | ||
subscription<TPath extends string, TInput, TOutput extends Subscription>(path: TPath, route: Route<TContext, TInput, TOutput>): Router<TContext, TQueries, TMutations, format<TSubscriptions & Record<TPath, typeof route>>, TMiddleware>; | ||
/** | ||
@@ -61,3 +68,3 @@ * Merge router with other router | ||
*/ | ||
merge<TChildRouter extends AnyRouter<TContext>>(router: TChildRouter): Router<TContext, TQueries & TChildRouter['_def']['queries'], TMutations & TChildRouter['_def']['mutations'], TSubscriptions & TChildRouter['_def']['subscriptions']>; | ||
merge<TChildRouter extends AnyRouter<TContext>>(router: TChildRouter): Router<TContext, format<TQueries & TChildRouter['_def']['queries']>, format<TMutations & TChildRouter['_def']['mutations']>, format<TSubscriptions & TChildRouter['_def']['subscriptions']>, TMiddleware>; | ||
/** | ||
@@ -68,3 +75,4 @@ * Merge router with other 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}`>>; | ||
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 inhertMiddlewares; | ||
private static getInput; | ||
@@ -78,3 +86,8 @@ invoke(opts: { | ||
has(what: 'subscriptions' | 'mutations' | 'queries', path: string): boolean; | ||
/** | ||
* Function to be called before any route is invoked | ||
* Can be async or sync | ||
*/ | ||
middleware(fn: TMiddleware): this; | ||
} | ||
export declare function router<TContext>(): Router<TContext, {}, {}, {}>; | ||
export declare function router<TContext>(): Router<TContext, {}, {}, {}, MiddlewareFunction<TContext>>; |
@@ -16,2 +16,3 @@ "use strict"; | ||
subscriptions: {}, | ||
middlewares: [], | ||
}; | ||
@@ -34,2 +35,3 @@ } | ||
subscriptions: {}, | ||
middlewares: [], | ||
}); | ||
@@ -57,2 +59,3 @@ return this.merge(router); | ||
subscriptions: {}, | ||
middlewares: [], | ||
}); | ||
@@ -74,2 +77,3 @@ return this.merge(router); | ||
_a), | ||
middlewares: [], | ||
}); | ||
@@ -93,6 +97,6 @@ return this.merge(router); | ||
var duplicateQueries = Object.keys(router._def.queries).filter(function (key) { | ||
return _this.has('queries', key); | ||
return _this.has('queries', prefix + key); | ||
}); | ||
var duplicateMutations = Object.keys(router._def.mutations).filter(function (key) { return _this.has('mutations', key); }); | ||
var duplicateSubscriptions = Object.keys(router._def.subscriptions).filter(function (key) { return _this.has('subscriptions', 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 duplicates = tslib_1.__spreadArrays(duplicateQueries, duplicateMutations, duplicateSubscriptions); | ||
@@ -103,7 +107,17 @@ if (duplicates.length) { | ||
return new Router({ | ||
queries: tslib_1.__assign(tslib_1.__assign({}, this._def.queries), Router.prefixRoutes(router._def.queries, prefix)), | ||
mutations: tslib_1.__assign(tslib_1.__assign({}, this._def.mutations), Router.prefixRoutes(router._def.mutations, prefix)), | ||
subscriptions: tslib_1.__assign(tslib_1.__assign({}, this._def.subscriptions), Router.prefixRoutes(router._def.subscriptions, prefix)), | ||
queries: tslib_1.__assign(tslib_1.__assign({}, this._def.queries), this.inhertMiddlewares(Router.prefixRoutes(router._def.queries, prefix))), | ||
mutations: tslib_1.__assign(tslib_1.__assign({}, this._def.mutations), this.inhertMiddlewares(Router.prefixRoutes(router._def.mutations, prefix))), | ||
subscriptions: tslib_1.__assign(tslib_1.__assign({}, this._def.subscriptions), this.inhertMiddlewares(Router.prefixRoutes(router._def.subscriptions, prefix))), | ||
middlewares: this._def.middlewares, | ||
}); | ||
}; | ||
Router.prototype.inhertMiddlewares = 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) { | ||
@@ -132,13 +146,30 @@ if (!route.input) { | ||
Router.prototype.invoke = function (opts) { | ||
var _a; | ||
return tslib_1.__awaiter(this, void 0, void 0, function () { | ||
var target, route, input, ctx; | ||
return tslib_1.__generator(this, function (_a) { | ||
if (!this.has(opts.target, opts.path)) { | ||
throw new errors_1.RouteNotFoundError("No such route \"" + opts.path + "\""); | ||
var target, route, ctx, _i, _b, fn, input; | ||
return tslib_1.__generator(this, function (_c) { | ||
switch (_c.label) { | ||
case 0: | ||
if (!this.has(opts.target, opts.path)) { | ||
throw new errors_1.RouteNotFoundError("No such route \"" + 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; | ||
case 1: | ||
if (!(_i < _b.length)) return [3 /*break*/, 4]; | ||
fn = _b[_i]; | ||
return [4 /*yield*/, fn({ ctx: ctx })]; | ||
case 2: | ||
_c.sent(); | ||
_c.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 })]; | ||
} | ||
target = this._def[opts.target]; | ||
route = target[opts.path]; | ||
input = Router.getInput(route, opts.input); | ||
ctx = opts.ctx; | ||
return [2 /*return*/, route.resolve({ ctx: ctx, input: input })]; | ||
}); | ||
@@ -150,2 +181,10 @@ }); | ||
}; | ||
/** | ||
* Function to be called before any route is invoked | ||
* Can be async or sync | ||
*/ | ||
Router.prototype.middleware = function (fn) { | ||
this._def.middlewares.push(fn); | ||
return this; | ||
}; | ||
return Router; | ||
@@ -152,0 +191,0 @@ }()); |
{ | ||
"name": "@trpc/server", | ||
"version": "2.1.0", | ||
"version": "2.2.0", | ||
"description": "TRPC Server", | ||
@@ -9,3 +9,3 @@ "author": "KATT", | ||
"type": "git", | ||
"url": "git+https://github.com/trpcio/trpc.git", | ||
"url": "git+https://github.com/trpc/trpc.git", | ||
"directory": "packages/server" | ||
@@ -53,3 +53,3 @@ }, | ||
}, | ||
"gitHead": "a781ed088e72b49702359d4f9368cf2ddabe6130" | ||
"gitHead": "e9f4f7750c0ff7b66c1ff291e45a42eb86d05f8a" | ||
} |
# @trpc/server | ||
https://github.com/trpcio/trpc | ||
https://github.com/trpc/trpc |
48654
1026
4