|
| 1 | +import { expectType, expectError } from 'tsd'; |
| 2 | +import { |
| 3 | + API, |
| 4 | + Request, |
| 5 | + Response, |
| 6 | + CookieOptions, |
| 7 | + CorsOptions, |
| 8 | + FileOptions, |
| 9 | + LoggerOptions, |
| 10 | + Options, |
| 11 | + Middleware, |
| 12 | + ErrorHandlingMiddleware, |
| 13 | + HandlerFunction, |
| 14 | + METHODS, |
| 15 | + RouteError, |
| 16 | + MethodError, |
| 17 | + ConfigurationError, |
| 18 | + ResponseError, |
| 19 | + FileError, |
| 20 | +} from './index'; |
| 21 | +import { |
| 22 | + APIGatewayProxyEvent, |
| 23 | + APIGatewayProxyEventV2, |
| 24 | + Context, |
| 25 | + ALBEvent, |
| 26 | +} from 'aws-lambda'; |
| 27 | + |
| 28 | +const options: Options = { |
| 29 | + base: '/api', |
| 30 | + version: 'v1', |
| 31 | + logger: { |
| 32 | + level: 'info', |
| 33 | + access: true, |
| 34 | + timestamp: true, |
| 35 | + }, |
| 36 | + compression: true, |
| 37 | +}; |
| 38 | +expectType<Options>(options); |
| 39 | + |
| 40 | +const req = {} as Request; |
| 41 | +expectType<string>(req.method); |
| 42 | +expectType<string>(req.path); |
| 43 | +expectType<{ [key: string]: string | undefined }>(req.params); |
| 44 | +expectType<{ [key: string]: string | undefined }>(req.query); |
| 45 | +expectType<{ [key: string]: string | undefined }>(req.headers); |
| 46 | +expectType<any>(req.body); |
| 47 | +expectType<{ [key: string]: string }>(req.cookies); |
| 48 | + |
| 49 | +const apiGwV1Event: APIGatewayProxyEvent = { |
| 50 | + body: '{"test":"body"}', |
| 51 | + headers: { 'content-type': 'application/json' }, |
| 52 | + multiValueHeaders: { 'content-type': ['application/json'] }, |
| 53 | + httpMethod: 'POST', |
| 54 | + isBase64Encoded: false, |
| 55 | + path: '/test', |
| 56 | + pathParameters: { id: '123' }, |
| 57 | + queryStringParameters: { query: 'test' }, |
| 58 | + multiValueQueryStringParameters: { query: ['test'] }, |
| 59 | + stageVariables: { stage: 'dev' }, |
| 60 | + requestContext: { |
| 61 | + accountId: '', |
| 62 | + apiId: '', |
| 63 | + authorizer: {}, |
| 64 | + protocol: '', |
| 65 | + httpMethod: 'POST', |
| 66 | + identity: { |
| 67 | + accessKey: null, |
| 68 | + accountId: null, |
| 69 | + apiKey: null, |
| 70 | + apiKeyId: null, |
| 71 | + caller: null, |
| 72 | + cognitoAuthenticationProvider: null, |
| 73 | + cognitoAuthenticationType: null, |
| 74 | + cognitoIdentityId: null, |
| 75 | + cognitoIdentityPoolId: null, |
| 76 | + principalOrgId: null, |
| 77 | + sourceIp: '', |
| 78 | + user: null, |
| 79 | + userAgent: null, |
| 80 | + userArn: null, |
| 81 | + }, |
| 82 | + path: '/test', |
| 83 | + stage: 'dev', |
| 84 | + requestId: '', |
| 85 | + requestTimeEpoch: 0, |
| 86 | + resourceId: '', |
| 87 | + resourcePath: '', |
| 88 | + }, |
| 89 | + resource: '', |
| 90 | +}; |
| 91 | + |
| 92 | +const apiGwV2Event: APIGatewayProxyEventV2 = { |
| 93 | + version: '2.0', |
| 94 | + routeKey: 'POST /test', |
| 95 | + rawPath: '/test', |
| 96 | + rawQueryString: 'query=test', |
| 97 | + headers: { 'content-type': 'application/json' }, |
| 98 | + requestContext: { |
| 99 | + accountId: '', |
| 100 | + apiId: '', |
| 101 | + domainName: '', |
| 102 | + domainPrefix: '', |
| 103 | + http: { |
| 104 | + method: 'POST', |
| 105 | + path: '/test', |
| 106 | + protocol: 'HTTP/1.1', |
| 107 | + sourceIp: '', |
| 108 | + userAgent: '', |
| 109 | + }, |
| 110 | + requestId: '', |
| 111 | + routeKey: 'POST /test', |
| 112 | + stage: 'dev', |
| 113 | + time: '', |
| 114 | + timeEpoch: 0, |
| 115 | + }, |
| 116 | + body: '{"test":"body"}', |
| 117 | + isBase64Encoded: false, |
| 118 | +}; |
| 119 | + |
| 120 | +const albEvent: ALBEvent = { |
| 121 | + requestContext: { |
| 122 | + elb: { |
| 123 | + targetGroupArn: '', |
| 124 | + }, |
| 125 | + }, |
| 126 | + httpMethod: 'GET', |
| 127 | + path: '/test', |
| 128 | + queryStringParameters: {}, |
| 129 | + headers: {}, |
| 130 | + body: '', |
| 131 | + isBase64Encoded: false, |
| 132 | +}; |
| 133 | + |
| 134 | +const context: Context = { |
| 135 | + callbackWaitsForEmptyEventLoop: true, |
| 136 | + functionName: '', |
| 137 | + functionVersion: '', |
| 138 | + invokedFunctionArn: '', |
| 139 | + memoryLimitInMB: '', |
| 140 | + awsRequestId: '', |
| 141 | + logGroupName: '', |
| 142 | + logStreamName: '', |
| 143 | + getRemainingTimeInMillis: () => 0, |
| 144 | + done: () => {}, |
| 145 | + fail: () => {}, |
| 146 | + succeed: () => {}, |
| 147 | +}; |
| 148 | + |
| 149 | +const api = new API(); |
| 150 | +expectType<Promise<any>>(api.run(apiGwV1Event, context)); |
| 151 | +expectType<Promise<any>>(api.run(apiGwV2Event, context)); |
| 152 | +// @ts-expect-error ALB events are not supported |
| 153 | +expectType<void & Promise<any>>(api.run(albEvent, context)); |
| 154 | + |
| 155 | +const res = {} as Response; |
| 156 | +expectType<Response>(res.status(200)); |
| 157 | +expectType<Response>(res.header('Content-Type', 'application/json')); |
| 158 | +expectType<Response>( |
| 159 | + res.cookie('session', 'value', { |
| 160 | + httpOnly: true, |
| 161 | + secure: true, |
| 162 | + }) |
| 163 | +); |
| 164 | + |
| 165 | +expectType<void>(res.send({ message: 'test' })); |
| 166 | +expectType<void>(res.json({ message: 'test' })); |
| 167 | +expectType<void>(res.html('<div>test</div>')); |
| 168 | + |
| 169 | +expectType<void>(res.error('Test error')); |
| 170 | +expectType<void>( |
| 171 | + res.error(500, 'Server error', { details: 'Additional info' }) |
| 172 | +); |
| 173 | + |
| 174 | +expectType<void>(res.redirect('/new-path')); |
| 175 | + |
| 176 | +const middleware: Middleware = (req, res, next) => { |
| 177 | + next(); |
| 178 | +}; |
| 179 | +expectType<Middleware>(middleware); |
| 180 | + |
| 181 | +const errorMiddleware: ErrorHandlingMiddleware = (error, req, res, next) => { |
| 182 | + res.status(500).json({ error: error.message }); |
| 183 | +}; |
| 184 | +expectType<ErrorHandlingMiddleware>(errorMiddleware); |
| 185 | + |
| 186 | +const handler: HandlerFunction = (req, res) => { |
| 187 | + res.json({ success: true }); |
| 188 | +}; |
| 189 | +expectType<HandlerFunction>(handler); |
| 190 | + |
| 191 | +const cookieOptions: CookieOptions = { |
| 192 | + domain: 'example.com', |
| 193 | + httpOnly: true, |
| 194 | + secure: true, |
| 195 | + sameSite: 'Strict', |
| 196 | +}; |
| 197 | +expectType<CookieOptions>(cookieOptions); |
| 198 | + |
| 199 | +const corsOptions: CorsOptions = { |
| 200 | + origin: '*', |
| 201 | + methods: 'GET,POST', |
| 202 | + headers: 'Content-Type,Authorization', |
| 203 | + credentials: true, |
| 204 | +}; |
| 205 | +expectType<CorsOptions>(corsOptions); |
| 206 | + |
| 207 | +const fileOptions: FileOptions = { |
| 208 | + maxAge: 3600, |
| 209 | + root: '/public', |
| 210 | + lastModified: true, |
| 211 | + headers: { 'Cache-Control': 'public' }, |
| 212 | +}; |
| 213 | +expectType<FileOptions>(fileOptions); |
| 214 | + |
| 215 | +const loggerOptions: LoggerOptions = { |
| 216 | + level: 'info', |
| 217 | + access: true, |
| 218 | + timestamp: true, |
| 219 | + sampling: { |
| 220 | + target: 10, |
| 221 | + rate: 0.1, |
| 222 | + }, |
| 223 | +}; |
| 224 | +expectType<LoggerOptions>(loggerOptions); |
| 225 | + |
| 226 | +const methods: METHODS[] = [ |
| 227 | + 'GET', |
| 228 | + 'POST', |
| 229 | + 'PUT', |
| 230 | + 'DELETE', |
| 231 | + 'OPTIONS', |
| 232 | + 'HEAD', |
| 233 | + 'ANY', |
| 234 | +]; |
| 235 | +expectType<METHODS[]>(methods); |
| 236 | + |
| 237 | +const routeError = new RouteError('Route not found', '/api/test'); |
| 238 | +expectType<RouteError>(routeError); |
| 239 | + |
| 240 | +const methodError = new MethodError( |
| 241 | + 'Method not allowed', |
| 242 | + 'POST' as METHODS, |
| 243 | + '/api/test' |
| 244 | +); |
| 245 | +expectType<MethodError>(methodError); |
| 246 | + |
| 247 | +const configError = new ConfigurationError('Invalid configuration'); |
| 248 | +expectType<ConfigurationError>(configError); |
| 249 | + |
| 250 | +const responseError = new ResponseError('Response error', 500); |
| 251 | +expectType<ResponseError>(responseError); |
| 252 | + |
| 253 | +const fileError = new FileError('File not found', { |
| 254 | + code: 'ENOENT', |
| 255 | + syscall: 'open', |
| 256 | +}); |
| 257 | +expectType<FileError>(fileError); |
0 commit comments