From a984b54b41dd3af424879e40cfacdc8f26f8bf0a Mon Sep 17 00:00:00 2001 From: Jean-Philipe Pellerin Date: Tue, 19 Jan 2016 15:51:21 -0500 Subject: [PATCH] Definition files for hapi/confidence --- confidence/confidence-tests.ts | 79 ++++++++++++++++++++++++++++++++++ confidence/confidence.d.ts | 48 +++++++++++++++++++++ 2 files changed, 127 insertions(+) create mode 100644 confidence/confidence-tests.ts create mode 100644 confidence/confidence.d.ts diff --git a/confidence/confidence-tests.ts b/confidence/confidence-tests.ts new file mode 100644 index 000000000..60825e012 --- /dev/null +++ b/confidence/confidence-tests.ts @@ -0,0 +1,79 @@ +/// + +import Confidence = require('confidence'); + +let criteria = { + "env": "production", + "platform": "ios", + "xfactor": "yes", + "random": { + "a": 15 + } +}; + +/** +* The configurations in Confidence style +*/ +let config = { + "key1": "abc", + "key2": { + "$filter": "env", + "production": { + "deeper": { + "$value": "value" + } + }, + "$default": { + "$filter": "platform", + "android": 0, + "ios": 1, + "$default": 2 + } + }, + "key3": { + "sub1": 123, + "sub2": { + "$filter": "xfactor", + "yes": 6 + } + }, + "ab": { + "$filter": "random.a", + "$range": [ + { "limit": 10, "value": 4 }, + { "limit": 20, "value": 5 } + ], + "$default": 6 + }, + "$meta": { + "description": "example file" + } +}; + + +/** +* Creates an empty configuration storage container +*/ +let store = new Confidence.Store(config); + + +/** +* Validates the provided configuration, clears any existing configuration, then loads the configuration +*/ +store.load(config); + + +/** +* Retrieves a value from the configuration document after applying the provided criteria +*/ +store.get('/key1'); +//criteria - optional object +store.get('/key2', criteria); + + +/** +* Retrieves the metadata (if any) from the configuration document after applying the provided criteria +*/ +store.meta('/key1'); +//criteria - optional object +store.meta('/key2', criteria); diff --git a/confidence/confidence.d.ts b/confidence/confidence.d.ts new file mode 100644 index 000000000..4c30b4407 --- /dev/null +++ b/confidence/confidence.d.ts @@ -0,0 +1,48 @@ +// Type definitions for Confidence v1.4.2 +// Project: https://github.com/hapijs/confidence.git +// Definitions by: Jean-Philippe Pellerin +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +/** +* Confidence is a configuration document format, an API, and a foundation for A/B testing. +* The configuration format is designed to work with any existing JSON-based configuration, +* serving values based on object path ('/a/b/c' translates to a.b.c). In addition, +* confidence defines special $-prefixed keys used to filter values for a given criteria. +*/ +declare module 'confidence' { + + export class Store { + + /** + * @constructor + * @param {any} document - the configuration document for this document store + */ + constructor(document?: any); + + /** + * Validates the provided configuration, clears any existing configuration, then loads the configuration where: + * @param {any} document - an object containing a confidence configuration object generated from a parsed JSON document. If the document is invlaid, will throw an error. + */ + load(document: any): void; + + + /** + * Retrieves a value from the configuration document after applying the provided criteria where: + * @param {string} key - the requested key path. All keys must begin with '/'. '/' returns the the entire document. + * @param {any} criteria - optional object used as criteria for applying filters in the configuration document. Defaults to {}. + * + * @return {any} Returns the value found after applying the criteria. If the key is invalid or not found, returns undefined. + */ + get(key: string, criteria?: any): any; + + + /** + * Retrieves the metadata (if any) from the configuration document after applying the provided criteria where: + * @param {string} key - the requested key path. All keys must begin with '/'. '/' returns the the entire document. + * @param {any} criteria - optional object used as criteria for applying filters in the configuration document. Defaults to {}. + * + * @return {any} Returns the metadata found after applying the criteria. If the key is invalid or not found, or if no metadata is available, returns undefined. + */ + meta(key: string, criteria?: any): any; + } +}