Skip to content

Commit

Permalink
Add method Client.me to get current permissions (#51)
Browse files Browse the repository at this point in the history
* add method Client.me to get current permissions

* update CHANGELOG
  • Loading branch information
atimin committed Dec 23, 2022
1 parent ca843a1 commit e6b24d9
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 6 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -56,4 +56,4 @@ main()
## References

* [Documentation](https://js.reduct.store/)
* [ReductStore HTTP API](https://docs.reduct.store/http-api)
* [ReductStore HTTP API](https://docs.reduct.store/http-api)
9 changes: 9 additions & 0 deletions src/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,13 @@ export class Client {
async deleteToken(name: string): Promise<void> {
await this.httpClient.delete(`/tokens/${name}`);
}

/**
* Get current API token and its permissions
* @return {Promise<Token>} the token
*/
async me(): Promise<Token> {
const {data} = await this.httpClient.get("/me");
return Token.parse(data);
}
}
2 changes: 1 addition & 1 deletion test/Client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
12 changes: 9 additions & 3 deletions test/Token.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand All @@ -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)}
]);
Expand All @@ -43,12 +43,18 @@ 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");

expect(await client.getTokenList()).toEqual([
{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: []});
});
});

0 comments on commit e6b24d9

Please sign in to comment.