Skip to content

Commit

Permalink
feat: base.getUsers (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
miyajan authored Jul 14, 2020
1 parent 00390a8 commit b36e14e
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 0 deletions.
42 changes: 42 additions & 0 deletions docs/base.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Base

- [getUsers](#getusers)

## Overview

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

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

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

## Methods

### getUsers

Get users specified by conditions.

#### Parameters

| Name | Type | Required | Description |
| ------ | :----: | :------: | ------------------------------------------------------------------------------------------------------------------------------------------ |
| limit | Number | | The number of users 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`. |
| name | String | | The name for searching users. |

#### Returns

See the example response in the `Reference`.

#### Reference

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

export type DiscriminatedAuth = PasswordAuth | SessionAuth | OAuthTokenAuth;

Expand Down Expand Up @@ -95,6 +96,7 @@ const buildDiscriminatedAuth = (auth: Auth): DiscriminatedAuth => {
export class GaroonRestAPIClient {
readonly schedule: ScheduleClient;
readonly workflow: WorkflowClient;
readonly base: BaseClient;
private readonly baseUrl: string;

constructor(options: Options = {}) {
Expand All @@ -112,6 +114,7 @@ export class GaroonRestAPIClient {
});
this.schedule = new ScheduleClient(httpClient);
this.workflow = new WorkflowClient(httpClient);
this.base = new BaseClient(httpClient);
}

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

export class BaseClient {
private readonly client: HttpClient;

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

public getUsers(params?: {
limit?: number;
offset?: number;
name?: string;
}): Promise<{
users: Array<{
id: string;
name: string;
code: string;
}>;
}> {
const path = buildPath({ endpointName: "base/users" });
return this.client.get(path, params ?? {});
}
}
42 changes: 42 additions & 0 deletions src/client/__tests__/BaseClient.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { MockClient } from "../../http/MockClient";
import { GaroonRequestConfigBuilder } from "../../GaroonRequestConfigBuilder";
import { errorResponseHandler } from "../../GaroonRestAPIClient";
import { BaseClient } from "../BaseClient";

describe("BaseClient", () => {
let mockClient: MockClient;
let baseClient: BaseClient;

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

describe("getUsers", () => {
const params = {
limit: 100,
offset: 0,
name: "test",
};
beforeEach(async () => {
await baseClient.getUsers(params);
});
it("should pass the path to the http client", () => {
expect(mockClient.getLogs()[0].path).toBe("/api/v1/base/users");
});
it("should send a get request", () => {
expect(mockClient.getLogs()[0].method).toBe("get");
});
it("should pass params as a param to the http client", () => {
expect(mockClient.getLogs()[0].params).toEqual(params);
});
});
});

0 comments on commit b36e14e

Please sign in to comment.