Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: application service types default to any #1566

Merged
merged 1 commit into from
Mar 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/authentication-client/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Application } from '@feathersjs/feathers';
import { Storage, MemoryStorage, StorageWrapper } from './storage';

declare module '@feathersjs/feathers/lib/declarations' {
interface Application<ServiceTypes = {}> { // eslint-disable-line
interface Application<ServiceTypes, AppSettings> { // eslint-disable-line
io?: any;
rest?: any;
authentication: AuthenticationClient;
Expand Down
2 changes: 1 addition & 1 deletion packages/authentication/src/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import jsonwebtoken from 'jsonwebtoken';
const debug = Debug('@feathersjs/authentication/service');

declare module '@feathersjs/feathers/lib/declarations' {
interface FeathersApplication<ServiceTypes = {}, AppSettings = {}> { // eslint-disable-line
interface FeathersApplication<ServiceTypes, AppSettings> { // eslint-disable-line
/**
* Returns the default authentication service or the
* authentication service for a given path.
Expand Down
2 changes: 1 addition & 1 deletion packages/express/src/declarations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export interface ExpressOverrides<ServiceTypes> {
use: ExpressUseHandler<this, ServiceTypes>;
}

export type Application<ServiceTypes = {}, AppSettings = {}> =
export type Application<ServiceTypes = any, AppSettings = any> =
Omit<Express, 'listen'|'use'> &
FeathersApplication<ServiceTypes, AppSettings> &
ExpressOverrides<ServiceTypes>;
Expand Down
2 changes: 1 addition & 1 deletion 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 = {}, C = {}> (feathersApp?: FeathersApplication, expressApp: Express = express()): Application<S, C> {
export default function feathersExpress<S = any, C = any> (feathersApp?: FeathersApplication, expressApp: Express = express()): Application<S, C> {
if (!feathersApp) {
return expressApp as any;
}
Expand Down
40 changes: 15 additions & 25 deletions packages/feathers/src/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,12 @@ export class Feathers<ServiceTypes, AppSettings> extends EventEmitter implements
this.legacyHooks = enableLegacyHooks(this);
}

get<L extends keyof AppSettings> (
name: AppSettings[L] extends never ? string : L
): (AppSettings[L] extends never ? any : AppSettings[L])|undefined {
return (this.settings as any)[name];
get<L extends keyof AppSettings & string> (name: L): AppSettings[L] {
return this.settings[name];
}

set<L extends keyof AppSettings> (
name: AppSettings[L] extends never ? string : L,
value: AppSettings[L] extends never ? any : AppSettings[L]
) {
(this.settings as any)[name] = value;
set<L extends keyof AppSettings & string> (name: L, value: AppSettings[L]) {
this.settings[name] = value;
return this;
}

Expand All @@ -64,35 +59,30 @@ export class Feathers<ServiceTypes, AppSettings> extends EventEmitter implements
throw new Error(`Can not find service '${location}'`);
}

service<L extends keyof ServiceTypes> (
location: ServiceTypes[L] extends never ? string : L
): (ServiceTypes[L] extends never
? FeathersService<this, Service<any>>
: FeathersService<this, ServiceTypes[L]>
) {
const path: any = stripSlashes(location as string) || '/';
const current = (this.services as any)[path];
service<L extends keyof ServiceTypes & string> (
location: L
): FeathersService<this, keyof any extends keyof ServiceTypes ? Service<any> : 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<L extends keyof ServiceTypes> (
path: ServiceTypes[L] extends never ? string : L,
service: (ServiceTypes[L] extends never ?
ServiceInterface<any> : ServiceTypes[L]
) | Application,
use<L extends keyof ServiceTypes & string> (
path: L,
service: (keyof any extends keyof ServiceTypes ? ServiceInterface<any> : 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;

Expand All @@ -118,7 +108,7 @@ export class Feathers<ServiceTypes, AppSettings> extends EventEmitter implements
protoService.setup(this, location);
}

(this.services as any)[location] = protoService;
this.services[location] = protoService;

return this;
}
Expand Down
30 changes: 10 additions & 20 deletions packages/feathers/src/declarations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export type ServiceMixin<A> = (service: FeathersService<A>, path: string, option
export type ServiceGenericType<S> = S extends ServiceInterface<infer T> ? T : any;
export type ServiceGenericData<S> = S extends ServiceInterface<infer _T, infer D> ? D : any;

export interface FeathersApplication<ServiceTypes = {}, AppSettings = {}> {
export interface FeathersApplication<ServiceTypes = any, AppSettings = any> {
/**
* The Feathers application version
*/
Expand Down Expand Up @@ -145,20 +145,15 @@ export interface FeathersApplication<ServiceTypes = {}, AppSettings = {}> {
*
* @param name The setting name
*/
get<L extends keyof AppSettings> (
name: AppSettings[L] extends never ? string : L
): (AppSettings[L] extends never ? any : AppSettings[L]);
get<L extends keyof AppSettings & string> (name: L): AppSettings[L];

/**
* Set an application setting
*
* @param name The setting name
* @param value The setting value
*/
set<L extends keyof AppSettings> (
name: AppSettings[L] extends never ? string : L,
value: AppSettings[L] extends never ? any : AppSettings[L]
): this;
set<L extends keyof AppSettings & string> (name: L, value: AppSettings[L]): this;

/**
* Runs a callback configure function with the current application instance.
Expand Down Expand Up @@ -186,11 +181,9 @@ export interface FeathersApplication<ServiceTypes = {}, AppSettings = {}> {
* Feathers application to use a sub-app under the `path` prefix.
* @param options The options for this service
*/
use<L extends keyof ServiceTypes> (
path: ServiceTypes[L] extends never ? string : L,
service: (ServiceTypes[L] extends never ?
ServiceInterface<any> : ServiceTypes[L]
) | Application,
use<L extends keyof ServiceTypes & string> (
path: L,
service: (keyof any extends keyof ServiceTypes ? ServiceInterface<any> : ServiceTypes[L]) | Application,
options?: ServiceOptions
): this;

Expand All @@ -201,12 +194,9 @@ export interface FeathersApplication<ServiceTypes = {}, AppSettings = {}> {
*
* @param path The name of the service.
*/
service<L extends keyof ServiceTypes> (
path: ServiceTypes[L] extends never ? string : L
): (ServiceTypes[L] extends never
? FeathersService<this, Service<any>>
: FeathersService<this, ServiceTypes[L]>
);
service<L extends keyof ServiceTypes & string> (
path: L
): FeathersService<this, keyof any extends keyof ServiceTypes ? Service<any> : ServiceTypes[L]>;

setup (server?: any): Promise<this>;

Expand All @@ -220,7 +210,7 @@ export interface FeathersApplication<ServiceTypes = {}, AppSettings = {}> {

// This needs to be an interface instead of a type
// so that the declaration can be extended by other modules
export interface Application<ServiceTypes = {}, AppSettings = {}> extends FeathersApplication<ServiceTypes, AppSettings>, EventEmitter {
export interface Application<ServiceTypes = any, AppSettings = any> extends FeathersApplication<ServiceTypes, AppSettings>, EventEmitter {

}

Expand Down
2 changes: 1 addition & 1 deletion packages/feathers/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import version from './version';
import { Feathers } from './application';
import { Application } from './declarations';

export function feathers<T = {}, S = {}> () {
export function feathers<T = any, S = any> () {
return new Feathers<T, S>() as Application<T, S>;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/socketio/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { disconnect, params, authentication, FeathersSocket } from './middleware
const debug = Debug('@feathersjs/socketio');

declare module '@feathersjs/feathers/lib/declarations' {
interface Application<ServiceTypes = {}, AppSettings = {}> { // eslint-disable-line
interface Application<ServiceTypes, AppSettings> { // eslint-disable-line
io: Server;
listen (options: any): Promise<http.Server>;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/transport-commons/src/channels/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ declare module '@feathersjs/feathers/lib/declarations' {
registerPublisher (event: Event, publisher: Publisher<ServiceGenericType<S>>): this;
}

interface Application<ServiceTypes = {}, AppSettings = {}> {
interface Application<ServiceTypes, AppSettings> { // eslint-disable-line
channels: string[];

channel (name: string[]): Channel;
Expand Down
2 changes: 1 addition & 1 deletion packages/transport-commons/src/routing/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ declare module '@feathersjs/feathers/lib/declarations' {
params: { [key: string]: string }
}

interface Application<ServiceTypes = {}, AppSettings = {}> { // eslint-disable-line
interface Application<ServiceTypes, AppSettings> { // eslint-disable-line
routes: Router<any>;
lookup (path: string): RouteLookup;
}
Expand Down