Skip to content

Commit

Permalink
Merge pull request #78 from solaoi/feature_add-cookie-settings
Browse files Browse the repository at this point in the history
cookie設定を追加
  • Loading branch information
solaoi authored Apr 11, 2023
2 parents 936d57f + 02e212d commit d7fbd55
Show file tree
Hide file tree
Showing 10 changed files with 96 additions and 4 deletions.
17 changes: 16 additions & 1 deletion app/api/[[...slug]].ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const Handler: BlitzApiHandler = async (req: BlitzApiRequest, res: BlitzApiRespo
statusCode,
sleep,
response,
cookies,
logs,
ntimesError,
ntimesErrorStatusCode,
Expand Down Expand Up @@ -86,7 +87,21 @@ const Handler: BlitzApiHandler = async (req: BlitzApiRequest, res: BlitzApiRespo
res.status(Number(ntimesErrorStatusCode)).end()
return
}
res.status(Number(statusCode)).setHeader("Content-Type", contentType).end(response)
if (cookies !== "") {
res
.status(Number(statusCode))
.setHeader("Content-Type", contentType)
.setHeader(
"Set-Cookie",
cookies
.replaceAll("\r", "")
.split("\n")
.filter((v) => v !== "")
)
.end(response)
} else {
res.status(Number(statusCode)).setHeader("Content-Type", contentType).end(response)
}
}

