Git Product home page Git Product logo

coffee's Introduction

coffee

Type-safe, Easy To Use, Deno Configuration

Getting Started

Getting started with coffee is as easy as writing two lines of code ...

// coffee.ts
import coffee from "https://deno.land/x/coffee/mod.ts";
const dbName: string = coffee.get("database.name").string();
- coffee.ts
- config
  - default.json

// default.json
{
  "database": {
    "name": "my-db-name",
  }
}

and run

deno run --allow-read --allow-env coffee.ts

Type Safety

coffee's goal is to have the most safe types, so you can get the configurations in the desired types.

const name: string = coffee.get("person.name").string();
const age: number = coffee.get("person.age").number();
const passed: boolean = coffee.get("person.test.passed").boolean();

NOTE

coffee saves raw, unknown type values in value property of result of get method.

const magical: unknown = coffee.get("person.superpower").value;

Has & Set

check configuration is available on config files with has method and set new configuration during runtume with set.

coffee.has("requests.limit"); // false
coffee.set("requests.limit" , 100);
coffee.has("requests.limit"); // true

Custom Environment Variables

If you want to use environment variables, just create a file called custom-environment-variable and give it values that you want to read from environment variables.

ENV_NAME=coffee

- coffee.ts
- config
  - custom-environment-variables.json

  // custom-environment-variables.json 
  {
   "name" : "ENV_NAME"
  }
// coffee.ts
import coffee from "https://deno.land/x/coffee/mod.ts";
const name: string = coffee.get("database.name").string(); // coffee, reads from ENV_NAME environment variable

Related Environment Variables

coffee can read related environment variables from desired environment specified in DENO_ENV env.

DENO_ENV=production

- coffee.ts
- config
  - production.json

  // production.json
  {
   "something" : "amazing"
  }
// coffee.ts
import coffee from "https://deno.land/x/coffee/mod.ts";
const name: string = coffee.get("something").string();

Command Line Arguments

coffee can read forced configs from command line arguments.

note for get config in coffee configurations we shoud prefix flags by "config".

cd examples
deno run --allow-read --allow-env coffee.ts --config.username=something
// coffee.ts
import coffee from "https://deno.land/x/coffee/mod.ts";
const username: string = coffee.get("username").string();

Customize Coffee Load

coffee can read the configurations from the desired directory.

- coffee.ts
- custom
  - default.json
  - cev.json
  - production.json
// coffee.ts
import coffee from "https://deno.land/x/coffee/mod.ts";
coffee.load({ 
   configDir: "./custom", // specify the custom config directory
   customEnvVarFileName: "cev", // specify the desired custom environment variable config file name
   env: "production" // force relative environment variables to loads from this env
},);
const dbName: string = coffee.get("database.name").string();

Error Handling

coffee tries to make a specific Error for each situation.

import coffee, { errors } from "../mod.ts";

try {
  coffee.load({ configDir: "./bad-config-dir" });
} catch (e) {
  if (e instanceof errors.NoConfigDir) {
    // now IDE knows about "e" and can provide hints.
    e.message; // "./bad-config-dir" directory is not exists.
  }
}

there are 4 kinds of Error Classes that exported from coffee module:

NoConfigDir : throwed when no config dir available.

NoConfigFile : throwed when there is no valid config file in config directory.

BadConfigPath : throwed when tries to get an undefined path from configs.

BadConfigType : throwed when tries to get an config with wrong type.

Supported Formats

We currently support these formats :

json

{
  "database":{
    "name" : "my-db-name"
  }
}

yml

database:
  name: my-db-name

Examples

A number of examples are included in examples folder.

Contributing

We are very pleased with your cooperation, so feel free to open issues and pull requests.

coffee's People

Contributors

aslemammad avatar disizali avatar erfanium avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

coffee's Issues

Roadmap 1.0.0

todo list in the way to 1.0.0 :

  • custom environment variables
  • support .json
  • project structure
    • config/[environment].js-json
    • default configuration file
    • ability to explicity specify config file
    • auto detect environment for target configuration
  • make api available
    • coffee.get
    • coffee.has
    • coffee.set
  • ci
    • tests
  • error handling
  • documentaion
  • publish to deno.land/x and update example links

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.