@cinerino/sdk 18.1.0-alpha.5 → 18.1.0-alpha.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/abstract/transporters.d.ts +1 -0
- package/lib/abstract/transporters.js +23 -10
- package/lib/bundle.js +3 -3
- package/lib/bundle.js.map +4 -4
- package/package.json +2 -1
|
@@ -8,9 +8,15 @@ exports.StubTransporter = exports.DefaultTransporter = exports.RequestError = ex
|
|
|
8
8
|
* transporters
|
|
9
9
|
*/
|
|
10
10
|
const debug_1 = __importDefault(require("debug"));
|
|
11
|
-
// import fetch from 'isomorphic-fetch';
|
|
12
11
|
const debug = (0, debug_1.default)('cinerino-sdk:transporters');
|
|
13
|
-
|
|
12
|
+
let version = 'unknown';
|
|
13
|
+
try {
|
|
14
|
+
const pkg = require('../../package.json'); // eslint-disable-line @typescript-eslint/no-require-imports
|
|
15
|
+
version = pkg.version;
|
|
16
|
+
}
|
|
17
|
+
catch {
|
|
18
|
+
// 万が一ビルド後の配置で package.json が読めない場合のフォールバック
|
|
19
|
+
}
|
|
14
20
|
/**
|
|
15
21
|
* トランスポーター抽象クラス
|
|
16
22
|
*/
|
|
@@ -36,7 +42,7 @@ class DefaultTransporter {
|
|
|
36
42
|
/**
|
|
37
43
|
* Default user agent.
|
|
38
44
|
*/
|
|
39
|
-
|
|
45
|
+
static USER_AGENT = `@cinerino/sdk/${version}`;
|
|
40
46
|
expectedStatusCodes;
|
|
41
47
|
constructor(expectedStatusCodes) {
|
|
42
48
|
this.expectedStatusCodes = expectedStatusCodes;
|
|
@@ -45,14 +51,21 @@ class DefaultTransporter {
|
|
|
45
51
|
* Configures request options before making a request.
|
|
46
52
|
*/
|
|
47
53
|
static CONFIGURE(options) {
|
|
48
|
-
//
|
|
49
|
-
|
|
54
|
+
// ブラウザ環境では User-Agent の指定が拒否されるため、Node.js環境(サーバーサイド)でのみセットする
|
|
55
|
+
const isNode = typeof window === 'undefined' && typeof process !== 'undefined';
|
|
50
56
|
/* istanbul ignore else */
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
57
|
+
if (isNode) {
|
|
58
|
+
// Headers インスタンス化して大文字・小文字を吸収する
|
|
59
|
+
const headers = new Headers(options.headers);
|
|
60
|
+
const currentUserAgent = headers.get('user-agent');
|
|
61
|
+
if (!currentUserAgent) {
|
|
62
|
+
headers.set('user-agent', DefaultTransporter.USER_AGENT);
|
|
63
|
+
}
|
|
64
|
+
else if (!currentUserAgent.includes(DefaultTransporter.USER_AGENT)) {
|
|
65
|
+
headers.set('user-agent', `${currentUserAgent} ${DefaultTransporter.USER_AGENT}`);
|
|
66
|
+
}
|
|
67
|
+
options.headers = headers;
|
|
68
|
+
}
|
|
56
69
|
return options;
|
|
57
70
|
}
|
|
58
71
|
/**
|