@holz/env-filter 0.9.1-rc.8 → 0.9.1

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@holz/env-filter",
3
- "version": "0.9.1-rc.8+6bcf3fb",
3
+ "version": "0.9.1",
4
4
  "description": "Filter logs based on the current environment.",
5
5
  "type": "module",
6
6
  "repository": {
@@ -10,6 +10,7 @@
10
10
  },
11
11
  "exports": {
12
12
  ".": {
13
+ "source": "./src/index.ts",
13
14
  "types": "./dist/holz-env-filter.d.ts",
14
15
  "require": "./dist/holz-env-filter.cjs",
15
16
  "import": "./dist/holz-env-filter.js"
@@ -22,7 +23,9 @@
22
23
  "license": "MIT",
23
24
  "sideEffects": false,
24
25
  "files": [
25
- "dist"
26
+ "dist",
27
+ "src",
28
+ "!src/**/__tests__"
26
29
  ],
27
30
  "keywords": [
28
31
  "holz",
@@ -38,21 +41,21 @@
38
41
  "test:types": "tsc"
39
42
  },
40
43
  "dependencies": {
41
- "@holz/pattern-filter": "^0.9.1-rc.8+6bcf3fb"
44
+ "@holz/pattern-filter": "^0.9.1"
42
45
  },
43
46
  "peerDependencies": {
44
- "@holz/core": "^0.8.0"
47
+ "@holz/core": "^0.8.0 || ^0.9.0"
45
48
  },
46
49
  "devDependencies": {
47
- "@holz/core": "^0.9.1-rc.8+6bcf3fb",
50
+ "@holz/core": "^0.9.1",
48
51
  "@microsoft/api-extractor": "^7.58.8",
49
52
  "@types/node": "^24.0.0",
50
- "@vitest/coverage-v8": "^4.0.0",
53
+ "@vitest/coverage-v8": "^5.0.0",
51
54
  "typescript": "^6.0.0",
52
55
  "vite": "^8.0.0",
53
56
  "vite-plugin-dts": "^5.0.0",
54
57
  "vite-tsconfig-paths": "^6.0.0",
55
- "vitest": "^4.0.0"
58
+ "vitest": "^5.0.0"
56
59
  },
57
- "gitHead": "6bcf3fbc31a0cb3597e62832bfa672535fcaa144"
60
+ "gitHead": "87f40467fe9aa798fa894c04a208751403c45e8c"
58
61
  }
@@ -0,0 +1,45 @@
1
+ import type { LogProcessor } from '@holz/core';
2
+ import { createPatternFilter } from '@holz/pattern-filter';
3
+ import { getEnvironmentVariable, getLocalStorageKey } from './get-env';
4
+
5
+ export const createEnvironmentFilter = ({
6
+ processor,
7
+ pattern,
8
+ defaultPattern,
9
+ localStorageKey = 'debug',
10
+ environmentVariable = 'DEBUG',
11
+ }: Config): LogProcessor => {
12
+ /**
13
+ * Load from localStorage falling back to `process.env`. We always check
14
+ * both because we might be in Electron, where both are defined and equally
15
+ * valid.
16
+ */
17
+ const loadPatternFromEnvironment = () => {
18
+ return (
19
+ getLocalStorageKey(localStorageKey) ??
20
+ getEnvironmentVariable(environmentVariable)
21
+ );
22
+ };
23
+
24
+ return createPatternFilter({
25
+ processor,
26
+ pattern: pattern ?? loadPatternFromEnvironment() ?? defaultPattern ?? '*',
27
+ });
28
+ };
29
+
30
+ interface Config {
31
+ /** Replace the detected pattern with something else. */
32
+ pattern?: string;
33
+
34
+ /** The default pattern to use if no pattern is provided. */
35
+ defaultPattern?: string;
36
+
37
+ /** Where to send the logs if they pass the filter. */
38
+ processor: LogProcessor;
39
+
40
+ /** The localStorage key where patterns are saved. Default is `debug`. */
41
+ localStorageKey?: string;
42
+
43
+ /** The environment variable where patterns are saved. Default is `DEBUG`. */
44
+ environmentVariable?: string;
45
+ }
package/src/get-env.ts ADDED
@@ -0,0 +1,16 @@
1
+ export const getEnvironmentVariable = (key: string): undefined | string => {
2
+ if (typeof process !== 'undefined' && 'env' in process) {
3
+ return process.env[key];
4
+ }
5
+ };
6
+
7
+ export const getLocalStorageKey = (key: string): undefined | string => {
8
+ try {
9
+ if (typeof localStorage !== 'undefined') {
10
+ return localStorage.getItem(key) ?? undefined;
11
+ }
12
+ } catch {
13
+ // Sometimes localStorage fails (e.g. in Safari private mode). That's
14
+ // okay. We'll just use a fallback pattern.
15
+ }
16
+ };
package/src/index.ts ADDED
@@ -0,0 +1 @@
1
+ export { createEnvironmentFilter } from './environment-filter';