1
- import { assertType , test } from 'vitest' ;
1
+ import { assertType , expectTypeOf , test } from 'vitest' ;
2
2
3
3
import type { AuthObject } from '../authObjects' ;
4
4
import type { GetAuthFn , MachineAuthObject , SessionAuthObject } from '../types' ;
@@ -12,57 +12,57 @@ test('infers the correct AuthObject type for each accepted token type', () => {
12
12
const request = new Request ( 'https://example.com' ) ;
13
13
14
14
// Session token by default
15
- assertType < SessionAuthObject > ( getAuth ( request ) ) ;
15
+ expectTypeOf ( getAuth ( request ) ) . toMatchTypeOf < SessionAuthObject > ( ) ;
16
16
17
17
// Individual token types
18
- assertType < SessionAuthObject > ( getAuth ( request , { acceptsToken : 'session_token' } ) ) ;
19
- assertType < MachineAuthObject < 'api_key' > > ( getAuth ( request , { acceptsToken : 'api_key' } ) ) ;
20
- assertType < MachineAuthObject < 'machine_token' > > ( getAuth ( request , { acceptsToken : 'machine_token' } ) ) ;
21
- assertType < MachineAuthObject < 'oauth_token' > > ( getAuth ( request , { acceptsToken : 'oauth_token' } ) ) ;
18
+ expectTypeOf ( getAuth ( request , { acceptsToken : 'session_token' } ) ) . toMatchTypeOf < SessionAuthObject > ( ) ;
19
+ expectTypeOf ( getAuth ( request , { acceptsToken : 'api_key' } ) ) . toMatchTypeOf < MachineAuthObject < 'api_key' > > ( ) ;
20
+ expectTypeOf ( getAuth ( request , { acceptsToken : 'machine_token' } ) ) . toMatchTypeOf < MachineAuthObject < 'machine_token' > > ( ) ;
21
+ expectTypeOf ( getAuth ( request , { acceptsToken : 'oauth_token' } ) ) . toMatchTypeOf < MachineAuthObject < 'oauth_token' > > ( ) ;
22
22
23
23
// Array of token types
24
- assertType < SessionAuthObject | MachineAuthObject < ' machine_token'> > (
25
- getAuth ( request , { acceptsToken : [ 'session_token' , ' machine_token'] } ) ,
26
- ) ;
27
- assertType < MachineAuthObject < 'machine_token' | 'oauth_token' > > (
28
- getAuth ( request , { acceptsToken : [ 'machine_token' , 'oauth_token' ] } ) ,
29
- ) ;
24
+ expectTypeOf ( getAuth ( request , { acceptsToken : [ 'session_token' , ' machine_token'] } ) ) . toMatchTypeOf <
25
+ SessionAuthObject | MachineAuthObject < ' machine_token'>
26
+ > ( ) ;
27
+ expectTypeOf ( getAuth ( request , { acceptsToken : [ 'machine_token' , 'oauth_token' ] } ) ) . toMatchTypeOf <
28
+ MachineAuthObject < 'machine_token' | 'oauth_token' >
29
+ > ( ) ;
30
30
31
31
// Any token type
32
- assertType < AuthObject > ( getAuth ( request , { acceptsToken : 'any' } ) ) ;
32
+ expectTypeOf ( getAuth ( request , { acceptsToken : 'any' } ) ) . toMatchTypeOf < AuthObject > ( ) ;
33
33
} ) ;
34
34
35
35
test ( 'verifies correct properties exist for each token type' , ( ) => {
36
36
const request = new Request ( 'https://example.com' ) ;
37
37
38
38
// Session token should have userId
39
39
const sessionAuth = getAuth ( request , { acceptsToken : 'session_token' } ) ;
40
- assertType < string | null > ( sessionAuth . userId ) ;
40
+ expectTypeOf ( sessionAuth . userId ) . toMatchTypeOf < string | null > ( ) ;
41
41
42
42
// All machine tokens should have id and subject
43
43
const apiKeyAuth = getAuth ( request , { acceptsToken : 'api_key' } ) ;
44
44
const machineTokenAuth = getAuth ( request , { acceptsToken : 'machine_token' } ) ;
45
45
const oauthTokenAuth = getAuth ( request , { acceptsToken : 'oauth_token' } ) ;
46
46
47
- assertType < string | null > ( apiKeyAuth . id ) ;
48
- assertType < string | null > ( machineTokenAuth . id ) ;
49
- assertType < string | null > ( oauthTokenAuth . id ) ;
50
- assertType < string | null > ( apiKeyAuth . subject ) ;
51
- assertType < string | null > ( machineTokenAuth . subject ) ;
52
- assertType < string | null > ( oauthTokenAuth . subject ) ;
47
+ expectTypeOf ( apiKeyAuth . id ) . toMatchTypeOf < string | null > ( ) ;
48
+ expectTypeOf ( machineTokenAuth . id ) . toMatchTypeOf < string | null > ( ) ;
49
+ expectTypeOf ( oauthTokenAuth . id ) . toMatchTypeOf < string | null > ( ) ;
50
+ expectTypeOf ( apiKeyAuth . subject ) . toMatchTypeOf < string | null > ( ) ;
51
+ expectTypeOf ( machineTokenAuth . subject ) . toMatchTypeOf < string | null > ( ) ;
52
+ expectTypeOf ( oauthTokenAuth . subject ) . toMatchTypeOf < string | null > ( ) ;
53
53
54
54
// Only api_key and machine_token should have name and claims
55
- assertType < string | null > ( apiKeyAuth . name ) ;
56
- assertType < Record < string , any > | null > ( apiKeyAuth . claims ) ;
55
+ expectTypeOf ( apiKeyAuth . name ) . toMatchTypeOf < string | null > ( ) ;
56
+ expectTypeOf ( apiKeyAuth . claims ) . toMatchTypeOf < Record < string , any > | null > ( ) ;
57
57
58
- assertType < string | null > ( machineTokenAuth . name ) ;
59
- assertType < Record < string , any > | null > ( machineTokenAuth . claims ) ;
58
+ expectTypeOf ( machineTokenAuth . name ) . toMatchTypeOf < string | null > ( ) ;
59
+ expectTypeOf ( machineTokenAuth . claims ) . toMatchTypeOf < Record < string , any > | null > ( ) ;
60
60
61
61
// oauth_token should NOT have name and claims
62
62
// @ts -expect-error oauth_token does not have name property
63
- void oauthTokenAuth . name ;
63
+ assertType < string | null > ( oauthTokenAuth . name ) ;
64
64
// @ts -expect-error oauth_token does not have claims property
65
- void oauthTokenAuth . claims ;
65
+ assertType < Record < string , any > | null > ( oauthTokenAuth . claims ) ;
66
66
} ) ;
67
67
68
68
test ( 'verifies discriminated union works correctly with acceptsToken: any' , ( ) => {
@@ -72,36 +72,36 @@ test('verifies discriminated union works correctly with acceptsToken: any', () =
72
72
73
73
if ( auth . tokenType === 'session_token' ) {
74
74
// Should be SessionAuthObject - has userId
75
- assertType < string | null > ( auth . userId ) ;
75
+ expectTypeOf ( auth . userId ) . toMatchTypeOf < string | null > ( ) ;
76
76
// Should NOT have machine token properties
77
77
// @ts -expect-error session_token does not have id property
78
- void auth . id ;
78
+ assertType < string | null > ( auth . id ) ;
79
79
} else if ( auth . tokenType === 'api_key' ) {
80
80
// Should be AuthenticatedMachineObject<'api_key'> - has id, name, claims
81
- assertType < string | null > ( auth . id ) ;
82
- assertType < string | null > ( auth . name ) ;
83
- assertType < Record < string , any > | null > ( auth . claims ) ;
81
+ expectTypeOf ( auth . id ) . toMatchTypeOf < string | null > ( ) ;
82
+ expectTypeOf ( auth . name ) . toMatchTypeOf < string | null > ( ) ;
83
+ expectTypeOf ( auth . claims ) . toMatchTypeOf < Record < string , any > | null > ( ) ;
84
84
// Should NOT have session token properties
85
85
// @ts -expect-error api_key does not have userId property
86
- void auth . userId ;
86
+ assertType < string | null > ( auth . userId ) ;
87
87
} else if ( auth . tokenType === 'machine_token' ) {
88
88
// Should be AuthenticatedMachineObject<'machine_token'> - has id, name, claims
89
- assertType < string | null > ( auth . id ) ;
90
- assertType < string | null > ( auth . name ) ;
91
- assertType < Record < string , any > | null > ( auth . claims ) ;
89
+ expectTypeOf ( auth . id ) . toMatchTypeOf < string | null > ( ) ;
90
+ expectTypeOf ( auth . name ) . toMatchTypeOf < string | null > ( ) ;
91
+ expectTypeOf ( auth . claims ) . toMatchTypeOf < Record < string , any > | null > ( ) ;
92
92
// Should NOT have session token properties
93
93
// @ts -expect-error machine_token does not have userId property
94
- void auth . userId ;
94
+ assertType < string | null > ( auth . userId ) ;
95
95
} else if ( auth . tokenType === 'oauth_token' ) {
96
96
// Should be AuthenticatedMachineObject<'oauth_token'> - has id but NOT name/claims
97
- assertType < string | null > ( auth . id ) ;
97
+ expectTypeOf ( auth . id ) . toMatchTypeOf < string | null > ( ) ;
98
98
// Should NOT have name or claims
99
99
// @ts -expect-error oauth_token does not have name property
100
- void auth . name ;
100
+ assertType < string | null > ( auth . name ) ;
101
101
// @ts -expect-error oauth_token does not have claims property
102
- void auth . claims ;
102
+ assertType < Record < string , any > | null > ( auth . claims ) ;
103
103
// Should NOT have session token properties
104
104
// @ts -expect-error oauth_token does not have userId property
105
- void auth . userId ;
105
+ assertType < string | null > ( auth . userId ) ;
106
106
}
107
107
} ) ;
0 commit comments