Skip to content

Commit

Permalink
feat: add TS libdefs
Browse files Browse the repository at this point in the history
  • Loading branch information
antongolub committed Jul 27, 2021
1 parent 7d4d70f commit d157e29
Show file tree
Hide file tree
Showing 4 changed files with 4,613 additions and 149 deletions.
58 changes: 58 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/// <reference types="node" />

import {
Change,
Control,
createClient as _createClient,
SearchCallbackResponse,
SearchEntryObject,
SearchOptions,
SearchReference
} from 'ldapjs'

import {EventEmitter} from 'events'

export * from 'ldapjs'

export interface Client extends EventEmitter {
// connected: boolean

bind(dn: string, password: string, controls?: Control | Array<Control>): Promise<void>

add(name: string, entry: Object, controls?: Control | Array<Control>): Promise<void>

compare(name: string, attr: string, value: string, controls?: Control | Array<Control>): Promise<boolean | undefined>

del(name: string, controls?: Control | Array<Control>): Promise<void>

exop(name: string, value: string, controls?: Control | Array<Control>): Promise<{
value: string,
response: any
}>

modify(name: string, change: Change | Array<Change>, controls?: Control | Array<Control>): Promise<void>

modifyDN(name: string, newName: string, controls?: Control | Array<Control>): Promise<void>

search(base: string, options?: SearchOptions, controls?: Control | Array<Control>): Promise<SearchCallbackResponse>

starttls(options: Object, controls?: Control | Array<Control>): Promise<void>

unbind(): Promise<void>

destroy(err?: any): Promise<void>

// Additional methods
searchReturnAll(base: string, options?: SearchOptions, controls?: Control | Array<Control>): Promise<{
entries: Array<SearchEntryObject>,
referrals: Array<SearchReference>
}>

findUser(base: string, username: string, options?: SearchOptions): Promise<any>

userInGroup(base: string, username: string, groupName: string): Promise<boolean>
}

export declare const createClient: {
(...args: Parameters<typeof _createClient>): Promise<Client>
}
69 changes: 69 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import {expectType} from 'tsd'
import {
createClient,
Client,
Change,
SearchCallbackResponse,
SearchEntryObject,
SearchReference
} from '.'

expectType<Promise<Client>>(createClient())

const client = await createClient()

expectType<Promise<void>>(client.bind('cn=foo', 'bar'))
expectType<Promise<void>>(client.bind('cn=foo', 'bar', {}))
expectType<Promise<void>>(client.bind('cn=foo', 'bar', []))
expectType<Promise<void>>(client.bind('cn=foo', 'bar', [{}]))

expectType<Promise<void>>(client.add('cn=foo', {}))
expectType<Promise<void>>(client.add('cn=foo', {}, []))
expectType<Promise<void>>(client.add('cn=foo', {}, {}))

expectType<Promise<boolean | undefined>>(client.compare('cn=foo', 'bar', 'baz'))
expectType<Promise<boolean | undefined>>(client.compare('cn=foo', 'bar', 'baz', {}))
expectType<Promise<boolean | undefined>>(client.compare('cn=foo', 'bar', 'baz', []))

expectType<Promise<void>>(client.del('cn=foo'))
expectType<Promise<void>>(client.del('cn=foo', {}))

expectType<Promise<{ value: string, response: any }>>(client.exop('cn=foo', 'bar'))
expectType<Promise<{ value: string, response: any }>>(client.exop('cn=foo', 'bar', {}))

const change: Change = {
operation: 'op',
modification: {}
}
expectType<Promise<void>>(client.modify('cn=foo', change))
expectType<Promise<void>>(client.modify('cn=foo', [change]))
expectType<Promise<void>>(client.modify('cn=foo', [change], []))

expectType<Promise<void>>(client.modifyDN('cn=foo', 'bar'))
expectType<Promise<void>>(client.modifyDN('cn=foo', 'bar', []))

expectType<Promise<SearchCallbackResponse>>(client.search('cn=foo'))
expectType<Promise<SearchCallbackResponse>>(client.search('cn=foo', {scope: 'sub'}))
expectType<Promise<SearchCallbackResponse>>(client.search('cn=foo', {filter: '(objectclass=*)'}))
expectType<Promise<SearchCallbackResponse>>(client.search('cn=foo', {filter: '(objectclass=*)'}, {}))

expectType<Promise<void>>(client.starttls('cn=foo'))
expectType<Promise<void>>(client.starttls('cn=foo', {}))

expectType<Promise<void>>(client.unbind())

expectType<Promise<void>>(client.destroy())
expectType<Promise<void>>(client.destroy(new Error()))

type SearchResult = {
entries: SearchEntryObject[],
referrals: SearchReference[]
}
expectType<Promise<SearchResult>>(client.searchReturnAll('cn=foo'))
expectType<Promise<SearchResult>>(client.searchReturnAll('cn=foo', {scope: 'base'}))
expectType<Promise<SearchResult>>(client.searchReturnAll('cn=foo', {scope: 'base'}, []))

expectType<Promise<any>>(client.findUser('cn=foo', 'bar'))
expectType<Promise<any>>(client.findUser('cn=foo', 'bar', {scope: 'sub'}))

expectType<Promise<boolean>>(client.userInGroup('cn=foo', 'bar', 'baz'))
Loading

0 comments on commit d157e29

Please sign in to comment.