Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add method Client.me to get current permissions #51

Merged
merged 2 commits into from
Dec 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: []});
});
});