Skip to content

Commit

Permalink
Merge pull request #2 from solaoi/feature_add-new-feature
Browse files Browse the repository at this point in the history
Feature add new feature
  • Loading branch information
solaoi authored Sep 20, 2021
2 parents eabfc06 + aac95db commit 078456a
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 62 deletions.
7 changes: 6 additions & 1 deletion app/api/[[...slug]].ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { BlitzApiRequest, BlitzApiResponse, BlitzApiHandler } from "blitz"
import db, { prisma } from "db"

const snooze = (ms) => new Promise((resolve) => setTimeout(resolve, ms))

const dynamicHandler = async (method, slug) => {
const project = await db.project.findFirst({ where: { basePath: `/${slug[0]}` } })
if (!project) {
Expand All @@ -24,7 +26,10 @@ const Handler: BlitzApiHandler = async (req: BlitzApiRequest, res: BlitzApiRespo
if (!obj) {
return res.status(404).end()
}
const { contentType, statusCode, response } = obj
const { contentType, statusCode, sleep, response } = obj
if (sleep !== 0) {
await snooze(sleep * 1000)
}

return res.status(Number(statusCode)).setHeader("Content-Type", contentType).end(response)
}
Expand Down
64 changes: 33 additions & 31 deletions app/pages/projects/[projectId].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -209,38 +209,40 @@ export const Project = () => {
</Button>
</Link>
</Flex>
<Table variant="simple" mb={5}>
<Thead>
<Tr>
<Th>path</Th>
<Th>method</Th>
<Th>content-type</Th>
<Th>status-code</Th>
<Th>copy-url</Th>
</Tr>
</Thead>
<Tbody>
{stubs.map((stub) => (
<Link
href={Routes.ShowStubPage({ stubId: stub.id })}
key={`${stub.projectId}_${stub.id}`}
>
<Tr
style={{ cursor: "pointer" }}
_hover={{ color: "#fff", bg: "teal.400", borderColor: "teal.400" }}
<Box overflowX="auto">
<Table variant="simple" mb={5}>
<Thead>
<Tr>
<Th>path</Th>
<Th>method</Th>
<Th>content-type</Th>
<Th>status-code</Th>
<Th>copy-url</Th>
</Tr>
</Thead>
<Tbody>
{stubs.map((stub) => (
<Link
href={Routes.ShowStubPage({ stubId: stub.id })}
key={`${stub.projectId}_${stub.id}`}
>
<Td>{stub.path}</Td>
<Td>{stub.method}</Td>
<Td>{stub.contentType}</Td>
<Td>{stub.statusCode}</Td>
<Td>
<CopyUrlButton project={project} stub={stub} />
</Td>
</Tr>
</Link>
))}
</Tbody>
</Table>
<Tr
style={{ cursor: "pointer" }}
_hover={{ color: "#fff", bg: "teal.400", borderColor: "teal.400" }}
>
<Td>{stub.path}</Td>
<Td>{stub.method}</Td>
<Td>{stub.contentType}</Td>
<Td>{stub.statusCode}</Td>
<Td>
<CopyUrlButton project={project} stub={stub} />
</Td>
</Tr>
</Link>
))}
</Tbody>
</Table>
</Box>

<Flex justify="space-between">
<Button
Expand Down
58 changes: 30 additions & 28 deletions app/pages/projects/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export const ProjectsList = () => {
</Button>
</Link>
</Flex>
<InputGroup w="30%">
<InputGroup w="30%" minW="6rem">
<InputLeftElement pointerEvents="none">
<SearchIcon color="gray.300" />
</InputLeftElement>
Expand All @@ -116,33 +116,35 @@ export const ProjectsList = () => {
}}
/>
</InputGroup>
<Table variant="simple" mb={5}>
<Thead>
<Tr>
<Th>name</Th>
<Th>base-path</Th>
<Th>created-by</Th>
<Th>updated-by</Th>
</Tr>
</Thead>
<Tbody>
{projects.map((project) => (
<Link href={Routes.ShowProjectPage({ projectId: project.id })} key={project.id}>
<Tr
style={{ cursor: "pointer" }}
_hover={{ color: "#fff", bg: "teal.400", borderColor: "teal.400" }}
>
<Td>{project.name}</Td>
<Td>{project.basePath}</Td>
<Td>{project.createdBy}</Td>
<Suspense fallback={<Td></Td>}>
<UpdatedBy project={project} />
</Suspense>
</Tr>
</Link>
))}
</Tbody>
</Table>
<Box overflowX="auto">
<Table variant="simple" mb={5}>
<Thead>
<Tr>
<Th>name</Th>
<Th>base-path</Th>
<Th>created-by</Th>
<Th>updated-by</Th>
</Tr>
</Thead>
<Tbody>
{projects.map((project) => (
<Link href={Routes.ShowProjectPage({ projectId: project.id })} key={project.id}>
<Tr
style={{ cursor: "pointer" }}
_hover={{ color: "#fff", bg: "teal.400", borderColor: "teal.400" }}
>
<Td>{project.name}</Td>
<Td>{project.basePath}</Td>
<Td>{project.createdBy}</Td>
<Suspense fallback={<Td></Td>}>
<UpdatedBy project={project} />
</Suspense>
</Tr>
</Link>
))}
</Tbody>
</Table>
</Box>

<Flex justify="space-between">
<Button
Expand Down
2 changes: 2 additions & 0 deletions app/pages/stubs/[stubId].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ export const Stub = () => {
<Text mb="1">method</Text>
<Text mb="1">contentType</Text>
<Text mb="1">statusCode</Text>
<Text mb="1">sleep</Text>
<Text>response</Text>
</Flex>
</Box>
Expand All @@ -132,6 +133,7 @@ export const Stub = () => {
<Text mb="1">{stub.method}</Text>
<Text mb="1">{stub.contentType}</Text>
<Text mb="1">{stub.statusCode}</Text>
<Text mb="1">{stub.sleep} s</Text>
<Box w="100%" p="2" bgColor="#3c3c3c" color="#fff" borderRadius="lg">
<pre style={{ whiteSpace: "pre-wrap" }}>
{(() => {
Expand Down
3 changes: 3 additions & 0 deletions app/stubs/components/StubForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export function StubForm<S extends z.ZodType<any, any>>(props: FormProps<S>) {
{ value: "PUT", label: "PUT" },
{ value: "PATCH", label: "PATCH" },
{ value: "DELETE", label: "DELETE" },
{ value: "OPTIONS", label: "OPTIONS" },
]}
/>
<LabeledSelectField
Expand All @@ -33,6 +34,7 @@ export function StubForm<S extends z.ZodType<any, any>>(props: FormProps<S>) {
{ value: "application/json", label: "application/json" },
{ value: "application/xml", label: "application/xml" },
{ value: "text/plain", label: "text/plain" },
{ value: "text/json", label: "text/json" },
{ value: "text/html", label: "text/html" },
{ value: "text/javascript", label: "text/javascript" },
{ value: "text/css", label: "text/css" },
Expand All @@ -41,6 +43,7 @@ export function StubForm<S extends z.ZodType<any, any>>(props: FormProps<S>) {
]}
/>
<LabeledTextField name="statusCode" label="StatusCode" placeholder="200" />
<LabeledTextField type="number" name="sleep" label="Sleep(seconds)" placeholder="0" />
<LabeledTextAreaField name="response" label="Response" placeholder="Response" />
</Form>
)
Expand Down
4 changes: 3 additions & 1 deletion app/stubs/mutations/createStub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ const CreateStub = z.object({
path: z.string().regex(/^\/.*[^\/]$/, {
message: "Slashes are only allowed at the beginning of a path and in the middle of a path.",
}),
method: z.enum(["GET", "POST", "PUT", "PATCH", "DELETE"]),
method: z.enum(["GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"]),
contentType: z.enum([
"application/json",
"application/xml",
"text/plain",
"text/json",
"text/html",
"text/javascript",
"text/css",
Expand All @@ -23,6 +24,7 @@ const CreateStub = z.object({
.string()
.regex(/^\d{3}$/, { message: "The status code must be a three-digit number." }),
response: z.string().default(""),
sleep: z.number().min(0).default(0),
projectId: z.number(),
})

Expand Down
4 changes: 3 additions & 1 deletion app/stubs/mutations/updateStub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ const UpdateStub = z.object({
path: z.string().regex(/^\/.*[^\/]$/, {
message: "Slashes are only allowed at the beginning of a path and in the middle of a path.",
}),
method: z.enum(["GET", "POST", "PUT", "PATCH", "DELETE"]),
method: z.enum(["GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"]),
contentType: z.enum([
"application/json",
"application/xml",
"text/plain",
"text/json",
"text/html",
"text/javascript",
"text/css",
Expand All @@ -23,6 +24,7 @@ const UpdateStub = z.object({
.string()
.regex(/^\d{3}$/, { message: "The status code must be a three-digit number." }),
response: z.string(),
sleep: z.number().min(0),
})

export default resolver.pipe(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ CREATE TABLE "Stub" (
"contentType" TEXT NOT NULL,
"statusCode" TEXT NOT NULL,
"response" TEXT NOT NULL,
"sleep" INTEGER NOT NULL,
"projectId" INTEGER NOT NULL,
CONSTRAINT "Stub_projectId_fkey" FOREIGN KEY ("projectId") REFERENCES "Project" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);
Expand Down
1 change: 1 addition & 0 deletions db/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ model Stub {
contentType String
statusCode String
response String
sleep Int
project Project @relation(fields: [projectId], references: [id], onDelete: Cascade)
projectId Int
}

0 comments on commit 078456a

Please sign in to comment.