added a function which applies a custom rate limiter when using ape key

opened results endpoint to ape keys (1 req per hour)
updated docs to mention new open endpoint
This commit is contained in:
Miodec 2022-10-28 18:42:44 +02:00
parent b00e5d893e
commit 508c289c92
4 changed files with 50 additions and 4 deletions

View file

@ -9,14 +9,19 @@ import * as RateLimit from "../../middlewares/rate-limit";
import { Router } from "express";
import { authenticateRequest } from "../../middlewares/auth";
import joi from "joi";
import { withApeRateLimiter } from "../../middlewares/ape-rate-limit";
import {
withApeRateLimiter,
withCustomApeRateLimiter,
} from "../../middlewares/ape-rate-limit";
const router = Router();
router.get(
"/",
authenticateRequest(),
RateLimit.resultsGet,
authenticateRequest({
acceptApeKeys: true,
}),
withCustomApeRateLimiter(RateLimit.resultsGet_ape, RateLimit.resultsGet),
asyncHandler(ResultController.getResults)
);

View file

@ -1,7 +1,7 @@
{
"swagger": "2.0",
"info": {
"description": "Documentation for the public endpoints provided by the Monketype API server.\n\nNote that authentication is performed with the Authorization HTTP header in the format `Authorization: ApeKey YOUR_APE_KEY`\n\nThere is a rate limit of `30 requests per minute` across all endpoints. Rate limit rates are shared across all ape keys.",
"description": "Documentation for the public endpoints provided by the Monketype API server.\n\nNote that authentication is performed with the Authorization HTTP header in the format `Authorization: ApeKey YOUR_APE_KEY`\n\nThere is a rate limit of `30 requests per minute` across all endpoints with some endpoints being more strict. Rate limit rates are shared across all ape keys.",
"version": "1.0.0",
"title": "Monkeytype API",
"termsOfService": "https://monkeytype.com/terms-of-service",
@ -95,6 +95,20 @@
}
}
},
"/results": {
"get": {
"tags": ["results"],
"summary": "Gets up to 1000 of the most recent results (endpoint limited to 1 request per hour)",
"responses": {
"200": {
"description": "",
"schema": {
"$ref": "#/definitions/Results"
}
}
}
}
},
"/results/last": {
"get": {
"tags": ["results"],
@ -498,6 +512,12 @@
}
}
},
"Results": {
"type": "array",
"items": {
"$ref": "#/definitions/Result"
}
},
"Result": {
"type": "object",
"properties": {

View file

@ -45,3 +45,16 @@ export function withApeRateLimiter(
return defaultRateLimiter(req, res, next);
};
}
export function withCustomApeRateLimiter(
customRateLimiter: RateLimitRequestHandler,
defaultRateLimiter: RateLimitRequestHandler
): RequestHandler {
return (req: MonkeyTypes.Request, res: Response, next: NextFunction) => {
if (req.ctx.decodedToken.type === "ApeKey") {
return customRateLimiter(req, res, next);
}
return defaultRateLimiter(req, res, next);
};
}

View file

@ -241,6 +241,14 @@ export const resultsGet = rateLimit({
handler: customHandler,
});
// Results Routing
export const resultsGet_ape = rateLimit({
windowMs: ONE_HOUR_MS,
max: 1 * REQUEST_MULTIPLIER,
keyGenerator: getKeyWithUid,
handler: customHandler,
});
export const resultsAdd = rateLimit({
windowMs: ONE_HOUR_MS,
max: 500 * REQUEST_MULTIPLIER,