Skip to content

Commit

Permalink
Merge pull request #44 from duplojs/feat/43
Browse files Browse the repository at this point in the history
feat(43): rework async
  • Loading branch information
mathcovax authored Sep 12, 2024
2 parents 96a293c + 826a007 commit febf1f1
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# zod-accelerator
# @duplojs/zod-accelerator
[![NPM version](https://img.shields.io/npm/v/@duplojs/zod-accelerator)](https://www.npmjs.com/package/@duplojs/zod-accelerator)

## Instalation
Expand Down
6 changes: 4 additions & 2 deletions scripts/accelerator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as zod from "zod";
import { z as zod } from "zod";
import { ZodAcceleratorContent } from "./content";
import { ZodAcceleratorParser } from "./parser";
import { ZodAcceleratorError } from "./error";
Expand Down Expand Up @@ -75,7 +75,9 @@ export abstract class ZodAccelerator {
ZodAccelerator.accelerators.push(new zodAccelerator());
}

public static injectZod<Z extends typeof zod>(zod: Z) {
public static injectZod<
Z extends typeof zod,
>(zod: Z) {
this.zod = zod;
}
}
1 change: 0 additions & 1 deletion scripts/accelerators/branded.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import * as zod from "zod";
import { ZodAnyAccelerator } from "./any";
import { ZodAccelerator } from "../accelerator";

Expand Down
8 changes: 3 additions & 5 deletions scripts/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,21 +98,19 @@ export class ZodAcceleratorContent {
}

public toFunction() {
const isAsync = !!this.content.find((line) => line.includes("await"));

const functionContent = this.replacer(
[
`(${isAsync ? "async " : ""}function ($input){`,
"function ($input){",
/* js */"const ZodAcceleratorError = this.ZodAcceleratorError;",
...this.content,
/* js */"return {success: true, data: $input};",
"})",
"}",
]
.join("\n"),
)
.replace(/\$path\.?/g, ".");

const fnc: AnyFunction = shadowEval(functionContent);
const fnc: AnyFunction = shadowEval(`(${functionContent.includes("await") ? "async " : ""}${functionContent})`);

return fnc.bind({
ZodAcceleratorError: ZodAcceleratorError,
Expand Down
6 changes: 5 additions & 1 deletion scripts/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@ export type AcceleratorSafeParseError<
};

export class ZodAcceleratorParser<
_zodSchema extends zod.ZodType,
_zodSchema extends zod.ZodType = zod.ZodType,
_output extends _zodSchema["_output"] = _zodSchema["_output"],
> {
public get isAsync() {
return this.buidledParse.constructor.name === "AsyncFunction";
}

public constructor(
private buidledParse: (input: unknown) => PromiseOrNot<AcceleratorSafeParseError<_output>>,
) { }
Expand Down

0 comments on commit febf1f1

Please sign in to comment.