From d93ba9a17edd20d3397bb00f4f6e82e804e42ed6 Mon Sep 17 00:00:00 2001 From: Dmitrii Maganov Date: Sat, 20 Mar 2021 21:41:58 +0300 Subject: [PATCH] feat: Application service types default to any (#1566) --- packages/authentication-client/src/index.ts | 2 +- packages/authentication/src/service.ts | 2 +- packages/express/src/declarations.ts | 2 +- packages/express/src/index.ts | 2 +- packages/feathers/src/application.ts | 40 +++++++------------ packages/feathers/src/declarations.ts | 30 +++++--------- packages/feathers/src/index.ts | 2 +- packages/socketio/src/index.ts | 2 +- .../transport-commons/src/channels/index.ts | 2 +- .../transport-commons/src/routing/index.ts | 2 +- 10 files changed, 33 insertions(+), 53 deletions(-) diff --git a/packages/authentication-client/src/index.ts b/packages/authentication-client/src/index.ts index 4ee6b2005..34f405b97 100644 --- a/packages/authentication-client/src/index.ts +++ b/packages/authentication-client/src/index.ts @@ -4,7 +4,7 @@ import { Application } from '@feathersjs/feathers'; import { Storage, MemoryStorage, StorageWrapper } from './storage'; declare module '@feathersjs/feathers/lib/declarations' { - interface Application { // eslint-disable-line + interface Application { // eslint-disable-line io?: any; rest?: any; authentication: AuthenticationClient; diff --git a/packages/authentication/src/service.ts b/packages/authentication/src/service.ts index b578981a8..70866376d 100644 --- a/packages/authentication/src/service.ts +++ b/packages/authentication/src/service.ts @@ -10,7 +10,7 @@ import jsonwebtoken from 'jsonwebtoken'; const debug = Debug('@feathersjs/authentication/service'); declare module '@feathersjs/feathers/lib/declarations' { - interface FeathersApplication { // eslint-disable-line + interface FeathersApplication { // eslint-disable-line /** * Returns the default authentication service or the * authentication service for a given path. diff --git a/packages/express/src/declarations.ts b/packages/express/src/declarations.ts index 5872277a7..676e98315 100644 --- a/packages/express/src/declarations.ts +++ b/packages/express/src/declarations.ts @@ -25,7 +25,7 @@ export interface ExpressOverrides { use: ExpressUseHandler; } -export type Application = +export type Application = Omit & FeathersApplication & ExpressOverrides; diff --git a/packages/express/src/index.ts b/packages/express/src/index.ts index a702f4173..9698dc679 100644 --- a/packages/express/src/index.ts +++ b/packages/express/src/index.ts @@ -21,7 +21,7 @@ export * from './declarations'; const debug = Debug('@feathersjs/express'); -export default function feathersExpress (feathersApp?: FeathersApplication, expressApp: Express = express()): Application { +export default function feathersExpress (feathersApp?: FeathersApplication, expressApp: Express = express()): Application { if (!feathersApp) { return expressApp as any; } diff --git a/packages/feathers/src/application.ts b/packages/feathers/src/application.ts index 85f3d1ef0..19b80c14c 100644 --- a/packages/feathers/src/application.ts +++ b/packages/feathers/src/application.ts @@ -40,17 +40,12 @@ export class Feathers extends EventEmitter implements this.legacyHooks = enableLegacyHooks(this); } - get ( - name: AppSettings[L] extends never ? string : L - ): (AppSettings[L] extends never ? any : AppSettings[L])|undefined { - return (this.settings as any)[name]; + get (name: L): AppSettings[L] { + return this.settings[name]; } - set ( - name: AppSettings[L] extends never ? string : L, - value: AppSettings[L] extends never ? any : AppSettings[L] - ) { - (this.settings as any)[name] = value; + set (name: L, value: AppSettings[L]) { + this.settings[name] = value; return this; } @@ -64,35 +59,30 @@ export class Feathers extends EventEmitter implements throw new Error(`Can not find service '${location}'`); } - service ( - location: ServiceTypes[L] extends never ? string : L - ): (ServiceTypes[L] extends never - ? FeathersService> - : FeathersService - ) { - const path: any = stripSlashes(location as string) || '/'; - const current = (this.services as any)[path]; + service ( + location: L + ): FeathersService : ServiceTypes[L]> { + const path = (stripSlashes(location) || '/') as L; + const current = this.services[path]; if (typeof current === 'undefined') { this.use(path, this.defaultService(path) as any); return this.service(path); } - return current; + return current as any; } - use ( - path: ServiceTypes[L] extends never ? string : L, - service: (ServiceTypes[L] extends never ? - ServiceInterface : ServiceTypes[L] - ) | Application, + use ( + path: L, + service: (keyof any extends keyof ServiceTypes ? ServiceInterface : ServiceTypes[L]) | Application, options?: ServiceOptions ): this { if (typeof path !== 'string') { throw new Error(`'${path}' is not a valid service path.`); } - const location = stripSlashes(path) || '/'; + const location = (stripSlashes(path) || '/') as L; const subApp = service as FeathersApplication; const isSubApp = typeof subApp.service === 'function' && subApp.services; @@ -118,7 +108,7 @@ export class Feathers extends EventEmitter implements protoService.setup(this, location); } - (this.services as any)[location] = protoService; + this.services[location] = protoService; return this; } diff --git a/packages/feathers/src/declarations.ts b/packages/feathers/src/declarations.ts index 8a9fa38b7..b827c0cbe 100644 --- a/packages/feathers/src/declarations.ts +++ b/packages/feathers/src/declarations.ts @@ -105,7 +105,7 @@ export type ServiceMixin = (service: FeathersService, path: string, option export type ServiceGenericType = S extends ServiceInterface ? T : any; export type ServiceGenericData = S extends ServiceInterface ? D : any; -export interface FeathersApplication { +export interface FeathersApplication { /** * The Feathers application version */ @@ -145,9 +145,7 @@ export interface FeathersApplication { * * @param name The setting name */ - get ( - name: AppSettings[L] extends never ? string : L - ): (AppSettings[L] extends never ? any : AppSettings[L]); + get (name: L): AppSettings[L]; /** * Set an application setting @@ -155,10 +153,7 @@ export interface FeathersApplication { * @param name The setting name * @param value The setting value */ - set ( - name: AppSettings[L] extends never ? string : L, - value: AppSettings[L] extends never ? any : AppSettings[L] - ): this; + set (name: L, value: AppSettings[L]): this; /** * Runs a callback configure function with the current application instance. @@ -186,11 +181,9 @@ export interface FeathersApplication { * Feathers application to use a sub-app under the `path` prefix. * @param options The options for this service */ - use ( - path: ServiceTypes[L] extends never ? string : L, - service: (ServiceTypes[L] extends never ? - ServiceInterface : ServiceTypes[L] - ) | Application, + use ( + path: L, + service: (keyof any extends keyof ServiceTypes ? ServiceInterface : ServiceTypes[L]) | Application, options?: ServiceOptions ): this; @@ -201,12 +194,9 @@ export interface FeathersApplication { * * @param path The name of the service. */ - service ( - path: ServiceTypes[L] extends never ? string : L - ): (ServiceTypes[L] extends never - ? FeathersService> - : FeathersService - ); + service ( + path: L + ): FeathersService : ServiceTypes[L]>; setup (server?: any): Promise; @@ -220,7 +210,7 @@ export interface FeathersApplication { // This needs to be an interface instead of a type // so that the declaration can be extended by other modules -export interface Application extends FeathersApplication, EventEmitter { +export interface Application extends FeathersApplication, EventEmitter { } diff --git a/packages/feathers/src/index.ts b/packages/feathers/src/index.ts index 7daadc8e2..3a23c6f8e 100644 --- a/packages/feathers/src/index.ts +++ b/packages/feathers/src/index.ts @@ -4,7 +4,7 @@ import version from './version'; import { Feathers } from './application'; import { Application } from './declarations'; -export function feathers () { +export function feathers () { return new Feathers() as Application; } diff --git a/packages/socketio/src/index.ts b/packages/socketio/src/index.ts index c77fcea20..05d7a415f 100644 --- a/packages/socketio/src/index.ts +++ b/packages/socketio/src/index.ts @@ -9,7 +9,7 @@ import { disconnect, params, authentication, FeathersSocket } from './middleware const debug = Debug('@feathersjs/socketio'); declare module '@feathersjs/feathers/lib/declarations' { - interface Application { // eslint-disable-line + interface Application { // eslint-disable-line io: Server; listen (options: any): Promise; } diff --git a/packages/transport-commons/src/channels/index.ts b/packages/transport-commons/src/channels/index.ts index d57225ff3..15f62989b 100644 --- a/packages/transport-commons/src/channels/index.ts +++ b/packages/transport-commons/src/channels/index.ts @@ -18,7 +18,7 @@ declare module '@feathersjs/feathers/lib/declarations' { registerPublisher (event: Event, publisher: Publisher>): this; } - interface Application { + interface Application { // eslint-disable-line channels: string[]; channel (name: string[]): Channel; diff --git a/packages/transport-commons/src/routing/index.ts b/packages/transport-commons/src/routing/index.ts index fae496f8e..6bcd141f6 100644 --- a/packages/transport-commons/src/routing/index.ts +++ b/packages/transport-commons/src/routing/index.ts @@ -7,7 +7,7 @@ declare module '@feathersjs/feathers/lib/declarations' { params: { [key: string]: string } } - interface Application { // eslint-disable-line + interface Application { // eslint-disable-line routes: Router; lookup (path: string): RouteLookup; }