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

custom logger #3

Merged
merged 6 commits into from
Jun 1, 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: 1 addition & 3 deletions biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@
},
"recommended": true,
"correctness": {
"noUnusedVariables": "error"
},
"nursery": {
"noUnusedVariables": "error",
"noUnusedImports": "error"
}
}
Expand Down
33 changes: 8 additions & 25 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
"build": "tsup",
"prepublishOnly": "pnpm build",
"type-check": "tsc --noEmit",
"format": "pnpm dlx @biomejs/biome format ./src/**/*.ts --write",
"lint": "pnpm dlx @biomejs/biome lint ./src/**/*.ts",
"format": "biome format ./src --write",
"lint": "biome lint ./src",
"test": "vitest run --silent",
"test:watch": "vitest",
"prebuild": "pnpm run type-check",
Expand All @@ -21,9 +21,7 @@
"release:dry-run": "standard-version --skip.changelog --dry-run",
"yalc:publish": "yalc publish"
},
"files": [
"dist/**"
],
"files": ["dist/**"],
"exports": {
".": {
"types": "./dist/index.d.ts",
Expand Down Expand Up @@ -52,15 +50,9 @@
},
"typesVersions": {
"*": {
"env-adapter": [
"./dist/env-adapter.d.ts"
],
"json-adapter": [
"./dist/json-adapter.d.ts"
],
"dotenv-adapter": [
"./dist/dotenv-adapter.d.ts"
]
"env-adapter": ["./dist/env-adapter.d.ts"],
"json-adapter": ["./dist/json-adapter.d.ts"],
"dotenv-adapter": ["./dist/dotenv-adapter.d.ts"]
}
},
"publishConfig": {
Expand All @@ -74,18 +66,9 @@
"url": "https://github.com/alexmarqs/zod-config/issues"
},
"homepage": "https://github.com/alexmarqs/zod-config#readme",
"keywords": [
"zod",
"config",
"env",
"json",
"dotenv",
"typescript",
"adapters",
"typesafe"
],
"keywords": ["zod", "config", "env", "json", "dotenv", "typescript", "adapters", "typesafe"],
"devDependencies": {
"@biomejs/biome": "1.4.1",
"@biomejs/biome": "1.7.3",
"@types/node": "20.10.7",
"dotenv": "16.4.1",
"standard-version": "9.5.0",
Expand Down
82 changes: 51 additions & 31 deletions pnpm-lock.yaml

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

2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export type { Adapter, Config } from "./types";
export type { Adapter, Config, Logger } from "./types";
export { loadConfig } from "./lib/config";
8 changes: 5 additions & 3 deletions src/lib/adapters/dotenv-adapter/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import { parse } from "dotenv";
import { readFile } from "fs/promises";
import { Adapter } from "../../../types";
import { readFile } from "node:fs/promises";
import type { Adapter } from "../../../types";
import { filterByPrefixKey } from "../utils";

export type DotEnvAdapterProps = {
path: string;
prefixKey?: string;
silentFail?: boolean;
};

const ADAPTER_NAME = "dotenv adapter";

export const dotEnvAdapter = ({ path, prefixKey }: DotEnvAdapterProps): Adapter => {
export const dotEnvAdapter = ({ path, prefixKey, silentFail }: DotEnvAdapterProps): Adapter => {
return {
name: ADAPTER_NAME,
read: async () => {
Expand All @@ -32,5 +33,6 @@ export const dotEnvAdapter = ({ path, prefixKey }: DotEnvAdapterProps): Adapter
);
}
},
silentFail,
};
};
6 changes: 4 additions & 2 deletions src/lib/adapters/env-adapter/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { Adapter } from "../../../types";
import type { Adapter } from "../../../types";
import { filterByPrefixKey } from "../utils";

export type EnvAdapterProps = {
customEnv?: Record<string, any>;
prefixKey?: string;
silentFail?: boolean;
};

const ADAPTER_NAME = "env adapter";

export const envAdapter = ({ customEnv, prefixKey }: EnvAdapterProps = {}): Adapter => {
export const envAdapter = ({ customEnv, prefixKey, silentFail }: EnvAdapterProps = {}): Adapter => {
return {
name: ADAPTER_NAME,
read: async () => {
Expand All @@ -20,5 +21,6 @@ export const envAdapter = ({ customEnv, prefixKey }: EnvAdapterProps = {}): Adap

return data;
},
silentFail,
};
};
8 changes: 5 additions & 3 deletions src/lib/adapters/json-adapter/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { Adapter } from "../../../types";
import type { Adapter } from "../../../types";
import { filterByPrefixKey } from "../utils";
import { readFile } from "fs/promises";
import { readFile } from "node:fs/promises";

export type JsonAdapterProps = {
path: string;
prefixKey?: string;
silentFail?: boolean;
};

const ADAPTER_NAME = "json adapter";

export const jsonAdapter = ({ path, prefixKey }: JsonAdapterProps): Adapter => {
export const jsonAdapter = ({ path, prefixKey, silentFail }: JsonAdapterProps): Adapter => {
return {
name: ADAPTER_NAME,
read: async () => {
Expand All @@ -31,5 +32,6 @@ export const jsonAdapter = ({ path, prefixKey }: JsonAdapterProps): Adapter => {
);
}
},
silentFail,
};
};
20 changes: 12 additions & 8 deletions src/lib/config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AnyZodObject, z } from "zod";
import { Adapter, Config } from "../types";
import type { AnyZodObject, z } from "zod";
import type { Adapter, Config, Logger } from "../types";
import { deepMerge } from "./adapters/utils";

/**
Expand All @@ -15,10 +15,12 @@ export const loadConfig = async <T extends AnyZodObject>(
config: Config<T>,
): Promise<z.infer<T>> => {
const { schema, adapters, onError, onSuccess } = config;
const logger = config.logger ?? console;

// Read data from adapters
const data = await getDataFromAdapters(
Array.isArray(adapters) ? adapters : adapters ? [adapters] : [],
logger,
);

// Validate data against schema
Expand All @@ -43,7 +45,7 @@ export const loadConfig = async <T extends AnyZodObject>(
return result.data;
};

const getDataFromAdapters = async (adapters?: Adapter[]) => {
const getDataFromAdapters = async (adapters: Adapter[], logger: Logger) => {
// If no adapters are provided, we will read from process.env
if (!adapters || adapters.length === 0) {
return process.env;
Expand All @@ -55,11 +57,13 @@ const getDataFromAdapters = async (adapters?: Adapter[]) => {
try {
return await adapter.read();
} catch (error) {
console.warn(
`Cannot read data from ${adapter.name}: ${
error instanceof Error ? error.message : error
}`,
);
if (!adapter.silentFail) {
logger.warn(
`Cannot read data from ${adapter.name}: ${
error instanceof Error ? error.message : error
}`,
);
}
return {};
}
}),
Expand Down
8 changes: 7 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import { z } from "zod";
import type { z } from "zod";

export type Adapter = {
name: string;
read: () => Promise<z.infer<z.AnyZodObject>>;
silentFail?: boolean;
};

export type Config<T extends z.AnyZodObject = z.AnyZodObject> = {
schema: T;
adapters?: Adapter[] | Adapter;
onSuccess?: (data: z.infer<T>) => void;
onError?: (error: z.ZodError<z.infer<T>>) => void;
logger?: Logger;
};

export type Logger = {
warn: (message: string) => void;
};
Loading
Loading