Skip to content

Commit 70f0ed1

Browse files
authored
Merge pull request #9 from solaoi/feature_add-ntimes-error-for-retry-checking
add ntimes error on request a endpoint for testing retry
2 parents 2eeb3db + bd36203 commit 70f0ed1

File tree

11 files changed

+224
-43
lines changed

11 files changed

+224
-43
lines changed

app/api/[[...slug]].ts

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,25 @@ const Handler: BlitzApiHandler = async (req: BlitzApiRequest, res: BlitzApiRespo
2626
if (!obj) {
2727
return res.status(404).end()
2828
}
29-
const { id, contentType, statusCode, sleep, response, logs } = obj
30-
if (sleep !== 0) {
31-
await snooze(sleep * 1000)
32-
}
29+
const {
30+
id,
31+
contentType,
32+
statusCode,
33+
sleep,
34+
response,
35+
logs,
36+
ntimesError,
37+
ntimesErrorStatusCode,
38+
ntimesErrorCounter,
39+
} = obj
40+
41+
const ntimesErrorCount = (() => {
42+
if (ntimesError === 0 || ntimesError === ntimesErrorCounter) {
43+
return 0
44+
} else {
45+
return ntimesErrorCounter + 1
46+
}
47+
})()
3348

3449
const RECENT_LOGS = 3
3550
const { slug: _, ...query } = req.query
@@ -55,8 +70,25 @@ const Handler: BlitzApiHandler = async (req: BlitzApiRequest, res: BlitzApiRespo
5570
log,
5671
...logArr.filter((_, i) => logArr.length < RECENT_LOGS || i !== logArr.length - 1),
5772
].join("\t")
58-
await db.stub.update({ where: { id }, data: { logs: updatedLogs } })
5973

74+
const inNtimesErrorTerm = ntimesError > 0 && ntimesError >= ntimesErrorCounter + 1
75+
const sleepFunc = async () => {
76+
if (!inNtimesErrorTerm && sleep !== 0) {
77+
return snooze(sleep * 1000)
78+
}
79+
}
80+
81+
await Promise.all([
82+
sleepFunc(),
83+
db.stub.update({
84+
where: { id },
85+
data: { logs: updatedLogs, ntimesErrorCounter: ntimesErrorCount },
86+
}),
87+
])
88+
89+
if (inNtimesErrorTerm) {
90+
return res.status(Number(ntimesErrorStatusCode)).end()
91+
}
6092
return res.status(Number(statusCode)).setHeader("Content-Type", contentType).end(response)
6193
}
6294

app/core/components/LabeledTextField.tsx

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@ import { forwardRef, PropsWithoutRef, ComponentPropsWithoutRef } from "react"
22
import { useField } from "react-final-form"
33

44
import { Input } from "@chakra-ui/input"
5+
import {
6+
NumberInput,
7+
NumberInputField,
8+
NumberInputStepper,
9+
NumberIncrementStepper,
10+
NumberDecrementStepper,
11+
} from "@chakra-ui/react"
512
import { FormControl, FormLabel } from "@chakra-ui/form-control"
613

