diff --git a/types/mongodb-uri/index.d.ts b/types/mongodb-uri/index.d.ts new file mode 100644 index 0000000000..6d6d9c1839 --- /dev/null +++ b/types/mongodb-uri/index.d.ts @@ -0,0 +1,101 @@ +// Type definitions for mongodb-uri 0.9 +// Project: https://github.com/mongolab/mongodb-uri-node +// Definitions by: 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; diff --git a/types/mongodb-uri/mongodb-uri-tests.ts b/types/mongodb-uri/mongodb-uri-tests.ts new file mode 100644 index 0000000000..2befe964ed --- /dev/null +++ b/types/mongodb-uri/mongodb-uri-tests.ts @@ -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); diff --git a/types/mongodb-uri/tsconfig.json b/types/mongodb-uri/tsconfig.json new file mode 100644 index 0000000000..68ca627c9a --- /dev/null +++ b/types/mongodb-uri/tsconfig.json @@ -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" + ] +} diff --git a/types/mongodb-uri/tslint.json b/types/mongodb-uri/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/mongodb-uri/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }