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.
This commit is contained in:
Steffen Langer
2019-04-26 17:27:51 -07:00
committed by Pranav Senthilnathan
parent 197b5fb3c5
commit 5820570c76
2 changed files with 36 additions and 5 deletions
@@ -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}));
+20 -3
View File
@@ -3,15 +3,31 @@
// Definitions by: Jan Lohage <https://github.com/j2L4e>
// Aleksey Klimenko <https://github.com/DadUndead>
// 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: (<T>(app: FeathersApplication<T>) => Application<T>) & typeof self;
export default feathersExpress;
export type Application<T> = express.Application & FeathersApplication<T>;
type Omit<T, K> = Pick<T, Exclude<keyof T, K>>;
// TypeScript methods cannot be overloaded with a different signature. Derive two application types without the use methods.
type ExpressAndFeathersApplicationWithoutUse<T> = Omit<express.Application, 'use'> & Omit<FeathersApplication<T>, 'use'>;
// Give the "any" type for the feathers options object a more precise name.
export type FeathersServiceOptions = any;
export interface FeathersRouterMatcher<T> {
(path: PathParams, ...handlers: Array<(RequestHandler | RequestHandlerParams | Partial<ServiceMethods<any> & SetupMethod> | Application)>): T;
}
type FeathersApplicationRequestHandler<T> = express.IRouterHandler<T> & FeathersRouterMatcher<T> & ((...handlers: RequestHandlerParams[]) => T);
export interface Application<T = any> extends ExpressAndFeathersApplicationWithoutUse<T> {
use: FeathersApplicationRequestHandler<T>;
}
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: {