@cinerino/sdk 18.1.0-alpha.5 → 18.1.0-alpha.7

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.
@@ -20,6 +20,7 @@ export declare class DefaultTransporter implements Transporter {
20
20
  /**
21
21
  * Default user agent.
22
22
  */
23
+ static readonly USER_AGENT: string;
23
24
  expectedStatusCodes: number[];
24
25
  constructor(expectedStatusCodes: number[]);
25
26
  /**
@@ -8,9 +8,24 @@ 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
- // const pkg = require('../package.json');
12
+ let packageInfo = { name: 'unknown', version: 'unknown' };
13
+ try {
14
+ packageInfo = require('../../package.json'); // eslint-disable-line @typescript-eslint/no-require-imports
15
+ }
16
+ catch {
17
+ // 万が一ビルド後の配置で package.json が読めない場合のフォールバック
18
+ }
19
+ // 存在する環境変数のみを配列で抽出し、半角スペースで結合する
20
+ // process が未定義のブラウザ環境でも ReferenceError にならないよう安全に参照する
21
+ /* istanbul ignore next */
22
+ const gaeEnvParts = [
23
+ typeof process !== 'undefined' ? process.env.GAE_APPLICATION : undefined,
24
+ typeof process !== 'undefined' ? process.env.GAE_SERVICE : undefined,
25
+ typeof process !== 'undefined' ? process.env.GAE_VERSION : undefined,
26
+ ].filter((val) => typeof val === 'string' && val.trim().length > 0);
27
+ // GAE情報とパッケージ情報を結合
28
+ const defaultUserAgentHeader4node = [...gaeEnvParts, `${packageInfo.name}/${packageInfo.version}`].join(' ');
14
29
  /**
15
30
  * トランスポーター抽象クラス
16
31
  */
@@ -36,7 +51,7 @@ class DefaultTransporter {
36
51
  /**
37
52
  * Default user agent.
38
53
  */
39
- // public static readonly USER_AGENT: string = `cinerino-sdk/${pkg.version}`;
54
+ static USER_AGENT = defaultUserAgentHeader4node;
40
55
  expectedStatusCodes;
41
56
  constructor(expectedStatusCodes) {
42
57
  this.expectedStatusCodes = expectedStatusCodes;
@@ -45,14 +60,21 @@ class DefaultTransporter {
45
60
  * Configures request options before making a request.
46
61
  */
47
62
  static CONFIGURE(options) {
48
- // set transporter user agent
49
- // options.headers = (options.headers !== undefined) ? options.headers : {};
63
+ // ブラウザ環境では User-Agent の指定が拒否されるため、Node.js環境(サーバーサイド)でのみセットする
64
+ const isNode = typeof window === 'undefined' && typeof process !== 'undefined';
50
65
  /* istanbul ignore else */
51
- // if (!(<any>options.headers)['User-Agent']) {
52
- // (<any>options.headers)['User-Agent'] = DefaultTransporter.USER_AGENT;
53
- // } else if ((<any>options.headers)['User-Agent'].indexOf(DefaultTransporter.USER_AGENT) === -1) {
54
- // (<any>options.headers)['User-Agent'] = `${(<any>options.headers)['User-Agent']} ${DefaultTransporter.USER_AGENT}`;
55
- // }
66
+ if (isNode) {
67
+ // Headers インスタンス化して大文字・小文字を吸収する
68
+ const headers = new Headers(options.headers);
69
+ const currentUserAgent = headers.get('user-agent');
70
+ if (!currentUserAgent) {
71
+ headers.set('user-agent', DefaultTransporter.USER_AGENT);
72
+ }
73
+ else if (!currentUserAgent.includes(DefaultTransporter.USER_AGENT)) {
74
+ headers.set('user-agent', `${currentUserAgent} ${DefaultTransporter.USER_AGENT}`);
75
+ }
76
+ options.headers = headers;
77
+ }
56
78
  return options;
57
79
  }
58
80
  /**