Skip to content

Commit

Permalink
fix(typescript): Overall typing improvements (#2478)
Browse files Browse the repository at this point in the history
  • Loading branch information
vonagam committed Nov 7, 2021
1 parent b7143d4 commit b8eb804
Show file tree
Hide file tree
Showing 35 changed files with 132 additions and 130 deletions.
2 changes: 1 addition & 1 deletion packages/adapter-commons/test/service.test.ts
Expand Up @@ -27,7 +27,7 @@ describe('@feathersjs/adapter-commons/service', () => {
});

describe('works when methods exist', () => {
class MethodService extends AdapterService implements InternalServiceMethods<any> {
class MethodService extends AdapterService implements InternalServiceMethods {
_find (_params?: Params) {
return Promise.resolve([]);
}
Expand Down
1 change: 1 addition & 0 deletions packages/adapter-tests/package.json
Expand Up @@ -48,6 +48,7 @@
"access": "public"
},
"devDependencies": {
"@types/mocha": "^9.0.0",
"@types/node": "^16.10.4",
"mocha": "^9.1.2",
"shx": "^0.3.3",
Expand Down
2 changes: 1 addition & 1 deletion packages/authentication-client/src/index.ts
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, AppSettings> { // eslint-disable-line
interface Application<Services, Settings> { // eslint-disable-line
io?: any;
rest?: any;
authentication: AuthenticationClient;
Expand Down
2 changes: 1 addition & 1 deletion packages/authentication-local/src/hooks/hash-password.ts
Expand Up @@ -18,7 +18,7 @@ export default function hashPassword (field: string, options: HashPasswordOption
throw new Error('The hashPassword hook requires a field name option');
}

return async (context: HookContext<any, any>, next?: NextFunction) => {
return async (context: HookContext, next?: NextFunction) => {
const { app, data, params } = context;

if (data !== undefined) {
Expand Down
2 changes: 1 addition & 1 deletion packages/authentication-local/src/hooks/protect.ts
@@ -1,7 +1,7 @@
import omit from 'lodash/omit';
import { HookContext, NextFunction } from '@feathersjs/feathers';

export default (...fields: string[]) => async (context: HookContext<any, any>, next?: NextFunction) => {
export default (...fields: string[]) => async (context: HookContext, next?: NextFunction) => {
const o = (current: any) => {
if (typeof current === 'object' && !Array.isArray(current)) {
const data = typeof current.toJSON === 'function'
Expand Down
2 changes: 1 addition & 1 deletion packages/authentication/src/hooks/authenticate.ts
Expand Up @@ -20,7 +20,7 @@ export default (originalSettings: string | AuthenticateHookSettings, ...original
throw new Error('The authenticate hook needs at least one allowed strategy');
}

return async (context: HookContext<any, any>, _next?: NextFunction) => {
return async (context: HookContext, _next?: NextFunction) => {
const next = typeof _next === 'function' ? _next : async () => context;
const { app, params, type, path, service } = context;
const { strategies } = settings;
Expand Down
8 changes: 2 additions & 6 deletions packages/authentication/src/index.ts
@@ -1,9 +1,5 @@
import * as hooks from './hooks';

const { authenticate } = hooks;

export { hooks };
export { authenticate };
export * as hooks from './hooks';
export { authenticate } from './hooks';
export {
AuthenticationBase,
AuthenticationRequest,
Expand Down
2 changes: 1 addition & 1 deletion packages/authentication/src/service.ts
Expand Up @@ -10,7 +10,7 @@ import jsonwebtoken from 'jsonwebtoken';
const debug = createDebug('@feathersjs/authentication/service');

declare module '@feathersjs/feathers/lib/declarations' {
interface FeathersApplication<ServiceTypes, AppSettings> { // eslint-disable-line
interface FeathersApplication<Services, Settings> { // 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/authentication/src/strategy.ts
Expand Up @@ -22,7 +22,7 @@ export class AuthenticationBaseStrategy implements AuthenticationStrategy {
return this.authentication.configuration[this.name];
}

get entityService (): Service<any> {
get entityService (): Service {
const { service } = this.configuration;

if (!service) {
Expand Down
2 changes: 1 addition & 1 deletion packages/authentication/test/hooks/authenticate.test.ts
Expand Up @@ -10,7 +10,7 @@ describe('authentication/hooks/authenticate', () => {
let app: Application<{
authentication: AuthenticationService,
'auth-v2': AuthenticationService,
users: Partial<ServiceMethods<any>> & { id: string }
users: Partial<ServiceMethods> & { id: string }
}>;

beforeEach(() => {
Expand Down
4 changes: 2 additions & 2 deletions packages/authentication/test/jwt.test.ts
Expand Up @@ -12,8 +12,8 @@ const { authenticate } = hooks;
describe('authentication/jwt', () => {
let app: Application<{
authentication: AuthenticationService,
users: Partial<Service<any>>,
protected: Partial<Service<any>>
users: Partial<Service>,
protected: Partial<Service>
}>;
let user: any;
let accessToken: string;
Expand Down
4 changes: 3 additions & 1 deletion packages/express/package.json
Expand Up @@ -51,16 +51,18 @@
"dependencies": {
"@feathersjs/commons": "^5.0.0-pre.14",
"@feathersjs/errors": "^5.0.0-pre.14",
"@feathersjs/feathers": "^5.0.0-pre.14",
"@feathersjs/transport-commons": "^5.0.0-pre.14",
"@types/express": "^4.17.13",
"@types/express-serve-static-core": "^4.17.24",
"express": "^4.17.1",
"lodash": "^4.17.21"
},
"devDependencies": {
"@feathersjs/authentication": "^5.0.0-pre.14",
"@feathersjs/authentication-local": "^5.0.0-pre.14",
"@feathersjs/feathers": "^5.0.0-pre.14",
"@feathersjs/tests": "^5.0.0-pre.14",
"@types/lodash": "^4.14.175",
"@types/mocha": "^9.0.0",
"@types/node": "^16.10.4",
"axios": "^0.23.0",
Expand Down
20 changes: 10 additions & 10 deletions packages/express/src/declarations.ts
Expand Up @@ -5,34 +5,34 @@ import {
HookContext, ServiceMethods, ServiceInterface
} from '@feathersjs/feathers';

interface ExpressUseHandler<T, ServiceTypes> {
<L extends keyof ServiceTypes & string> (
interface ExpressUseHandler<T, Services> {
<L extends keyof Services & string> (
path: L,
...middlewareOrService: (
Express|express.RequestHandler|
(keyof any extends keyof ServiceTypes ? ServiceInterface<any> : ServiceTypes[L])
(keyof any extends keyof Services ? ServiceInterface : Services[L])
)[]
): T;
(path: string|RegExp, ...expressHandlers: express.RequestHandler[]): T;
(...expressHandlers: express.RequestHandler[]): T;
(handler: Express|express.ErrorRequestHandler): T;
}

export interface ExpressOverrides<ServiceTypes> {
export interface ExpressOverrides<Services> {
listen(port: number, hostname: string, backlog: number, callback?: () => void): Promise<http.Server>;
listen(port: number, hostname: string, callback?: () => void): Promise<http.Server>;
listen(port: number|string|any, callback?: () => void): Promise<http.Server>;
listen(callback?: () => void): Promise<http.Server>;
use: ExpressUseHandler<this, ServiceTypes>;
use: ExpressUseHandler<this, Services>;
}

export type Application<ServiceTypes = any, AppSettings = any> =
export type Application<Services = any, Settings = any> =
Omit<Express, 'listen'|'use'> &
FeathersApplication<ServiceTypes, AppSettings> &
ExpressOverrides<ServiceTypes>;
FeathersApplication<Services, Settings> &
ExpressOverrides<Services>;

declare module '@feathersjs/feathers/lib/declarations' {
export interface ServiceOptions {
interface ServiceOptions {
middleware?: {
before: express.RequestHandler[],
after: express.RequestHandler[]
Expand All @@ -54,7 +54,7 @@ declare module 'express-serve-static-core' {
// eslint-disable-next-line
<P extends Params = ParamsDictionary, ResBody = any, ReqBody = any>(
path: PathParams,
...handlers: (RequestHandler<P, ResBody, ReqBody> | Partial<ServiceMethods<any, any>> | Application)[]
...handlers: (RequestHandler<P, ResBody, ReqBody> | Partial<ServiceMethods> | Application)[]
): T;
}
}
7 changes: 3 additions & 4 deletions packages/express/src/rest.ts
@@ -1,15 +1,14 @@
import { MethodNotAllowed } from '@feathersjs/errors';
import { HookContext } from '@feathersjs/hooks';
import { createDebug } from '@feathersjs/commons';
import { http } from '@feathersjs/transport-commons';
import { createContext, defaultServiceMethods, getServiceOptions } from '@feathersjs/feathers';
import { HookContext, createContext, defaultServiceMethods, getServiceOptions } from '@feathersjs/feathers';
import { Request, Response, NextFunction, RequestHandler, Router } from 'express';

import { parseAuthentication } from './authentication';

const debug = createDebug('@feathersjs/express/rest');

export type ServiceCallback = (req: Request, res: Response, options: http.ServiceParams) => Promise<HookContext|any>;
export type ServiceCallback = (req: Request, res: Response, options: http.ServiceParams) => Promise<HookContext>;

export const feathersParams = (req: Request, _res: Response, next: NextFunction) => {
req.feathers = {
Expand Down Expand Up @@ -69,7 +68,7 @@ export const serviceMethodHandler = (
const args = getArgs(options);
const context = createContext(service, method);

res.hook = context as any;
res.hook = context;

return service[method](...args, context);
});
Expand Down
26 changes: 13 additions & 13 deletions packages/feathers/src/application.ts
Expand Up @@ -21,13 +21,13 @@ import { enableLegacyHooks } from './hooks/legacy';

const debug = createDebug('@feathersjs/feathers');

export class Feathers<ServiceTypes, AppSettings> extends EventEmitter implements FeathersApplication<ServiceTypes, AppSettings> {
services: ServiceTypes = ({} as ServiceTypes);
settings: AppSettings = ({} as AppSettings);
mixins: ServiceMixin<Application<ServiceTypes, AppSettings>>[] = [ hookMixin, eventMixin ];
export class Feathers<Services, Settings> extends EventEmitter implements FeathersApplication<Services, Settings> {
services: Services = ({} as Services);
settings: Settings = ({} as Settings);
mixins: ServiceMixin<Application<Services, Settings>>[] = [ hookMixin, eventMixin ];
version: string = version;
_isSetup = false;
appHooks: HookMap<Application<ServiceTypes, AppSettings>, any> = {
appHooks: HookMap<Application<Services, Settings>, any> = {
[HOOKS]: [ (eventHook as any) ]
};

Expand All @@ -38,11 +38,11 @@ export class Feathers<ServiceTypes, AppSettings> extends EventEmitter implements
this.legacyHooks = enableLegacyHooks(this);
}

get<L extends keyof AppSettings & string> (name: L): AppSettings[L] {
get<L extends keyof Settings & string> (name: L): Settings[L] {
return this.settings[name];
}

set<L extends keyof AppSettings & string> (name: L, value: AppSettings[L]) {
set<L extends keyof Settings & string> (name: L, value: Settings[L]) {
this.settings[name] = value;
return this;
}
Expand All @@ -53,13 +53,13 @@ export class Feathers<ServiceTypes, AppSettings> extends EventEmitter implements
return this;
}

defaultService (location: string): ServiceInterface<any> {
defaultService (location: string): ServiceInterface {
throw new Error(`Can not find service '${location}'`);
}

service<L extends keyof ServiceTypes & string> (
service<L extends keyof Services & string> (
location: L
): FeathersService<this, keyof any extends keyof ServiceTypes ? Service<any> : ServiceTypes[L]> {
): FeathersService<this, keyof any extends keyof Services ? Service : Services[L]> {
const path = (stripSlashes(location) || '/') as L;
const current = this.services[path];

Expand All @@ -71,9 +71,9 @@ export class Feathers<ServiceTypes, AppSettings> extends EventEmitter implements
return current as any;
}

use<L extends keyof ServiceTypes & string> (
use<L extends keyof Services & string> (
path: L,
service: keyof any extends keyof ServiceTypes ? ServiceInterface<any> | Application : ServiceTypes[L],
service: keyof any extends keyof Services ? ServiceInterface | Application : Services[L],
options?: ServiceOptions
): this {
if (typeof path !== 'string') {
Expand Down Expand Up @@ -127,7 +127,7 @@ export class Feathers<ServiceTypes, AppSettings> extends EventEmitter implements
if (Array.isArray(hookMap)) {
this.appHooks[HOOKS].push(...hookMap as any);
} else {
const methodHookMap = hookMap as HookMap<Application<ServiceTypes, AppSettings>, any>;
const methodHookMap = hookMap as HookMap<Application<Services, Settings>, any>;

Object.keys(methodHookMap).forEach(key => {
const methodHooks = this.appHooks[key] || [];
Expand Down

0 comments on commit b8eb804

Please sign in to comment.