Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix] remove process var #29

Merged
merged 1 commit into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ const outputData = zodAccelerateSchema.parse(inputData);
## Implémentation dans duplojs
```ts
import Duplo, {zod} from "@duplojs/duplojs";
import {ZodAccelerator} from "@duplojs/zod-accelerator";
import duploZodAccelerator from "@duplojs/zod-accelerator/duplo";

const duplo = Duplo({port: 1506, host: "localhost", environment: "DEV"});

duplo.use(ZodAccelerator.duplojs, {DEV: true});
duplo.use(duploZodAccelerator, {DEV: true});
```
4 changes: 4 additions & 0 deletions duplo.d.ts
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;
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 12 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,23 @@
"json-schema",
"parser",
"validate",
"typecheck"
"typecheck",
"zod",
"faster",
"zod faster"
],
"types": "./types/index.d.ts",
"exports": {
"default": {
".": {
"import": "./dist/index.mjs",
"require": "./dist/index.cjs",
"types": "./types/index.d.ts"
}
},
"./duplo": {
"types": "./duplo.d.ts",
"import": "./dist/duplo.mjs",
"require": "./dist/duplo.cjs"
}
},
"files": [
"dist",
Expand All @@ -35,7 +43,7 @@
"test:types": "tsc"
},
"dependencies": {
"@duplojs/duplojs": ">=1.1.11 <2.0.0",
"@duplojs/duplojs": ">=1.8.20 <2.0.0",
"@duplojs/editor-tools": "^1.1.3 <2.0.0",
"zod": "^3.22.4"
},
Expand Down
21 changes: 21 additions & 0 deletions rollup.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,25 @@ export default defineConfig([
json(),
]
},
{
input: "scripts/duplo.ts",
output: [
{
file: "dist/duplo.mjs",
format: "esm"
},
{
file: "dist/duplo.cjs",
format: "cjs",
}
],
plugins: [
typescript({
exclude: ["node_mudules"],
tsconfig: "tsconfig.json",
module: "ESNext",
}),
json(),
]
},
]);
78 changes: 78 additions & 0 deletions scripts/duplo.ts
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);
}
2 changes: 2 additions & 0 deletions scripts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,5 @@ import "./lib/accelerators/lazy";
export * from "./lib/accelerator";
export * from "./lib/error";
export * from "./lib/parser";

export {ZodAccelerator as default} from "./lib/accelerator";
71 changes: 0 additions & 71 deletions scripts/lib/accelerator.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,14 @@
import * as zod from "zod";
import {ZodAcceleratorContent} from "./content";
import {ZodAcceleratorParser} from "./parser";
import {AbstractRoute, DuploConfig, DuploInstance, Process, Route} from "@duplojs/duplojs";
import {duploExtends, duploInject} from "@duplojs/editor-tools";
import {ZodAcceleratorError} from "./error";
import packageJson from "../../package.json";

declare module "zod" {
interface ZodType{
accelerator?: ZodAcceleratorParser<this>;
}
}

declare module "@duplojs/duplojs" {
interface Plugins {
"@duplojs/zod-accelerator": {version: string}
}
}

export abstract class ZodAccelerator{
protected static accelerators: ZodAccelerator[] = [];

Expand Down Expand Up @@ -77,66 +68,4 @@ export abstract class ZodAccelerator{
public static autoInstance(zodAccelerator: { new(...args: any[]): ZodAccelerator }){
ZodAccelerator.accelerators.push(new zodAccelerator());
}

public static duplojs(
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);
}
}
3 changes: 2 additions & 1 deletion scripts/lib/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export class ZodAcceleratorContent{
"$this.": "this.{id}_",
"$id": "{id}",
};
private static inc = 0;

readonly id = ZodAcceleratorContent.generateId();
content: string[] = [];
Expand Down Expand Up @@ -107,6 +108,6 @@ export class ZodAcceleratorContent{
}

private static generateId(){
return `duploj$${process.hrtime.bigint()}`;
return `duploj$${(this.inc++)}`;
}
}
6 changes: 3 additions & 3 deletions test/E2E/route/route.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import Duplo, {zod} from "@duplojs/duplojs";
import {parentPort} from "worker_threads";
import {ZodAccelerator} from "../../../scripts";
import duploZodAccelerator from "../../../scripts/duplo";

const duplo = Duplo({port: 1506, host: "localhost", environment: "DEV"});

duplo.use(ZodAccelerator.duplojs, {DEV: true});
duplo.use(duploZodAccelerator, {DEV: true});

duplo.declareRoute("GET", "/test/1")
.extract({
Expand All @@ -20,7 +20,7 @@ duplo.declareRoute("POST", "/test/2")
.extract({
body: zod.object({
test1: zod.string()
})
}).passthrough()
})
.handler(({pickup}, res) => {
res.code(200).info("s").send(pickup("body"));
Expand Down
Loading