Skip to content

Commit df28b76

Browse files
authored
feat(typescript): Improve params and query typeability (#2600)
1 parent 97313e1 commit df28b76

File tree

24 files changed

+234
-123
lines changed

24 files changed

+234
-123
lines changed

packages/adapter-commons/src/service.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,16 @@ const alwaysMulti: { [key: string]: boolean } = {
1616
update: false
1717
};
1818

19+
export interface PaginationOptions {
20+
default?: number;
21+
max?: number;
22+
}
23+
1924
export interface ServiceOptions {
2025
events?: string[];
2126
multi?: boolean|string[];
2227
id?: string;
23-
paginate?: {
24-
default?: number;
25-
max?: number;
26-
}
28+
paginate?: PaginationOptions
2729
/**
2830
* @deprecated renamed to `allow`.
2931
*/
@@ -38,6 +40,7 @@ export interface AdapterOptions<M = any> extends Pick<ServiceOptions, 'multi'|'a
3840

3941
export interface AdapterParams<M = any> extends Params {
4042
adapter?: Partial<AdapterOptions<M>>;
43+
paginate?: false|PaginationOptions;
4144
}
4245

4346
/**

packages/authentication-oauth/src/strategy.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// @ts-ignore
33
import querystring from 'querystring';
44
import {
5-
AuthenticationRequest, AuthenticationBaseStrategy, AuthenticationResult
5+
AuthenticationRequest, AuthenticationBaseStrategy, AuthenticationResult, AuthenticationParams
66
} from '@feathersjs/authentication';
77
import { Params } from '@feathersjs/feathers';
88
import { NotAuthenticated } from '@feathersjs/errors';
@@ -84,7 +84,7 @@ export class OAuthStrategy extends AuthenticationBaseStrategy {
8484
return redirect;
8585
}
8686

87-
async getRedirect (data: AuthenticationResult|Error, params?: Params): Promise<string | null> {
87+
async getRedirect (data: AuthenticationResult|Error, params?: AuthenticationParams): Promise<string | null> {
8888
const queryRedirect = (params && params.redirect) || '';
8989
const redirect = await this.getAllowedOrigin(params);
9090

@@ -156,7 +156,7 @@ export class OAuthStrategy extends AuthenticationBaseStrategy {
156156
});
157157
}
158158

159-
async authenticate (authentication: AuthenticationRequest, originalParams: Params) {
159+
async authenticate (authentication: AuthenticationRequest, originalParams: AuthenticationParams) {
160160
const entity: string = this.configuration.entity;
161161
const { provider, ...params } = originalParams;
162162
const profile = await this.getProfile(authentication, params);

packages/authentication-oauth/test/fixture.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import { feathers, Params } from '@feathersjs/feathers';
1+
import { feathers } from '@feathersjs/feathers';
22
import express, { rest, errorHandler } from '@feathersjs/express';
33
import { memory } from '@feathersjs/memory';
4-
import { AuthenticationService, JWTStrategy, AuthenticationRequest } from '@feathersjs/authentication';
4+
import { AuthenticationService, JWTStrategy, AuthenticationRequest, AuthenticationParams } from '@feathersjs/authentication';
55
import { express as oauth, OAuthStrategy } from '../src';
66

77
export class TestOAuthStrategy extends OAuthStrategy {
8-
async authenticate (data: AuthenticationRequest, params: Params) {
8+
async authenticate (data: AuthenticationRequest, params: AuthenticationParams) {
99
const { fromMiddleware } = params;
1010
const authResult = await super.authenticate(data, params);
1111

packages/authentication/src/core.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ export interface AuthenticationRequest {
1818
[key: string]: any;
1919
}
2020

21+
export interface AuthenticationParams extends Params {
22+
payload?: { [key: string]: any };
23+
jwtOptions?: SignOptions;
24+
authStrategies?: string[];
25+
secret?: string;
26+
[key: string]: any;
27+
}
28+
2129
export type ConnectionEvent = 'login' | 'logout' | 'disconnect';
2230

2331
export interface AuthenticationStrategy {
@@ -57,7 +65,7 @@ export interface AuthenticationStrategy {
5765
* @param authentication The authentication request
5866
* @param params The service call parameters
5967
*/
60-
authenticate? (authentication: AuthenticationRequest, params: Params): Promise<AuthenticationResult>;
68+
authenticate? (authentication: AuthenticationRequest, params: AuthenticationParams): Promise<AuthenticationResult>;
6169
/**
6270
* Update a real-time connection according to this strategy.
6371
*
@@ -232,7 +240,7 @@ export class AuthenticationBase {
232240
* @param params Service call parameters
233241
* @param allowed A list of allowed strategy names
234242
*/
235-
async authenticate (authentication: AuthenticationRequest, params: Params, ...allowed: string[]) {
243+
async authenticate (authentication: AuthenticationRequest, params: AuthenticationParams, ...allowed: string[]) {
236244
const { strategy } = authentication || {};
237245
const [ authStrategy ] = this.getStrategies(strategy);
238246
const strategyAllowed = allowed.includes(strategy);

packages/authentication/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export {
55
AuthenticationRequest,
66
AuthenticationResult,
77
AuthenticationStrategy,
8+
AuthenticationParams,
89
ConnectionEvent
910
} from './core';
1011
export { AuthenticationBaseStrategy } from './strategy';

packages/authentication/src/jwt.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { createDebug } from '@feathersjs/commons';
88
import lt from 'long-timeout';
99

1010
import { AuthenticationBaseStrategy } from './strategy';
11-
import { AuthenticationRequest, AuthenticationResult, ConnectionEvent } from './core';
11+
import { AuthenticationParams, AuthenticationRequest, AuthenticationResult, ConnectionEvent } from './core';
1212

1313
const debug = createDebug('@feathersjs/authentication/jwt');
1414
const SPLIT_HEADER = /(\S+)\s+(\S+)/;
@@ -116,7 +116,7 @@ export class JWTStrategy extends AuthenticationBaseStrategy {
116116
return authResult.authentication.payload.sub;
117117
}
118118

119-
async authenticate (authentication: AuthenticationRequest, params: Params) {
119+
async authenticate (authentication: AuthenticationRequest, params: AuthenticationParams) {
120120
const { accessToken } = authentication;
121121
const { entity } = this.configuration;
122122

packages/authentication/src/service.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import merge from 'lodash/merge';
22
import { NotAuthenticated } from '@feathersjs/errors';
3-
import { AuthenticationBase, AuthenticationResult, AuthenticationRequest } from './core';
3+
import { AuthenticationBase, AuthenticationResult, AuthenticationRequest, AuthenticationParams } from './core';
44
import { connection, event } from './hooks';
55
import '@feathersjs/transport-commons';
66
import { createDebug } from '@feathersjs/commons';
7-
import { Params, ServiceMethods, ServiceAddons } from '@feathersjs/feathers';
7+
import { ServiceMethods, ServiceAddons } from '@feathersjs/feathers';
88
import jsonwebtoken from 'jsonwebtoken';
99

1010
const debug = createDebug('@feathersjs/authentication/service');
@@ -29,7 +29,7 @@ declare module '@feathersjs/feathers/lib/declarations' {
2929
// eslint-disable-next-line
3030
export interface AuthenticationService extends ServiceAddons<AuthenticationResult, AuthenticationResult> {}
3131

32-
export class AuthenticationService extends AuthenticationBase implements Partial<ServiceMethods<AuthenticationResult>> {
32+
export class AuthenticationService extends AuthenticationBase implements Partial<ServiceMethods<AuthenticationResult, AuthenticationRequest, AuthenticationParams>> {
3333
constructor (app: any, configKey = 'authentication', options = {}) {
3434
super(app, configKey, options);
3535

@@ -51,7 +51,7 @@ export class AuthenticationService extends AuthenticationBase implements Partial
5151
* @param _authResult The current authentication result
5252
* @param params The service call parameters
5353
*/
54-
async getPayload (_authResult: AuthenticationResult, params: Params) {
54+
async getPayload (_authResult: AuthenticationResult, params: AuthenticationParams) {
5555
// Uses `params.payload` or returns an empty payload
5656
const { payload = {} } = params;
5757

@@ -65,7 +65,7 @@ export class AuthenticationService extends AuthenticationBase implements Partial
6565
* @param authResult The authentication result
6666
* @param params Service call parameters
6767
*/
68-
async getTokenOptions (authResult: AuthenticationResult, params: Params) {
68+
async getTokenOptions (authResult: AuthenticationResult, params: AuthenticationParams) {
6969
const { service, entity, entityId } = this.configuration;
7070
const jwtOptions = merge({}, params.jwtOptions, params.jwt);
7171
const value = service && entity && authResult[entity];
@@ -92,7 +92,7 @@ export class AuthenticationService extends AuthenticationBase implements Partial
9292
* @param data The authentication request (should include `strategy` key)
9393
* @param params Service call parameters
9494
*/
95-
async create (data: AuthenticationRequest, params?: Params) {
95+
async create (data: AuthenticationRequest, params?: AuthenticationParams) {
9696
const authStrategies = params.authStrategies || this.configuration.authStrategies;
9797

9898
if (!authStrategies.length) {
@@ -131,7 +131,7 @@ export class AuthenticationService extends AuthenticationBase implements Partial
131131
* @param id The JWT to remove or null
132132
* @param params Service call parameters
133133
*/
134-
async remove (id: string | null, params?: Params) {
134+
async remove (id: string | null, params?: AuthenticationParams) {
135135
const { authentication } = params;
136136
const { authStrategies } = this.configuration;
137137

packages/authentication/test/hooks/event.test.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import assert from 'assert';
2-
import { feathers, Params, HookContext } from '@feathersjs/feathers';
2+
import { feathers, HookContext } from '@feathersjs/feathers';
33

44
import hook from '../../src/hooks/event';
5-
import { AuthenticationRequest, AuthenticationResult } from '../../src/core';
5+
import { AuthenticationParams, AuthenticationRequest, AuthenticationResult } from '../../src/core';
66

77
describe('authentication/hooks/events', () => {
8-
const app = feathers().use('/authentication', {
8+
const app = feathers().use('authentication', {
99
async create (data: AuthenticationRequest) {
1010
return data;
1111
},
@@ -27,7 +27,7 @@ describe('authentication/hooks/events', () => {
2727
message: 'test'
2828
};
2929

30-
app.once('login', (result: AuthenticationResult, params: Params, context: HookContext) => {
30+
app.once('login', (result: AuthenticationResult, params: AuthenticationParams, context: HookContext) => {
3131
try {
3232
assert.deepStrictEqual(result, data);
3333
assert.ok(params.testParam);
@@ -41,11 +41,11 @@ describe('authentication/hooks/events', () => {
4141
service.create(data, {
4242
testParam: true,
4343
provider: 'test'
44-
});
44+
}as any);
4545
});
4646

4747
it('logout', done => {
48-
app.once('logout', (result: AuthenticationResult, params: Params, context: HookContext) => {
48+
app.once('logout', (result: AuthenticationResult, params: AuthenticationParams, context: HookContext) => {
4949
try {
5050
assert.deepStrictEqual(result, {
5151
id: 'test'
@@ -61,7 +61,7 @@ describe('authentication/hooks/events', () => {
6161
service.remove('test', {
6262
testParam: true,
6363
provider: 'test'
64-
});
64+
} as any);
6565
});
6666

6767
it('does nothing when provider is not set', done => {

packages/express/src/declarations.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ interface ExpressUseHandler<T, Services> {
99
<L extends keyof Services & string> (
1010
path: L,
1111
...middlewareOrService: (
12-
Express|express.RequestHandler|
12+
Express|express.RequestHandler|express.RequestHandler[]|
1313
(keyof any extends keyof Services ? ServiceInterface : Services[L])
1414
)[]
1515
): T;
@@ -44,7 +44,7 @@ declare module '@feathersjs/feathers/lib/declarations' {
4444

4545
declare module 'express-serve-static-core' {
4646
interface Request {
47-
feathers?: Partial<FeathersParams>;
47+
feathers?: Partial<FeathersParams> & { [key: string]: any };
4848
lookup?: RouteLookup;
4949
}
5050

packages/express/test/rest.test.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -380,10 +380,9 @@ describe('@feathersjs/express/rest provider', () => {
380380
it('allows middleware arrays before and after a service', async () => {
381381
const app = expressify(feathers());
382382

383-
app
384-
.use(express.json())
385-
.configure(rest())
386-
.use('/todo', [function (req: Request, _res: Response, next: NextFunction) {
383+
app.use(express.json());
384+
app.configure(rest());
385+
app.use('/todo', [function (req: Request, _res: Response, next: NextFunction) {
387386
req.body.before = ['before first'];
388387
next();
389388
}, function (req: Request, _res: Response, next: NextFunction) {

0 commit comments

Comments
 (0)