11import { EventEmitter } from 'events' ;
22import {
3+ CallBack ,
34 Change ,
45 Client as _Client ,
56 ClientOptions as _ClientOptions ,
7+ CompareCallback ,
68 Control ,
79 createClient as _createClient ,
810 Error ,
11+ ErrorCallback ,
12+ SearchCallBack ,
913 SearchCallbackResponse ,
1014 SearchOptions ,
1115} from 'ldapjs' ;
@@ -28,53 +32,81 @@ export class Client extends EventEmitter {
2832
2933 bind ( dn : string , password : string , controls ?: Control | Control [ ] ) {
3034 return new Promise ( ( resolve , reject ) => {
31- this . ldapjs . bind ( dn , password , controls , ( error , result ) => {
35+ const cb : CallBack = ( error , result ) => {
3236 if ( error ) {
3337 reject ( error ) ;
3438 }
3539
3640 resolve ( result ) ;
37- } ) ;
41+ } ;
42+
43+ if ( ! controls ) {
44+ this . ldapjs . bind ( dn , password , cb ) ;
45+ } else {
46+ this . ldapjs . bind ( dn , password , controls , cb ) ;
47+ }
3848 } ) ;
3949 }
4050
4151 add ( name : string , entry : Record < string , any > , controls ?: Control | Control [ ] ) {
4252 return new Promise < void > ( ( resolve , reject ) => {
43- this . ldapjs . add ( name , entry , controls , ( error ) => {
44- ! error ? resolve ( ) : reject ( error ) ;
45- } ) ;
53+ const cb : ErrorCallback = ( error ) => ( ! error ? resolve ( ) : reject ( error ) ) ;
54+
55+ if ( ! controls ) {
56+ this . ldapjs . add ( name , entry , cb ) ;
57+ } else {
58+ this . ldapjs . add ( name , entry , controls , cb ) ;
59+ }
4660 } ) ;
4761 }
4862
4963 compare ( name : string , attr : string , value : string , controls ?: Control | Control [ ] ) {
5064 return new Promise < boolean > ( ( resolve , reject ) => {
51- this . ldapjs . compare ( name , attr , value , controls , ( error : Error | null , matched ?: boolean ) => {
65+ const cb : CompareCallback = ( error : Error | null , matched ?: boolean ) => {
5266 ! error ? resolve ( matched ?? false ) : reject ( error ) ;
53- } ) ;
67+ } ;
68+
69+ if ( ! controls ) {
70+ this . ldapjs . compare ( name , attr , value , cb ) ;
71+ } else {
72+ this . ldapjs . compare ( name , attr , value , controls , cb ) ;
73+ }
5474 } ) ;
5575 }
5676
5777 del ( name : string , controls ?: Control | Control [ ] ) {
5878 return new Promise < void > ( ( resolve , reject ) => {
59- this . ldapjs . del ( name , controls , ( error ) => {
60- ! error ? resolve ( ) : reject ( error ) ;
61- } ) ;
79+ const cb : ErrorCallback = ( error ) => ( ! error ? resolve ( ) : reject ( error ) ) ;
80+
81+ if ( controls ) {
82+ this . ldapjs . del ( name , cb ) ;
83+ } else {
84+ this . ldapjs . del ( name , controls , cb ) ;
85+ }
6286 } ) ;
6387 }
6488
6589 modify ( name : string , change : Change | Change [ ] , controls ?: Control | Control [ ] ) {
6690 return new Promise < void > ( ( resolve , reject ) => {
67- this . ldapjs . modify ( name , change , controls , ( error ) => {
68- ! error ? resolve ( ) : reject ( error ) ;
69- } ) ;
91+ const cb : ErrorCallback = ( error ) => ( ! error ? resolve ( ) : reject ( error ) ) ;
92+
93+ if ( controls ) {
94+ this . ldapjs . modify ( name , change , cb ) ;
95+ } else {
96+ this . ldapjs . modify ( name , change , controls , cb ) ;
97+ }
7098 } ) ;
7199 }
72100
73101 modifyDN ( name : string , newName : string , controls ?: Control | Control [ ] ) {
74102 return new Promise < void > ( ( resolve , reject ) => {
75- this . ldapjs . modifyDN ( name , newName , controls , ( error ) => {
76- ! error ? resolve ( ) : reject ( error ) ;
77- } ) ;
103+ const cb : ErrorCallback = ( error ) => ( ! error ? resolve ( ) : reject ( error ) ) ;
104+
105+ if ( controls ) {
106+ this . ldapjs . modifyDN ( name , newName , cb ) ;
107+ } else {
108+ this . ldapjs . modifyDN ( name , newName , controls , cb ) ;
109+ }
78110 } ) ;
79111 }
80112
@@ -85,28 +117,25 @@ export class Client extends EventEmitter {
85117 _bypass ?: boolean ,
86118 ) : Promise < SearchCallbackResponse > {
87119 return new Promise < SearchCallbackResponse > ( ( resolve , reject ) => {
88- this . ldapjs . search (
89- base ,
90- options ,
91- controls ,
92- ( error , result ) => {
93- ! error ? resolve ( result ) : reject ( error ) ;
94- } ,
95- _bypass ?? false ,
96- ) ;
120+ const cb : SearchCallBack = ( error , result ) => ( ! error ? resolve ( result ) : reject ( error ) ) ;
121+
122+ if ( ! controls ) {
123+ this . ldapjs . search ( base , options , cb , _bypass ?? false ) ;
124+ } else {
125+ this . ldapjs . search ( base , options , controls , cb , _bypass ?? false ) ;
126+ }
97127 } ) ;
98128 }
99129
100130 starttls ( options : Record < string , any > , controls ?: Control | Control [ ] , _bypass ?: boolean ) : Promise < void > {
101131 return new Promise < void > ( ( resolve , reject ) => {
102- this . ldapjs . starttls (
103- options ,
104- controls ,
105- ( error , result ) => {
106- ! error ? resolve ( result ) : reject ( error ) ;
107- } ,
108- _bypass ?? false ,
109- ) ;
132+ const cb : CallBack = ( error , result ) => ( ! error ? resolve ( result ) : reject ( error ) ) ;
133+
134+ if ( ! controls ) {
135+ this . ldapjs . starttls ( options , [ ] , cb , _bypass ?? false ) ; // @types /ldapjs are wrong here
136+ } else {
137+ this . ldapjs . starttls ( options , controls , cb , _bypass ?? false ) ;
138+ }
110139 } ) ;
111140 }
112141
0 commit comments