Skip to content

Commit d29c6d4

Browse files
authored
Merge pull request #24 from oslabs-beta/sh/middleware-framework
Initial tests for middleware functionality.
2 parents 35a928d + 206a859 commit d29c6d4

File tree

8 files changed

+618
-13
lines changed

8 files changed

+618
-13
lines changed

.eslintrc.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@
1919
},
2020
"plugins": ["import", "prettier"],
2121
"rules": {
22-
22+
"no-plusplus": [2, {
23+
"allowForLoopAfterthoughts": true
24+
}],
2325
"prettier/prettier": [
2426
"error"
2527
]
2628
},
27-
"ignorePatterns": ["jest.config.js"]
29+
"ignorePatterns": ["jest.config.ts"]
2830
}

jest.config.js

Lines changed: 0 additions & 7 deletions
This file was deleted.

jest.config.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import type { Config } from '@jest/types';
2+
3+
const config: Config.InitialOptions = {
4+
verbose: true,
5+
roots: ['./test'],
6+
preset: 'ts-jest',
7+
testEnvironment: 'node',
8+
moduleFileExtensions: ['js', 'ts'],
9+
};
10+
11+
export default config;

package-lock.json

Lines changed: 237 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
"version": "1.0.0",
44
"description": "A GraphQL rate limiting library using query complexity analysis.",
55
"main": "index.js",
6+
"type": "module",
67
"scripts": {
78
"test": "jest --passWithNoTests",
89
"lint": "eslint src test",
9-
"lint:fix": "eslint --fix src test",
10+
"lint:fix": "eslint --fix src test @types",
1011
"prettier": "prettier --write .",
1112
"prepare": "husky install"
1213
},
@@ -43,6 +44,7 @@
4344
"prettier": "2.6.2",
4445
"redis-mock": "^0.56.3",
4546
"ts-jest": "^28.0.2",
47+
"ts-node": "^10.8.0",
4648
"typescript": "^4.6.4"
4749
},
4850
"lint-staged": {

src/@types/rateLimit.d.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,6 @@ interface TokenBucketOptions {
4040
}
4141

4242
// TODO: This will be a union type where we can specify Option types for other Rate Limiters
43-
type RateLimiterOptions = TokenBucketOptions;
43+
// Record<string, never> represents the empty object for alogorithms that don't require settings
44+
// and might be able to be removed in the future.
45+
type RateLimiterOptions = TokenBucketOptions | Record<string, never>;

src/middleware/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ import { defaultTypeWeightsConfig } from '../analysis/buildTypeWeights';
1717
* Defaults to {mutation: 10, object: 1, field: 0, connection: 2}
1818
* @returns {RequestHandler} express middleware that computes the complexity of req.query and calls the next middleware
1919
* if the query is allowed or sends a 429 status if the request is blocked
20-
* @throws ValidationError if GraphQL Schema is invalid
20+
* FIXME: How about the specific GraphQLError?
21+
* @throws ValidationError if GraphQL Schema is invalid.
2122
*/
2223
export function expressRateLimiter(
2324
rateLimiter: RateLimiterSelection,
@@ -31,7 +32,6 @@ export function expressRateLimiter(
3132
// TODO: Connect to Redis store using provided options. Default to localhost:6379
3233
// TODO: Configure the selected RateLimtier
3334
// TODO: Configure the complexity analysis algorithm to run for incoming requests
34-
3535
const middleware: RequestHandler = (req: Request, res: Response, next: NextFunction) => {
3636
// TODO: Parse query from req.query, compute complexity and pass necessary info to rate limiter
3737
// TODO: Call next if query is successful, send 429 status if query blocked, call next(err) with any thrown errors

0 commit comments

Comments
 (0)