-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from joschi/yaml-adapter
feat: add YAML adapter
- Loading branch information
Showing
7 changed files
with
236 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -61,6 +61,12 @@ | |
"import": "./dist/directory-adapter.mjs", | ||
"module": "./dist/directory-adapter.mjs", | ||
"require": "./dist/directory-adapter.js" | ||
}, | ||
"./yaml-adapter": { | ||
"types": "./dist/yaml-adapter.d.ts", | ||
"import": "./dist/yaml-adapter.mjs", | ||
"module": "./dist/yaml-adapter.mjs", | ||
"require": "./dist/yaml-adapter.js" | ||
} | ||
}, | ||
"typesVersions": { | ||
|
@@ -79,6 +85,9 @@ | |
], | ||
"directory-adapter": [ | ||
"./dist/directory-adapter.d.ts" | ||
], | ||
"yaml-adapter": [ | ||
"./dist/yaml-adapter.d.ts" | ||
] | ||
} | ||
}, | ||
|
@@ -98,6 +107,7 @@ | |
"config", | ||
"env", | ||
"json", | ||
"yaml", | ||
"dotenv", | ||
"typescript", | ||
"adapters", | ||
|
@@ -115,14 +125,19 @@ | |
}, | ||
"peerDependencies": { | ||
"dotenv": ">=15", | ||
"yaml": "^2.x", | ||
"zod": "^3.x" | ||
}, | ||
"peerDependenciesMeta": { | ||
"dotenv": { | ||
"optional": true | ||
}, | ||
"yaml": { | ||
"optional": true | ||
} | ||
}, | ||
"engines": { | ||
"node": ">=14.0.0" | ||
} | ||
}, | ||
"packageManager": "[email protected]" | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import type { Adapter } from "../../../types"; | ||
import { filterByPrefixKey } from "../utils"; | ||
import { readFile } from "node:fs/promises"; | ||
import YAML from "yaml"; | ||
|
||
export type YamlAdapterProps = { | ||
path: string; | ||
prefixKey?: string; | ||
silent?: boolean; | ||
}; | ||
|
||
const ADAPTER_NAME = "yaml adapter"; | ||
|
||
export const yamlAdapter = ({ path, prefixKey, silent }: YamlAdapterProps): Adapter => { | ||
return { | ||
name: ADAPTER_NAME, | ||
read: async () => { | ||
try { | ||
const data = await readFile(path, "utf-8"); | ||
|
||
const parsedData = YAML.parse(data) || {}; | ||
|
||
if (prefixKey) { | ||
return filterByPrefixKey(parsedData, prefixKey); | ||
} | ||
|
||
return parsedData; | ||
} catch (error) { | ||
throw new Error( | ||
`Failed to parse / read YAML file at ${path}: ${ | ||
error instanceof Error ? error.message : error | ||
}`, | ||
); | ||
} | ||
}, | ||
silent, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
import { yamlAdapter } from "@/lib/adapters/yaml-adapter"; | ||
import { loadConfig } from "@/lib/config"; | ||
import type { Logger } from "@/types"; | ||
import { unlink, writeFile } from "node:fs/promises"; | ||
import path from "node:path"; | ||
import { afterAll, beforeAll, describe, expect, it, vi } from "vitest"; | ||
import { z } from "zod"; | ||
import YAML from "yaml"; | ||
|
||
describe("yaml adapter", () => { | ||
const testFilePath = path.join(__dirname, "test-yaml-adapter.yaml"); | ||
|
||
beforeAll(async () => { | ||
await writeFile(testFilePath, YAML.stringify({ HOST: "localhost", PORT: "3000" })); | ||
}); | ||
|
||
afterAll(async () => { | ||
await unlink(testFilePath); | ||
}); | ||
|
||
it("should return parsed data when schema is valid", async () => { | ||
// given | ||
const schema = z.object({ | ||
HOST: z.string(), | ||
PORT: z.string().regex(/^\d+$/), | ||
}); | ||
|
||
// when | ||
const config = await loadConfig({ | ||
schema, | ||
adapters: yamlAdapter({ | ||
path: testFilePath, | ||
}), | ||
}); | ||
|
||
// then | ||
expect(config.HOST).toBe("localhost"); | ||
expect(config.PORT).toBe("3000"); | ||
}); | ||
it("should throw zod error when schema is invalid", async () => { | ||
// given | ||
const schema = z.object({ | ||
HOST: z.string(), | ||
PORT: z.number(), | ||
}); | ||
|
||
// when | ||
// then | ||
expect( | ||
loadConfig({ | ||
schema, | ||
adapters: yamlAdapter({ | ||
path: testFilePath, | ||
}), | ||
}), | ||
).rejects.toThrowError(z.ZodError); | ||
}); | ||
it("should log error from adapter errors + throw zod error when schema is invalid", async () => { | ||
// given | ||
const schema = z.object({ | ||
HOST: z.string(), | ||
PORT: z.number(), | ||
}); | ||
const consoleErrorSpy = vi.spyOn(console, "warn"); | ||
|
||
// when | ||
// then | ||
await expect( | ||
loadConfig({ | ||
schema, | ||
adapters: yamlAdapter({ | ||
path: "not-exist.yaml", | ||
}), | ||
}), | ||
).rejects.toThrowError(z.ZodError); | ||
|
||
expect(consoleErrorSpy).toHaveBeenCalledWith( | ||
"Cannot read data from yaml adapter: Failed to parse / read YAML file at not-exist.yaml: ENOENT: no such file or directory, open 'not-exist.yaml'", | ||
); | ||
}); | ||
it("should log error from adapter errors (custom logger) + throw zod error when schema is invalid", async () => { | ||
// given | ||
const schema = z.object({ | ||
HOST: z.string(), | ||
PORT: z.number(), | ||
}); | ||
|
||
const customLogger: Logger = { | ||
warn: (_msg) => {}, | ||
}; | ||
|
||
const customLoggerWarnSpy = vi.spyOn(customLogger, "warn"); | ||
|
||
// when | ||
// then | ||
await expect( | ||
loadConfig({ | ||
schema, | ||
adapters: yamlAdapter({ | ||
path: "not-exist.yaml", | ||
}), | ||
logger: customLogger, | ||
}), | ||
).rejects.toThrowError(z.ZodError); | ||
|
||
expect(customLoggerWarnSpy).toHaveBeenCalledWith( | ||
"Cannot read data from yaml adapter: Failed to parse / read YAML file at not-exist.yaml: ENOENT: no such file or directory, open 'not-exist.yaml'", | ||
); | ||
}); | ||
it("throw zod error when schema is invalid but not log error from adapter errors when silent is true", async () => { | ||
// given | ||
const schema = z.object({ | ||
HOST: z.string(), | ||
PORT: z.number(), | ||
}); | ||
|
||
const consoleErrorSpy = vi.spyOn(console, "warn"); | ||
|
||
// when | ||
// then | ||
expect( | ||
loadConfig({ | ||
schema, | ||
adapters: yamlAdapter({ | ||
path: "not-exist.yaml", | ||
silent: true, | ||
}), | ||
}), | ||
).rejects.toThrowError(z.ZodError); | ||
|
||
expect(consoleErrorSpy).not.toHaveBeenCalled(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters