@motionpicture/coa-service 9.3.0-alpha.3 → 9.3.0-alpha.5
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/README.md +0 -5
- package/example/delReserve.js +10 -9
- package/example/findFilms.js +2 -1
- package/lib/auth/refreshTokenClient.d.ts +2 -6
- package/lib/auth/refreshTokenClient.js +4 -50
- package/lib/index.d.ts +3 -5
- package/lib/index.js +5 -6
- package/lib/service.d.ts +6 -4
- package/lib/service.js +1 -3
- package/lib/transporter/coaServiceError.d.ts +10 -0
- package/lib/transporter/coaServiceError.js +16 -0
- package/lib/transporter/requestOption.d.ts +16 -0
- package/lib/transporter/requestOption.js +2 -0
- package/lib/transporters.d.ts +17 -37
- package/lib/transporters.js +126 -150
- package/package.json +3 -7
package/README.md
CHANGED
|
@@ -17,7 +17,6 @@ COA SDK for Node.js
|
|
|
17
17
|
- [Environment variables](#environment-variables)
|
|
18
18
|
- [劇場情報を取得する](#劇場情報を取得する)
|
|
19
19
|
- [Code Samples](#code-samples)
|
|
20
|
-
- [Documentation](#documentation)
|
|
21
20
|
|
|
22
21
|
## Specification
|
|
23
22
|
|
|
@@ -73,7 +72,3 @@ masterService.theater({
|
|
|
73
72
|
## Code Samples
|
|
74
73
|
|
|
75
74
|
Code sample are [here](https://github.com/motionpicture/coa-service/tree/master/example).
|
|
76
|
-
|
|
77
|
-
## Documentation
|
|
78
|
-
|
|
79
|
-
`npm run doc` emits typedoc to ./docs.
|
package/example/delReserve.js
CHANGED
|
@@ -14,25 +14,26 @@ const service = new COA.service.Reserve(
|
|
|
14
14
|
);
|
|
15
15
|
|
|
16
16
|
service.delReserve({
|
|
17
|
-
reserveNum: '
|
|
18
|
-
theaterCode: '
|
|
19
|
-
dateJouei:
|
|
20
|
-
titleCode:
|
|
21
|
-
titleBranchNum:
|
|
22
|
-
timeBegin:
|
|
23
|
-
telNum: '
|
|
17
|
+
reserveNum: '45701',
|
|
18
|
+
theaterCode: '120',
|
|
19
|
+
"dateJouei": "20240125",
|
|
20
|
+
"titleCode": "99100",
|
|
21
|
+
"titleBranchNum": "0",
|
|
22
|
+
"timeBegin": "1720",
|
|
23
|
+
telNum: '09096793896',
|
|
24
24
|
listSeat: [
|
|
25
25
|
{
|
|
26
26
|
seatSection: '0',
|
|
27
|
-
seatNum:
|
|
27
|
+
"seatNum": "b-18",
|
|
28
28
|
},
|
|
29
29
|
{
|
|
30
30
|
seatSection: '0',
|
|
31
|
-
seatNum:
|
|
31
|
+
"seatNum": "b-19",
|
|
32
32
|
}
|
|
33
33
|
]
|
|
34
34
|
}).then((result) => {
|
|
35
35
|
console.log(result);
|
|
36
36
|
}).catch((err) => {
|
|
37
37
|
console.error(err);
|
|
38
|
+
console.log('err instanceof COA.COAServiceError?:', err instanceof COA.COAServiceError);
|
|
38
39
|
});
|
package/example/findFilms.js
CHANGED
|
@@ -10,7 +10,8 @@ const service = new COA.service.Master(
|
|
|
10
10
|
endpoint: process.env.COA_ENDPOINT,
|
|
11
11
|
auth: new COA.auth.RefreshToken({
|
|
12
12
|
endpoint: process.env.COA_ENDPOINT,
|
|
13
|
-
refreshToken: process.env.COA_REFRESH_TOKEN
|
|
13
|
+
refreshToken: process.env.COA_REFRESH_TOKEN,
|
|
14
|
+
useFetch: true
|
|
14
15
|
})
|
|
15
16
|
},
|
|
16
17
|
{ timeout: 1000 }
|
|
@@ -17,10 +17,6 @@ export declare class RefreshTokenClient {
|
|
|
17
17
|
credentials: ICredentials;
|
|
18
18
|
options: IOptions;
|
|
19
19
|
constructor(options: IOptions);
|
|
20
|
-
/**
|
|
21
|
-
* トークンエンドポイントからアクセストークンを取得します。
|
|
22
|
-
*/
|
|
23
|
-
getTokenWithRequest(): Promise<ICredentials>;
|
|
24
20
|
/**
|
|
25
21
|
* トークンエンドポイントからアクセストークンを取得します。
|
|
26
22
|
*/
|
|
@@ -42,11 +38,11 @@ export declare class RefreshTokenClient {
|
|
|
42
38
|
* APIリクエストを投げる
|
|
43
39
|
* 認証エラー(401,403)であれば自動的に一度だけアクセストークンをリフレッシュします。
|
|
44
40
|
*/
|
|
45
|
-
request(options: IRequestOptions, expectedStatusCodes: number[]): Promise<
|
|
41
|
+
request(options: IRequestOptions, expectedStatusCodes: number[]): Promise<import("../transporters").IResponseBodyAsJson>;
|
|
46
42
|
/**
|
|
47
43
|
* 認証情報が適切である前提でAPIリクエストを投げる
|
|
48
44
|
*/
|
|
49
|
-
protected makeRequest(options: IRequestOptions, expectedStatusCodes: number[]): Promise<
|
|
45
|
+
protected makeRequest(options: IRequestOptions, expectedStatusCodes: number[]): Promise<import("../transporters").IResponseBodyAsJson>;
|
|
50
46
|
/**
|
|
51
47
|
* 認証情報を更新する
|
|
52
48
|
*/
|
|
@@ -13,10 +13,10 @@ exports.RefreshTokenClient = void 0;
|
|
|
13
13
|
const createDebug = require("debug");
|
|
14
14
|
const http_status_1 = require("http-status");
|
|
15
15
|
const node_fetch_1 = require("node-fetch");
|
|
16
|
-
const request = require("request");
|
|
17
16
|
const transporters_1 = require("../transporters");
|
|
18
17
|
const debug = createDebug('coa-service:auth:refreshTokenClient');
|
|
19
18
|
const TIMEOUT = 10000;
|
|
19
|
+
const MAX_NUM_TRY_REQUEST = 1;
|
|
20
20
|
/**
|
|
21
21
|
* リフレッシュトークン認証クライアント
|
|
22
22
|
*/
|
|
@@ -25,48 +25,6 @@ class RefreshTokenClient {
|
|
|
25
25
|
this.options = options;
|
|
26
26
|
this.credentials = {};
|
|
27
27
|
}
|
|
28
|
-
/**
|
|
29
|
-
* トークンエンドポイントからアクセストークンを取得します。
|
|
30
|
-
*/
|
|
31
|
-
getTokenWithRequest() {
|
|
32
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
33
|
-
return new Promise((resolve, reject) => {
|
|
34
|
-
debug('requesting an access token...');
|
|
35
|
-
request.post({
|
|
36
|
-
baseUrl: this.options.endpoint,
|
|
37
|
-
uri: '/token/access_token',
|
|
38
|
-
form: {
|
|
39
|
-
refresh_token: this.options.refreshToken
|
|
40
|
-
},
|
|
41
|
-
json: true
|
|
42
|
-
}, (error, response, body) => {
|
|
43
|
-
if (error instanceof Error) {
|
|
44
|
-
reject(new Error(error.message));
|
|
45
|
-
return;
|
|
46
|
-
}
|
|
47
|
-
if (response.statusCode !== http_status_1.OK) {
|
|
48
|
-
let err = new Error('Unexpected error occurred.');
|
|
49
|
-
if (typeof body === 'string') {
|
|
50
|
-
err = new Error(body);
|
|
51
|
-
}
|
|
52
|
-
else if (typeof body.message === 'string' && body.message.length > 0) {
|
|
53
|
-
// エラーレスポンスにメッセージがあった場合
|
|
54
|
-
err = new Error(body.message);
|
|
55
|
-
}
|
|
56
|
-
else if (body.status !== undefined && body.status !== 0) {
|
|
57
|
-
// エラーレスポンスにステータスがあった場合
|
|
58
|
-
err = new Error(body.status);
|
|
59
|
-
}
|
|
60
|
-
reject(err);
|
|
61
|
-
}
|
|
62
|
-
else {
|
|
63
|
-
this.credentials = body;
|
|
64
|
-
resolve(this.credentials);
|
|
65
|
-
}
|
|
66
|
-
});
|
|
67
|
-
});
|
|
68
|
-
});
|
|
69
|
-
}
|
|
70
28
|
/**
|
|
71
29
|
* トークンエンドポイントからアクセストークンを取得します。
|
|
72
30
|
*/
|
|
@@ -193,11 +151,10 @@ class RefreshTokenClient {
|
|
|
193
151
|
let retry = true;
|
|
194
152
|
let result;
|
|
195
153
|
let numberOfTry = 0;
|
|
196
|
-
|
|
197
|
-
while (numberOfTry >= 0) {
|
|
154
|
+
while (result === undefined) {
|
|
198
155
|
try {
|
|
199
156
|
numberOfTry += 1;
|
|
200
|
-
if (numberOfTry >
|
|
157
|
+
if (numberOfTry > MAX_NUM_TRY_REQUEST) {
|
|
201
158
|
retry = false;
|
|
202
159
|
}
|
|
203
160
|
options.auth = { bearer: yield this.getAccessToken() };
|
|
@@ -225,9 +182,6 @@ class RefreshTokenClient {
|
|
|
225
182
|
}
|
|
226
183
|
}
|
|
227
184
|
}
|
|
228
|
-
else {
|
|
229
|
-
// no operation
|
|
230
|
-
}
|
|
231
185
|
throw error;
|
|
232
186
|
}
|
|
233
187
|
}
|
|
@@ -241,7 +195,7 @@ class RefreshTokenClient {
|
|
|
241
195
|
return __awaiter(this, void 0, void 0, function* () {
|
|
242
196
|
const transporter = (this.options.useFetch === true)
|
|
243
197
|
? new transporters_1.FetchTransporter(expectedStatusCodes)
|
|
244
|
-
: new transporters_1.
|
|
198
|
+
: new transporters_1.FetchTransporter(expectedStatusCodes);
|
|
245
199
|
return transporter.request(options);
|
|
246
200
|
});
|
|
247
201
|
}
|
package/lib/index.d.ts
CHANGED
|
@@ -2,10 +2,11 @@
|
|
|
2
2
|
* coa-service
|
|
3
3
|
*/
|
|
4
4
|
import * as factory from './factory';
|
|
5
|
+
import { RefreshTokenClient } from './auth/refreshTokenClient';
|
|
5
6
|
import { MasterService } from './service/master';
|
|
6
7
|
import { ReserveService } from './service/reserve';
|
|
7
|
-
import {
|
|
8
|
-
export
|
|
8
|
+
import { COAServiceError } from './transporter/coaServiceError';
|
|
9
|
+
export { COAServiceError, factory };
|
|
9
10
|
export declare namespace auth {
|
|
10
11
|
/**
|
|
11
12
|
* リフレッシュトークン認証クライアント
|
|
@@ -13,9 +14,6 @@ export declare namespace auth {
|
|
|
13
14
|
class RefreshToken extends RefreshTokenClient {
|
|
14
15
|
}
|
|
15
16
|
}
|
|
16
|
-
/**
|
|
17
|
-
* recommended
|
|
18
|
-
*/
|
|
19
17
|
export declare namespace service {
|
|
20
18
|
/**
|
|
21
19
|
* マスターサービス
|
package/lib/index.js
CHANGED
|
@@ -1,14 +1,16 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.service = exports.auth = exports.factory = void 0;
|
|
3
|
+
exports.service = exports.auth = exports.factory = exports.COAServiceError = void 0;
|
|
4
4
|
/**
|
|
5
5
|
* coa-service
|
|
6
6
|
*/
|
|
7
7
|
const factory = require("./factory");
|
|
8
|
+
exports.factory = factory;
|
|
9
|
+
const refreshTokenClient_1 = require("./auth/refreshTokenClient");
|
|
8
10
|
const master_1 = require("./service/master");
|
|
9
11
|
const reserve_1 = require("./service/reserve");
|
|
10
|
-
const
|
|
11
|
-
exports.
|
|
12
|
+
const coaServiceError_1 = require("./transporter/coaServiceError");
|
|
13
|
+
Object.defineProperty(exports, "COAServiceError", { enumerable: true, get: function () { return coaServiceError_1.COAServiceError; } });
|
|
12
14
|
var auth;
|
|
13
15
|
(function (auth) {
|
|
14
16
|
/**
|
|
@@ -18,9 +20,6 @@ var auth;
|
|
|
18
20
|
}
|
|
19
21
|
auth.RefreshToken = RefreshToken;
|
|
20
22
|
})(auth = exports.auth || (exports.auth = {}));
|
|
21
|
-
/**
|
|
22
|
-
* recommended
|
|
23
|
-
*/
|
|
24
23
|
var service;
|
|
25
24
|
(function (service) {
|
|
26
25
|
/**
|
package/lib/service.d.ts
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
|
-
import * as request from 'request';
|
|
2
1
|
import { IRequestOptions, RefreshTokenClient } from './auth/refreshTokenClient';
|
|
3
2
|
/**
|
|
4
3
|
* service constructor options
|
|
5
4
|
*/
|
|
6
|
-
|
|
5
|
+
interface IOptions {
|
|
7
6
|
endpoint: string;
|
|
8
7
|
auth: RefreshTokenClient;
|
|
9
8
|
}
|
|
10
|
-
|
|
9
|
+
interface ICustomRequestOption {
|
|
10
|
+
timeout?: number;
|
|
11
|
+
}
|
|
11
12
|
/**
|
|
12
13
|
* base service class
|
|
13
14
|
*/
|
|
@@ -18,5 +19,6 @@ export declare class Service {
|
|
|
18
19
|
/**
|
|
19
20
|
* Create and send request to API
|
|
20
21
|
*/
|
|
21
|
-
request(options: IRequestOptions, expectedStatusCodes: number[]): Promise<
|
|
22
|
+
request(options: IRequestOptions, expectedStatusCodes: number[]): Promise<import("./transporters").IResponseBodyAsJson>;
|
|
22
23
|
}
|
|
24
|
+
export {};
|
package/lib/service.js
CHANGED
|
@@ -23,9 +23,7 @@ class Service {
|
|
|
23
23
|
*/
|
|
24
24
|
request(options, expectedStatusCodes) {
|
|
25
25
|
return __awaiter(this, void 0, void 0, function* () {
|
|
26
|
-
const requestOptions = Object.assign(Object.assign({ baseUrl: this.options.endpoint,
|
|
27
|
-
// json: true,
|
|
28
|
-
method: 'GET' }, this.requestOptions), options);
|
|
26
|
+
const requestOptions = Object.assign(Object.assign({ baseUrl: this.options.endpoint, method: 'GET' }, this.requestOptions), options);
|
|
29
27
|
return this.options.auth.request(requestOptions, expectedStatusCodes);
|
|
30
28
|
});
|
|
31
29
|
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { IRequestOptions } from './requestOption';
|
|
2
|
+
/**
|
|
3
|
+
* COAServiceError
|
|
4
|
+
*/
|
|
5
|
+
export declare class COAServiceError extends Error {
|
|
6
|
+
code: number;
|
|
7
|
+
status: string;
|
|
8
|
+
requestOptions: IRequestOptions | undefined;
|
|
9
|
+
constructor(code: number, status: string, message?: string, requestOptions?: IRequestOptions);
|
|
10
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.COAServiceError = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* COAServiceError
|
|
6
|
+
*/
|
|
7
|
+
class COAServiceError extends Error {
|
|
8
|
+
constructor(code, status, message, requestOptions) {
|
|
9
|
+
super(message);
|
|
10
|
+
this.name = 'COAServiceError';
|
|
11
|
+
this.code = code;
|
|
12
|
+
this.status = status;
|
|
13
|
+
this.requestOptions = requestOptions;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
exports.COAServiceError = COAServiceError;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
interface IHeaders {
|
|
2
|
+
[key: string]: any;
|
|
3
|
+
}
|
|
4
|
+
type IQS = Record<string, string | number | any[]>;
|
|
5
|
+
export interface IRequestOptions {
|
|
6
|
+
baseUrl?: string;
|
|
7
|
+
auth?: {
|
|
8
|
+
bearer?: string;
|
|
9
|
+
};
|
|
10
|
+
qs?: IQS;
|
|
11
|
+
method?: string;
|
|
12
|
+
headers?: IHeaders;
|
|
13
|
+
timeout?: number;
|
|
14
|
+
uri: string;
|
|
15
|
+
}
|
|
16
|
+
export {};
|
package/lib/transporters.d.ts
CHANGED
|
@@ -1,49 +1,29 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { COAServiceError } from './transporter/coaServiceError';
|
|
2
|
+
import { IRequestOptions } from './transporter/requestOption';
|
|
2
3
|
/**
|
|
3
4
|
* リクエスト成功の場合のレスポンス本文のstatus属性の値
|
|
4
5
|
* 失敗の場合はstring型だが、成功の場合のみnumber型が返却されるので注意すること。
|
|
5
6
|
*/
|
|
6
7
|
export declare const RESPONSE_BODY_STAUS_SUCCESS = 0;
|
|
7
|
-
export
|
|
8
|
+
export { COAServiceError, IRequestOptions };
|
|
9
|
+
export type IResponseBodyAsJson = {
|
|
10
|
+
/**
|
|
11
|
+
* COAの提示するステータス
|
|
12
|
+
* 0でなければBadRequest
|
|
13
|
+
*/
|
|
14
|
+
status?: string | number;
|
|
15
|
+
/**
|
|
16
|
+
* COAの提示するメッセージ
|
|
17
|
+
*/
|
|
18
|
+
message?: string;
|
|
19
|
+
} & Record<string, any>;
|
|
20
|
+
export type IResponseBody = IResponseBodyAsJson;
|
|
8
21
|
/**
|
|
9
22
|
* transporter abstract class
|
|
10
23
|
* トランスポーター抽象クラス
|
|
11
24
|
*/
|
|
12
25
|
export declare abstract class Transporter {
|
|
13
|
-
abstract request(options: IRequestOptions): Promise<
|
|
14
|
-
}
|
|
15
|
-
export type IBodyResponseCallback = Promise<any>;
|
|
16
|
-
/**
|
|
17
|
-
* COAServiceError
|
|
18
|
-
*/
|
|
19
|
-
export declare class COAServiceError extends Error {
|
|
20
|
-
code: number;
|
|
21
|
-
status: string;
|
|
22
|
-
requestOptions: IRequestOptions | undefined;
|
|
23
|
-
constructor(code: number, status: string, message?: string, requestOptions?: IRequestOptions);
|
|
24
|
-
}
|
|
25
|
-
/**
|
|
26
|
-
* DefaultTransporter
|
|
27
|
-
*/
|
|
28
|
-
export declare class DefaultTransporter implements Transporter {
|
|
29
|
-
/**
|
|
30
|
-
* Default user agent.
|
|
31
|
-
*/
|
|
32
|
-
static readonly USER_AGENT: string;
|
|
33
|
-
expectedStatusCodes: number[];
|
|
34
|
-
constructor(expectedStatusCodes: number[]);
|
|
35
|
-
/**
|
|
36
|
-
* Configures request options before making a request.
|
|
37
|
-
*/
|
|
38
|
-
static CONFIGURE(options: IRequestOptions): IRequestOptions & Pick<request.OptionsWithUri, 'json' | 'useQuerystring'>;
|
|
39
|
-
/**
|
|
40
|
-
* Makes a request with given options and invokes callback.
|
|
41
|
-
*/
|
|
42
|
-
request(options: IRequestOptions): Promise<any>;
|
|
43
|
-
/**
|
|
44
|
-
* Wraps the response callback.
|
|
45
|
-
*/
|
|
46
|
-
private wrapCallback;
|
|
26
|
+
abstract request(options: IRequestOptions): Promise<IResponseBody>;
|
|
47
27
|
}
|
|
48
28
|
/**
|
|
49
29
|
* FetchTransporter
|
|
@@ -62,7 +42,7 @@ export declare class FetchTransporter implements Transporter {
|
|
|
62
42
|
/**
|
|
63
43
|
* Makes a request with given options and invokes callback.
|
|
64
44
|
*/
|
|
65
|
-
request(options: IRequestOptions): Promise<
|
|
45
|
+
request(options: IRequestOptions): Promise<IResponseBody>;
|
|
66
46
|
/**
|
|
67
47
|
* Wraps the response callback.
|
|
68
48
|
*/
|
package/lib/transporters.js
CHANGED
|
@@ -9,7 +9,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
9
9
|
});
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.FetchTransporter = exports.
|
|
12
|
+
exports.FetchTransporter = exports.Transporter = exports.COAServiceError = exports.RESPONSE_BODY_STAUS_SUCCESS = void 0;
|
|
13
13
|
// tslint:disable:max-classes-per-file
|
|
14
14
|
/**
|
|
15
15
|
* transporters
|
|
@@ -18,7 +18,8 @@ const createDebug = require("debug");
|
|
|
18
18
|
const http_status_1 = require("http-status");
|
|
19
19
|
const node_fetch_1 = require("node-fetch");
|
|
20
20
|
const querystring = require("querystring");
|
|
21
|
-
const
|
|
21
|
+
const coaServiceError_1 = require("./transporter/coaServiceError");
|
|
22
|
+
Object.defineProperty(exports, "COAServiceError", { enumerable: true, get: function () { return coaServiceError_1.COAServiceError; } });
|
|
22
23
|
const debug = createDebug('coa-service:transporters');
|
|
23
24
|
// tslint:disable-next-line
|
|
24
25
|
const pkg = require('../package.json');
|
|
@@ -34,123 +35,104 @@ exports.RESPONSE_BODY_STAUS_SUCCESS = 0;
|
|
|
34
35
|
class Transporter {
|
|
35
36
|
}
|
|
36
37
|
exports.Transporter = Transporter;
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
}
|
|
136
|
-
else {
|
|
137
|
-
// consider 200,201
|
|
138
|
-
return body;
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
else {
|
|
144
|
-
// no operation
|
|
145
|
-
}
|
|
146
|
-
throw err;
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
/**
|
|
150
|
-
* Default user agent.
|
|
151
|
-
*/
|
|
152
|
-
DefaultTransporter.USER_AGENT = `coa-service/${pkg.version}`;
|
|
153
|
-
exports.DefaultTransporter = DefaultTransporter;
|
|
38
|
+
// export class DefaultTransporter implements Transporter {
|
|
39
|
+
// /**
|
|
40
|
+
// * Default user agent.
|
|
41
|
+
// */
|
|
42
|
+
// public static readonly USER_AGENT: string = `coa-service/${pkg.version}`;
|
|
43
|
+
// public expectedStatusCodes: number[];
|
|
44
|
+
// constructor(expectedStatusCodes: number[]) {
|
|
45
|
+
// this.expectedStatusCodes = expectedStatusCodes;
|
|
46
|
+
// }
|
|
47
|
+
// /**
|
|
48
|
+
// * Configures request options before making a request.
|
|
49
|
+
// */
|
|
50
|
+
// public static CONFIGURE(
|
|
51
|
+
// options: IRequestOptions
|
|
52
|
+
// ): IRequestOptions & Pick<request.OptionsWithUri, 'json' | 'useQuerystring'> {
|
|
53
|
+
// // set transporter user agent
|
|
54
|
+
// options.headers = (options.headers !== undefined) ? options.headers : {};
|
|
55
|
+
// // tslint:disable-next-line:no-single-line-block-comment
|
|
56
|
+
// /* istanbul ignore else */
|
|
57
|
+
// if (!options.headers['User-Agent']) {
|
|
58
|
+
// options.headers['User-Agent'] = DefaultTransporter.USER_AGENT;
|
|
59
|
+
// } else if (options.headers['User-Agent'].indexOf(DefaultTransporter.USER_AGENT) === -1) {
|
|
60
|
+
// options.headers['User-Agent'] = `${options.headers['User-Agent']} ${DefaultTransporter.USER_AGENT}`;
|
|
61
|
+
// } else {
|
|
62
|
+
// // no operation
|
|
63
|
+
// }
|
|
64
|
+
// return {
|
|
65
|
+
// ...options,
|
|
66
|
+
// json: true,
|
|
67
|
+
// useQuerystring: true
|
|
68
|
+
// };
|
|
69
|
+
// }
|
|
70
|
+
// /**
|
|
71
|
+
// * Makes a request with given options and invokes callback.
|
|
72
|
+
// */
|
|
73
|
+
// public async request(options: IRequestOptions): Promise<IResponseBody> {
|
|
74
|
+
// const requestOptions = DefaultTransporter.CONFIGURE(options);
|
|
75
|
+
// debug('requesting...', requestOptions);
|
|
76
|
+
// return new Promise<any>((resolve, reject) => {
|
|
77
|
+
// request(requestOptions, (error, response, body) => {
|
|
78
|
+
// try {
|
|
79
|
+
// resolve(this.wrapCallback(error, response, body, requestOptions));
|
|
80
|
+
// } catch (callbackErr) {
|
|
81
|
+
// reject(callbackErr);
|
|
82
|
+
// }
|
|
83
|
+
// });
|
|
84
|
+
// });
|
|
85
|
+
// }
|
|
86
|
+
// /**
|
|
87
|
+
// * Wraps the response callback.
|
|
88
|
+
// */
|
|
89
|
+
// private wrapCallback(error: any, response: request.RequestResponse, body: any, options: IRequestOptions): IResponseBody {
|
|
90
|
+
// const requestOptions = { uri: options.uri };
|
|
91
|
+
// let err: COAServiceError =
|
|
92
|
+
// new COAServiceError(INTERNAL_SERVER_ERROR, '', 'An unexpected error occurred.', requestOptions);
|
|
93
|
+
// if (error instanceof Error) {
|
|
94
|
+
// throw new COAServiceError(INTERNAL_SERVER_ERROR, '', error.message, requestOptions);
|
|
95
|
+
// }
|
|
96
|
+
// debug('request processed', error, body, response.statusCode);
|
|
97
|
+
// const statusFromBody = body?.status;
|
|
98
|
+
// // tslint:disable-next-line:no-single-line-block-comment
|
|
99
|
+
// /* istanbul ignore else */
|
|
100
|
+
// if (response.statusCode !== undefined) {
|
|
101
|
+
// if (this.expectedStatusCodes.indexOf(response.statusCode) < 0) {
|
|
102
|
+
// if (typeof body === 'string') {
|
|
103
|
+
// // Consider all 4xx and 5xx responses errors.
|
|
104
|
+
// err = new COAServiceError(response.statusCode, '', body, requestOptions);
|
|
105
|
+
// } else {
|
|
106
|
+
// // エラーレスポンスにステータスがあった場合
|
|
107
|
+
// // tslint:disable-next-line:no-single-line-block-comment
|
|
108
|
+
// /* istanbul ignore else */
|
|
109
|
+
// if (statusFromBody !== undefined) {
|
|
110
|
+
// err = new COAServiceError(response.statusCode, statusFromBody, body.message, requestOptions);
|
|
111
|
+
// } else {
|
|
112
|
+
// // no operation
|
|
113
|
+
// }
|
|
114
|
+
// }
|
|
115
|
+
// } else {
|
|
116
|
+
// // HTTPステータスコード2xxでも、レスポンス本文のステータスが0でなければBadRequest
|
|
117
|
+
// if (statusFromBody !== undefined && statusFromBody !== RESPONSE_BODY_STAUS_SUCCESS) {
|
|
118
|
+
// err = new COAServiceError(response.statusCode, statusFromBody, body.message, requestOptions);
|
|
119
|
+
// } else {
|
|
120
|
+
// // if (response.statusCode === NO_CONTENT) {
|
|
121
|
+
// // // consider 204
|
|
122
|
+
// // return;
|
|
123
|
+
// // } else {
|
|
124
|
+
// // // consider 200,201
|
|
125
|
+
// // return body;
|
|
126
|
+
// // }
|
|
127
|
+
// return body;
|
|
128
|
+
// }
|
|
129
|
+
// }
|
|
130
|
+
// } else {
|
|
131
|
+
// // no operation
|
|
132
|
+
// }
|
|
133
|
+
// throw err;
|
|
134
|
+
// }
|
|
135
|
+
// }
|
|
154
136
|
/**
|
|
155
137
|
* FetchTransporter
|
|
156
138
|
*/
|
|
@@ -167,10 +149,10 @@ class FetchTransporter {
|
|
|
167
149
|
// tslint:disable-next-line:no-single-line-block-comment
|
|
168
150
|
/* istanbul ignore else */
|
|
169
151
|
if (!options.headers['User-Agent']) {
|
|
170
|
-
options.headers['User-Agent'] =
|
|
152
|
+
options.headers['User-Agent'] = FetchTransporter.USER_AGENT;
|
|
171
153
|
}
|
|
172
|
-
else if (options.headers['User-Agent'].indexOf(
|
|
173
|
-
options.headers['User-Agent'] = `${options.headers['User-Agent']} ${
|
|
154
|
+
else if (options.headers['User-Agent'].indexOf(FetchTransporter.USER_AGENT) === -1) {
|
|
155
|
+
options.headers['User-Agent'] = `${options.headers['User-Agent']} ${FetchTransporter.USER_AGENT}`;
|
|
174
156
|
}
|
|
175
157
|
else {
|
|
176
158
|
// no operation
|
|
@@ -197,7 +179,7 @@ class FetchTransporter {
|
|
|
197
179
|
wrapCallback(res, options) {
|
|
198
180
|
return __awaiter(this, void 0, void 0, function* () {
|
|
199
181
|
const requestOptions = { uri: options.uri };
|
|
200
|
-
let err = new COAServiceError(http_status_1.INTERNAL_SERVER_ERROR, '', 'An unexpected error occurred.', requestOptions);
|
|
182
|
+
let err = new coaServiceError_1.COAServiceError(http_status_1.INTERNAL_SERVER_ERROR, '', 'An unexpected error occurred.', requestOptions);
|
|
201
183
|
debug('fetch processed. res.status:', res.status, 'content-type:', res.headers.get('content-type'));
|
|
202
184
|
let body = {};
|
|
203
185
|
try {
|
|
@@ -207,50 +189,44 @@ class FetchTransporter {
|
|
|
207
189
|
else {
|
|
208
190
|
body = yield res.json();
|
|
209
191
|
}
|
|
192
|
+
debug('fetch processed. body:', body);
|
|
210
193
|
}
|
|
211
194
|
catch (error) {
|
|
212
195
|
// no op
|
|
213
196
|
}
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
197
|
+
if (typeof body === 'string') {
|
|
198
|
+
// Consider all 4xx and 5xx responses errors.
|
|
199
|
+
err = new coaServiceError_1.COAServiceError(res.status, '', body, requestOptions);
|
|
200
|
+
}
|
|
201
|
+
else {
|
|
217
202
|
if (this.expectedStatusCodes.indexOf(res.status) < 0) {
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
203
|
+
// エラーレスポンスにステータスがあった場合
|
|
204
|
+
// tslint:disable-next-line:no-single-line-block-comment
|
|
205
|
+
/* istanbul ignore else */
|
|
206
|
+
if (body.status !== undefined) {
|
|
207
|
+
err = new coaServiceError_1.COAServiceError(res.status, String(body.status), body.message, requestOptions);
|
|
221
208
|
}
|
|
222
209
|
else {
|
|
223
|
-
//
|
|
224
|
-
// tslint:disable-next-line:no-single-line-block-comment
|
|
225
|
-
/* istanbul ignore else */
|
|
226
|
-
if (body.status !== undefined) {
|
|
227
|
-
err = new COAServiceError(res.status, body.status, body.message, requestOptions);
|
|
228
|
-
}
|
|
229
|
-
else {
|
|
230
|
-
// no operation
|
|
231
|
-
}
|
|
210
|
+
// no operation
|
|
232
211
|
}
|
|
233
212
|
}
|
|
234
213
|
else {
|
|
235
214
|
// HTTPステータスコード2xxでも、レスポンス本文のステータスが0でなければBadRequest
|
|
236
215
|
if (body.status !== undefined && body.status !== exports.RESPONSE_BODY_STAUS_SUCCESS) {
|
|
237
|
-
err = new COAServiceError(res.status, body.status, body.message, requestOptions);
|
|
216
|
+
err = new coaServiceError_1.COAServiceError(res.status, String(body.status), body.message, requestOptions);
|
|
238
217
|
}
|
|
239
218
|
else {
|
|
240
|
-
if (res.status ===
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
219
|
+
// if (res.status === NO_CONTENT) {
|
|
220
|
+
// // consider 204
|
|
221
|
+
// return;
|
|
222
|
+
// } else {
|
|
223
|
+
// // consider 200,201
|
|
224
|
+
// return body;
|
|
225
|
+
// }
|
|
226
|
+
return body;
|
|
248
227
|
}
|
|
249
228
|
}
|
|
250
229
|
}
|
|
251
|
-
else {
|
|
252
|
-
// no operation
|
|
253
|
-
}
|
|
254
230
|
throw err;
|
|
255
231
|
});
|
|
256
232
|
}
|
package/package.json
CHANGED
|
@@ -15,24 +15,20 @@
|
|
|
15
15
|
"dependencies": {
|
|
16
16
|
"debug": "^3.2.7",
|
|
17
17
|
"http-status": "^1.5.2",
|
|
18
|
-
"node-fetch": "2.7.0"
|
|
19
|
-
"request": "^2.88.2"
|
|
18
|
+
"node-fetch": "2.7.0"
|
|
20
19
|
},
|
|
21
20
|
"description": "COA client library for Node.js",
|
|
22
21
|
"devDependencies": {
|
|
23
22
|
"@types/debug": "0.0.30",
|
|
24
23
|
"@types/http-status": "^0.2.30",
|
|
25
24
|
"@types/mocha": "^5.2.0",
|
|
26
|
-
"@types/nock": "^9.1.3",
|
|
27
25
|
"@types/node": "18.19.2",
|
|
28
26
|
"@types/node-fetch": "2.6.11",
|
|
29
|
-
"@types/request": "^2.48.5",
|
|
30
27
|
"@types/sinon": "^4.3.3",
|
|
31
28
|
"coveralls": "^3.0.1",
|
|
32
29
|
"grunt-contrib-watch": "^1.1.0",
|
|
33
30
|
"mocha": "^5.2.0",
|
|
34
31
|
"moment": "^2.22.1",
|
|
35
|
-
"nock": "^9.3.0",
|
|
36
32
|
"nyc": "^15.1.0",
|
|
37
33
|
"rimraf": "^2.6.2",
|
|
38
34
|
"sinon": "^5.0.10",
|
|
@@ -43,7 +39,7 @@
|
|
|
43
39
|
},
|
|
44
40
|
"engines": {
|
|
45
41
|
"node": ">=18.0.0",
|
|
46
|
-
"npm": ">=
|
|
42
|
+
"npm": ">=8.0.0"
|
|
47
43
|
},
|
|
48
44
|
"keywords": [],
|
|
49
45
|
"license": "UNLICENSED",
|
|
@@ -80,5 +76,5 @@
|
|
|
80
76
|
"postversion": "git push origin --tags",
|
|
81
77
|
"prepublishOnly": "npm run clean && npm run build && npm test && npm run doc"
|
|
82
78
|
},
|
|
83
|
-
"version": "9.3.0-alpha.
|
|
79
|
+
"version": "9.3.0-alpha.5"
|
|
84
80
|
}
|