|
| 1 | +import type { |
| 2 | + FastifyInstance, |
| 3 | + FastifyReply, |
| 4 | + FastifyRequest, |
| 5 | + FastifySchema, |
| 6 | +} from "fastify"; |
| 7 | + |
| 8 | +type DelayRequest = FastifyRequest<{ |
| 9 | + Params: { delay: string }; |
| 10 | + Querystring: Record<string, string>; |
| 11 | +}>; |
| 12 | + |
| 13 | +const delaySchema: FastifySchema = { |
| 14 | + description: "Returns a delayed response (max of 10 seconds)", |
| 15 | + tags: ["Dynamic Data"], |
| 16 | + params: { |
| 17 | + type: "object", |
| 18 | + properties: { |
| 19 | + delay: { |
| 20 | + type: "string", |
| 21 | + description: "Delay in seconds (max 10)", |
| 22 | + }, |
| 23 | + }, |
| 24 | + required: ["delay"], |
| 25 | + }, |
| 26 | + querystring: { |
| 27 | + type: "object", |
| 28 | + additionalProperties: true, |
| 29 | + }, |
| 30 | + response: { |
| 31 | + 200: { |
| 32 | + type: "object", |
| 33 | + properties: { |
| 34 | + args: { type: "object" }, |
| 35 | + data: { type: "string" }, |
| 36 | + files: { type: "object" }, |
| 37 | + form: { type: "object" }, |
| 38 | + headers: { type: "object" }, |
| 39 | + origin: { type: "string" }, |
| 40 | + url: { type: "string" }, |
| 41 | + }, |
| 42 | + }, |
| 43 | + 400: { |
| 44 | + type: "object", |
| 45 | + properties: { |
| 46 | + error: { type: "string" }, |
| 47 | + }, |
| 48 | + }, |
| 49 | + }, |
| 50 | +}; |
| 51 | + |
| 52 | +const MAX_DELAY = 10; // Maximum delay in seconds |
| 53 | + |
| 54 | +function sleep(ms: number): Promise<void> { |
| 55 | + return new Promise((resolve) => setTimeout(resolve, ms)); |
| 56 | +} |
| 57 | + |
| 58 | +export const delayRoute = (fastify: FastifyInstance) => { |
| 59 | + const handler = async (request: DelayRequest, reply: FastifyReply) => { |
| 60 | + const delay = Number.parseFloat(request.params.delay); |
| 61 | + |
| 62 | + if (Number.isNaN(delay) || delay < 0) { |
| 63 | + return reply |
| 64 | + .code(400) |
| 65 | + .send({ error: "delay must be a non-negative number" }); |
| 66 | + } |
| 67 | + |
| 68 | + // Cap delay at MAX_DELAY seconds |
| 69 | + const actualDelay = Math.min(delay, MAX_DELAY); |
| 70 | + await sleep(actualDelay * 1000); |
| 71 | + |
| 72 | + const { protocol } = request; |
| 73 | + /* v8 ignore next -- @preserve */ |
| 74 | + const host = request.headers.host || "localhost"; |
| 75 | + |
| 76 | + return { |
| 77 | + /* v8 ignore next -- @preserve */ |
| 78 | + args: request.query || {}, |
| 79 | + data: "", |
| 80 | + files: {}, |
| 81 | + form: {}, |
| 82 | + headers: request.headers, |
| 83 | + origin: request.ip, |
| 84 | + url: `${protocol}://${host}${request.url}`, |
| 85 | + }; |
| 86 | + }; |
| 87 | + |
| 88 | + fastify.get("/delay/:delay", { schema: delaySchema }, handler); |
| 89 | + fastify.post("/delay/:delay", { schema: delaySchema }, handler); |
| 90 | + fastify.put("/delay/:delay", { schema: delaySchema }, handler); |
| 91 | + fastify.patch("/delay/:delay", { schema: delaySchema }, handler); |
| 92 | + fastify.delete("/delay/:delay", { schema: delaySchema }, handler); |
| 93 | +}; |
0 commit comments