-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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 dev endpoints (@fehmer)
- Loading branch information
Showing
13 changed files
with
105 additions
and
77 deletions.
There are no files selected for viewing
Empty file.
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,35 +1,22 @@ | ||
import { Router } from "express"; | ||
import joi from "joi"; | ||
import { createTestData } from "../controllers/dev"; | ||
import { isDevEnvironment } from "../../utils/misc"; | ||
import { devContract } from "@monkeytype/contracts/dev"; | ||
import { initServer } from "@ts-rest/express"; | ||
import { validate } from "../../middlewares/configuration"; | ||
import { validateRequest } from "../../middlewares/validation"; | ||
import { asyncHandler } from "../../middlewares/utility"; | ||
|
||
const router = Router(); | ||
import { isDevEnvironment } from "../../utils/misc"; | ||
import * as DevController from "../controllers/dev"; | ||
import { callController } from "../ts-rest-adapter"; | ||
|
||
router.use( | ||
validate({ | ||
criteria: () => { | ||
return isDevEnvironment(); | ||
}, | ||
invalidMessage: "Development endpoints are only available in DEV mode.", | ||
}) | ||
); | ||
const onlyAvailableOnDev = validate({ | ||
criteria: () => { | ||
return isDevEnvironment(); | ||
}, | ||
invalidMessage: "Development endpoints are only available in DEV mode.", | ||
}); | ||
|
||
router.post( | ||
"/generateData", | ||
validateRequest({ | ||
body: { | ||
username: joi.string().required(), | ||
createUser: joi.boolean().optional(), | ||
firstTestTimestamp: joi.number().optional(), | ||
lastTestTimestamp: joi.number().optional(), | ||
minTestsPerDay: joi.number().optional(), | ||
maxTestsPerDay: joi.number().optional(), | ||
}, | ||
}), | ||
asyncHandler(createTestData) | ||
); | ||
const s = initServer(); | ||
|
||
export default router; | ||
export default s.router(devContract, { | ||
generateData: { | ||
middleware: [onlyAvailableOnDev], | ||
handler: async (r) => callController(DevController.createTestData)(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 was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,7 @@ | ||
import Quotes from "./quotes"; | ||
import Users from "./users"; | ||
import Dev from "./dev"; | ||
|
||
export default { | ||
Quotes, | ||
Users, | ||
Dev, | ||
}; |
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 was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { initContract } from "@ts-rest/core"; | ||
import { z } from "zod"; | ||
import { | ||
CommonResponses, | ||
EndpointMetadata, | ||
responseWithData, | ||
} from "./schemas/api"; | ||
import { IdSchema } from "./schemas/util"; | ||
|
||
export const GenerateDataRequestSchema = z.object({ | ||
username: z.string(), | ||
createUser: z | ||
.boolean() | ||
.optional() | ||
.describe( | ||
"If `true` create user with <username>@example.com and password `password`. If false user has to exist." | ||
), | ||
firstTestTimestamp: z.number().int().nonnegative().optional(), | ||
lastTestTimestamp: z.number().int().nonnegative().optional(), | ||
minTestsPerDay: z.number().int().nonnegative().optional(), | ||
maxTestsPerDay: z.number().int().nonnegative().optional(), | ||
}); | ||
export type GenerateDataRequest = z.infer<typeof GenerateDataRequestSchema>; | ||
|
||
export const GenerateDataResponseSchema = responseWithData( | ||
z.object({ | ||
uid: IdSchema, | ||
email: z.string().email(), | ||
}) | ||
); | ||
export type GenerateDataResponse = z.infer<typeof GenerateDataResponseSchema>; | ||
|
||
const c = initContract(); | ||
export const devContract = c.router( | ||
{ | ||
generateData: { | ||
summary: "generate data", | ||
description: "Generate test results for the given user.", | ||
method: "POST", | ||
path: "/generateData", | ||
body: GenerateDataRequestSchema.strict(), | ||
responses: { | ||
200: GenerateDataResponseSchema, | ||
}, | ||
}, | ||
}, | ||
{ | ||
pathPrefix: "/dev", | ||
strictStatusCodes: true, | ||
metadata: { | ||
openApiTags: "dev", | ||
authenticationOptions: { | ||
isPublic: true, | ||
}, | ||
} as EndpointMetadata, | ||
commonResponses: CommonResponses, | ||
} | ||
); |
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