API Reference
Complete reference for @powerduck/conf-patch v0.3.4. Every exported symbol is documented with its exact signature and defaults.
There are two entry points:
@powerduck/conf-patch— everything below (core + file layer + OpenAPI validation + utilities).@powerduck/conf-patch/core— only the browser-safe subset:patchContent,setContentValue,deleteContentValue,PatchContentOptions, the assertion helpers, and the typesConfigFormat,JsonPatchOp,JsonPathSegment.
Core layer (browser-safe)
patchContent
Applies an array of RFC 6902 patch operations to a configuration string. Pure function — no filesystem access.
function patchContent(
content: string,
ops: JsonPatchOp[],
format: ConfigFormat,
options?: PatchContentOptions,
): string;
| Parameter | Type | Description |
|---|---|---|
content | string | The raw configuration content. Must be a valid format. |
ops | JsonPatchOp[] | Operations to apply in order. An empty array returns content unchanged. |
format | ConfigFormat | "json" | "jsonc" | "yaml". |
options.strict | boolean | When true (default), a failed operation throws. When false, it is skipped with a console.warn. |
Returns: the patched configuration content (string).
Throws: TypeError if content is not a string or ops is invalid; an error describing the failing operation when strict is true.
import { patchContent } from "@powerduck/conf-patch/core";
patchContent(
'{"name": "app"}',
[{ op: "add", path: ["version"], value: "1.0.0" }],
"json",
);
// => '{\n "name": "app",\n "version": "1.0.0"\n}'
setContentValue
Sets or creates a single value. Internally calls patchContent with one add operation. add replaces an existing object property or inserts at an array index (RFC 6902).
function setContentValue(
content: string,
path: readonly (string | number)[],
value: unknown,
format: ConfigFormat,
): string;
| Parameter | Type | Description |
|---|---|---|
content | string | The raw configuration content. |
path | readonly (string | number)[] | Segment path, e.g. ["server", "port"]. Must be non-empty. |
value | unknown | The value to write. |
format | ConfigFormat | The configuration format. |
Returns: the updated content (string).
setContentValue("name: app\n", ["server", "port"], 8080, "yaml");
deleteContentValue
Removes a key or array element. Internally calls patchContent with one remove operation. The path must exist.
function deleteContentValue(
content: string,
path: readonly (string | number)[],
format: ConfigFormat,
): string;
deleteContentValue(
'{"name": "app", "legacy": true}',
["legacy"],
"json",
);
PatchContentOptions
interface PatchContentOptions {
/** When true, failed operations throw. When false, they are skipped with a warning. Default: true */
strict?: boolean;
}
File layer (Node.js / Electron only)
readConfigFile
Reads UTF-8 text from a local file path or file:// URL.
function readConfigFile(filePath: string): Promise<string>;
| Parameter | Type | Description |
|---|---|---|
filePath | string | Absolute path, relative path, or file:// URL. Must be non-empty. |
Returns: the raw UTF-8 content (Promise<string>).
Throws: an error wrapping the underlying filesystem failure if the file cannot be read.
writeConfigFile
Writes content to a file using an atomic write (temp file + rename) and, by default, an exclusive file lock. Parent directories are created before locking.
function writeConfigFile(
filePath: string,
content: string,
options?: WriteConfigOptions,
): Promise<void>;
| Parameter | Type | Description |
|---|---|---|
filePath | string | Path to the configuration file. |
content | string | The content to write. Must be a string. |
options | WriteConfigOptions | See below. |
Throws: TypeError on bad input; wraps filesystem failures on write.
WriteConfigOptions
| Option | Type | Default | Description |
|---|---|---|---|
lock | boolean | true | Enable file locking during the write. |
lockTimeoutMs | number | withFileLock default (10s) | Max time (ms) to wait to acquire the lock. |
lockRetryDelayMs | number | withFileLock default (25ms) | Initial retry delay (ms) before exponential backoff. |
lockStaleThresholdMs | number | withFileLock default | Lock age (ms) after which recovery is allowed. |
allowStaleRecovery | boolean | false | Whether stale locks may be automatically reclaimed. |
import { writeConfigFile } from "@powerduck/conf-patch";
await writeConfigFile("config.json", '{"name": "app"}');