Cutwater

Cutwater

  • Docs
  • Help
  • GitHub

›Introduction

Introduction

  • Getting Started

API Reference

  • @codification/cutwater-build-core
  • @codification/cutwater-core
  • @codification/cutwater-node-core
  • @codification/cutwater-logging
  • @codification/cutwater-aws

Getting Started

Installation

Via npm:

npm install @codification/cutwater-core

Via yarn:

yarn add @codification/cutwater-core

Documentation

  • Release Notes
  • API Documentation

Quick Start Guide

Configuration

import { Config } from 'cutwater-core';

const url = Config.get('API_URL', 'https://api.example.com');
// Returns 'https://api.example.com' if there is no value for API_URL

const otherUrl = Config.getRequired('API_URL', 'API_URL is required!');
// Will throw an error (optionally with the provided message) if API_URL does not exist

Config.put('BACKUP_API_URL', 'https://api-backup.example.com');

Environment

import { Env } from 'cutwater-core';

if (Env.isProd()) {
  console.log('Yeah, we made it to production!');
}
if (Env.isDev()) {
  console.log('Not yet I guess.');
}

String Utility Functions

import { contains, startWith, endsWith } from 'cutwater-core';

if (contains('Check This', 'This')) {
  console.log('Yes, it contains it.');
}
if (startsWith('x-forward', 'x-')) {
  console.log('A custom header.');
}
if (endsWith('x-Forward-Cookies', 'cookies', true)) {
  console.log('Case insensitivity FTW.');
}

Time

import { TimeUnit, TZUtils } from 'cutwater-core';

const oneDayInSeconds = TimeUnit.days(1).toSeconds();
const fiveMinutesInMillis = TimeUnit.minutes(5).toMillis();

console.log(TZUtils.timestamp());
// 2018-10-06 15:22:12,345 (This is UTC)

TZUtils.timezoneOffset = TimeUnit.hours(-5).toMinutes();
console.log(TZUtils.timestamp());
// 2018-10-06 10:22:12,345 (Now we get the time in Ecuador, UTC-5)

const localizedDate = TZUtils.now();
// localizedDate is the current date/time based on the timezoneOffset, Ecuador in this case.

Logging

import { LoggerFactory } from 'cutwater-core';

const LOG = LoggerFactory.getLogger();
LOG.info('Hey, here is a log message.');
LOG.debug('Examine this object: %j', someObj);

Http

Note: The http related functions are designed to simplify aspects of working with the http module in Node.js

import { isResponseOk, LoggerFactory, mergeHeaders, toBodyText } from 'cutwater-core';

const LOG = LoggerFactory.getLogger();
const response = magicalHttpRequestFunction();
if(isResponseOk(response)){
  toBodyText(response).then(
    bodyTxt => {
      LOG.info('The body text was: %s', bodyTxt);
    }
  ).catch(
    err => {
      LOG.error('Oops! Problem reading the body: %j',err);
    }
  )

  const nextRequestHeaders = mergeHeaders(response.headers,{'x-custom-header':'Custom Value'},true);
  // Will add the 'x-custom-header' to the received headers, or overwrite if it already exists.
}
Last updated on 2019-3-20
@codification/cutwater-build-core →
  • Installation
  • Documentation
  • Quick Start Guide
    • Configuration
    • Environment
    • String Utility Functions
    • Time
    • Logging
    • Http
Cutwater
Docs
Getting Started
Community
Stack Overflow
More
GitHubStar
Copyright © 2019 Codification.org