-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: inject into data-cleaner 4.2 namespace export
BREAKING CHANGE: injecting into data-cleaner is now opttional and doesn't go into root export
- Loading branch information
1 parent
cc317a3
commit 08c9d23
Showing
11 changed files
with
437 additions
and
109 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
coverage: false | ||
node-arg: ["-r", "@swc-node/register"] | ||
test-regex: "\\.test\\.ts$" |
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
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,63 @@ | ||
import { cleanAny, Cleaner, ValidationError } from "data-cleaner" | ||
import { Files } from "formidable" | ||
import createError from "http-errors" | ||
import Koa from "koa" | ||
|
||
export interface CleanKoaSchema<BodyT, ReturnT> { | ||
body?: Cleaner<BodyT, any> // TODO: any -> type of ctx.request.body | ||
files?: Cleaner<Files> | ||
clean?: Cleaner<ReturnT, CleanKoaRequest<BodyT>> | ||
errorCode?: number | ||
} | ||
|
||
export interface CleanKoaRequest<BodyT> { | ||
body: BodyT | ||
files: Files // TODO: allow this to be optional | ||
} | ||
|
||
export function cleanKoa< | ||
BodyT = any, | ||
StateT = any, | ||
ContextT extends Koa.ParameterizedContext<StateT> = Koa.ParameterizedContext<StateT>, | ||
ReturnT = CleanKoaRequest<BodyT> | ||
>(schema: CleanKoaSchema<BodyT, ReturnT>): Cleaner<ReturnT, ContextT> { | ||
return cleanAny<ReturnT, ContextT>({ | ||
async clean(ctx, opts) { | ||
try { | ||
let res: any = {} | ||
if (schema.body) { | ||
res.body = await schema.body(ctx.request.body, opts) | ||
} | ||
if (schema.files) { | ||
res.files = await schema.files(ctx.request.files, opts) | ||
} | ||
if (schema.clean) { | ||
res = await schema.clean(res, opts) | ||
} | ||
return res | ||
} catch (err) { | ||
if (err instanceof ValidationError) { | ||
const http_error = createError<number>( | ||
400, | ||
JSON.stringify({ errors: err.messages || err.errors }), | ||
{ | ||
headers: { "Content-Type": "application/json" }, | ||
} | ||
) | ||
if (schema.errorCode) { | ||
// avoid createError deprecation warning for status < 400 | ||
http_error.status = schema.errorCode | ||
} | ||
// prevent Koa from resetting content-type, see https://github.com/koajs/koa/issues/787 | ||
Object.defineProperty(ctx.response, "type", { | ||
set() { | ||
// do nothing. | ||
}, | ||
}) | ||
throw http_error | ||
} | ||
throw err | ||
} | ||
}, | ||
}) | ||
} |
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 |
---|---|---|
@@ -1,69 +1 @@ | ||
import * as clean from "data-cleaner" | ||
import { Cleaner } from "data-cleaner" | ||
import { Files } from "formidable" | ||
import createError from "http-errors" | ||
import Koa from "koa" | ||
|
||
export interface KoaSchema<BodyT, ReturnT> { | ||
body?: Cleaner<BodyT, any> // TODO: any -> type of ctx.request.body | ||
files?: Cleaner<Files> | ||
clean?: Cleaner<ReturnT, CleanKoaRequest<BodyT>> | ||
errorCode?: number | ||
} | ||
|
||
export interface CleanKoaRequest<BodyT> { | ||
body: BodyT | ||
files: Files // TODO: allow this to be optional | ||
} | ||
|
||
export default function clean_koa< | ||
BodyT = any, | ||
StateT = any, | ||
ContextT extends Koa.ParameterizedContext<StateT> = Koa.ParameterizedContext<StateT>, | ||
ReturnT = CleanKoaRequest<BodyT> | ||
>(schema: KoaSchema<BodyT, ReturnT>): Cleaner<ReturnT, ContextT> { | ||
return clean.any<ReturnT, ContextT>({ | ||
async clean(ctx, opts) { | ||
try { | ||
let res: any = {} | ||
if (schema.body) { | ||
res.body = await schema.body(ctx.request.body, opts) | ||
} | ||
if (schema.files) { | ||
res.files = await schema.files(ctx.request.files, opts) | ||
} | ||
if (schema.clean) { | ||
res = await schema.clean(res, opts) | ||
} | ||
return res | ||
} catch (err) { | ||
if (err instanceof clean.ValidationError) { | ||
const http_error = createError<number>( | ||
400, | ||
JSON.stringify({ errors: err.messages || err.errors }), | ||
{ | ||
headers: { "Content-Type": "application/json" }, | ||
} | ||
) | ||
// avoid createError deprecation warning for status < 400 | ||
http_error.status = schema.errorCode || 200 | ||
// prevent Koa from resetting content-type, see https://github.com/koajs/koa/issues/787 | ||
Object.defineProperty(ctx.response, "type", { | ||
set() { | ||
// do nothing. | ||
}, | ||
}) | ||
throw http_error | ||
} | ||
throw err | ||
} | ||
}, | ||
}) | ||
} | ||
|
||
declare module "data-cleaner" { | ||
export const koa: typeof clean_koa | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-var-requires | ||
require("data-cleaner").koa = clean_koa | ||
export * from "./clean-koa" |
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,12 @@ | ||
import { clean } from "data-cleaner" | ||
|
||
import { cleanKoa } from "./clean-koa" | ||
|
||
declare module "data-cleaner" { | ||
// eslint-disable-next-line @typescript-eslint/no-namespace | ||
namespace clean { | ||
let koa: typeof cleanKoa | ||
} | ||
} | ||
|
||
clean.koa = cleanKoa |
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,11 @@ | ||
import "data-cleaner-koa/register" | ||
|
||
import { clean } from "data-cleaner" | ||
import tap from "tap" | ||
|
||
tap.test("simple", async (tap) => { | ||
const clean_request = clean.koa({ body: clean.integer() }) | ||
tap.strictSame(await clean_request({ request: { body: 123 } } as any), { | ||
body: 123, | ||
}) | ||
}) |
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,10 @@ | ||
import { clean } from "data-cleaner" | ||
import { cleanKoa } from "data-cleaner-koa" | ||
import tap from "tap" | ||
|
||
tap.test("simple", async (tap) => { | ||
const clean_request = cleanKoa({ body: clean.integer() }) | ||
tap.strictSame(await clean_request({ request: { body: 123 } } as any), { | ||
body: 123, | ||
}) | ||
}) |
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
Oops, something went wrong.