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

feat(43): rework async #44

Merged
merged 1 commit into from
Sep 12, 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
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
Loading