From 723255ef95287a9a6ae505757a8e35e6960c46ea Mon Sep 17 00:00:00 2001 From: Kodai Aoyama Date: Tue, 11 Apr 2023 11:29:23 +0900 Subject: [PATCH 1/3] =?UTF-8?q?cookie=E8=A8=AD=E5=AE=9A=E3=82=92=E8=BF=BD?= =?UTF-8?q?=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/[[...slug]].ts | 17 ++++++++++- app/lib/sqlite3Client.ts | 1 + app/pages/projects/[projectId].tsx | 9 +++++- app/pages/stubs/[stubId].tsx | 32 ++++++++++++++++++++- app/stubs/components/StubForm.tsx | 6 ++++ app/stubs/mutations/createStub.ts | 1 + app/stubs/mutations/updateStub.ts | 1 + db/migrations/20230411015209_/migration.sql | 28 ++++++++++++++++++ db/schema.prisma | 1 + 9 files changed, 93 insertions(+), 3 deletions(-) create mode 100644 db/migrations/20230411015209_/migration.sql diff --git a/app/api/[[...slug]].ts b/app/api/[[...slug]].ts index db7521e..18b9f80 100644 --- a/app/api/[[...slug]].ts +++ b/app/api/[[...slug]].ts @@ -34,6 +34,7 @@ const Handler: BlitzApiHandler = async (req: BlitzApiRequest, res: BlitzApiRespo statusCode, sleep, response, + cookies, logs, ntimesError, ntimesErrorStatusCode, @@ -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 diff --git a/app/lib/sqlite3Client.ts b/app/lib/sqlite3Client.ts index 539f1c5..b188a35 100644 --- a/app/lib/sqlite3Client.ts +++ b/app/lib/sqlite3Client.ts @@ -30,6 +30,7 @@ type StubTableType = { contentType: string statusCode: string response: string + cookies: string sleep: number logs: string ntimesError: number diff --git a/app/pages/projects/[projectId].tsx b/app/pages/projects/[projectId].tsx index 9b1dae0..b574054 100644 --- a/app/pages/projects/[projectId].tsx +++ b/app/pages/projects/[projectId].tsx @@ -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, } } diff --git a/app/pages/stubs/[stubId].tsx b/app/pages/stubs/[stubId].tsx index 35da86a..0d20b94 100644 --- a/app/pages/stubs/[stubId].tsx +++ b/app/pages/stubs/[stubId].tsx @@ -99,7 +99,7 @@ export const Stub = () => { _hover={{ bg: "teal.400", borderColor: "teal.400" }} leftIcon={} 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( @@ -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, @@ -166,6 +173,20 @@ export const Stub = () => { statusCode + {stub.cookies !== "" && + stub.cookies + .replaceAll("\r", "") + .split("\n") + .map((_, i) => { + if (i === 0) { + return ( + + cookies + + ) + } + return + })} {stub.sleep !== 0 && ( <> @@ -217,6 +238,15 @@ export const Stub = () => { {stub.statusCode} + {stub.cookies !== "" && + stub.cookies + .replaceAll("\r", "") + .split("\n") + .map((v, i) => ( + + {v} + + ))} {stub.sleep !== 0 && ( <> diff --git a/app/stubs/components/StubForm.tsx b/app/stubs/components/StubForm.tsx index cc8e47d..8206d4c 100644 --- a/app/stubs/components/StubForm.tsx +++ b/app/stubs/components/StubForm.tsx @@ -44,6 +44,12 @@ export function StubForm>(props: FormProps) { ]} /> + diff --git a/app/stubs/mutations/createStub.ts b/app/stubs/mutations/createStub.ts index c9ef209..6669d8f 100644 --- a/app/stubs/mutations/createStub.ts +++ b/app/stubs/mutations/createStub.ts @@ -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), diff --git a/app/stubs/mutations/updateStub.ts b/app/stubs/mutations/updateStub.ts index 0ec8dcf..2b60d60 100644 --- a/app/stubs/mutations/updateStub.ts +++ b/app/stubs/mutations/updateStub.ts @@ -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), diff --git a/db/migrations/20230411015209_/migration.sql b/db/migrations/20230411015209_/migration.sql new file mode 100644 index 0000000..90ae601 --- /dev/null +++ b/db/migrations/20230411015209_/migration.sql @@ -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; diff --git a/db/schema.prisma b/db/schema.prisma index f51817e..d574d61 100644 --- a/db/schema.prisma +++ b/db/schema.prisma @@ -86,6 +86,7 @@ model Stub { method String contentType String statusCode String + cookies String @default("") response String sleep Int @default(0) logs String From df031855ce5f96b21a50663fa3bf49c851a9a61b Mon Sep 17 00:00:00 2001 From: Kodai Aoyama Date: Tue, 11 Apr 2023 11:31:59 +0900 Subject: [PATCH 2/3] update version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 2065fed..8965040 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "co-metub", - "version": "1.0.26", + "version": "1.0.27", "license": "MIT", "engines": { "node": "18", From 02e212d7052f7c58dfb4dee99b4f0c8e6fffc6b3 Mon Sep 17 00:00:00 2001 From: Kodai Aoyama Date: Tue, 11 Apr 2023 11:36:15 +0900 Subject: [PATCH 3/3] fix --- app/pages/stubs/[stubId].tsx | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/pages/stubs/[stubId].tsx b/app/pages/stubs/[stubId].tsx index 0d20b94..37daf90 100644 --- a/app/pages/stubs/[stubId].tsx +++ b/app/pages/stubs/[stubId].tsx @@ -177,6 +177,7 @@ export const Stub = () => { stub.cookies .replaceAll("\r", "") .split("\n") + .filter((v) => v !== "") .map((_, i) => { if (i === 0) { return ( @@ -242,6 +243,7 @@ export const Stub = () => { stub.cookies .replaceAll("\r", "") .split("\n") + .filter((v) => v !== "") .map((v, i) => ( {v}