Skip to content

Commit d2d29ad

Browse files
Danilo AlonsoDanilo Alonso
Danilo Alonso
authored and
Danilo Alonso
committed
fix: 🐛 accurate auth typings
Previously would resolve to `any`, they now pickup the generic and the ref overrides.
1 parent 22377ee commit d2d29ad

File tree

3 files changed

+82
-22
lines changed

3 files changed

+82
-22
lines changed

lib/types/request.d.ts

+21-9
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,24 @@ export interface AppCredentials {
2727
* User-extensible type for request.auth credentials.
2828
*/
2929
export interface AuthCredentials<
30-
AuthUser extends object = UserCredentials,
31-
AuthApp extends object = AppCredentials,
30+
AuthUser = UserCredentials,
31+
AuthApp = AppCredentials
3232
> {
3333
/**
3434
* The application scopes to be granted.
3535
* [See docs](https://github.com/hapijs/hapi/blob/master/API.md#-routeoptionsauthaccessscope)
3636
*/
3737
scope?: string[] | undefined;
38+
3839
/**
3940
* If set, will only work with routes that set `access.entity` to `user`.
4041
*/
41-
user?: MergeType<UserCredentials, AuthUser> | undefined;
42+
user?: AuthUser
4243

4344
/**
4445
* If set, will only work with routes that set `access.entity` to `app`.
4546
*/
46-
app?: MergeType<AppCredentials, AuthApp> | undefined;
47+
app?: AuthApp;
4748
}
4849

4950
export interface AuthArtifacts {
@@ -65,15 +66,20 @@ export type AuthMode = 'required' | 'optional' | 'try';
6566
* [See docs](https://github.com/hapijs/hapi/blob/master/API.md#-requestauth)
6667
*/
6768
export interface RequestAuth<
68-
AuthUser extends object = UserCredentials,
69-
AuthApp extends object = AppCredentials,
70-
CredentialsExtra extends object = Record<string, unknown>,
69+
AuthUser = UserCredentials,
70+
AuthApp = AppCredentials,
71+
CredentialsExtra = Record<string, unknown>,
7172
ArtifactsExtra = Record<string, unknown>
7273
> {
7374
/** an artifact object received from the authentication strategy and used in authentication-related actions. */
7475
artifacts: ArtifactsExtra;
7576
/** the credential object received during the authentication process. The presence of an object does not mean successful authentication. */
76-
credentials: MergeType<CredentialsExtra, AuthCredentials<AuthUser, AuthApp>>;
77+
credentials: (
78+
79+
AuthCredentials<AuthUser, AuthApp> &
80+
CredentialsExtra
81+
);
82+
7783
/** the authentication error is failed and mode set to 'try'. */
7884
error: Error;
7985
/** true if the request has been successfully authenticated, otherwise false. */
@@ -89,6 +95,7 @@ export interface RequestAuth<
8995
strategy: string;
9096
}
9197

98+
9299
/**
93100
* 'peek' - emitted for each chunk of payload data read from the client connection. The event method signature is function(chunk, encoding).
94101
* 'finish' - emitted when the request payload finished reading. The event method signature is function ().
@@ -296,7 +303,12 @@ export type ReqRef = Partial<Record<keyof ReqRefDefaults, unknown>>;
296303
/**
297304
* Utilities for merging request refs and other things
298305
*/
299-
export type MergeType<T extends object, U extends object> = Omit<T, keyof U> & U;
306+
export type MergeType<T, U> = {
307+
[K in keyof T]: K extends keyof U
308+
? U[K]
309+
: T[K];
310+
} & U;
311+
300312
export type MergeRefs<T extends ReqRef> = MergeType<ReqRefDefaults, T>;
301313

302314
/**

lib/types/response.d.ts

+12-12
Original file line numberDiff line numberDiff line change
@@ -386,19 +386,19 @@ export type ResponseValue = string | object;
386386

387387
export interface AuthenticationData<
388388

389-
AuthUser extends object = UserCredentials,
390-
AuthApp extends object = AppCredentials,
391-
CredentialsExtra extends object = Record<string, unknown>,
389+
AuthUser = UserCredentials,
390+
AuthApp = AppCredentials,
391+
CredentialsExtra = Record<string, unknown>,
392392
ArtifactsExtra = AuthArtifacts
393393
> {
394394
credentials: MergeType<CredentialsExtra, AuthCredentials<AuthUser, AuthApp>>;
395395
artifacts?: ArtifactsExtra | undefined;
396396
}
397397

398398
export interface Auth<
399-
AuthUser extends object = UserCredentials,
400-
AuthApp extends object = AppCredentials,
401-
CredentialsExtra extends object = Record<string, unknown>,
399+
AuthUser = UserCredentials,
400+
AuthApp = AppCredentials,
401+
CredentialsExtra = Record<string, unknown>,
402402
ArtifactsExtra = AuthArtifacts
403403
> {
404404
readonly isAuth: true;
@@ -459,9 +459,9 @@ export interface ResponseToolkit<Refs extends ReqRef = ReqRefDefaults> {
459459
* @return Return value: an internal authentication object.
460460
*/
461461
authenticated <
462-
AuthUser extends object = MergeRefs<Refs>['AuthUser'],
463-
AuthApp extends object = MergeRefs<Refs>['AuthApp'],
464-
CredentialsExtra extends object = MergeRefs<Refs>['AuthCredentialsExtra'],
462+
AuthUser = MergeRefs<Refs>['AuthUser'],
463+
AuthApp = MergeRefs<Refs>['AuthApp'],
464+
CredentialsExtra = MergeRefs<Refs>['AuthCredentialsExtra'],
465465
ArtifactsExtra = MergeRefs<Refs>['AuthArtifactsExtra']
466466
>(
467467
data: (
@@ -537,9 +537,9 @@ export interface ResponseToolkit<Refs extends ReqRef = ReqRefDefaults> {
537537
* [See docs](https://github.com/hapijs/hapi/blob/master/API.md#-hunauthenticatederror-data)
538538
*/
539539
unauthenticated <
540-
AuthUser extends object = MergeRefs<Refs>['AuthUser'],
541-
AuthApp extends object = MergeRefs<Refs>['AuthApp'],
542-
CredentialsExtra extends object = MergeRefs<Refs>['AuthCredentialsExtra'],
540+
AuthUser = MergeRefs<Refs>['AuthUser'],
541+
AuthApp = MergeRefs<Refs>['AuthApp'],
542+
CredentialsExtra = MergeRefs<Refs>['AuthCredentialsExtra'],
543543
ArtifactsExtra = MergeRefs<Refs>['AuthArtifactsExtra']
544544
>(
545545
error: Error,

test/types/index.ts

+49-1
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,18 @@ import {
1010
Server,
1111
ServerRoute,
1212
server as createServer,
13-
ServerRegisterPluginObject
13+
UserCredentials
1414
} from '../..';
1515

1616
const { expect: check } = lab;
1717

18+
declare module '../..' {
19+
interface UserCredentials {
20+
someId: string;
21+
someName: string;
22+
}
23+
}
24+
1825
interface ServerAppSpace {
1926
multi?: number;
2027
}
@@ -27,13 +34,42 @@ check.type<MyServer>(server);
2734

2835
server.app.multi = 10;
2936

37+
const genericRoute: ServerRoute = {
38+
method: 'GET',
39+
path: '/',
40+
handler: (request, h) => {
41+
42+
check.type<UserCredentials>(request.auth.credentials!.user!);
43+
44+
return 'hello!';
45+
}
46+
}
47+
48+
server.route(genericRoute);
49+
3050
interface RequestDecorations {
3151
Server: MyServer;
3252
RequestApp: {
3353
word: string;
3454
},
3555
RouteApp: {
3656
prefix: string[];
57+
},
58+
AuthUser: {
59+
id: string,
60+
name: string
61+
email: string
62+
},
63+
AuthCredentialsExtra: {
64+
test: number
65+
}
66+
AuthApp: {
67+
key: string
68+
name: string
69+
},
70+
AuthArtifactsExtra: {
71+
some: string
72+
thing: number
3773
}
3874
}
3975

@@ -61,6 +97,18 @@ const route: ServerRoute<RequestDecorations> = {
6197
check.type<number>(request.server.app.multi!);
6298
check.type<string[]>(request.route.settings.app!.prefix);
6399

100+
check.type<number>(request.auth.credentials!.test);
101+
102+
check.type<string>(request.auth.credentials!.user!.email);
103+
check.type<string>(request.auth.credentials!.user!.id);
104+
check.type<string>(request.auth.credentials!.user!.name);
105+
106+
check.type<string>(request.auth.credentials!.app!.name);
107+
check.type<string>(request.auth.credentials!.app!.key);
108+
109+
check.type<string>(request.auth.artifacts.some);
110+
check.type<number>(request.auth.artifacts.thing);
111+
64112
return 'hello!'
65113
}
66114
};

0 commit comments

Comments
 (0)