-
Notifications
You must be signed in to change notification settings - Fork 213
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add readBodySafe and getQuerySafe helpers
- Loading branch information
1 parent
d9a9f76
commit 34c8373
Showing
6 changed files
with
278 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import type { Schema } from "@decs/typeschema"; | ||
import { assert } from "@decs/typeschema"; | ||
import { createError } from "src/error"; | ||
|
||
export const assertSchema = async ( | ||
schema: Schema<unknown>, | ||
payload: unknown, | ||
onError?: (err: any) => any | ||
) => { | ||
try { | ||
return await assert(schema, payload); | ||
} catch (error) { | ||
if (onError) { | ||
return onError(error); | ||
} | ||
throw createError({ statusCode: 500, statusMessage: "Assertion Error." }); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,193 @@ | ||
import supertest, { SuperTest, Test } from "supertest"; | ||
import { describe, it, expect, expectTypeOf, beforeEach } from "vitest"; | ||
import { z } from "zod"; | ||
import { | ||
createApp, | ||
toNodeListener, | ||
App, | ||
eventHandler, | ||
readBodySafe, | ||
createError, | ||
getQuerySafe, | ||
} from "../src"; | ||
|
||
describe("", () => { | ||
let app: App; | ||
let request: SuperTest<Test>; | ||
|
||
beforeEach(() => { | ||
app = createApp({ debug: true }); | ||
request = supertest(toNodeListener(app)); | ||
}); | ||
|
||
describe("query validation", () => { | ||
it("can parse and return safe query params", async () => { | ||
app.use( | ||
"/", | ||
eventHandler(async (event) => { | ||
const schema = z.object({ | ||
bool: z.string(), | ||
name: z.string(), | ||
number: z.string(), | ||
}); | ||
const query = await getQuerySafe(event, schema); | ||
expectTypeOf(query).toMatchTypeOf<{ | ||
bool: string; | ||
name: string; | ||
number: string; | ||
}>(); | ||
return query; | ||
}) | ||
); | ||
const result = await request.get( | ||
"/api/test?bool=true&name=string&number=1" | ||
); | ||
expect(result.statusCode).toBe(200); | ||
expect(result.body).toMatchObject({ | ||
bool: "true", | ||
name: "string", | ||
number: "1", | ||
}); | ||
}); | ||
}); | ||
|
||
describe("body validation", () => { | ||
it("can parse and return safe x-www-form-urlencoded data", async () => { | ||
app.use( | ||
"/", | ||
eventHandler(async (event) => { | ||
const schema = z.object({ | ||
firstName: z.string(), | ||
lastName: z.string(), | ||
}); | ||
const data = await readBodySafe(event, schema); | ||
expectTypeOf(data).toMatchTypeOf<{ | ||
firstName: string; | ||
lastName: string; | ||
}>(); | ||
return { ...data }; | ||
}) | ||
); | ||
|
||
const result = await request | ||
.post("/api/test") | ||
.set("content-type", "application/x-www-form-urlencoded") | ||
.send("firstName=John&lastName=Doe"); | ||
|
||
expect(result.status).toBe(200); | ||
expect(result.body).toMatchObject({ firstName: "John", lastName: "Doe" }); | ||
}); | ||
|
||
it("can parse and return safe json data", async () => { | ||
app.use( | ||
"/", | ||
eventHandler(async (event) => { | ||
const schema = z.object({ | ||
firstName: z.string(), | ||
lastName: z.string(), | ||
}); | ||
const data = await readBodySafe(event, schema); | ||
expectTypeOf(data).toMatchTypeOf<{ | ||
firstName: string; | ||
lastName: string; | ||
}>(); | ||
return { ...data }; | ||
}) | ||
); | ||
|
||
const result = await request | ||
.post("/api/test") | ||
.set("content-type", "application/json") | ||
.send({ firstName: "John", lastName: "Doe", age: 30 }); | ||
|
||
expect(result.status).toBe(200); | ||
expect(result.body).toMatchObject({ | ||
firstName: "John", | ||
lastName: "Doe", | ||
}); | ||
}); | ||
|
||
it("can throw an error on schema mismatch", async () => { | ||
app.use( | ||
"/", | ||
eventHandler(async (event) => { | ||
const schema = z.object({ | ||
firstName: z.string(), | ||
lastName: z.number(), | ||
}); | ||
const data = await readBodySafe(event, schema); | ||
return { ...data }; | ||
}) | ||
); | ||
|
||
const result = await request | ||
.post("/api/test") | ||
.set("content-type", "application/x-www-form-urlencoded") | ||
.send("firstName=John&lastName=Doe"); | ||
|
||
expect(result.status).toBe(500); | ||
expect(result.body).toMatchObject({ statusCode: 500 }); | ||
}); | ||
|
||
it("can throw a custom error when assertion fails", async () => { | ||
app.use( | ||
"/", | ||
eventHandler(async (event) => { | ||
const schema = z.object({ | ||
firstName: z.string(), | ||
lastName: z.number(), | ||
}); | ||
const data = await readBodySafe(event, schema, () => { | ||
throw createError({ | ||
statusMessage: "Invalid data", | ||
statusCode: 400, | ||
}); | ||
}); | ||
return { ...data }; | ||
}) | ||
); | ||
|
||
const result = await request | ||
.post("/api/test") | ||
.set("content-type", "application/x-www-form-urlencoded") | ||
.send("firstName=John&lastName=Doe"); | ||
|
||
expect(result.status).toBe(400); | ||
expect(result.body).toMatchObject({ | ||
statusMessage: "Invalid data", | ||
statusCode: 400, | ||
}); | ||
}); | ||
|
||
it("can throw with a custom validator schema", async () => { | ||
app.use( | ||
"/", | ||
eventHandler(async (event) => { | ||
const data = await readBodySafe(event, (body) => { | ||
if ( | ||
body && | ||
typeof body === "object" && | ||
"firstName" in body && | ||
"lastName" in body && | ||
"age" in body | ||
) { | ||
return body; | ||
} | ||
throw new TypeError("Custom Error"); | ||
}); | ||
return data; | ||
}) | ||
); | ||
|
||
const result = await request | ||
.post("/api/test") | ||
.set("content-type", "application/x-www-form-urlencoded") | ||
.send("firstName=John&lastName=Doe"); | ||
|
||
expect(result.status).toBe(500); | ||
expect(result.body).toMatchObject({ | ||
statusCode: 500, | ||
}); | ||
}); | ||
}); | ||
}); |