@motionpicture/coa-service 9.3.0-alpha.0 → 9.3.0-alpha.2

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.
@@ -12,7 +12,8 @@ const service = new COA.service.Master(
12
12
  endpoint: process.env.COA_ENDPOINT,
13
13
  refreshToken: process.env.COA_REFRESH_TOKEN
14
14
  })
15
- }
15
+ },
16
+ { timeout: 1000 }
16
17
  );
17
18
 
18
19
  service.title({
@@ -9,7 +9,8 @@ const service = new COA.service.Master(
9
9
  endpoint: process.env.COA_ENDPOINT,
10
10
  auth: new COA.auth.RefreshToken({
11
11
  endpoint: process.env.COA_ENDPOINT,
12
- refreshToken: process.env.COA_REFRESH_TOKEN
12
+ refreshToken: process.env.COA_REFRESH_TOKEN,
13
+ useFetch: true
13
14
  })
14
15
  }
15
16
  );
@@ -16,7 +16,7 @@ const service = new COA.service.Master(
16
16
  );
17
17
 
18
18
  service.schedule({
19
- theaterCode: '012',
19
+ theaterCode: '120',
20
20
  begin: '20200213',
21
21
  end: '20200218'
22
22
  }).then((result) => {
@@ -15,7 +15,7 @@ const service = new COA.service.Master(
15
15
  );
16
16
 
17
17
  service.screen({
18
- theaterCode: '118'
18
+ theaterCode: '120'
19
19
  }).then((result) => {
20
20
  fs.writeFileSync(`${__dirname}/output/screen.json`, JSON.stringify(result, null, ' '));
21
21
  console.log(result);
@@ -15,7 +15,7 @@ const service = new COA.service.Master(
15
15
  );
16
16
 
17
17
  service.theater({
18
- theaterCode: '118'
18
+ theaterCode: '120'
19
19
  }).then((result) => {
20
20
  fs.writeFileSync(`${__dirname}/output/theater.json`, JSON.stringify(result, null, ' '));
21
21
  console.log(result);
@@ -15,7 +15,7 @@ const service = new COA.service.Master(
15
15
  );
16
16
 
17
17
  service.ticket({
18
- theaterCode: '118'
18
+ theaterCode: '120'
19
19
  }).then((result) => {
20
20
  fs.writeFileSync(`${__dirname}/output/ticket.json`, JSON.stringify(result, null, ' '));
21
21
  console.log(result);
@@ -8,14 +8,15 @@ const service = new COA.service.Reserve({
8
8
  endpoint: process.env.COA_ENDPOINT,
9
9
  auth: new COA.auth.RefreshToken({
10
10
  endpoint: process.env.COA_ENDPOINT,
11
- refreshToken: process.env.COA_REFRESH_TOKEN
11
+ refreshToken: process.env.COA_REFRESH_TOKEN,
12
+ useFetch: true
12
13
  })
13
14
  });
14
15
 
15
16
  service.stateReserve({
16
- theaterCode: '118',
17
- reserveNum: '99150',
18
- telNum: '09012345678'
17
+ theaterCode: '112',
18
+ reserveNum: '588456',
19
+ telNum: '0362778824'
19
20
  }).then((result) => {
20
21
  fs.writeFileSync(`${__dirname}/output/stateReserve.json`, JSON.stringify(result, null, ' '));
21
22
  console.log(result);
@@ -15,7 +15,7 @@ const service = new COA.service.Reserve(
15
15
  );
16
16
 
17
17
  service.stateReserveSeat({
18
- theaterCode: '118',
18
+ theaterCode: '120',
19
19
  dateJouei: '20190605',
20
20
  titleCode: '16421',
21
21
  titleBranchNum: '0',
@@ -0,0 +1,21 @@
1
+ /**
2
+ * 購入チケット内容取得の例
3
+ */
4
+ const COA = require('../');
5
+ const fs = require('fs');
6
+
7
+ const service = new COA.service.Reserve({
8
+ endpoint: process.env.COA_ENDPOINT,
9
+ auth: new COA.auth.RefreshToken({
10
+ endpoint: process.env.COA_ENDPOINT,
11
+ refreshToken: process.env.COA_REFRESH_TOKEN
12
+ })
13
+ });
14
+
15
+ service.updReserve({
16
+ }).then((result) => {
17
+ fs.writeFileSync(`${__dirname}/output/updReserve.json`, JSON.stringify(result, null, ' '));
18
+ console.log(result);
19
+ }).catch((err) => {
20
+ console.error(err);
21
+ });
@@ -3,6 +3,7 @@ import { ICredentials } from './credentials';
3
3
  export interface IOptions {
4
4
  endpoint: string;
5
5
  refreshToken?: string;
6
+ useFetch: boolean;
6
7
  }
7
8
  export { IRequestOptions };
8
9
  /**
@@ -208,6 +208,8 @@ class RefreshTokenClient {
208
208
  /* istanbul ignore else */
209
209
  if (error instanceof Error) {
210
210
  const statusCode = error.code;
211
+ // tslint:disable-next-line:no-single-line-block-comment
212
+ /* istanbul ignore else */
211
213
  if (statusCode === http_status_1.UNAUTHORIZED || statusCode === http_status_1.FORBIDDEN) {
212
214
  if (retry) {
213
215
  // 多くの場合、認証エラーは、トークンの期限が原因なので、一度だけリフレッシュするのは有効なはず。
@@ -234,10 +236,11 @@ class RefreshTokenClient {
234
236
  /**
235
237
  * 認証情報が適切である前提でAPIリクエストを投げる
236
238
  */
237
- // tslint:disable-next-line:prefer-function-over-method
238
239
  makeRequest(options, expectedStatusCodes) {
239
240
  return __awaiter(this, void 0, void 0, function* () {
240
- const transporter = new transporters_1.DefaultTransporter(expectedStatusCodes);
241
+ const transporter = (this.options.useFetch === true)
242
+ ? new transporters_1.FetchTransporter(expectedStatusCodes)
243
+ : new transporters_1.DefaultTransporter(expectedStatusCodes);
241
244
  return transporter.request(options);
242
245
  });
243
246
  }
@@ -1,5 +1,10 @@
1
1
  import * as request from 'request';
2
- export type IRequestOptions = Pick<request.OptionsWithUri, 'uri' | 'method' | 'qs' | 'auth' | 'headers'>;
2
+ /**
3
+ * リクエスト成功の場合のレスポンス本文のstatus属性の値
4
+ * 失敗の場合はstring型だが、成功の場合のみnumber型が返却されるので注意すること。
5
+ */
6
+ export declare const RESPONSE_BODY_STAUS_SUCCESS = 0;
7
+ export type IRequestOptions = Pick<request.OptionsWithUri, 'uri' | 'method' | 'qs' | 'auth' | 'headers' | 'baseUrl' | 'timeout'>;
3
8
  /**
4
9
  * transporter abstract class
5
10
  * トランスポーター抽象クラス
@@ -34,7 +39,30 @@ export declare class DefaultTransporter implements Transporter {
34
39
  /**
35
40
  * Makes a request with given options and invokes callback.
36
41
  */
37
- request(options: Pick<request.OptionsWithUri, 'uri' | 'method' | 'qs' | 'headers'>): Promise<any>;
42
+ request(options: IRequestOptions): Promise<any>;
43
+ /**
44
+ * Wraps the response callback.
45
+ */
46
+ private wrapCallback;
47
+ }
48
+ /**
49
+ * FetchTransporter
50
+ */
51
+ export declare class FetchTransporter implements Transporter {
52
+ /**
53
+ * Default user agent.
54
+ */
55
+ static readonly USER_AGENT: string;
56
+ expectedStatusCodes: number[];
57
+ constructor(expectedStatusCodes: number[]);
58
+ /**
59
+ * Configures request options before making a request.
60
+ */
61
+ static CONFIGURE(options: IRequestOptions): IRequestOptions;
62
+ /**
63
+ * Makes a request with given options and invokes callback.
64
+ */
65
+ request(options: IRequestOptions): Promise<any>;
38
66
  /**
39
67
  * Wraps the response callback.
40
68
  */
@@ -9,13 +9,14 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
9
9
  });
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.DefaultTransporter = exports.COAServiceError = exports.Transporter = void 0;
12
+ exports.FetchTransporter = exports.DefaultTransporter = exports.COAServiceError = exports.Transporter = exports.RESPONSE_BODY_STAUS_SUCCESS = void 0;
13
13
  // tslint:disable:max-classes-per-file
14
14
  /**
15
15
  * transporters
16
16
  */
17
17
  const createDebug = require("debug");
18
18
  const http_status_1 = require("http-status");
19
+ const querystring = require("querystring");
19
20
  const request = require("request");
20
21
  const debug = createDebug('coa-service:transporters');
21
22
  // tslint:disable-next-line
@@ -24,7 +25,7 @@ const pkg = require('../package.json');
24
25
  * リクエスト成功の場合のレスポンス本文のstatus属性の値
25
26
  * 失敗の場合はstring型だが、成功の場合のみnumber型が返却されるので注意すること。
26
27
  */
27
- const RESPONSE_BODY_STAUS_SUCCESS = 0;
28
+ exports.RESPONSE_BODY_STAUS_SUCCESS = 0;
28
29
  /**
29
30
  * transporter abstract class
30
31
  * トランスポーター抽象クラス
@@ -122,7 +123,7 @@ class DefaultTransporter {
122
123
  }
123
124
  else {
124
125
  // HTTPステータスコード2xxでも、レスポンス本文のステータスが0でなければBadRequest
125
- if (body.status !== undefined && body.status !== RESPONSE_BODY_STAUS_SUCCESS) {
126
+ if (body.status !== undefined && body.status !== exports.RESPONSE_BODY_STAUS_SUCCESS) {
126
127
  err = new COAServiceError(response.statusCode, body.status, body.message, requestOptions);
127
128
  }
128
129
  else {
@@ -148,3 +149,113 @@ class DefaultTransporter {
148
149
  */
149
150
  DefaultTransporter.USER_AGENT = `coa-service/${pkg.version}`;
150
151
  exports.DefaultTransporter = DefaultTransporter;
152
+ /**
153
+ * FetchTransporter
154
+ */
155
+ class FetchTransporter {
156
+ constructor(expectedStatusCodes) {
157
+ this.expectedStatusCodes = expectedStatusCodes;
158
+ }
159
+ /**
160
+ * Configures request options before making a request.
161
+ */
162
+ static CONFIGURE(options) {
163
+ // set transporter user agent
164
+ options.headers = (options.headers !== undefined) ? options.headers : {};
165
+ // tslint:disable-next-line:no-single-line-block-comment
166
+ /* istanbul ignore else */
167
+ if (!options.headers['User-Agent']) {
168
+ options.headers['User-Agent'] = DefaultTransporter.USER_AGENT;
169
+ }
170
+ else if (options.headers['User-Agent'].indexOf(DefaultTransporter.USER_AGENT) === -1) {
171
+ options.headers['User-Agent'] = `${options.headers['User-Agent']} ${DefaultTransporter.USER_AGENT}`;
172
+ }
173
+ else {
174
+ // no operation
175
+ }
176
+ return options;
177
+ }
178
+ /**
179
+ * Makes a request with given options and invokes callback.
180
+ */
181
+ request(options) {
182
+ var _a;
183
+ return __awaiter(this, void 0, void 0, function* () {
184
+ const requestOptions = FetchTransporter.CONFIGURE(options);
185
+ debug('requesting...', requestOptions);
186
+ const input = `${requestOptions.baseUrl}${requestOptions.uri}?${querystring.stringify(requestOptions.qs)}`;
187
+ const accessToken = (_a = requestOptions.auth) === null || _a === void 0 ? void 0 : _a.bearer;
188
+ debug('fetching...', input);
189
+ return fetch(input, Object.assign({ method: requestOptions.method, headers: Object.assign(Object.assign({}, requestOptions.headers), (typeof accessToken === 'string') ? { Authorization: `Bearer ${accessToken}` } /* istanbul ignore else */ : undefined) }, (typeof requestOptions.timeout === 'number') ? { signal: AbortSignal.timeout(requestOptions.timeout) } : undefined))
190
+ .then((res) => __awaiter(this, void 0, void 0, function* () { return this.wrapCallback(res, requestOptions); }));
191
+ });
192
+ }
193
+ /**
194
+ * Wraps the response callback.
195
+ */
196
+ wrapCallback(res, options) {
197
+ return __awaiter(this, void 0, void 0, function* () {
198
+ const requestOptions = { uri: options.uri };
199
+ let err = new COAServiceError(http_status_1.INTERNAL_SERVER_ERROR, '', 'An unexpected error occurred.', requestOptions);
200
+ debug('fetch processed. res.status:', res.status, 'content-type:', res.headers.get('content-type'));
201
+ let body = {};
202
+ try {
203
+ if (res.headers.get('content-type') === 'text/plain') {
204
+ body = yield res.text();
205
+ }
206
+ else {
207
+ body = yield res.json();
208
+ }
209
+ }
210
+ catch (error) {
211
+ // no op
212
+ }
213
+ // tslint:disable-next-line:no-single-line-block-comment
214
+ /* istanbul ignore else */
215
+ if (res.status !== undefined) {
216
+ if (this.expectedStatusCodes.indexOf(res.status) < 0) {
217
+ if (typeof body === 'string') {
218
+ // Consider all 4xx and 5xx responses errors.
219
+ err = new COAServiceError(res.status, '', body, requestOptions);
220
+ }
221
+ else {
222
+ // エラーレスポンスにステータスがあった場合
223
+ // tslint:disable-next-line:no-single-line-block-comment
224
+ /* istanbul ignore else */
225
+ if (body.status !== undefined) {
226
+ err = new COAServiceError(res.status, body.status, body.message, requestOptions);
227
+ }
228
+ else {
229
+ // no operation
230
+ }
231
+ }
232
+ }
233
+ else {
234
+ // HTTPステータスコード2xxでも、レスポンス本文のステータスが0でなければBadRequest
235
+ if (body.status !== undefined && body.status !== exports.RESPONSE_BODY_STAUS_SUCCESS) {
236
+ err = new COAServiceError(res.status, body.status, body.message, requestOptions);
237
+ }
238
+ else {
239
+ if (res.status === http_status_1.NO_CONTENT) {
240
+ // consider 204
241
+ return;
242
+ }
243
+ else {
244
+ // consider 200,201
245
+ return body;
246
+ }
247
+ }
248
+ }
249
+ }
250
+ else {
251
+ // no operation
252
+ }
253
+ throw err;
254
+ });
255
+ }
256
+ }
257
+ /**
258
+ * Default user agent.
259
+ */
260
+ FetchTransporter.USER_AGENT = `coa-service/${pkg.version}`;
261
+ exports.FetchTransporter = FetchTransporter;
package/package.json CHANGED
@@ -78,5 +78,5 @@
78
78
  "postversion": "git push origin --tags",
79
79
  "prepublishOnly": "npm run clean && npm run build && npm test && npm run doc"
80
80
  },
81
- "version": "9.3.0-alpha.0"
81
+ "version": "9.3.0-alpha.2"
82
82
  }