forked from monkeytypegame/monkeytype
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
impr: use tsrest for ape-keys endpoint (@fehmer) (monkeytypegame#5694)
!nuf
- Loading branch information
Showing
19 changed files
with
642 additions
and
263 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,91 +1,42 @@ | ||
import joi from "joi"; | ||
import { Router } from "express"; | ||
import { authenticateRequest } from "../../middlewares/auth"; | ||
import * as ApeKeyController from "../controllers/ape-key"; | ||
import { apeKeysContract } from "@monkeytype/contracts/ape-keys"; | ||
import { initServer } from "@ts-rest/express"; | ||
import * as RateLimit from "../../middlewares/rate-limit"; | ||
import * as ApeKeyController from "../controllers/ape-key"; | ||
import { callController } from "../ts-rest-adapter"; | ||
import { checkUserPermissions } from "../../middlewares/permission"; | ||
import { validate } from "../../middlewares/configuration"; | ||
import { asyncHandler } from "../../middlewares/utility"; | ||
import { validateRequest } from "../../middlewares/validation"; | ||
|
||
const apeKeyNameSchema = joi | ||
.string() | ||
.regex(/^[0-9a-zA-Z_.-]+$/) | ||
.max(20) | ||
.messages({ | ||
"string.pattern.base": "Invalid ApeKey name", | ||
"string.max": "ApeKey name exceeds maximum of 20 characters", | ||
}); | ||
|
||
const checkIfUserCanManageApeKeys = checkUserPermissions({ | ||
criteria: (user) => { | ||
// Must be an exact check | ||
return user.canManageApeKeys !== false; | ||
}, | ||
invalidMessage: "You have lost access to ape keys, please contact support", | ||
}); | ||
|
||
const router = Router(); | ||
|
||
router.use( | ||
const commonMiddleware = [ | ||
validate({ | ||
criteria: (configuration) => { | ||
return configuration.apeKeys.endpointsEnabled; | ||
}, | ||
invalidMessage: "ApeKeys are currently disabled.", | ||
}) | ||
); | ||
|
||
router.get( | ||
"/", | ||
authenticateRequest(), | ||
RateLimit.apeKeysGet, | ||
checkIfUserCanManageApeKeys, | ||
asyncHandler(ApeKeyController.getApeKeys) | ||
); | ||
|
||
router.post( | ||
"/", | ||
authenticateRequest(), | ||
RateLimit.apeKeysGenerate, | ||
checkIfUserCanManageApeKeys, | ||
validateRequest({ | ||
body: { | ||
name: apeKeyNameSchema.required(), | ||
enabled: joi.boolean().required(), | ||
}, | ||
}), | ||
asyncHandler(ApeKeyController.generateApeKey) | ||
); | ||
|
||
router.patch( | ||
"/:apeKeyId", | ||
authenticateRequest(), | ||
RateLimit.apeKeysUpdate, | ||
checkIfUserCanManageApeKeys, | ||
validateRequest({ | ||
params: { | ||
apeKeyId: joi.string().token().required(), | ||
}, | ||
body: { | ||
name: apeKeyNameSchema, | ||
enabled: joi.boolean(), | ||
}, | ||
}), | ||
asyncHandler(ApeKeyController.editApeKey) | ||
); | ||
|
||
router.delete( | ||
"/:apeKeyId", | ||
authenticateRequest(), | ||
RateLimit.apeKeysDelete, | ||
checkIfUserCanManageApeKeys, | ||
validateRequest({ | ||
params: { | ||
apeKeyId: joi.string().token().required(), | ||
checkUserPermissions({ | ||
criteria: (user) => { | ||
return user.canManageApeKeys ?? false; | ||
}, | ||
invalidMessage: "You have lost access to ape keys, please contact support", | ||
}), | ||
asyncHandler(ApeKeyController.deleteApeKey) | ||
); | ||
]; | ||
|
||
export default router; | ||
const s = initServer(); | ||
export default s.router(apeKeysContract, { | ||
get: { | ||
middleware: [...commonMiddleware, RateLimit.apeKeysGet], | ||
handler: async (r) => callController(ApeKeyController.getApeKeys)(r), | ||
}, | ||
add: { | ||
middleware: [...commonMiddleware, RateLimit.apeKeysGenerate], | ||
handler: async (r) => callController(ApeKeyController.generateApeKey)(r), | ||
}, | ||
save: { | ||
middleware: [...commonMiddleware, RateLimit.apeKeysUpdate], | ||
handler: async (r) => callController(ApeKeyController.editApeKey)(r), | ||
}, | ||
delete: { | ||
middleware: [...commonMiddleware, RateLimit.apeKeysDelete], | ||
handler: async (r) => callController(ApeKeyController.deleteApeKey)(r), | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.