From eae899753b1dfd5dd2f7aa15d69989bdc28f6d38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Valerio=20Coltr=C3=A8?= Date: Mon, 8 Jul 2019 23:48:17 +0200 Subject: [PATCH] [ldapjs-client] Basic Typings (#36243) * initialized ldapjs-client typings * client is a Class, not a factory function * removed JSDOC, fixed linting * fix tsconfig and typescript version. lint should go green * removing unnecessary generic * unknown type is supported after TS 3.0 * enabling npm-naming and complying to it * exporting interface in commonjs style, and minor cleanup --- types/ldapjs-client/index.d.ts | 88 ++++++++++++++++++++++ types/ldapjs-client/ldapjs-client-tests.ts | 44 +++++++++++ types/ldapjs-client/tsconfig.json | 24 ++++++ types/ldapjs-client/tslint.json | 3 + 4 files changed, 159 insertions(+) create mode 100644 types/ldapjs-client/index.d.ts create mode 100644 types/ldapjs-client/ldapjs-client-tests.ts create mode 100644 types/ldapjs-client/tsconfig.json create mode 100644 types/ldapjs-client/tslint.json diff --git a/types/ldapjs-client/index.d.ts b/types/ldapjs-client/index.d.ts new file mode 100644 index 0000000000..dec3e45925 --- /dev/null +++ b/types/ldapjs-client/index.d.ts @@ -0,0 +1,88 @@ +// Type definitions for ldapjs-client 0.1 +// Project: https://github.com/zont/ldapjs-client#readme +// Definitions by: Valerio Coltrè +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 3.0 + +declare namespace LdapClient { + interface Change { + operation: 'add' | 'delete' | 'replace'; + modification: { + [key: string]: any; + }; + } + + interface SearchOptions { + scope?: string; + filter?: string; + attributes?: string[]; + sizeLimit?: number; + timeLimit?: number; + typesOnly?: boolean; + } + + interface ClientOptions { + url: string; + tlsOptions?: object; + timeout?: number; + } +} + +declare class LdapClient { + constructor(options: LdapClient.ClientOptions); + + /** + * Adds an entry to the LDAP server. + */ + add(dn: string, entry: object): Promise; + + /** + * Performs a simple authentication against the server. + */ + bind(dn: string, password: string): Promise; + + /** + * Deletes an entry from the LDAP server. + */ + del(dn: string): Promise; + + /** + * Disconnect from the LDAP server and do not allow reconnection. + * + * If the client is instantiated with proper reconnection options, it's + * possible to initiate new requests after a call to unbind since the client + * will attempt to reconnect in order to fulfill the request. + * + * Calling destroy will prevent any further reconnection from occurring. + */ + destroy(): Promise; + + /** + * Performs an LDAP modify against the server. + * + * @param dn the DN of the entry to modify. + * @param change update to perform (can be [Change]). + */ + modify(dn: string, change: LdapClient.Change): Promise; + + /** + * Performs an LDAP modifyDN against the server. + */ + modifyDN(dn: string, newName: string): Promise; + + /** + * Performs an LDAP search against the server. + * + * Note that the defaults for options are a 'base' search. + */ + // tslint:disable-next-line: no-unnecessary-generics + search(base: string, options: LdapClient.SearchOptions): Promise; + + /** + * Unbinds this client from the LDAP server. + * + */ + unbind(): Promise; +} + +export = LdapClient; diff --git a/types/ldapjs-client/ldapjs-client-tests.ts b/types/ldapjs-client/ldapjs-client-tests.ts new file mode 100644 index 0000000000..ab577fa072 --- /dev/null +++ b/types/ldapjs-client/ldapjs-client-tests.ts @@ -0,0 +1,44 @@ +import LdapJS = require('ldapjs-client'); + +const clientOptions: LdapJS.ClientOptions = { + url: 'ldap://anyserver:389', +}; + +const client = new LdapJS(clientOptions); + +const dn = 'cn=foo, o=example'; + +interface LdapObject { + cn: string; + ou: string; +} + +(async () => { + await client.add(dn, { cn: 'foo' }); + + await client.bind(dn, 'pazzvord'); + await client.del(dn); + + const change: LdapJS.Change = { + operation: 'add', + modification: { + pets: ['cat', 'dog'], + }, + }; + + await client.modify(dn, change); + await client.modifyDN(dn, 'cn=baz, o=example'); + + const options: LdapJS.SearchOptions = { + filter: '(&(l=Seattle)(email=*@foo.com))', + scope: 'sub', + attributes: ['dn', 'sn', 'cn'], + }; + + const searchResult = await client.search(dn, options) as LdapObject[]; + type resultType = typeof searchResult; + + await client.unbind(); + + client.destroy(); +})(); diff --git a/types/ldapjs-client/tsconfig.json b/types/ldapjs-client/tsconfig.json new file mode 100644 index 0000000000..b66a13e4fd --- /dev/null +++ b/types/ldapjs-client/tsconfig.json @@ -0,0 +1,24 @@ +{ + "compilerOptions": { + "target": "es6", + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictFunctionTypes": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "ldapjs-client-tests.ts" + ] +} diff --git a/types/ldapjs-client/tslint.json b/types/ldapjs-client/tslint.json new file mode 100644 index 0000000000..f93cf8562a --- /dev/null +++ b/types/ldapjs-client/tslint.json @@ -0,0 +1,3 @@ +{ + "extends": "dtslint/dt.json" +}