Skip to content

Commit e13be0e

Browse files
authored
Added getAllUsers (#13)
1 parent 952d68a commit e13be0e

File tree

4 files changed

+45
-6
lines changed

4 files changed

+45
-6
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,20 @@
55
],
66
"name": "@ofs-users/proxy",
77
"type": "module",
8-
"version": "1.6.0",
8+
"version": "1.7.2",
99
"description": "A Javascript proxy to access Oracle Field Service via REST API",
1010
"main": "dist/ofs.es.js",
1111
"module": "dist/ofs.es.js",
1212
"types": "dist/OFS.d.ts",
1313
"repository": {
14-
"url": "https://github.com/oracle-samples/ofs-proxy-js"
14+
"url": "git+https://github.com/oracle-samples/ofs-proxy-js.git"
1515
},
1616
"scripts": {
1717
"build": "tsc",
1818
"start": "ts-node src/index.ts",
1919
"test": "jest",
2020
"dist": "rollup --config rollup.config.mjs",
21-
"prepare": "npm run dist"
21+
"prepare": "rollup --config rollup.config.mjs"
2222
},
2323
"keywords": [
2424
"ofsc",
@@ -66,4 +66,4 @@
6666
"dependencies": {
6767
"tslib": "^2.4.1"
6868
}
69-
}
69+
}

src/OFS.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,38 @@ export class OFS {
287287
return this._get(partialURL, { offset: offset, limit: limit });
288288
}
289289

290+
/**
291+
* Retrieves all users from the OFS API.
292+
* @returns An object containing all users.
293+
*/
294+
async getAllUsers() {
295+
const partialURL = "/rest/ofscCore/v1/users";
296+
// Start with offset 0 and keep getting results until we get less than 100
297+
var offset = 0;
298+
var limit = 100;
299+
var result: any = undefined;
300+
var allResults: any = { totalResults: 0, items: [] };
301+
do {
302+
result = await this._get(partialURL, {
303+
offset: offset,
304+
limit: limit,
305+
});
306+
if (result.status < 400) {
307+
if (allResults.totalResults == 0) {
308+
allResults = result.data;
309+
} else {
310+
allResults.items = allResults.items.concat(
311+
result.data.items
312+
);
313+
}
314+
offset += limit;
315+
} else {
316+
return result;
317+
}
318+
} while (result.data.items.length == limit);
319+
return allResults;
320+
}
321+
290322
async getUserDetails(uname: string): Promise<OFSResponse> {
291323
const partialURL = `/rest/ofscCore/v1/users/${uname}`;
292324
return this._get(partialURL);

test/general/base.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,10 @@ test("Get User Details", async () => {
140140
expect(result.data.login).toBe("admin");
141141
console.log(result.data);
142142
});
143+
144+
test("Get all Users", async () => {
145+
var result = await myProxy.getAllUsers();
146+
expect(result.totalResults).toBeGreaterThan(200);
147+
expect(result.items.length).toEqual(result.totalResults);
148+
expect(result.items[0].login).toBe("admin");
149+
});

0 commit comments

Comments
 (0)