Skip to content

Commit 521315d

Browse files
author
Mathieu Bour
committed
feat(client): controls arguments are now optional
Bump version to 0.1.2
1 parent f0762f2 commit 521315d

File tree

4 files changed

+69
-36
lines changed

4 files changed

+69
-36
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
# Changelog
22

3-
## [Version 0.1.1 - Export `Client`](https://github.com/csquare-ai/ldapjs-client/releases/tag/0.1.0)
3+
## [Version 0.1.2 - Optional Controls](https://github.com/csquare-ai/ldapjs-client/releases/tag/0.1.2)
4+
5+
- **feat**: controls arguments are now optional
6+
7+
## [Version 0.1.1 - Export `Client`](https://github.com/csquare-ai/ldapjs-client/releases/tag/0.1.1)
48

59
- **feat**: export `Client` class
610

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@csquare/ldapjs-client",
3-
"version": "0.1.1",
3+
"version": "0.1.2",
44
"description": "Promisified wrapper around the ldapjs client.",
55
"keywords": [
66
"ldap",

src/client.ts

Lines changed: 62 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
import { EventEmitter } from 'events';
22
import {
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

Comments
 (0)