From e6b24d9f7ed981016bcabaf155a9c64bc0bcf223 Mon Sep 17 00:00:00 2001 From: Alexey Timin Date: Fri, 23 Dec 2022 13:50:24 +0100 Subject: [PATCH] Add method Client.me to get current permissions (#51) * add method Client.me to get current permissions * update CHANGELOG --- CHANGELOG.md | 5 +++++ README.md | 4 ++-- src/Client.ts | 9 +++++++++ test/Client.test.ts | 2 +- test/Token.test.ts | 12 +++++++++--- 5 files changed, 26 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index baeb34a..e5131e9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added: + +- Add support for ReductStore API version 1.2 with method Client.me to get current + permissions, [PR-51](https://github.com/reductstore/reduct-js/pull/51) + ### Changed: - Update documentation after rebranding, [PR-50](https://github.com/reductstore/reduct-py/pull/50) diff --git a/README.md b/README.md index 48da6bd..38d9975 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ data stored in ReductStore. ## Features * Promise-based API for easy asynchronous programming -* Support for ReductStore API version 1.1 +* Support for ReductStore API version 1.2 * Token-based authentication for secure access to the database ## Getting Started @@ -56,4 +56,4 @@ main() ## References * [Documentation](https://js.reduct.store/) -* [ReductStore HTTP API](https://docs.reduct.store/http-api) \ No newline at end of file +* [ReductStore HTTP API](https://docs.reduct.store/http-api) diff --git a/src/Client.ts b/src/Client.ts index f5cc716..bc367b4 100644 --- a/src/Client.ts +++ b/src/Client.ts @@ -152,4 +152,13 @@ export class Client { async deleteToken(name: string): Promise { await this.httpClient.delete(`/tokens/${name}`); } + + /** + * Get current API token and its permissions + * @return {Promise} the token + */ + async me(): Promise { + const {data} = await this.httpClient.get("/me"); + return Token.parse(data); + } } diff --git a/test/Client.test.ts b/test/Client.test.ts index 67d57d6..6713926 100644 --- a/test/Client.test.ts +++ b/test/Client.test.ts @@ -50,7 +50,7 @@ describe("Client", () => { await rec.write("somedata"); const info: ServerInfo = await client.getInfo(); - expect(info.version >= "1.1.0").toBeTruthy(); + expect(info.version >= "1.2.0").toBeTruthy(); expect(info.bucketCount).toEqual(2n); expect(info.usage).toEqual(16n); diff --git a/test/Token.test.ts b/test/Token.test.ts index 70eed1e..8f3edd8 100644 --- a/test/Token.test.ts +++ b/test/Token.test.ts @@ -15,7 +15,7 @@ describe_token()("With Token API Client", () => { .then(() => done()); }); - test("should create a token", async () => { + it("should create a token", async () => { const permissions: TokenPermissions = { fullAccess: true, read: ["bucket_1"], @@ -30,7 +30,7 @@ describe_token()("With Token API Client", () => { expect(tokenInfo.createdAt).toBeLessThanOrEqual(Date.now()); }); - test("should list tokens", async () => { + it("should list tokens", async () => { expect(await client.getTokenList()).toEqual([ {name: "init-token", createdAt: expect.any(Number)} ]); @@ -43,7 +43,7 @@ describe_token()("With Token API Client", () => { ]); }); - test("should delete a token", async () => { + it("should delete a token", async () => { await client.createToken("token-1", {fullAccess: true}); await client.deleteToken("token-1"); @@ -51,4 +51,10 @@ describe_token()("With Token API Client", () => { {name: "init-token", createdAt: expect.any(Number)} ]); }); + + it("should provide current API token and its permissions", async () => { + const token: Token = await client.me(); + expect(token.name).toEqual("init-token"); + expect(token.permissions).toEqual({fullAccess: true, read: [], write: []}); + }); });