714
export interface LabeledTextFieldProps extends ComponentPropsWithoutRef<typeof Input> {
@@ -29,7 +36,17 @@ export const LabeledTextField = forwardRef<HTMLInputElement, LabeledTextFieldPro
2936
<FormControl {...outerProps}>
3037
<FormLabel>
3138
{label}
32-
<Input {...input} disabled={submitting} {...props} ref={ref} />
39+
{props.type === "number" ? (
40+
<NumberInput {...input} disabled={submitting} {...props} ref={ref}>
41+
<NumberInputField />
42+
<NumberInputStepper>
43+
<NumberIncrementStepper />
44+
<NumberDecrementStepper />
45+
</NumberInputStepper>
46+
</NumberInput>
47+
) : (
48+
<Input {...input} disabled={submitting} {...props} ref={ref} />
49+
)}
3350
</FormLabel>
3451
{touched && normalizedError && (
3552
<div role="alert" style={{ color: "red" }}>

app/core/layouts/Card.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
import React from "react"
22
import { Box, Heading } from "@chakra-ui/react"
33

4-
const Card = ({ heading, children }: { heading: string; children: any }) => {
4+
const Card = ({
5+
heading,
6+
children,
7+
bgColor = "#FFF",
8+
}: {
9+
heading: string
10+
children: any
11+
bgColor?: string
12+
}) => {
513
return (
6-
<Box maxW="sm" borderWidth="1px" borderRadius="lg" overflow="hidden">
14+
<Box maxW="sm" borderWidth="1px" borderRadius="lg" overflow="hidden" bgColor={bgColor}>
715
<Box m="5">
816
<Heading mb="5" as="h4" size="md">
917
{heading}

app/pages/stubs/[stubId].tsx

Lines changed: 66 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -111,29 +111,77 @@ export const Stub = () => {
111111
<Flex justify="space-between">
112112
<Box flex="1">
113113
<Flex direction="column" fontWeight="bold" color="#666">
114-
<Text mb="1">path</Text>
115-
<Text mb="1">createdBy</Text>
116-
<Text mb="1">createdAt</Text>
117-
<Text mb="1">updatedBy</Text>
118-
<Text mb="1">updatedAt</Text>
119-
<Text mb="1">method</Text>
120-
<Text mb="1">contentType</Text>
121-
<Text mb="1">statusCode</Text>
122-
<Text mb="1">sleep</Text>
114+
<Text mb="1" h="1.5rem">
115+
path
116+
</Text>
117+
<Text mb="1" h="1.5rem">
118+
createdBy
119+
</Text>
120+
<Text mb="1" h="1.5rem">
121+
createdAt
122+
</Text>
123+
<Text mb="1" h="1.5rem">
124+
updatedBy
125+
</Text>
126+
<Text mb="1" h="1.5rem">
127+
updatedAt
128+
</Text>
129+
<Text mb="1" h="1.5rem">
130+
method
131+
</Text>
132+
<Text mb="1" h="1.5rem">
133+
contentType
134+
</Text>
135+
<Text mb="1" h="1.5rem">
136+
statusCode
137+
</Text>
138+
<Text mb="1" h="1.5rem">
139+
sleep
140+
</Text>
141+
<Text mb="1" h="1.5rem">
142+
ntimesError
143+
</Text>
144+
<Text mb="1" h="1.5rem">
145+
ntimesErrorStatusCode
146+
</Text>
123147
<Text>response</Text>
124148
</Flex>
125149
</Box>
126150
<Box flex="2">
127151
<Flex direction="column">
128-
<Text mb="1">{stub.path}</Text>
129-
<Text mb="1">{stub.createdBy}</Text>
130-
<Text mb="1">{stub.createdAt.toLocaleString()}</Text>
131-
<Text mb="1">{stub.updatedBy}</Text>
132-
<Text mb="1">{stub.updatedAt.toLocaleString()}</Text>
133-
<Text mb="1">{stub.method}</Text>
134-
<Text mb="1">{stub.contentType}</Text>
135-
<Text mb="1">{stub.statusCode}</Text>
136-
<Text mb="1">{stub.sleep} s</Text>
152+
<Text mb="1" h="1.5rem">
153+
{stub.path}
154+
</Text>
155+
<Text mb="1" h="1.5rem">
156+
{stub.createdBy}
157+
</Text>
158+
<Text mb="1" h="1.5rem">
159+
{stub.createdAt.toLocaleString()}
160+
</Text>
161+
<Text mb="1" h="1.5rem">
162+
{stub.updatedBy}
163+
</Text>
164+
<Text mb="1" h="1.5rem">
165+
{stub.updatedAt.toLocaleString()}
166+
</Text>
167+
<Text mb="1" h="1.5rem">
168+
{stub.method}
169+
</Text>
170+
<Text mb="1" h="1.5rem">
171+
{stub.contentType}
172+
</Text>
173+
<Text mb="1" h="1.5rem">
174+
{stub.statusCode}
175+
</Text>
176+
<Text mb="1" h="1.5rem">
177+
{stub.sleep} s
178+
</Text>
179+
<Text mb="1" h="1.5rem">
180+
{stub.ntimesError} times
181+
</Text>
182+
<Text mb="1" h="1.5rem">
183+
{stub.ntimesErrorStatusCode}
184+
</Text>
137185
<Box w="100%" p="2" bgColor="#3c3c3c" color="#fff" borderRadius="lg">
138186
<pre style={{ whiteSpace: "pre-wrap" }}>
139187
{(() => {

app/pages/stubs/[stubId]/edit.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,10 @@ export const EditStub = () => {
7070
<Flex align="center" justify="center" m="6">
7171
<Card heading="Edit Stub">
7272
<StubForm
73-
initialValues={{ ...stub, updatedBy: currentUser?.name }}
73+
initialValues={{
74+
...stub,
75+
updatedBy: currentUser?.name,
76+
}}
7477
submitText="UPDATE"
7578
// TODO use a zod schema for form validation
7679
// - Tip: extract mutation's schema into a shared `validations.ts` file and

app/pages/stubs/new.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,13 @@ const NewStubPage: BlitzPage = () => {
5252
<Flex align="center" justify="center" m="6">
5353
<Card heading="New Stub">
5454
<StubForm
55-
initialValues={{ createdBy: currentUser?.name, updatedBy: currentUser?.name, sleep: 0 }}
55+
initialValues={{
56+
createdBy: currentUser?.name,
57+
updatedBy: currentUser?.name,
58+
sleep: 0,
59+
ntimesError: 0,
60+
ntimesErrorStatusCode: "500",
61+
}}
5662
submitText="ADD"
5763
// TODO use a zod schema for form validation
5864
// - Tip: extract mutation's schema into a shared `validations.ts` file and

app/stubs/components/StubForm.tsx

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import { LabeledSelectField } from "app/core/components/LabeledSelectField"
55
import { useRouterQuery } from "blitz"
66
import { z } from "zod"
77
export { FORM_ERROR } from "app/core/components/Form"
8+
import Card from "../../core/layouts/Card"
9+
import { border } from "@chakra-ui/styled-system"
810

911
export function StubForm<S extends z.ZodType<any, any>>(props: FormProps<S>) {
1012
const query = useRouterQuery()
@@ -43,8 +45,29 @@ export function StubForm<S extends z.ZodType<any, any>>(props: FormProps<S>) {
4345
]}
4446
/>
4547
<LabeledTextField name="statusCode" label="StatusCode" placeholder="200" />
46-
<LabeledTextField type="number" name="sleep" label="Sleep(seconds)" placeholder="0" />
4748
<LabeledTextAreaField name="response" label="Response" placeholder="Response" />
49+
<Card heading="Optional" bgColor="#E2E8F0">
50+
<LabeledTextField
51+
type="number"
52+
name="sleep"
53+
label="Sleep(seconds)"
54+
placeholder="0"
55+
style={{ borderColor: "#FFF" }}
56+
/>
57+
<LabeledTextField
58+
type="number"
59+
name="ntimesError"
60+
label="NtimesError"
61+
placeholder="0"
62+
style={{ borderColor: "#FFF" }}
63+
/>
64+
<LabeledTextField
65+
name="ntimesErrorStatusCode"
66+
label="NtimesErrorStatusCode"
67+
placeholder="500"
68+
style={{ borderColor: "#FFF" }}
69+
/>
70+
</Card>
4871
</Form>
4972
)
5073
}

app/stubs/mutations/createStub.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ const CreateStub = z.object({
2525
.regex(/^\d{3}$/, { message: "The status code must be a three-digit number." }),
2626
response: z.string().default(""),
2727
sleep: z.number().min(0).default(0),
28+
ntimesError: z.number().min(0).default(0),
29+
ntimesErrorStatusCode: z
30+
.string()
31+
.regex(/^\d{3}$/, { message: "The ntimes error status code must be a three-digit number." }),
32+
ntimesErrorCounter: z.number().min(0).default(0),
2833
logs: z.string().default(""),
2934
projectId: z.number(),
3035
})

app/stubs/mutations/updateStub.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ const UpdateStub = z.object({
2525
.regex(/^\d{3}$/, { message: "The status code must be a three-digit number." }),
2626
response: z.string(),
2727
sleep: z.number().min(0),
28+
ntimesError: z.number().min(0).default(0),
29+
ntimesErrorStatusCode: z
30+
.string()
31+
.regex(/^\d{3}$/, { message: "The ntimes error status code must be a three-digit number." }),
2832
})
2933

3034
export default resolver.pipe(
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
Warnings:
3+
4+
- Added the required column `ntimesErrorStatusCode` to the `Stub` table without a default value. This is not possible if the table is not empty.
5+
6+
*/
7+
-- RedefineTables
8+
PRAGMA foreign_keys=OFF;
9+
CREATE TABLE "new_Stub" (
10+
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
11+
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
12+
"updatedAt" DATETIME NOT NULL,
13+
"createdBy" TEXT NOT NULL,
14+
"updatedBy" TEXT NOT NULL,
15+
"path" TEXT NOT NULL,
16+
"method" TEXT NOT NULL,
17+
"contentType" TEXT NOT NULL,
18+
"statusCode" TEXT NOT NULL,
19+
"response" TEXT NOT NULL,
20+
"sleep" INTEGER NOT NULL DEFAULT 0,
21+
"logs" TEXT NOT NULL,
22+
"ntimesError" INTEGER NOT NULL DEFAULT 0,
23+
"ntimesErrorStatusCode" TEXT NOT NULL,
24+
"ntimesErrorCounter" INTEGER NOT NULL DEFAULT 0,
25+
"projectId" INTEGER NOT NULL,
26+
CONSTRAINT "Stub_projectId_fkey" FOREIGN KEY ("projectId") REFERENCES "Project" ("id") ON DELETE CASCADE ON UPDATE CASCADE
27+
);
28+
INSERT INTO "new_Stub" ("contentType", "createdAt", "createdBy", "id", "logs", "method", "path", "projectId", "response", "sleep", "statusCode", "updatedAt", "updatedBy") SELECT "contentType", "createdAt", "createdBy", "id", "logs", "method", "path", "projectId", "response", "sleep", "statusCode", "updatedAt", "updatedBy" FROM "Stub";
29+
DROP TABLE "Stub";
30+
ALTER TABLE "new_Stub" RENAME TO "Stub";
31+
PRAGMA foreign_key_check;
32+
PRAGMA foreign_keys=ON;

0 commit comments

Comments
 (0)