Skip to content

Commit

Permalink
fix: Resolve some type problems (#2260)
Browse files Browse the repository at this point in the history
  • Loading branch information
vonagam authored Mar 22, 2021
1 parent 2fb4cfa commit a3d75fa
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 12 deletions.
7 changes: 4 additions & 3 deletions packages/express/src/declarations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ import {
} from '@feathersjs/feathers';

interface ExpressUseHandler<T, ServiceTypes> {
<L extends keyof ServiceTypes> (
path: ServiceTypes[L] extends never ? string|RegExp : L,
<L extends keyof ServiceTypes & string> (
path: L,
...middlewareOrService: (
Express|express.RequestHandler|
(ServiceTypes[L] extends never ? ServiceInterface<any> : ServiceTypes[L])
(keyof any extends keyof ServiceTypes ? ServiceInterface<any> : ServiceTypes[L])
)[]
): T;
(path: RegExp, ...expressHandlers: express.RequestHandler[]): T;
(...expressHandlers: express.RequestHandler[]): T;
(handler: Express|express.ErrorRequestHandler): T;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/express/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export * from './declarations';

const debug = Debug('@feathersjs/express');

export default function feathersExpress<S = any, C = any> (feathersApp?: FeathersApplication, expressApp: Express = express()): Application<S, C> {
export default function feathersExpress<S = any, C = any> (feathersApp?: FeathersApplication<S, C>, expressApp: Express = express()): Application<S, C> {
if (!feathersApp) {
return expressApp as any;
}
Expand Down Expand Up @@ -62,7 +62,7 @@ export default function feathersExpress<S = any, C = any> (feathersApp?: Feather

debug('Registering service with middleware', middleware);
// Since this is a service, call Feathers `.use`
feathersApp.use.call(this, location, service, { middleware });
(feathersApp as FeathersApplication).use.call(this, location, service, { middleware });

return this;
},
Expand Down
8 changes: 4 additions & 4 deletions packages/feathers/src/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,15 @@ export class Feathers<ServiceTypes, AppSettings> extends EventEmitter implements

use<L extends keyof ServiceTypes & string> (
path: L,
service: (keyof any extends keyof ServiceTypes ? ServiceInterface<any> : ServiceTypes[L]) | Application,
service: keyof any extends keyof ServiceTypes ? ServiceInterface<any> | Application : ServiceTypes[L],
options?: ServiceOptions
): this {
if (typeof path !== 'string') {
throw new Error(`'${path}' is not a valid service path.`);
}

const location = (stripSlashes(path) || '/') as L;
const subApp = service as FeathersApplication;
const subApp = service as Application;
const isSubApp = typeof subApp.service === 'function' && subApp.services;

if (isSubApp) {
Expand Down Expand Up @@ -113,15 +113,15 @@ export class Feathers<ServiceTypes, AppSettings> extends EventEmitter implements
return this;
}

hooks (hookMap: HookOptions<Application<ServiceTypes, AppSettings>, any>) {
hooks (hookMap: HookOptions<this, any>) {
const legacyMap = hookMap as LegacyHookMap<this, any>;

if (legacyMap.before || legacyMap.after || legacyMap.error) {
return this.legacyHooks(legacyMap);
}

if (Array.isArray(hookMap)) {
this.appHooks[HOOKS].push(...hookMap);
this.appHooks[HOOKS].push(...hookMap as any);
} else {
const methodHookMap = hookMap as HookMap<Application<ServiceTypes, AppSettings>, any>;

Expand Down
6 changes: 3 additions & 3 deletions packages/feathers/src/declarations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ export interface FeathersApplication<ServiceTypes = any, AppSettings = any> {
*/
use<L extends keyof ServiceTypes & string> (
path: L,
service: (keyof any extends keyof ServiceTypes ? ServiceInterface<any> : ServiceTypes[L]) | Application,
service: keyof any extends keyof ServiceTypes ? ServiceInterface<any> | Application : ServiceTypes[L],
options?: ServiceOptions
): this;

Expand All @@ -205,7 +205,7 @@ export interface FeathersApplication<ServiceTypes = any, AppSettings = any> {
*
* @param map The application hook settings.
*/
hooks (map: HookOptions<Application<ServiceTypes, AppSettings>, any>): this;
hooks (map: HookOptions<this, any>): this;
}

// This needs to be an interface instead of a type
Expand Down Expand Up @@ -309,7 +309,7 @@ export interface HookContext<A = Application, S = any> extends BaseHookContext<S

// Legacy hook typings
export type LegacyHookFunction<A = Application, S = Service<any, any>> =
(this: S, context: HookContext<A, S>) => (Promise<HookContext<A, S> | void> | HookContext<A, S> | void);
(this: S, context: HookContext<A, S>) => (Promise<HookContext<Application, S> | void> | HookContext<Application, S> | void);

type LegacyHookMethodMap<A, S> =
{ [L in keyof S]?: SelfOrArray<LegacyHookFunction<A, S>>; } &
Expand Down

0 comments on commit a3d75fa

Please # to comment.