Skip to content

Commit

Permalink
feat: notification.getItems (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
miyajan authored Aug 20, 2020
1 parent 4c3f905 commit 56e6c85
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 0 deletions.
42 changes: 42 additions & 0 deletions docs/notification.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Notification

- [getItems](#getitems)

## Overview

```ts
const client = new GaroonRestAPIClient();

(async () => {
try {
console.log(await client.notification.getItems());
} catch (error) {
console.log(error);
}
})();
```

- All methods are defined on the `notification` property.
- This method returns a Promise object that is resolved with an object having properties in each `Returns` section.

## Methods

### getItems

Get unread notifications.

#### Parameters

| Name | Type | Required | Description |
| ------ | :-------------: | :------: | ------------------------------------------------------------------------------------------------------------------------------------------ |
| limit | Number | | The number of notifications to retrieve.<br />Must be between `1` and `1000`.<br />If nothing is specified, it will default to `100`. |
| offset | Number | | The number of retrievals that will be skipped.<br />Must be between `0` and `2147483647`. If nothing is specified, it will default to `0`. |
| fields | Array\<String\> | | The response properties in `items` property to get. |

#### Returns

See the example response in the `Reference`.

#### Reference

- https://developer.cybozu.io/hc/ja/articles/360017764051#step1
3 changes: 3 additions & 0 deletions src/GaroonRestAPIClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { GaroonRequestConfigBuilder } from "./GaroonRequestConfigBuilder";
import { WorkflowClient } from "./client/WorkflowClient";
import { BaseClient } from "./client/BaseClient";
import { PresenceClient } from "./client/PresenceClient";
import { NotificationClient } from "./client/NotificationClient";

export type DiscriminatedAuth = PasswordAuth | SessionAuth | OAuthTokenAuth;

Expand Down Expand Up @@ -99,6 +100,7 @@ export class GaroonRestAPIClient {
readonly workflow: WorkflowClient;
readonly base: BaseClient;
readonly presence: PresenceClient;
readonly notification: NotificationClient;
private readonly baseUrl: string;

constructor(options: Options = {}) {
Expand All @@ -118,6 +120,7 @@ export class GaroonRestAPIClient {
this.workflow = new WorkflowClient(httpClient);
this.base = new BaseClient(httpClient);
this.presence = new PresenceClient(httpClient);
this.notification = new NotificationClient(httpClient);
}

public getBaseUrl(): string {
Expand Down
46 changes: 46 additions & 0 deletions src/client/NotificationClient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { HttpClient } from "../http";
import { buildPath } from "../url";

export class NotificationClient {
private readonly client: HttpClient;

constructor(client: HttpClient) {
this.client = client;
}

public getItems(params?: {
limit?: number;
offset?: number;
fields?: string[];
}): Promise<{
items: Array<{
moduleId: string;
creator: {
id: string;
code: string;
name: string;
};
createdAt: string;
operation: "add" | "modify" | "remove";
url: string;
title: string;
body: string;
icon: string;
isRead: boolean;
}>;
hasNext: boolean;
}> {
const path = buildPath({ endpointName: `notification/items` });

if (!params) {
return this.client.get(path, {});
}

const { fields, ...rest } = params;
const data: Record<string, unknown> = rest as Record<string, unknown>;
if (fields) {
data.fields = fields.join(",");
}
return this.client.get(path, data);
}
}
46 changes: 46 additions & 0 deletions src/client/__tests__/NotificationClient.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { MockClient } from "../../http/MockClient";
import { GaroonRequestConfigBuilder } from "../../GaroonRequestConfigBuilder";
import { errorResponseHandler } from "../../GaroonRestAPIClient";
import { NotificationClient } from "../NotificationClient";

describe("NotificationClient", () => {
let mockClient: MockClient;
let notificationClient: NotificationClient;

beforeEach(() => {
const requestConfigBuilder = new GaroonRequestConfigBuilder({
baseUrl: "https://example.cybozu.com/g",
auth: {
type: "password",
username: "cybozu",
password: "cybozu",
},
});
mockClient = new MockClient({ requestConfigBuilder, errorResponseHandler });
notificationClient = new NotificationClient(mockClient);
});

describe("getItems", () => {
const params = {
limit: 100,
offset: 0,
fields: ["moduleId", "creator"],
};
beforeEach(async () => {
await notificationClient.getItems(params);
});
it("should pass the path to the http client", () => {
expect(mockClient.getLogs()[0].path).toBe("/api/v1/notification/items");
});
it("should send a get request", () => {
expect(mockClient.getLogs()[0].method).toBe("get");
});
it("should pass limit, offset and fields as a param to the http client", () => {
expect(mockClient.getLogs()[0].params).toEqual({
limit: 100,
offset: 0,
fields: "moduleId,creator",
});
});
});
});

0 comments on commit 56e6c85

Please sign in to comment.