export default Handler
1 change: 1 addition & 0 deletions app/lib/sqlite3Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type StubTableType = {
contentType: string
statusCode: string
response: string
cookies: string
sleep: number
logs: string
ntimesError: number
Expand Down
9 changes: 8 additions & 1 deletion app/pages/projects/[projectId].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -165,13 +165,20 @@ export const Project = () => {
onClick={async () => {
const { stubs } = await invoke(getStubs, { where: { projectId: projectId } })
const stubsForExport = stubs.map(
({ path, method, contentType, statusCode, response, sleep }) => {
({ path, method, contentType, statusCode, response, cookies, sleep }) => {
return {
path: `/api${project.basePath}${path}`,
method,
contentType,
statusCode,
response,
cookies:
cookies === ""
? null
: cookies
.replaceAll("\r", "")
.split("\n")
.filter((v) => v !== ""),
sleep: sleep * 1000,
}
}
Expand Down
34 changes: 33 additions & 1 deletion app/pages/stubs/[stubId].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export const Stub = () => {
_hover={{ bg: "teal.400", borderColor: "teal.400" }}
leftIcon={<DownloadIcon />}
onClick={async () => {
const { path, method, contentType, statusCode, response, sleep } = stub
const { path, method, contentType, statusCode, response, cookies, sleep } = stub
const blob = new Blob(
[
JSON.stringify(
Expand All @@ -109,6 +109,13 @@ export const Stub = () => {
contentType,
statusCode,
response,
cookies:
cookies === ""
? null
: cookies
.replaceAll("\r", "")
.split("\n")
.filter((v) => v !== ""),
sleep: sleep * 1000,
},
null,
Expand Down Expand Up @@ -166,6 +173,21 @@ export const Stub = () => {
<Text mb="1" h="1.5rem">
statusCode
</Text>
{stub.cookies !== "" &&
stub.cookies
.replaceAll("\r", "")
.split("\n")
.filter((v) => v !== "")
.map((_, i) => {
if (i === 0) {
return (
<Text key={"cookie_title_" + i} mb="1" h="1.5rem">
cookies
</Text>
)
}
return <Text key={"cookie_title_" + i} mb="1" h="1.5rem"></Text>
})}
{stub.sleep !== 0 && (
<>
<Text mb="1" h="1.5rem">
Expand Down Expand Up @@ -217,6 +239,16 @@ export const Stub = () => {
<Text mb="1" h="1.5rem">
{stub.statusCode}
</Text>
{stub.cookies !== "" &&
stub.cookies
.replaceAll("\r", "")
.split("\n")
.filter((v) => v !== "")
.map((v, i) => (
<Text key={"cookie_" + i} mb="1" h="1.5rem">
{v}
</Text>
))}
{stub.sleep !== 0 && (
<>
<Text mb="1" h="1.5rem">
Expand Down
6 changes: 6 additions & 0 deletions app/stubs/components/StubForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ export function StubForm<S extends z.ZodType<any, any>>(props: FormProps<S>) {
]}
/>
<LabeledTextField name="statusCode" label="StatusCode" placeholder="200" />
<LabeledTextAreaField
name="cookies"
label="Cookies"
placeholder="KEY1=VAL1; path=/; expires=Sat, 24 Nov 2012 08:34:29 GMT&#13;KEY2=VAL2; path=/; expires=Sat, 24 Nov 2012 08:34:29 GMT"
rows="4"
/>
<LabeledTextAreaField name="response" label="Response" placeholder="Response" rows="5" />
<LabeledTextAreaField name="memo" label="Memo" placeholder="Any additional comments" />
<Card heading="Optional" bgColor="#E2E8F0">
Expand Down
1 change: 1 addition & 0 deletions app/stubs/mutations/createStub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const CreateStub = z.object({
statusCode: z
.string()
.regex(/^\d{3}$/, { message: "The status code must be a three-digit number." }),
cookies: z.string().default(""),
response: z.string().default(""),
sleep: z.number().min(0).default(0),
ntimesError: z.number().min(0).default(0),
Expand Down
1 change: 1 addition & 0 deletions app/stubs/mutations/updateStub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const UpdateStub = z.object({
statusCode: z
.string()
.regex(/^\d{3}$/, { message: "The status code must be a three-digit number." }),
cookies: z.string().default(""),
response: z.string(),
sleep: z.number().min(0),
ntimesError: z.number().min(0).default(0),
Expand Down
28 changes: 28 additions & 0 deletions db/migrations/20230411015209_/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
-- RedefineTables
PRAGMA foreign_keys=OFF;
CREATE TABLE "new_Stub" (
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" DATETIME NOT NULL,
"createdBy" TEXT NOT NULL,
"updatedBy" TEXT NOT NULL,
"path" TEXT NOT NULL,
"method" TEXT NOT NULL,
"contentType" TEXT NOT NULL,
"statusCode" TEXT NOT NULL,
"cookies" TEXT NOT NULL DEFAULT '',
"response" TEXT NOT NULL,
"sleep" INTEGER NOT NULL DEFAULT 0,
"logs" TEXT NOT NULL,
"ntimesError" INTEGER NOT NULL DEFAULT 0,
"ntimesErrorStatusCode" TEXT NOT NULL DEFAULT '500',
"ntimesErrorCounter" INTEGER NOT NULL DEFAULT 0,
"memo" TEXT NOT NULL DEFAULT '',
"projectId" INTEGER NOT NULL,
CONSTRAINT "Stub_projectId_fkey" FOREIGN KEY ("projectId") REFERENCES "Project" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);
INSERT INTO "new_Stub" ("contentType", "createdAt", "createdBy", "id", "logs", "memo", "method", "ntimesError", "ntimesErrorCounter", "ntimesErrorStatusCode", "path", "projectId", "response", "sleep", "statusCode", "updatedAt", "updatedBy") SELECT "contentType", "createdAt", "createdBy", "id", "logs", "memo", "method", "ntimesError", "ntimesErrorCounter", "ntimesErrorStatusCode", "path", "projectId", "response", "sleep", "statusCode", "updatedAt", "updatedBy" FROM "Stub";
DROP TABLE "Stub";
ALTER TABLE "new_Stub" RENAME TO "Stub";
PRAGMA foreign_key_check;
PRAGMA foreign_keys=ON;
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 {
method String
contentType String
statusCode String
cookies String @default("")
response String
sleep Int @default(0)
logs String
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "co-metub",
"version": "1.0.26",
"version": "1.0.27",
"license": "MIT",
"engines": {
"node": "18",
Expand Down

0 comments on commit d7fbd55

Please sign in to comment.