* chore: initial commit

* feat(mongodb-uri): add types for mongodb-uri

* refactor(mongodb-uri): adjust to pass lint test

* Revert "chore: initial commit"

This reverts commit 51dab6d
This commit is contained in:
Mengjo Rodrick Nfinyoh 2018-07-03 21:22:35 +01:00 committed by Mohamed Hegazy
parent c24807a001
commit 592acfc14d
4 changed files with 137 additions and 0 deletions

101
types/mongodb-uri/index.d.ts vendored Normal file
View File

@ -0,0 +1,101 @@
// Type definitions for mongodb-uri 0.9
// Project: https://github.com/mongolab/mongodb-uri-node
// Definitions by: mernxl <https://github.com/mernxl>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
export interface Host {
host: string;
port?: number;
}
export interface parserOptions {
scheme: string;
}
export interface UriObject {
scheme: string;
hosts: Host[];
username?: string;
password?: string;
database?: string;
options?: any;
}
export class MongodbUriParser {
constructor(options?: parserOptions);
/**
* Takes a URI of the form:
*
* mongodb://[username[:password]@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database]][?options]
*
* scheme and hosts will always be present. Other fields will only be present in the result if they were
* present in the input.
*/
parse(uri: string): UriObject;
/**
* Takes a URI object and returns a URI string of the form:
*
* mongodb://[username[:password]@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database]][?options]
*
*/
format(uriObject?: UriObject): string;
/**
* Takes either a URI object or string and returns a Mongoose connection string. Specifically, instead of listing all
* hosts and ports in a single URI, a Mongoose connection string contains a list of URIs each with a single host and
* port pair.
*
* Useful in environments where a MongoDB URI environment variable is provided, but needs to be programmatically
* transformed into a string digestible by mongoose.connect()--for example, when deploying to a PaaS like Heroku
* using a MongoDB add-on like MongoLab.
*
*/
formatMongoose(uri: UriObject | string): string;
}
/**
* Takes a URI of the form:
*
* mongodb://[username[:password]@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database]][?options]
*
* and returns an object of the form:
*
* {
* scheme: !String,
* username: String=,
* password: String=,
* hosts: [ { host: !String, port: Number= }, ... ],
* database: String=,
* options: Object=
* }
*
* scheme and hosts will always be present. Other fields will only be present in the result if they were
* present in the input.
*
*/
export function parse(uri: string): UriObject;
/**
* Takes a URI object and returns a URI string of the form:
*
* mongodb://[username[:password]@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database]][?options]
*
* @param uriObject
*/
export function format(uriObject?: UriObject): string;
/**
* Takes either a URI object or string and returns a Mongoose connection string. Specifically, instead of listing all
* hosts and ports in a single URI, a Mongoose connection string contains a list of URIs each with a single host and
* port pair.
*
* Useful in environments where a MongoDB URI environment variable is provided, but needs to be programmatically
* transformed into a string digestible by mongoose.connect()--for example, when deploying to a PaaS like Heroku
* using a MongoDB add-on like MongoLab.
*
* @param uri
*/
export function formatMongoose(uri: UriObject | string): string;

View File

@ -0,0 +1,12 @@
import { format, formatMongoose, MongodbUriParser, parse } from 'mongodb-uri';
const urlString = 'mongodb://localhost';
const parser = new MongodbUriParser();
const parsed1 = parser.parse(urlString);
const urlString1 = parser.format(parsed1);
const formatMongoose1 = parser.formatMongoose(parsed1);
const parsed2 = parse(urlString);
const urlString2 = format(parsed2);
const formatMongoose2 = formatMongoose(parsed2);

View File

@ -0,0 +1,23 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"mongodb-uri-tests.ts"
]
}

View File

@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }