Launch Week Day 2: Introducing Reports: An Extensible Reporting Framework for Socket Data.Learn More
Socket
Book a DemoSign in
Socket

file-conf

Package Overview
Dependencies
Maintainers
0
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

file-conf

A structured configuration system with options for saving in multiple file formats.

latest
npmnpm
Version
2.0.1
Version published
Maintainers
0
Created
Source

File Conf

A structured configuration system with options for saving in multiple file formats.

Usage

To use this package, you need to create your own configuration class that extends the FileConf class provided by this module. This allows you to expose values that you want available as well as validate input that you allow through during runtime. This package includes the zod npm package as its peer dependency. You must be able to write a zod schema to use this module.

Example

import { FileConf } from 'file-conf';
import z from 'zod';

const mySchema = z
  .object({
    mode: z.enum(['development', 'production']).default('development'),
    database: z
      .object({
        host: z.string().default('somewhere.net'),
        port: z.coerce.number().default(5432),
        user: z.string().default('REDACTED'),
        password: z.string().default('REDACTED')
      })
      .default({})
  })
  .strict()
  .default({});

/**
 * @typedef {object} ConfigSchema
 * @property {string} mode
 * @property {object} database
 * @property {string} database.host
 * @property {number} database.port
 * @property {string} database.user
 * @property {string} database.password
 */

class Config extends FileConf {
  constructor() {
    super(mySchema, { format: 'yaml' });
    /** @type {ConfigSchema} */
    this.$json;
  }
  get mode() {
    return this.$json.mode;
  }
  get database() {
    return this.$json.database;
  }
}

const c = new Config();

console.log(c.mode); //prints "development"
console.log(c.database.user); //prints "REDACTED"

const dbConf = { database: { user: 'admin' } };


//passing "true" saves valid changes to disk
console.log(c.update(dbConf, true);); //prints "true" because it was a valid update

console.log(c.update({notValid: 1}, true)) //prints false because it was an invalid update

console.log(c.database.user); //prints "admin"

FAQs

Package last updated on 15 Jul 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts