@batuhanw/haf
Advanced tools
Comparing version 0.2.0 to 1.0.0
import type { FlattenedWithDotNotation, OptionalKeysOf, StringKeysOf, ArrayKeysOf } from './types'; | ||
interface Options<Schema> { | ||
interface HafConfig<Schema> { | ||
/** | ||
* @description name of the store file. | ||
* @required | ||
*/ | ||
name: string; | ||
/** | ||
* @description extension of the store file. | ||
* @optional | ||
* @default haf | ||
* @file /path/to/store/storeName.**haf** | ||
*/ | ||
extension?: string; | ||
/** | ||
* @description default JSON object to use when initializing the store. | ||
* @optional | ||
* @default {} | ||
*/ | ||
defaultSchema?: Partial<Schema>; | ||
/** | ||
* @description directory for the store file. | ||
* @default checks the environment variables in the following order: | ||
* - process.env.CONFIG_DIR // unix | ||
* - process.env.XDG_CONFIG_HOME // unix | ||
* - process.env.LOCALAPPDATA // windows | ||
* fallback: | ||
* - os.homedir() + '/.config' | ||
* @optional | ||
*/ | ||
storeDir?: string; | ||
} | ||
declare class Haf<Schema, FlattenedSchema = FlattenedWithDotNotation<Schema>> { | ||
#private; | ||
private configPath; | ||
constructor(options: Options<Schema>); | ||
private upsertSchema; | ||
private storePath; | ||
private defaultSchema; | ||
constructor(config: HafConfig<Schema>); | ||
private initializeStore; | ||
get store(): Schema | Partial<Schema>; | ||
set store(schema: Schema | Partial<Schema>); | ||
private set store(value); | ||
/** | ||
* @description returns the value at the provided path. | ||
* @param path | ||
* @example | ||
* ```typescript | ||
* const haf = new Haf<DogSchema>({ | ||
* name: 'dog', | ||
* defaultSchema: { | ||
* name: 'Popita' | ||
* } | ||
* }) | ||
* | ||
* haf.get('name') // Popita | ||
* ``` | ||
*/ | ||
get<Path extends StringKeysOf<FlattenedSchema>>(path: Path): FlattenedSchema[Path]; | ||
/** | ||
* @description sets the value to the provided path. | ||
* @param path | ||
* @param value | ||
* @example | ||
* ```typescript | ||
* const haf = new Haf<DogSchema>({ | ||
* name: 'dog', | ||
* }) | ||
* | ||
* haf.set('name', 'Popita') | ||
* ``` | ||
*/ | ||
set<Path extends StringKeysOf<FlattenedSchema>>(path: Path, value: FlattenedSchema[Path]): void; | ||
/** | ||
* @description appends the provided values to the array at the provided path. | ||
* @param path | ||
* @param ...values | ||
* @example | ||
* ```typescript | ||
* const haf = new Haf<DogSchema>({ | ||
* name: 'dog', | ||
* }) | ||
* | ||
* haf.set('favoriteToys', ['ball', 'bone']) | ||
* | ||
* haf.append('favoriteToys', 'socks', 'toilet paper') | ||
* | ||
* haf.get('favoriteToys') // ['ball', 'bone', 'socks', 'toilet paper'] | ||
* ``` | ||
*/ | ||
append<Path extends ArrayKeysOf<FlattenedSchema>, Values extends Extract<FlattenedSchema[Path], unknown[]>>(path: Path, ...values: Values): void; | ||
/** | ||
* @description sets the value of the provided path to undefined. | ||
*/ | ||
delete(path: OptionalKeysOf<FlattenedSchema>): void; | ||
/** | ||
* @description resets the store to defaultSchema values. Sets fields to undefined if not provided in defaultSchema. | ||
* @param path optional path to reset. if not provided, the entire store will be reset. | ||
* @example | ||
* ```typescript | ||
* const haf = new Haf<DogSchema>({ | ||
* name: 'dog', | ||
* defaultSchema: { | ||
* name: 'pop', | ||
* }); | ||
* | ||
* haf.set('name', 'pup'); | ||
* | ||
* haf.reset('name'); | ||
* | ||
* haf.get('name'); // pop | ||
* | ||
* haf.set('age', 3) | ||
* | ||
* haf.reset('age') | ||
* | ||
* haf.get('age') // undefined | ||
* ``` | ||
*/ | ||
reset(path?: StringKeysOf<FlattenedSchema>): void; | ||
/** | ||
* @description removes the store file from the file system. | ||
*/ | ||
nuke(): void; | ||
@@ -20,0 +121,0 @@ private _get; |
"use strict"; | ||
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) { | ||
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter"); | ||
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it"); | ||
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver); | ||
}; | ||
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) { | ||
if (kind === "m") throw new TypeError("Private method is not writable"); | ||
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter"); | ||
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it"); | ||
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value; | ||
}; | ||
var _Haf_options, _Haf_defaultOptions; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const fs_extra_1 = require("fs-extra"); | ||
const get_config_path_1 = require("./get-config-path"); | ||
const get_store_path_1 = require("./get-store-path"); | ||
class Haf { | ||
constructor(options) { | ||
_Haf_options.set(this, void 0); | ||
_Haf_defaultOptions.set(this, { | ||
name: 'haf', | ||
extension: '', | ||
defaultSchema: {}, | ||
}); | ||
__classPrivateFieldSet(this, _Haf_options, Object.assign(__classPrivateFieldGet(this, _Haf_defaultOptions, "f"), options), "f"); | ||
this.configPath = (0, get_config_path_1.getConfigPath)(__classPrivateFieldGet(this, _Haf_options, "f").name, __classPrivateFieldGet(this, _Haf_options, "f").extension); | ||
this.upsertSchema(); | ||
constructor(config) { | ||
var _a; | ||
this.storePath = (0, get_store_path_1.getStorePath)(config.name, config.extension, config.storeDir); | ||
this.defaultSchema = (_a = config.defaultSchema) !== null && _a !== void 0 ? _a : {}; | ||
this.initializeStore(); | ||
} | ||
upsertSchema() { | ||
if ((0, fs_extra_1.pathExistsSync)(this.configPath)) | ||
initializeStore() { | ||
if ((0, fs_extra_1.pathExistsSync)(this.storePath)) | ||
return; | ||
(0, fs_extra_1.writeJsonSync)(this.configPath, __classPrivateFieldGet(this, _Haf_options, "f").defaultSchema); | ||
(0, fs_extra_1.writeJsonSync)(this.storePath, this.defaultSchema); | ||
} | ||
get store() { | ||
return (0, fs_extra_1.readJsonSync)(this.configPath); | ||
return (0, fs_extra_1.readJsonSync)(this.storePath); | ||
} | ||
set store(schema) { | ||
(0, fs_extra_1.writeJsonSync)(this.configPath, schema); | ||
(0, fs_extra_1.writeJsonSync)(this.storePath, schema); | ||
} | ||
/** | ||
* @description returns the value at the provided path. | ||
* @param path | ||
* @example | ||
* ```typescript | ||
* const haf = new Haf<DogSchema>({ | ||
* name: 'dog', | ||
* defaultSchema: { | ||
* name: 'Popita' | ||
* } | ||
* }) | ||
* | ||
* haf.get('name') // Popita | ||
* ``` | ||
*/ | ||
get(path) { | ||
return this._get(path); | ||
} | ||
/** | ||
* @description sets the value to the provided path. | ||
* @param path | ||
* @param value | ||
* @example | ||
* ```typescript | ||
* const haf = new Haf<DogSchema>({ | ||
* name: 'dog', | ||
* }) | ||
* | ||
* haf.set('name', 'Popita') | ||
* ``` | ||
*/ | ||
set(path, value) { | ||
this._set(path, value); | ||
} | ||
/** | ||
* @description appends the provided values to the array at the provided path. | ||
* @param path | ||
* @param ...values | ||
* @example | ||
* ```typescript | ||
* const haf = new Haf<DogSchema>({ | ||
* name: 'dog', | ||
* }) | ||
* | ||
* haf.set('favoriteToys', ['ball', 'bone']) | ||
* | ||
* haf.append('favoriteToys', 'socks', 'toilet paper') | ||
* | ||
* haf.get('favoriteToys') // ['ball', 'bone', 'socks', 'toilet paper'] | ||
* ``` | ||
*/ | ||
append(path, ...values) { | ||
@@ -51,15 +79,45 @@ const existingValues = this._get(path); | ||
} | ||
/** | ||
* @description sets the value of the provided path to undefined. | ||
*/ | ||
delete(path) { | ||
this._set(path, undefined); | ||
} | ||
/** | ||
* @description resets the store to defaultSchema values. Sets fields to undefined if not provided in defaultSchema. | ||
* @param path optional path to reset. if not provided, the entire store will be reset. | ||
* @example | ||
* ```typescript | ||
* const haf = new Haf<DogSchema>({ | ||
* name: 'dog', | ||
* defaultSchema: { | ||
* name: 'pop', | ||
* }); | ||
* | ||
* haf.set('name', 'pup'); | ||
* | ||
* haf.reset('name'); | ||
* | ||
* haf.get('name'); // pop | ||
* | ||
* haf.set('age', 3) | ||
* | ||
* haf.reset('age') | ||
* | ||
* haf.get('age') // undefined | ||
* ``` | ||
*/ | ||
reset(path) { | ||
if (typeof path === 'undefined') { | ||
this.store = __classPrivateFieldGet(this, _Haf_defaultOptions, "f").defaultSchema || {}; | ||
this.store = this.defaultSchema; | ||
return; | ||
} | ||
const defaultValue = this._get(path, __classPrivateFieldGet(this, _Haf_defaultOptions, "f").defaultSchema); | ||
const defaultValue = this._get(path, this.defaultSchema); | ||
this._set(path, defaultValue); | ||
} | ||
/** | ||
* @description removes the store file from the file system. | ||
*/ | ||
nuke() { | ||
(0, fs_extra_1.removeSync)(this.configPath); | ||
(0, fs_extra_1.removeSync)(this.storePath); | ||
} | ||
@@ -88,3 +146,2 @@ _get(path, source) { | ||
} | ||
_Haf_options = new WeakMap(), _Haf_defaultOptions = new WeakMap(); | ||
exports.default = Haf; |
{ | ||
"name": "@batuhanw/haf", | ||
"version": "0.2.0", | ||
"version": "1.0.0", | ||
"engines": { | ||
"node": ">=16" | ||
}, | ||
"description": "Fully typed, modern, cross-platform persistent config solution for NodeJS projects", | ||
"description": "Fully typed, modern, cross-platform persistent JSON storage solution for NodeJS projects.", | ||
"main": "dist/index.js", | ||
@@ -14,3 +14,5 @@ "types": "dist/index.d.ts", | ||
"scripts": { | ||
"dev": "npm run build -- --watch", | ||
"test": "jest --runInBand", | ||
"test:watch": "npm run test -- --watch", | ||
"test:coverage": "jest --runInBand --coverage", | ||
@@ -48,3 +50,2 @@ "build": "rimraf dist && tsc --build tsconfig.build.json", | ||
"@types/node": "^18.19.3", | ||
"@types/rimraf": "^4.0.5", | ||
"@typescript-eslint/eslint-plugin": "^6.16.0", | ||
@@ -51,0 +52,0 @@ "@typescript-eslint/parser": "^6.16.0", |
@@ -9,3 +9,3 @@ # 🧠 🔒 Haf 🦺 ✏️ | ||
Haf is a fully typed 🔒, cross-platform, persistent 💾 config ⚙️ solution for your NodeJS projects with a great developer experience! | ||
Haf is a fully typed 🔒, cross-platform, persistent 💾 JSON storage ⚙️ solution for your NodeJS projects with a great developer experience! | ||
@@ -12,0 +12,0 @@ - ✏️ Auto-completed dot-notation suggestions as you type when you try to get()/set()/delete()/reset() data from the store. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
16957
11
317
0
1