Skip to content

Commit

Permalink
refactor(package): small refactoring + tests coverage improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
mrspartak committed Jun 18, 2024
1 parent 1d81dda commit 79858e2
Show file tree
Hide file tree
Showing 14 changed files with 227 additions and 105 deletions.
10 changes: 8 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
"check:format": "biome format --write ./src",
"check:lint": "biome lint ./src",
"local:node": "tsx local/main.ts",
"prepublishOnly": "node ./bin/prepublish.js && pnpm build && agadoo && pnpm test",
"release": "cross-env RELEASE_MODE=true np --no-tests"
"prepublishOnly": "node ./bin/prepublish.js && pnpm build && agadoo",
"release": "cross-env RELEASE_MODE=true np",
"doc": "typedoc --logLevel Verbose"
},
"author": "Spartak Kagramanyan",
"license": "MIT",
Expand All @@ -28,10 +29,15 @@
"agadoo": "^3.0.0",
"cross-env": "^7.0.3",
"lefthook": "^1.6.15",
"myzod": "^1.11.0",
"np": "^10.0.5",
"superstruct": "^1.0.4",
"tsup": "^8.1.0",
"tsx": "^4.15.4",
"typedoc": "^0.25.13",
"typedoc-material-theme": "^1.0.2",
"typedoc-plugin-coverage": "^3.2.0",
"typedoc-plugin-missing-exports": "^2.3.0",
"typescript": "^5.4.5",
"valibot": "^0.32.0",
"vitest": "^1.6.0",
Expand Down
99 changes: 99 additions & 0 deletions pnpm-lock.yaml

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

6 changes: 3 additions & 3 deletions src/factories/jsonFileFactory.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import type { Parser } from "../types/parser.js";
import type { Validator } from "../types/validator.js";
import { Exception } from "../util/exception.js";
import { deepMerge } from "../util/mergeObjects.js";
import { readFile } from "../util/readFile.js";
import { fromObject } from "./objectFactory.js";

export async function fromJSONFile<$Parser extends Parser>({
export async function fromJSONFile<$Validator extends Validator>({
path,
schema,
}: {
path: string | string[];
schema: $Parser;
schema: $Validator;
}) {
const paths = Array.isArray(path) ? path : [path];

Expand Down
6 changes: 3 additions & 3 deletions src/factories/objectFactory.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import type { Parser } from "../types/parser.js";
import type { Validator } from "../types/validator.js";
import { validate } from "../util/validation.js";

export async function fromObject<$Parser extends Parser>({
export async function fromObject<$Validator extends Validator>({
data,
schema,
}: {
data: unknown;
schema: $Parser;
schema: $Validator;
}) {
return validate(data, schema);
}
6 changes: 3 additions & 3 deletions src/factories/urlFactory.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import type { Parser } from "../types/parser.js";
import type { Validator } from "../types/validator.js";
import { Exception } from "../util/exception.js";
import { deepMerge } from "../util/mergeObjects.js";
import { fromObject } from "./objectFactory.js";

export async function fromURL<$Parser extends Parser>({
export async function fromURL<$Validator extends Validator>({
url,
schema,
}: {
url: string | string[];
schema: $Parser;
schema: $Validator;
}) {
const urls = Array.isArray(url) ? url : [url];

Expand Down
2 changes: 1 addition & 1 deletion src/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from "../index.js";
export * from "./parser.js";
export * from "./validator.js";
54 changes: 0 additions & 54 deletions src/types/parser.d.ts

This file was deleted.

52 changes: 52 additions & 0 deletions src/types/validator.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// KUDOS to trpc for the inspiration

export type ValidatorZod<TI, TPI> = {
_input: TI;
_output: TPI;
};

export type ValidatorValibot<TI, TPI> = {
types?: {
input: TI;
output: TPI;
};
};

export type ValidatorMyZod<TI> = {
parse: (input: any) => TI;
};

export type ValidatorSuperstruct<TI> = {
create: (input: unknown) => TI;
};

export type ValidatorCustomValidator<TI> = (input: unknown) => Promise<TI> | TI;

export type ValidatorYup<TI> = {
validateSync: (input: unknown) => TI;
};

export type ValidatorWithoutInput<TI> =
| ValidatorCustomValidator<TI>
| ValidatorMyZod<TI>
| ValidatorSuperstruct<TI>
| ValidatorYup<TI>;

export type ValidatorWithInputOutput<TI, TPI> = ValidatorZod<TI, TPI> | ValidatorValibot<TI, TPI>;

export type Validator = ValidatorWithInputOutput<any, any> | ValidatorWithoutInput<any>;

export type inferValidator<$Validator extends Validator> = $Validator extends ValidatorWithInputOutput<
infer $TIn,
infer $TOut
>
? {
in: $TIn;
out: $TOut;
}
: $Validator extends ValidatorWithoutInput<infer $InOut>
? {
in: $InOut;
out: $InOut;
}
: never;
Loading

0 comments on commit 79858e2

Please sign in to comment.