From 5820570c76b34ba0d8ebeaa541bb25558ca35093 Mon Sep 17 00:00:00 2001 From: Steffen Langer <13887114+SteffenLanger@users.noreply.github.com> Date: Sat, 27 Apr 2019 02:27:51 +0200 Subject: [PATCH] Feathers.js Express: Correct typings for app.use with both middleware and feathers service. (#34782) * Correct typings for app.use with both middleware and feathers service. * Correct typings for app.use with both middleware and feathers service. --- .../feathersjs__express-tests.ts | 18 +++++++++++++-- types/feathersjs__express/index.d.ts | 23 ++++++++++++++++--- 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/types/feathersjs__express/feathersjs__express-tests.ts b/types/feathersjs__express/feathersjs__express-tests.ts index 1bd1f5221a..05de4d4ef3 100644 --- a/types/feathersjs__express/feathersjs__express-tests.ts +++ b/types/feathersjs__express/feathersjs__express-tests.ts @@ -3,9 +3,23 @@ import feathersExpress, * as express from '@feathersjs/express'; const app = feathersExpress(feathers()); +const feathersServiceDummy = { + get : () => { + return Promise.resolve({}); + }, + find : () => { + return Promise.resolve([{}, {}]); + } +}; +const expressMiddlewareDummy = (req: express.Request, res: express.Response, next: express.NextFunction) => { + next(); + return app; +}; + app.use(express.json()); -app.use(express.urlencoded({ extended: true })); +app.use(express.urlencoded({extended : true})); app.use('/', express.static('./public')); +app.use('/', expressMiddlewareDummy, feathersServiceDummy); app.configure(express.rest()); app.use(express.notFound()); -app.use(express.errorHandler({ logger: console })); +app.use(express.errorHandler({logger : console})); diff --git a/types/feathersjs__express/index.d.ts b/types/feathersjs__express/index.d.ts index a14e91832b..e814b80873 100644 --- a/types/feathersjs__express/index.d.ts +++ b/types/feathersjs__express/index.d.ts @@ -3,15 +3,31 @@ // Definitions by: Jan Lohage // Aleksey Klimenko // Definitions: https://github.com/feathersjs-ecosystem/feathers-typescript -// TypeScript Version: 2.3 +// TypeScript Version: 2.8 -import { Application as FeathersApplication } from '@feathersjs/feathers'; +import { Application as FeathersApplication, ServiceMethods, SetupMethod } from '@feathersjs/feathers'; import * as express from 'express'; import * as self from '@feathersjs/express'; +import { IRouterHandler, PathParams, RequestHandler, RequestHandlerParams } from 'express-serve-static-core'; declare const feathersExpress: ((app: FeathersApplication) => Application) & typeof self; export default feathersExpress; -export type Application = express.Application & FeathersApplication; + +type Omit = Pick>; +// TypeScript methods cannot be overloaded with a different signature. Derive two application types without the use methods. +type ExpressAndFeathersApplicationWithoutUse = Omit & Omit, 'use'>; +// Give the "any" type for the feathers options object a more precise name. +export type FeathersServiceOptions = any; + +export interface FeathersRouterMatcher { + (path: PathParams, ...handlers: Array<(RequestHandler | RequestHandlerParams | Partial & SetupMethod> | Application)>): T; +} + +type FeathersApplicationRequestHandler = express.IRouterHandler & FeathersRouterMatcher & ((...handlers: RequestHandlerParams[]) => T); + +export interface Application extends ExpressAndFeathersApplicationWithoutUse { + use: FeathersApplicationRequestHandler; +} export function errorHandler(options?: { public?: string, @@ -19,6 +35,7 @@ export function errorHandler(options?: { html?: any, json?: any, }): express.ErrorRequestHandler; + export function notFound(): express.RequestHandler; export const rest: {