configly-js
Version:
The dead simple place to put and retrieve static/config data into your Node.js application / JS
84 lines (83 loc) • 3.27 kB
TypeScript
interface ConfiglyOptions {
host?: string;
enableCache?: boolean;
timeout?: number;
}
/**
* Config.ly: the dead simple place to store and retrieve your static/config data.
*
* Remember: *do NOT* assign the result of a get() to a long-lived variable; in order for
* the value to fetch from the server, you must call get().
*
* Each get(key) returns a Promise; the first argument to the Promise fulfillment method is the
* Configly value for the supplied key. Please see the example:
*
* const Configly = require('Configly');
* const configly = Configly.init('API_KEY');
*
* configly.get('keyOne').then((valueForKeyOne) => console.log(valueForKeyOne));
*
* // or
*
* const run = async () => {
* return await configly.get('keyOne');
* }
*
* Note that get(key) may make a server request or fetch a cached value. You should
* assume it'll make a (fast) HTTP request. If you need something guaranteed to be faster, we
* recommend storing the value to a local variable; BUT, be aware that this means you won't
* receive updates to that variable, so be sure to call get() periodically.
*/
export declare class Configly {
private static instance?;
private cache;
private cacheTtl;
private apiKey;
private options;
/**
* This method should NOT be called externally; please use Configly.init().
*/
private constructor();
static init(apiKey: string, options?: ConfiglyOptions): Configly;
static getInstance(): Configly;
static isInitialized(): boolean;
static getUnixTimestampSecs(): number;
_isCached(key: string): boolean;
_cacheGet(key: string): any;
/**
* Fetch the value for the supplied key. This is an async call; it may be lightning fast as the
* value may be cached.
*
* Configly.init() must be called before any invocation of get
*
* @param {String} key - the key to fetch.
* @param {Object?} [options] overrides the global parameters set in the constructor for this
* `get` request only (optional)
* @property {Number} enableCache (default: true) - disables the cache, resulting in an HTTP
* fetch on every `get` call.
* @property {Number} timeout (default: 3000) - timeout for request to Configly for data in ms.
* @return { Promse<String | Number | Boolean | Array | Object | Error> } returns, on success,
* a promise of fulfilled with the stored value(s) as typed in Config.ly. On error, returns
* a failed promise with error:
* - TypeError if key is not a string or omitted
* - Error if key an empty string
*/
get(key: string, options?: ConfiglyOptions): Promise<any>;
static makeError(status: string, message: string, originalError: any): {
status: string;
message: string;
originalError: any;
};
static handleGetError(error: any): Promise<never>;
/**
* Destroys singleton; really meant for testing snd likely should not be used
* externally.
*/
destroy(): void;
}
export declare const ERRORS: {
OTHER: string;
CONNECTION_ERROR: string;
INVALID_API_KEY: string;
};
export default Configly;