@ingestkorea/client-sens 1.9.0 → 1.10.0
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 +1 -0
- package/dist-cjs/SensClient.js +21 -18
- package/dist-cjs/commands/ListAlimtalkStatusCommand.js +5 -7
- package/dist-cjs/commands/ListSMSStatusCommand.js +5 -7
- package/dist-cjs/commands/SendAlimtalkCommand.js +6 -13
- package/dist-cjs/commands/SendMMSCommand.js +9 -19
- package/dist-cjs/commands/SendSMSCommand.js +5 -7
- package/dist-cjs/commands/constants.js +7 -18
- package/dist-cjs/index.js +1 -0
- package/dist-cjs/middleware/middleware-metadata.js +1 -1
- package/dist-cjs/middleware/middleware-retry.js +19 -26
- package/dist-cjs/models/SensError.js +14 -0
- package/dist-cjs/models/SensErrorInfo.js +29 -11
- package/dist-cjs/models/index.js +1 -0
- package/dist-cjs/protocols/constants.js +74 -48
- package/dist-es/SensClient.js +21 -18
- package/dist-es/commands/ListAlimtalkStatusCommand.js +5 -7
- package/dist-es/commands/ListSMSStatusCommand.js +5 -7
- package/dist-es/commands/SendAlimtalkCommand.js +6 -13
- package/dist-es/commands/SendMMSCommand.js +9 -19
- package/dist-es/commands/SendSMSCommand.js +5 -7
- package/dist-es/commands/constants.js +7 -18
- package/dist-es/index.js +1 -0
- package/dist-es/middleware/middleware-metadata.js +1 -1
- package/dist-es/middleware/middleware-retry.js +21 -32
- package/dist-es/models/SensError.js +14 -0
- package/dist-es/models/SensErrorInfo.js +29 -11
- package/dist-es/models/index.js +1 -0
- package/dist-es/protocols/constants.js +77 -51
- package/dist-types/index.d.ts +1 -0
- package/dist-types/models/SensClient.d.ts +0 -1
- package/dist-types/models/SensError.d.ts +17 -0
- package/dist-types/models/SensErrorInfo.d.ts +51 -15
- package/dist-types/models/index.d.ts +1 -0
- package/dist-types/protocols/constants.d.ts +1 -1
- package/package.json +2 -3
package/README.md
CHANGED
package/dist-cjs/SensClient.js
CHANGED
|
@@ -33,8 +33,14 @@ class SensClient {
|
|
|
33
33
|
const output = yield deserializer(response, this.config);
|
|
34
34
|
return output;
|
|
35
35
|
}
|
|
36
|
-
catch (
|
|
37
|
-
|
|
36
|
+
catch (err) {
|
|
37
|
+
if (err instanceof index_js_1.SensError)
|
|
38
|
+
throw err;
|
|
39
|
+
throw new index_js_1.SensError({
|
|
40
|
+
code: -1,
|
|
41
|
+
type: "SDK.UNKNOWN_ERROR",
|
|
42
|
+
message: err instanceof Error ? err.message : "unknown error",
|
|
43
|
+
});
|
|
38
44
|
}
|
|
39
45
|
});
|
|
40
46
|
}
|
|
@@ -48,20 +54,19 @@ const composeMiddleware = (middlewares, finalHandler) => {
|
|
|
48
54
|
};
|
|
49
55
|
const resolveCredentials = (config) => {
|
|
50
56
|
const { credentials } = config;
|
|
51
|
-
|
|
52
|
-
code:
|
|
53
|
-
type: "
|
|
54
|
-
message: "
|
|
55
|
-
invalidInputs: [{ type: "not-valid.args", message: "SensClient 초기화시 인증 정보를 확인해주세요" }],
|
|
57
|
+
const error = new index_js_1.SensError({
|
|
58
|
+
code: -1,
|
|
59
|
+
type: "SDK.AUTH_ERROR",
|
|
60
|
+
message: "SensClient 초기화시 인증 정보를 입력해주세요.",
|
|
56
61
|
});
|
|
57
62
|
if (!credentials)
|
|
58
63
|
throw error;
|
|
59
64
|
if (!credentials.accessKey) {
|
|
60
|
-
error.
|
|
65
|
+
error.message = "AccessKey 정보를 확인해주세요.";
|
|
61
66
|
throw error;
|
|
62
67
|
}
|
|
63
68
|
if (!credentials.secretKey) {
|
|
64
|
-
error.
|
|
69
|
+
error.message = "SecretKey 정보를 확인해주세요.";
|
|
65
70
|
throw error;
|
|
66
71
|
}
|
|
67
72
|
return {
|
|
@@ -70,24 +75,22 @@ const resolveCredentials = (config) => {
|
|
|
70
75
|
};
|
|
71
76
|
};
|
|
72
77
|
const resolveServiceId = (config) => {
|
|
73
|
-
var _a, _b
|
|
78
|
+
var _a, _b;
|
|
74
79
|
const { serviceId } = config;
|
|
75
|
-
|
|
76
|
-
code:
|
|
77
|
-
type: "
|
|
78
|
-
message: "
|
|
79
|
-
invalidInputs: [{ type: "not-valid.args", message: "SensClient 초기화시 서비스 정보를 확인해주세요" }],
|
|
80
|
+
const error = new index_js_1.SensError({
|
|
81
|
+
code: -1,
|
|
82
|
+
type: "SDK.GENERAL_ERROR",
|
|
83
|
+
message: "SensClient 초기화시 사용할 서비스 정보를 입력해주세요.",
|
|
80
84
|
});
|
|
81
85
|
if (!serviceId)
|
|
82
86
|
throw error;
|
|
83
87
|
const isReady = !!Object.values(serviceId).filter((d) => !!d).length;
|
|
84
88
|
if (!isReady) {
|
|
85
|
-
error.
|
|
89
|
+
error.message = "최소 1개 이상의 서비스 정보가 필요합니다";
|
|
86
90
|
throw error;
|
|
87
91
|
}
|
|
88
92
|
return {
|
|
89
93
|
kakao: (_a = serviceId.kakao) !== null && _a !== void 0 ? _a : "",
|
|
90
|
-
|
|
91
|
-
sms: (_c = serviceId.sms) !== null && _c !== void 0 ? _c : "",
|
|
94
|
+
sms: (_b = serviceId.sms) !== null && _b !== void 0 ? _b : "",
|
|
92
95
|
};
|
|
93
96
|
};
|
|
@@ -13,14 +13,12 @@ class ListAlimtalkStatusCommand extends index_js_1.SensCommand {
|
|
|
13
13
|
super(input);
|
|
14
14
|
if (input.nextToken && (!input.requestStartTime || !input.requestEndTime)) {
|
|
15
15
|
throw new index_js_1.SensError({
|
|
16
|
-
code:
|
|
17
|
-
type: "
|
|
16
|
+
code: -1,
|
|
17
|
+
type: "SDK.GENERAL_ERROR",
|
|
18
18
|
message: "페이지네이션 검색 조건 불일치",
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
message: "nextToken 사용 시 데이터 일관성을 위해 최초 요청 시의 옵션(requestStartTime, requestEndTime, pageSize 등)이 모두 동일하게 유지되어야 합니다.",
|
|
23
|
-
},
|
|
19
|
+
errors: [
|
|
20
|
+
"nextToken 사용 시 데이터 일관성을 위해 최초 요청 시의 옵션이 모두 동일하게 유지되어야 합니다.",
|
|
21
|
+
"requestStartTime, requestEndTime, pageSize 옵션을 확인해주세요.",
|
|
24
22
|
],
|
|
25
23
|
});
|
|
26
24
|
}
|
|
@@ -13,14 +13,12 @@ class ListSMSStatusCommand extends index_js_1.SensCommand {
|
|
|
13
13
|
super(input);
|
|
14
14
|
if (input.nextToken && (!input.requestStartTime || !input.requestEndTime)) {
|
|
15
15
|
throw new index_js_1.SensError({
|
|
16
|
-
code:
|
|
17
|
-
type: "
|
|
16
|
+
code: -1,
|
|
17
|
+
type: "SDK.GENERAL_ERROR",
|
|
18
18
|
message: "페이지네이션 검색 조건 불일치",
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
message: "nextToken 사용 시 데이터 일관성을 위해 최초 요청 시의 옵션(requestStartTime, requestEndTime, pageSize 등)이 모두 동일하게 유지되어야 합니다.",
|
|
23
|
-
},
|
|
19
|
+
errors: [
|
|
20
|
+
"nextToken 사용 시 데이터 일관성을 위해 최초 요청 시의 옵션이 모두 동일하게 유지되어야 합니다.",
|
|
21
|
+
"requestStartTime, requestEndTime, pageSize 옵션을 확인해주세요.",
|
|
24
22
|
],
|
|
25
23
|
});
|
|
26
24
|
}
|
|
@@ -22,24 +22,17 @@ exports.SendAlimtalkCommand = SendAlimtalkCommand;
|
|
|
22
22
|
const resolveAlimtalkMessages = (messages, config) => {
|
|
23
23
|
if (!messages.length || messages.length > config.maxRecipients) {
|
|
24
24
|
throw new index_js_1.SensError({
|
|
25
|
-
code:
|
|
26
|
-
type: "
|
|
27
|
-
message:
|
|
28
|
-
invalidInputs: [
|
|
29
|
-
{
|
|
30
|
-
type: "not-valid.args",
|
|
31
|
-
message: `메세지 동시 발송은 최소 1명, 최대 ${config.maxRecipients}명까지 가능합니다.`,
|
|
32
|
-
},
|
|
33
|
-
],
|
|
25
|
+
code: -1,
|
|
26
|
+
type: "SDK.GENERAL_ERROR",
|
|
27
|
+
message: `알림톡 동시 발송은 최소 1명, 최대 ${config.maxRecipients}명까지 가능합니다.`,
|
|
34
28
|
});
|
|
35
29
|
}
|
|
36
30
|
const result = messages.map((message) => {
|
|
37
31
|
if (message.content.length > config.maxContentLength) {
|
|
38
32
|
throw new index_js_1.SensError({
|
|
39
|
-
code:
|
|
40
|
-
type: "
|
|
41
|
-
message:
|
|
42
|
-
invalidInputs: [{ type: "not-valid.args", message: `메세지 최대 길이는 ${config.maxContentLength}글자입니다` }],
|
|
33
|
+
code: -1,
|
|
34
|
+
type: "SDK.GENERAL_ERROR",
|
|
35
|
+
message: `알림톡 최대 길이는 ${config.maxContentLength}글자입니다`,
|
|
43
36
|
});
|
|
44
37
|
}
|
|
45
38
|
return Object.assign({ to: message.to.replace(/\-/gi, ""), content: message.content, useSmsFailover: message.useSmsFailover != undefined ? message.useSmsFailover : false }, (message.buttons && message.buttons.length && { buttons: message.buttons }));
|
|
@@ -36,12 +36,9 @@ const resolveSendFile = (file, options) => {
|
|
|
36
36
|
const ext = parsed.ext.toLocaleLowerCase();
|
|
37
37
|
if (!options.supportExt.includes(ext)) {
|
|
38
38
|
throw new index_js_1.SensError({
|
|
39
|
-
code:
|
|
40
|
-
type: "
|
|
41
|
-
message: "
|
|
42
|
-
invalidInputs: [
|
|
43
|
-
{ type: "not-valid.args", message: `${options.supportExt.join(", ")} 형식의 첨부파일만 지원합니다.` },
|
|
44
|
-
],
|
|
39
|
+
code: -1,
|
|
40
|
+
type: "SDK.GENERAL_ERROR",
|
|
41
|
+
message: `MMS는 ${options.supportExt.join(", ")} 형식의 첨부파일만 보낼 수 있습니다.`,
|
|
45
42
|
});
|
|
46
43
|
}
|
|
47
44
|
if (file.body) {
|
|
@@ -55,10 +52,9 @@ const resolveSendFile = (file, options) => {
|
|
|
55
52
|
}
|
|
56
53
|
if (!(0, node_fs_1.existsSync)(file.name)) {
|
|
57
54
|
throw new index_js_1.SensError({
|
|
58
|
-
code:
|
|
59
|
-
type: "
|
|
60
|
-
message:
|
|
61
|
-
invalidInputs: [{ type: "not-valid.args", message: `${file.name} 파일이 존재하지 않습니다.` }],
|
|
55
|
+
code: -1,
|
|
56
|
+
type: "SDK.GENERAL_ERROR",
|
|
57
|
+
message: `${file.name} 첨부파일이 존재하지 않습니다.`,
|
|
62
58
|
});
|
|
63
59
|
}
|
|
64
60
|
const buffer = (0, node_fs_1.readFileSync)(file.name);
|
|
@@ -74,15 +70,9 @@ const checkSizeLimit = (buffer, maxKiB) => {
|
|
|
74
70
|
const kibSize = Math.ceil(byteSize / 1024);
|
|
75
71
|
if (kibSize > maxKiB) {
|
|
76
72
|
throw new index_js_1.SensError({
|
|
77
|
-
code:
|
|
78
|
-
type: "
|
|
79
|
-
message:
|
|
80
|
-
invalidInputs: [
|
|
81
|
-
{
|
|
82
|
-
type: "not-valid.args",
|
|
83
|
-
message: `최대 ${maxKiB}KiB까지 전송 가능합니다. 현재 ${kibSize}KiB`,
|
|
84
|
-
},
|
|
85
|
-
],
|
|
73
|
+
code: -1,
|
|
74
|
+
type: "SDK.GENERAL_ERROR",
|
|
75
|
+
message: `첨부파일은 ${maxKiB}KiB까지 전송 가능합니다. 현재 ${kibSize}KiB`,
|
|
86
76
|
});
|
|
87
77
|
}
|
|
88
78
|
return;
|
|
@@ -36,18 +36,16 @@ const getMessageType = (input) => {
|
|
|
36
36
|
const contentLength = (0, constants_1.getContentLength)(input.content);
|
|
37
37
|
if (!contentLength) {
|
|
38
38
|
throw new index_js_1.SensError({
|
|
39
|
-
code:
|
|
40
|
-
type: "
|
|
39
|
+
code: -1,
|
|
40
|
+
type: "SDK.GENERAL_ERROR",
|
|
41
41
|
message: "메세지 내용이 없습니다.",
|
|
42
|
-
invalidInputs: [{ type: "not-valid.args", message: "메세지 내용을 확인해주세요." }],
|
|
43
42
|
});
|
|
44
43
|
}
|
|
45
44
|
if (contentLength > LMS_MAX_CONTENT_LENGTH) {
|
|
46
45
|
throw new index_js_1.SensError({
|
|
47
|
-
code:
|
|
48
|
-
type: "
|
|
49
|
-
message:
|
|
50
|
-
invalidInputs: [{ type: "not-valid.args", message: `메세지 최대 길이는 ${LMS_MAX_CONTENT_LENGTH}글자입니다` }],
|
|
46
|
+
code: -1,
|
|
47
|
+
type: "SDK.GENERAL_ERROR",
|
|
48
|
+
message: `메세지 최대 길이는 ${LMS_MAX_CONTENT_LENGTH}글자입니다`,
|
|
51
49
|
});
|
|
52
50
|
}
|
|
53
51
|
return contentLength > SMS_MAX_CONTENT_LENGTH ? "LMS" : "SMS";
|
|
@@ -42,28 +42,17 @@ const resolveRequestDuration = (input, config) => {
|
|
|
42
42
|
const diff = end - start;
|
|
43
43
|
if (diff < 0) {
|
|
44
44
|
throw new index_js_1.SensError({
|
|
45
|
-
code:
|
|
46
|
-
type: "
|
|
47
|
-
message:
|
|
48
|
-
|
|
49
|
-
{
|
|
50
|
-
type: "not-valid.args",
|
|
51
|
-
message: "종료일이 시작일보다 빠를 수 없습니다.",
|
|
52
|
-
},
|
|
53
|
-
],
|
|
45
|
+
code: -1,
|
|
46
|
+
type: "SDK.GENERAL_ERROR",
|
|
47
|
+
message: "조회 가능 날짜 오류. 종료일이 시작일보다 빠를 수 없습니다.",
|
|
48
|
+
errors: [`종료일: ${input.endTime}`, `시작일: ${input.startTime}`],
|
|
54
49
|
});
|
|
55
50
|
}
|
|
56
51
|
if (diff > config.durationLimitMs) {
|
|
57
52
|
throw new index_js_1.SensError({
|
|
58
|
-
code:
|
|
59
|
-
type: "
|
|
60
|
-
message:
|
|
61
|
-
invalidInputs: [
|
|
62
|
-
{
|
|
63
|
-
type: "not-valid.args",
|
|
64
|
-
message: `조회 가능한 날짜 범위는 최대 ${config.durationLimitMs / (24 * 3600000)}일 이내여야 합니다.`,
|
|
65
|
-
},
|
|
66
|
-
],
|
|
53
|
+
code: -1,
|
|
54
|
+
type: "SDK.GENERAL_ERROR",
|
|
55
|
+
message: `최대 ${config.durationLimitMs / (24 * 3600000)}일 이내에서 조회 가능합니다.`,
|
|
67
56
|
});
|
|
68
57
|
}
|
|
69
58
|
return {
|
package/dist-cjs/index.js
CHANGED
|
@@ -16,3 +16,4 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
__exportStar(require("./commands/index.js"), exports);
|
|
18
18
|
__exportStar(require("./SensClient.js"), exports);
|
|
19
|
+
__exportStar(require("./models/SensError.js"), exports);
|
|
@@ -12,7 +12,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
12
12
|
exports.middlewareIngestkoreaMetadata = void 0;
|
|
13
13
|
const constants_js_1 = require("./constants.js");
|
|
14
14
|
const middlewareIngestkoreaMetadata = (next) => (input, context) => __awaiter(void 0, void 0, void 0, function* () {
|
|
15
|
-
input.request.headers = Object.assign(Object.assign({}, input.request.headers), { [constants_js_1.INGESTKOREA_USER_AGENT]: "@ingestkorea/client-sens/1.
|
|
15
|
+
input.request.headers = Object.assign(Object.assign({}, input.request.headers), { [constants_js_1.INGESTKOREA_USER_AGENT]: "@ingestkorea/client-sens/1.10.x" });
|
|
16
16
|
return next(input, context);
|
|
17
17
|
});
|
|
18
18
|
exports.middlewareIngestkoreaMetadata = middlewareIngestkoreaMetadata;
|
|
@@ -10,54 +10,47 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
exports.middlewareRetry = void 0;
|
|
13
|
-
const
|
|
13
|
+
const index_js_1 = require("../models/index.js");
|
|
14
14
|
const constants_js_1 = require("./constants.js");
|
|
15
15
|
const middlewareRetry = (next) => (input, context) => __awaiter(void 0, void 0, void 0, function* () {
|
|
16
|
-
var _a, _b;
|
|
17
|
-
var _c;
|
|
18
16
|
const MAX_RETRIES = 3;
|
|
17
|
+
const MIN_DELAY_MS = 300;
|
|
19
18
|
const BASE_DELAY_MS = 500;
|
|
20
19
|
const MAX_DELAY_MS = 5000;
|
|
21
20
|
let attempts = 0;
|
|
22
21
|
let totalRetryDelay = 0;
|
|
23
|
-
|
|
24
|
-
let lastError = new util_error_handler_1.IngestkoreaError({
|
|
25
|
-
code: 400,
|
|
26
|
-
type: "Bad Request",
|
|
27
|
-
message: "Invalid Request",
|
|
28
|
-
description: { attempts, maxRetries: MAX_RETRIES, totalRetryDelay },
|
|
29
|
-
});
|
|
22
|
+
input.request.headers = Object.assign({}, input.request.headers);
|
|
30
23
|
while (attempts < MAX_RETRIES) {
|
|
31
|
-
|
|
32
|
-
const requestLog = `attempt=${
|
|
24
|
+
attempts++;
|
|
25
|
+
const requestLog = `attempt=${attempts}; max=${MAX_RETRIES}; totalRetryDelay=${totalRetryDelay}`;
|
|
33
26
|
input.request.headers[constants_js_1.INGESTKOREA_REQUEST_LOG] = requestLog;
|
|
34
27
|
try {
|
|
35
28
|
const { response } = yield next(input, context);
|
|
36
|
-
response.headers[constants_js_1.INGESTKOREA_RETRY] =
|
|
29
|
+
response.headers[constants_js_1.INGESTKOREA_RETRY] = attempts.toString();
|
|
37
30
|
response.headers[constants_js_1.INGESTKOREA_RETRY_DELAY] = totalRetryDelay.toString();
|
|
38
31
|
return { response };
|
|
39
32
|
}
|
|
40
33
|
catch (error) {
|
|
41
|
-
attempts++;
|
|
42
|
-
lastError.error.description = { attempts, maxRetries: MAX_RETRIES, totalRetryDelay };
|
|
43
|
-
if (error instanceof util_error_handler_1.IngestkoreaError) {
|
|
44
|
-
lastError.error.description = Object.assign(Object.assign({}, lastError.error.description), { detail: error.error.description });
|
|
45
|
-
}
|
|
46
|
-
else {
|
|
47
|
-
lastError.error.description = Object.assign(Object.assign({}, lastError.error.description), { detail: String((_b = error === null || error === void 0 ? void 0 : error.message) !== null && _b !== void 0 ? _b : error) });
|
|
48
|
-
}
|
|
49
34
|
if (attempts >= MAX_RETRIES) {
|
|
50
|
-
|
|
51
|
-
|
|
35
|
+
throw new index_js_1.SensError({
|
|
36
|
+
code: -1,
|
|
37
|
+
type: "SDK.REQUEST_ERROR",
|
|
38
|
+
message: error instanceof Error ? error.message : requestLog,
|
|
39
|
+
});
|
|
52
40
|
}
|
|
53
41
|
const exp = BASE_DELAY_MS * Math.pow(2, (attempts - 1));
|
|
54
42
|
const capped = Math.min(MAX_DELAY_MS, exp);
|
|
55
|
-
const
|
|
43
|
+
const baseWait = Math.max(MIN_DELAY_MS, Math.floor(capped / 2));
|
|
44
|
+
const jitter = Math.floor(Math.random() * (capped - baseWait));
|
|
45
|
+
const delay = baseWait + jitter;
|
|
56
46
|
totalRetryDelay += delay;
|
|
57
|
-
lastError.error.description = { attempts, maxRetries: MAX_RETRIES, totalRetryDelay };
|
|
58
47
|
yield new Promise((resolve) => setTimeout(resolve, delay));
|
|
59
48
|
}
|
|
60
49
|
}
|
|
61
|
-
throw
|
|
50
|
+
throw new index_js_1.SensError({
|
|
51
|
+
code: -1,
|
|
52
|
+
type: "SDK.UNKNOWN_ERROR",
|
|
53
|
+
message: "Unexpected retry loop termination",
|
|
54
|
+
});
|
|
62
55
|
});
|
|
63
56
|
exports.middlewareRetry = middlewareRetry;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SensError = void 0;
|
|
4
|
+
class SensError extends Error {
|
|
5
|
+
constructor(info) {
|
|
6
|
+
super(info.message);
|
|
7
|
+
this.name = "SensError";
|
|
8
|
+
this.code = info.code;
|
|
9
|
+
this.type = info.type;
|
|
10
|
+
this.errors = info.errors || [];
|
|
11
|
+
this.timestamp = new Date().toISOString();
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
exports.SensError = SensError;
|
|
@@ -1,13 +1,31 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
exports.
|
|
3
|
+
exports.NCLOUD_ERROR_BODY_SPEC = exports.SENS_ERROR_BODY_SPEC = exports.NCLOUD_ERROR_CODE_SPEC = exports.SENS_ERROR_CODE_SPEC = void 0;
|
|
4
|
+
exports.SENS_ERROR_CODE_SPEC = {
|
|
5
|
+
400: "Bad Request",
|
|
6
|
+
401: "Unauthorized",
|
|
7
|
+
403: "Forbidden",
|
|
8
|
+
404: "Not Found",
|
|
9
|
+
429: "Too Many Requests",
|
|
10
|
+
500: "Internal Server Error",
|
|
11
|
+
};
|
|
12
|
+
exports.NCLOUD_ERROR_CODE_SPEC = {
|
|
13
|
+
"100": { status: 400, type: exports.SENS_ERROR_CODE_SPEC["400"] },
|
|
14
|
+
"200": { status: 401, type: exports.SENS_ERROR_CODE_SPEC["401"] },
|
|
15
|
+
"210": { status: 403, type: exports.SENS_ERROR_CODE_SPEC["403"] },
|
|
16
|
+
"300": { status: 404, type: exports.SENS_ERROR_CODE_SPEC["404"] },
|
|
17
|
+
"400": { status: 429, type: exports.SENS_ERROR_CODE_SPEC["429"] },
|
|
18
|
+
"410": { status: 429, type: exports.SENS_ERROR_CODE_SPEC["429"] },
|
|
19
|
+
"420": { status: 429, type: exports.SENS_ERROR_CODE_SPEC["429"] },
|
|
20
|
+
"900": { status: 500, type: exports.SENS_ERROR_CODE_SPEC["500"] },
|
|
21
|
+
};
|
|
22
|
+
exports.SENS_ERROR_BODY_SPEC = [
|
|
23
|
+
["status", "number", true],
|
|
24
|
+
["errorMessage", "string", false],
|
|
25
|
+
["errors", "object", false],
|
|
26
|
+
];
|
|
27
|
+
exports.NCLOUD_ERROR_BODY_SPEC = [
|
|
28
|
+
["errorCode", "string", true],
|
|
29
|
+
["message", "string", false],
|
|
30
|
+
["details", "string", false],
|
|
31
|
+
];
|
package/dist-cjs/models/index.js
CHANGED
|
@@ -29,5 +29,6 @@ __exportStar(require("./SendMMS.js"), exports);
|
|
|
29
29
|
__exportStar(require("./SendSMS.js"), exports);
|
|
30
30
|
__exportStar(require("./SensClient.js"), exports);
|
|
31
31
|
__exportStar(require("./SensCommand.js"), exports);
|
|
32
|
+
__exportStar(require("./SensError.js"), exports);
|
|
32
33
|
__exportStar(require("./SensErrorInfo.js"), exports);
|
|
33
34
|
__exportStar(require("./SensMiddleware.js"), exports);
|
|
@@ -28,75 +28,79 @@ const parseBody = (output) => __awaiter(void 0, void 0, void 0, function* () {
|
|
|
28
28
|
if (!isJsonResponse(headers["content-type"])) {
|
|
29
29
|
yield (0, util_http_handler_1.destroyStream)(streamBody);
|
|
30
30
|
throw new index_js_1.SensError({
|
|
31
|
-
code:
|
|
32
|
-
type: "
|
|
31
|
+
code: -1,
|
|
32
|
+
type: "SDK.REQUEST_ERROR",
|
|
33
33
|
message: "response content-type is not application/json",
|
|
34
34
|
});
|
|
35
35
|
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
message: "parse response body error",
|
|
47
|
-
});
|
|
48
|
-
}
|
|
36
|
+
try {
|
|
37
|
+
const data = yield (0, util_http_handler_1.collectBodyString)(streamBody);
|
|
38
|
+
return data.length ? JSON.parse(data) : {};
|
|
39
|
+
}
|
|
40
|
+
catch (e) {
|
|
41
|
+
throw new index_js_1.SensError({
|
|
42
|
+
code: -1,
|
|
43
|
+
type: "SDK.REQUEST_ERROR",
|
|
44
|
+
message: e instanceof Error ? e.message : "parse response body error",
|
|
45
|
+
});
|
|
49
46
|
}
|
|
50
|
-
return {};
|
|
51
47
|
});
|
|
52
48
|
exports.parseBody = parseBody;
|
|
53
49
|
const parseErrorBody = (output) => __awaiter(void 0, void 0, void 0, function* () {
|
|
54
50
|
const { statusCode, headers, body: streamBody } = output;
|
|
55
|
-
const error = new index_js_1.SensError({
|
|
56
|
-
code: statusCode !== null && statusCode !== void 0 ? statusCode : 400,
|
|
57
|
-
type: "Bad Request",
|
|
58
|
-
message: "something wrong",
|
|
59
|
-
});
|
|
60
51
|
if (!isJsonResponse(headers["content-type"])) {
|
|
61
52
|
yield (0, util_http_handler_1.destroyStream)(streamBody);
|
|
62
|
-
|
|
63
|
-
|
|
53
|
+
throw new index_js_1.SensError({
|
|
54
|
+
code: -1,
|
|
55
|
+
type: "SDK.REQUEST_ERROR",
|
|
56
|
+
message: "response content-type is not application/json",
|
|
57
|
+
});
|
|
64
58
|
}
|
|
65
59
|
const data = yield (0, util_http_handler_1.collectBodyString)(streamBody);
|
|
66
|
-
|
|
67
|
-
const
|
|
68
|
-
if (
|
|
69
|
-
|
|
60
|
+
const sensErrorBody = isSensErrorResponse(data);
|
|
61
|
+
const nCloudErrorBody = isNCloudErrorResponse(data);
|
|
62
|
+
if (sensErrorBody) {
|
|
63
|
+
const errorType = isSensErrorCode(sensErrorBody.status)
|
|
64
|
+
? index_js_1.SENS_ERROR_CODE_SPEC[sensErrorBody.status]
|
|
65
|
+
: "SDK.UNKNOWN_ERROR";
|
|
66
|
+
throw new index_js_1.SensError(Object.assign({ code: isSensErrorCode(sensErrorBody.status) ? sensErrorBody.status : statusCode, type: errorType, message: sensErrorBody.errorMessage || errorType }, (sensErrorBody.errors &&
|
|
67
|
+
Array.isArray(sensErrorBody.errors) && {
|
|
68
|
+
errors: sensErrorBody.errors.filter((d) => !!d),
|
|
69
|
+
})));
|
|
70
|
+
}
|
|
71
|
+
if (nCloudErrorBody) {
|
|
72
|
+
const { errorCode } = nCloudErrorBody.error;
|
|
73
|
+
const errorType = isNCloudErrorCode(errorCode)
|
|
74
|
+
? index_js_1.NCLOUD_ERROR_CODE_SPEC[errorCode].type
|
|
75
|
+
: "SDK.UNKNOWN_ERROR";
|
|
76
|
+
throw new index_js_1.SensError({
|
|
77
|
+
code: isNCloudErrorCode(errorCode) ? index_js_1.NCLOUD_ERROR_CODE_SPEC[errorCode].status : statusCode,
|
|
78
|
+
type: errorType,
|
|
79
|
+
message: nCloudErrorBody.error.details || "Something wrong",
|
|
80
|
+
});
|
|
70
81
|
}
|
|
71
|
-
throw new index_js_1.SensError(
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
}),
|
|
76
|
-
})
|
|
82
|
+
throw new index_js_1.SensError({
|
|
83
|
+
code: -1,
|
|
84
|
+
type: "SDK.UNKNOWN_ERROR",
|
|
85
|
+
message: "Invalid Naver Cloud Platform response format",
|
|
86
|
+
errors: [`${statusCode} - ${data.slice(0, 120)}...`],
|
|
87
|
+
});
|
|
77
88
|
});
|
|
78
89
|
exports.parseErrorBody = parseErrorBody;
|
|
79
|
-
const
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
429: "Too Many Requests",
|
|
85
|
-
500: "Internal Server Error",
|
|
90
|
+
const isSensErrorCode = (code) => {
|
|
91
|
+
return code in index_js_1.SENS_ERROR_CODE_SPEC;
|
|
92
|
+
};
|
|
93
|
+
const isNCloudErrorCode = (code) => {
|
|
94
|
+
return code in index_js_1.NCLOUD_ERROR_CODE_SPEC;
|
|
86
95
|
};
|
|
87
|
-
const
|
|
88
|
-
["status", "number", true],
|
|
89
|
-
["errorMessage", "string", false],
|
|
90
|
-
["errors", "object", false],
|
|
91
|
-
];
|
|
92
|
-
const isSensErrorBody = (input) => {
|
|
96
|
+
const isSensErrorResponse = (input) => {
|
|
93
97
|
if (typeof input !== "string")
|
|
94
98
|
return null;
|
|
95
99
|
try {
|
|
96
100
|
const data = JSON.parse(input);
|
|
97
101
|
if (!data || typeof data !== "object")
|
|
98
102
|
return null;
|
|
99
|
-
const isValid = SENS_ERROR_BODY_SPEC.every(([key, type, required]) => {
|
|
103
|
+
const isValid = index_js_1.SENS_ERROR_BODY_SPEC.every(([key, type, required]) => {
|
|
100
104
|
const value = data[key];
|
|
101
105
|
if (required && (value === undefined || value === null))
|
|
102
106
|
return false;
|
|
@@ -110,6 +114,27 @@ const isSensErrorBody = (input) => {
|
|
|
110
114
|
return null;
|
|
111
115
|
}
|
|
112
116
|
};
|
|
117
|
+
const isNCloudErrorResponse = (input) => {
|
|
118
|
+
if (typeof input !== "string")
|
|
119
|
+
return null;
|
|
120
|
+
try {
|
|
121
|
+
const data = JSON.parse(input);
|
|
122
|
+
if (!(data && typeof data == "object" && typeof data.error == "object"))
|
|
123
|
+
return null;
|
|
124
|
+
const isValid = index_js_1.NCLOUD_ERROR_BODY_SPEC.every(([key, type, required]) => {
|
|
125
|
+
const value = data.error[key];
|
|
126
|
+
if (required && (value === undefined || value === null))
|
|
127
|
+
return false;
|
|
128
|
+
if (value !== undefined && typeof value !== type)
|
|
129
|
+
return false;
|
|
130
|
+
return true;
|
|
131
|
+
});
|
|
132
|
+
return isValid ? data : null;
|
|
133
|
+
}
|
|
134
|
+
catch (_a) {
|
|
135
|
+
return null;
|
|
136
|
+
}
|
|
137
|
+
};
|
|
113
138
|
const compact = (obj) => {
|
|
114
139
|
if (Array.isArray(obj)) {
|
|
115
140
|
return obj.map((item) => (0, exports.compact)(item));
|
|
@@ -129,5 +154,6 @@ const convertToUtc = (input) => {
|
|
|
129
154
|
};
|
|
130
155
|
exports.convertToUtc = convertToUtc;
|
|
131
156
|
const isJsonResponse = (contentType) => {
|
|
132
|
-
|
|
157
|
+
var _a;
|
|
158
|
+
return (_a = contentType === null || contentType === void 0 ? void 0 : contentType.toLowerCase().includes("application/json")) !== null && _a !== void 0 ? _a : false;
|
|
133
159
|
};
|