-
-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathindex.test-d.ts
More file actions
143 lines (132 loc) · 3.66 KB
/
index.test-d.ts
File metadata and controls
143 lines (132 loc) · 3.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import {expectType} from 'tsd';
import express from 'express'
import {
OAuth2Server,
OAuthError,
Request,
Response,
AccessDeniedError,
InsufficientScopeError,
InvalidArgumentError,
InvalidClientError,
InvalidGrantError,
InvalidRequestError,
InvalidScopeError,
InvalidTokenError,
ServerError,
UnauthorizedClientError,
UnauthorizedRequestError,
UnsupportedGrantTypeError,
UnsupportedResponseTypeError
} from '.';
import {Request as UndiciRequest, Response as UndiciResponse} from 'undici';
// ----------------------------------------------------------------------------
// REQUEST
// ----------------------------------------------------------------------------
function testRequest(req: Request): void {
expectType<string>(req.method);
expectType<Record<string, string>>(req.headers);
expectType<Record<string, string>>(req.query);
expectType<any>(req.body);
}
const args = [
undefined,
{},
{method: 'get'},
{method: 'get', headers: {x: 'y'}},
{method: 'get', query: {moo: 'foo'}},
{method: 'get', headers: {x: 'y'}, query: {moo: 'foo'}},
{method: 'get', headers: {x: 'y'}, query: {moo: 'foo', bar: 'baz'}},
// check for express request compatibility
new express.Request({
method: 'get',
query: {moo: 'foo'},
headers: {x: 'y'}
}),
// check for compatibility with fetch api Request
// we use undici Request as a stand-in for fetch api Request
// because we still support older versions of Node.js
new UndiciRequest(
'https://example.com?moo=foo%2Cbar',
{
method: 'get', headers: {x: 'y'}
})
];
for (const arg of args) {
testRequest(new Request(arg));
}
// ----------------------------------------------------------------------------
// RESPONSE
// ----------------------------------------------------------------------------
function testResponse(res: Response): void {
expectType<number>(res.status);
expectType<Record<string, string>>(res.headers);
expectType<any>(res.body);
}
const resArgs = [
undefined,
{},
{status: 200},
{status: 200, headers: {x: 'y'}},
{status: 200, body: 'foo'},
{status: 200, headers: {x: 'y'}, body: 'foo'},
{status: 200, headers: {x: 'y'}, body: 'foo', statusText: 'OK'},
// check for express response compatibility
new express.Response({
status: 200,
headers: {x: 'y'},
body: 'foo'
}),
// check for compatibility with fetch api Response
// we use undici Response as a stand-in for fetch api Response
// because we still support older versions of Node.js
new UndiciResponse(
'foo',
{
status: 200,
headers: {x: 'y'},
statusText: 'OK'
})
];
for (const arg of resArgs) {
testResponse(new Response(arg));
}
// ----------------------------------------------------------------------------
// ERRORS
// ----------------------------------------------------------------------------
function testError(err: OAuthError): void {
expectType<string>(err.name);
expectType<string>(err.message);
expectType<number>(err.code);
}
const errorTypes = [
AccessDeniedError,
InsufficientScopeError,
InvalidArgumentError,
InvalidClientError,
InvalidGrantError,
InvalidRequestError,
InvalidScopeError,
InvalidTokenError,
ServerError,
UnauthorizedClientError,
UnauthorizedRequestError,
UnsupportedGrantTypeError,
UnsupportedResponseTypeError
];
const errorArgs = [
undefined,
{},
{message: 'foo'},
{message: 'foo', code: 400},
{message: 'foo', code: 400, data: {bar: 'baz'}},
// check for express error compatibility
new express.Error('foo'),
// native error compatibility
new Error('foo')
];
for (const arg of errorArgs) {
for (const ErrorType of errorTypes) {
testError(new ErrorType(arg));
}
}