Skip to content

Commit 4322ca9

Browse files
authored
feat: support on-premise environment (#27)
1 parent f4f4314 commit 4322ca9

File tree

6 files changed

+85
-21
lines changed

6 files changed

+85
-21
lines changed

README.md

+18-18
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ Here is a sample code that retrieves a schedule event.
4343

4444
```js
4545
const client = new GaroonRestAPIClient({
46-
baseUrl: "https://example.cybozu.com",
46+
baseUrl: "https://example.cybozu.com/g",
4747
// Use password authentication
4848
auth: {
4949
username: process.env.GAROON_USERNAME,
@@ -67,23 +67,23 @@ client.schedule
6767

6868
## Parameters for `GaroonRestAPIClient`
6969

70-
| Name | Type | Required | Description |
71-
| -------------------------- | :--------------------------------------------------------------: | :-------------------------: | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
72-
| baseUrl | String | Conditionally<br />Required | The base URL for your Garoon environment.<br />It must start with `https`. (e.g. https://example.cybozu.com) <br />Required in Node.js environment. If you omit it in browser environment, `location.origin` will be used. |
73-
| auth | Object | Conditionally<br />Required | The object for authentication. See [Authentication](#Authentication). |
74-
| basicAuth | Object | | If your Garoon environment uses Basic authentication, please specify its username and password. |
75-
| basicAuth.username | String | | The username of Basic authentication. |
76-
| basicAuth.password | String | | The password of Basic authentication. |
77-
| clientCertAuth | Object | | **This parameter is available only in Node.js environment.**<br />If your Garoon environment uses [Client Certificate authentication](https://jp.cybozu.help/general/en/admin/list_security/list_secureaccess/overview.html), please specify the certificate file and password. |
78-
| clientCertAuth.pfx | [Buffer](https://nodejs.org/api/buffer.html#buffer_class_buffer) | | The [client certificate file](https://jp.cybozu.help/general/en/user/list_access/remote/webbrowser.html). Required, unless you specify `pfxFilePath`. |
79-
| clientCertAuth.pfxFilePath | String | | The path to [client certificate file](https://jp.cybozu.help/general/en/user/list_access/remote/webbrowser.html). Required, unless you specify `pfx`. |
80-
| clientCertAuth.password | String | | The password of client certificate. |
81-
| proxy | Object | | **This parameter is available only in Node.js environment.**<br />If you use a proxy, please specify its configuration. |
82-
| proxy.host | String | | The host of the proxy server. |
83-
| proxy.port | Number | | The port of the proxy server. |
84-
| proxy.auth | Object | | If the proxy server requires Basic authentication, please specify its username and password. |
85-
| proxy.auth.username | String | | The username of Basic authentication for the proxy server. |
86-
| proxy.auth.password | String | | The password of Basic authentication for the proxy server. |
70+
| Name | Type | Required | Description |
71+
| -------------------------- | :--------------------------------------------------------------: | :-------------------------: | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
72+
| baseUrl | String | Conditionally<br />Required | The base URL for your Garoon environment.<br />On cybozu.com, it must end with `/g`. (e.g. https://example.cybozu.com/g) <br />If you use on-premise Garoon environment, it must end with `grn.cgi` or `grn.exe` (e.g. http://example.com/cgi-bin/cbgrn/grn.cgi) <br />Required in Node.js environment. If you omit it in browser environment, the base URL will be determined from `location`. |
73+
| auth | Object | Conditionally<br />Required | The object for authentication. See [Authentication](#Authentication). |
74+
| basicAuth | Object | | If your Garoon environment uses Basic authentication, please specify its username and password. |
75+
| basicAuth.username | String | | The username of Basic authentication. |
76+
| basicAuth.password | String | | The password of Basic authentication. |
77+
| clientCertAuth | Object | | **This parameter is available only in Node.js environment.**<br />If your Garoon environment uses [Client Certificate authentication](https://jp.cybozu.help/general/en/admin/list_security/list_secureaccess/overview.html), please specify the certificate file and password. |
78+
| clientCertAuth.pfx | [Buffer](https://nodejs.org/api/buffer.html#buffer_class_buffer) | | The [client certificate file](https://jp.cybozu.help/general/en/user/list_access/remote/webbrowser.html). Required, unless you specify `pfxFilePath`. |
79+
| clientCertAuth.pfxFilePath | String | | The path to [client certificate file](https://jp.cybozu.help/general/en/user/list_access/remote/webbrowser.html). Required, unless you specify `pfx`. |
80+
| clientCertAuth.password | String | | The password of client certificate. |
81+
| proxy | Object | | **This parameter is available only in Node.js environment.**<br />If you use a proxy, please specify its configuration. |
82+
| proxy.host | String | | The host of the proxy server. |
83+
| proxy.port | Number | | The port of the proxy server. |
84+
| proxy.auth | Object | | If the proxy server requires Basic authentication, please specify its username and password. |
85+
| proxy.auth.username | String | | The username of Basic authentication for the proxy server. |
86+
| proxy.auth.password | String | | The password of Basic authentication for the proxy server. |
8787

8888
### Authentication
8989

src/__tests__/url.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { buildPath } from "../url";
33
describe("buildPath", () => {
44
it("should build path", () => {
55
expect(buildPath({ endpointName: "schedule/events/1" })).toBe(
6-
"/g/api/v1/schedule/events/1"
6+
"/api/v1/schedule/events/1"
77
);
88
});
99
});
+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { buildBaseUrl } from "../browser";
2+
3+
describe("buildBaseUrl", () => {
4+
describe("when cybozu.com", () => {
5+
beforeEach(() => {
6+
(global as any).location = {
7+
protocol: "https:",
8+
host: "example.cybozu.com",
9+
href: "https://example.cybozu.com",
10+
};
11+
});
12+
13+
it("should build base url from location when baseUrl is not specified", () => {
14+
expect(buildBaseUrl()).toBe("https://example.cybozu.com/g");
15+
});
16+
17+
it("should return baseUrl when baseUrl is specified", () => {
18+
expect(buildBaseUrl("https://hoge.cybozu.com/g")).toBe(
19+
"https://hoge.cybozu.com/g"
20+
);
21+
});
22+
});
23+
24+
describe("when on-premise", () => {
25+
beforeEach(() => {
26+
(global as any).location = {
27+
protocol: "http:",
28+
host: "example.com",
29+
href: "http://example.com/cgi-bin/cbgrn/grn.cgi/index",
30+
};
31+
});
32+
33+
it("should build base url from location when baseUrl is not specified", () => {
34+
expect(buildBaseUrl()).toBe("http://example.com/cgi-bin/cbgrn/grn.cgi");
35+
});
36+
37+
it("should return baseUrl when baseUrl is specified", () => {
38+
expect(buildBaseUrl("http://hoge.com/scripts/garoon/grn.exe")).toBe(
39+
"http://hoge.com/scripts/garoon/grn.exe"
40+
);
41+
});
42+
});
43+
});

src/platform/browser.ts

+21-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,27 @@ export const buildFormDataValue = (data: unknown) => {
3636
};
3737

3838
export const buildBaseUrl = (baseUrl?: string) => {
39+
if (baseUrl) {
40+
return baseUrl;
41+
}
42+
3943
// We assume that location always exists in a browser environment
44+
45+
// on-premise
46+
if (location!.href.includes("grn.cgi")) {
47+
return location!.href.substring(
48+
0,
49+
location!.href.indexOf("grn.cgi") + "grn.cgi".length
50+
);
51+
}
52+
if (location!.href.includes("grn.exe")) {
53+
return location!.href.substring(
54+
0,
55+
location!.href.indexOf("grn.exe") + "grn.exe".length
56+
);
57+
}
58+
59+
// cybozu.com
4060
const { host, protocol } = location!;
41-
return baseUrl ?? `${protocol}//${host}`;
61+
return baseUrl ?? `${protocol}//${host}/g`;
4262
};

src/types/global/index.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ declare const location:
1717
| {
1818
host: string;
1919
protocol: string;
20+
href: string;
2021
}
2122
| undefined;
2223

src/url.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
export const buildPath = (params: { endpointName: string }) => {
22
const { endpointName } = params;
3-
return `/g/api/v1/${endpointName}`;
3+
return `/api/v1/${endpointName}`;
44
};

0 commit comments

Comments
 (0)