-
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.
Merge pull request #30 from duplojs/develop
[publish] 1.2.8
- Loading branch information
Showing
10 changed files
with
128 additions
and
85 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
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,4 @@ | ||
import duploZodAccelerator from "./types/duplo"; | ||
export * from "./types/duplo"; | ||
|
||
export default duploZodAccelerator; |
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
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,78 @@ | ||
import * as zod from "zod"; | ||
import {AbstractRoute, DuploConfig, DuploInstance, Process, Route} from "@duplojs/duplojs"; | ||
import {duploExtends, duploInject} from "@duplojs/editor-tools"; | ||
import packageJson from "../package.json"; | ||
import {ZodAccelerator} from "."; | ||
import {ZodAcceleratorError} from "./lib/error"; | ||
|
||
export * from "./lib/accelerator"; | ||
export * from "./lib/error"; | ||
export * from "./lib/parser"; | ||
|
||
declare module "@duplojs/duplojs" { | ||
interface Plugins { | ||
"@duplojs/zod-accelerator": {version: string} | ||
} | ||
} | ||
|
||
export default function duploZodAccelerator( | ||
instance: DuploInstance<DuploConfig>, | ||
params: Partial< | ||
Record< | ||
DuploConfig["environment"], | ||
boolean | ||
> | ||
> = {DEV: false, PROD: true} | ||
){ | ||
const environment = instance.config.environment; | ||
|
||
if(!params[environment]){ | ||
return; | ||
} | ||
|
||
instance.plugins["@duplojs/zod-accelerator"] = {version: packageJson.version}; | ||
|
||
const duploseHandler = (duplose: Route | Process | AbstractRoute) => { | ||
if(Object.keys(duplose.extracted).length === 0){ | ||
return; | ||
} | ||
|
||
Object.entries(duplose.extracted).forEach(([key, value]) => { | ||
if(!value){ | ||
return; | ||
} | ||
|
||
if(value instanceof zod.ZodType){ | ||
ZodAccelerator.build(value); | ||
} | ||
else { | ||
Object.entries(value).forEach(([key, deepValue]) => { | ||
ZodAccelerator.build(deepValue); | ||
}); | ||
} | ||
}); | ||
|
||
duploExtends(duplose, { | ||
ZodAcceleratorError, | ||
}); | ||
|
||
duploInject(duplose, ({}, d) => { | ||
d.stringDuploseFunction = d.stringDuploseFunction.replace( | ||
/\.parse\(request/g, | ||
".accelerator.parse(request" | ||
); | ||
d.stringDuploseFunction = d.stringDuploseFunction.replace( | ||
/error instanceof this\.ZodError/, | ||
"error instanceof this.extensions.ZodAcceleratorError" | ||
); | ||
}); | ||
}; | ||
|
||
instance.addHook("onDeclareRoute", duploseHandler); | ||
instance.addHook("onDeclareAbstractRoute", (abstractRoute) => { | ||
if(abstractRoute instanceof AbstractRoute){ | ||
duploseHandler(abstractRoute); | ||
} | ||
}); | ||
instance.addHook("onCreateProcess", duploseHandler); | ||
} |
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
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