@jakubmazanec/zod-utils 1.0.1 → 1.0.2-a1d4551
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/utils/readFile.js
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
import fs from 'node:fs/promises';
|
|
2
2
|
export async function readFile(path, schema, options) {
|
|
3
|
-
|
|
4
|
-
let fileParser = ((_options = options) === null || _options === void 0 ? void 0 : _options.parser) ?? JSON.parse;
|
|
3
|
+
let fileParser = options?.parser ?? JSON.parse;
|
|
5
4
|
let rawJson = fileParser(await fs.readFile(path, {
|
|
6
5
|
encoding: 'utf8'
|
|
7
6
|
}));
|
|
8
|
-
if (
|
|
7
|
+
if (options?.throwOnZodError) {
|
|
9
8
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-return -- return type is actually defined via the function overload above
|
|
10
9
|
return schema.parse(rawJson);
|
|
11
10
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/utils/readFile.ts"],"sourcesContent":["import {type If} from '@jakubmazanec/ts-utils';\nimport {type PathLike} from 'node:fs';\nimport fs from 'node:fs/promises';\nimport {type z, type ZodTypeAny, type ZodError} from 'zod';\n\nexport interface ReadFileOptions {\n parser?: (rawString: string) => unknown;\n throwOnZodError?: boolean;\n}\n\nexport async function readFile<\n S extends ZodTypeAny,\n O extends ReadFileOptions | undefined = undefined,\n>(\n path: fs.FileHandle | PathLike,\n schema: S,\n options?: O,\n): Promise<\n O extends undefined\n ? z.infer<S> | ZodError<z.infer<S>>\n : If<NonNullable<O>['throwOnZodError'], true, z.infer<S>, z.infer<S> | ZodError<z.infer<S>>>\n>;\nexport async function readFile(\n path: fs.FileHandle | PathLike,\n schema: ZodTypeAny,\n options?: ReadFileOptions,\n) {\n let fileParser = options?.parser ?? JSON.parse;\n let rawJson = fileParser(await fs.readFile(path, {encoding: 'utf8'})) as unknown;\n\n if (options?.throwOnZodError) {\n // eslint-disable-next-line @typescript-eslint/no-unsafe-return -- return type is actually defined via the function overload above\n return schema.parse(rawJson);\n }\n\n let result = schema.safeParse(rawJson);\n\n // eslint-disable-next-line @typescript-eslint/no-unsafe-return -- return type is actually defined via the function overload above\n return result.success ? result.data : result.error;\n}\n"],"names":["fs","readFile","path","schema","options","fileParser","parser","JSON","parse","rawJson","encoding","throwOnZodError","result","safeParse","success","data","error"],"mappings":"AAEA,OAAOA,QAAQ,mBAAmB;AAoBlC,OAAO,eAAeC,SACpBC,IAA8B,EAC9BC,MAAkB,EAClBC,OAAyB;
|
|
1
|
+
{"version":3,"sources":["../../src/utils/readFile.ts"],"sourcesContent":["import {type If} from '@jakubmazanec/ts-utils';\nimport {type PathLike} from 'node:fs';\nimport fs from 'node:fs/promises';\nimport {type z, type ZodTypeAny, type ZodError} from 'zod';\n\nexport interface ReadFileOptions {\n parser?: (rawString: string) => unknown;\n throwOnZodError?: boolean;\n}\n\nexport async function readFile<\n S extends ZodTypeAny,\n O extends ReadFileOptions | undefined = undefined,\n>(\n path: fs.FileHandle | PathLike,\n schema: S,\n options?: O,\n): Promise<\n O extends undefined\n ? z.infer<S> | ZodError<z.infer<S>>\n : If<NonNullable<O>['throwOnZodError'], true, z.infer<S>, z.infer<S> | ZodError<z.infer<S>>>\n>;\nexport async function readFile(\n path: fs.FileHandle | PathLike,\n schema: ZodTypeAny,\n options?: ReadFileOptions,\n) {\n let fileParser = options?.parser ?? JSON.parse;\n let rawJson = fileParser(await fs.readFile(path, {encoding: 'utf8'})) as unknown;\n\n if (options?.throwOnZodError) {\n // eslint-disable-next-line @typescript-eslint/no-unsafe-return -- return type is actually defined via the function overload above\n return schema.parse(rawJson);\n }\n\n let result = schema.safeParse(rawJson);\n\n // eslint-disable-next-line @typescript-eslint/no-unsafe-return -- return type is actually defined via the function overload above\n return result.success ? result.data : result.error;\n}\n"],"names":["fs","readFile","path","schema","options","fileParser","parser","JSON","parse","rawJson","encoding","throwOnZodError","result","safeParse","success","data","error"],"mappings":"AAEA,OAAOA,QAAQ,mBAAmB;AAoBlC,OAAO,eAAeC,SACpBC,IAA8B,EAC9BC,MAAkB,EAClBC,OAAyB;IAEzB,IAAIC,aAAaD,SAASE,UAAUC,KAAKC,KAAK;IAC9C,IAAIC,UAAUJ,WAAW,MAAML,GAAGC,QAAQ,CAACC,MAAM;QAACQ,UAAU;IAAM;IAElE,IAAIN,SAASO,iBAAiB;QAC5B,kIAAkI;QAClI,OAAOR,OAAOK,KAAK,CAACC;IACtB;IAEA,IAAIG,SAAST,OAAOU,SAAS,CAACJ;IAE9B,kIAAkI;IAClI,OAAOG,OAAOE,OAAO,GAAGF,OAAOG,IAAI,GAAGH,OAAOI,KAAK;AACpD"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jakubmazanec/zod-utils",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2-a1d4551",
|
|
4
4
|
"description": "Collection of Zod schemas and utilities.",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -26,7 +26,7 @@
|
|
|
26
26
|
"typecheck": "tsc --project tsconfig.typecheck.json"
|
|
27
27
|
},
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"@jakubmazanec/ts-utils": "
|
|
29
|
+
"@jakubmazanec/ts-utils": "2.1.2-a1d4551"
|
|
30
30
|
},
|
|
31
31
|
"devDependencies": {
|
|
32
32
|
"@jest/globals": "^29.5.0",
|