@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/dist-es/SensClient.js
CHANGED
|
@@ -23,8 +23,14 @@ class SensClient {
|
|
|
23
23
|
const output = await deserializer(response, this.config);
|
|
24
24
|
return output;
|
|
25
25
|
}
|
|
26
|
-
catch (
|
|
27
|
-
|
|
26
|
+
catch (err) {
|
|
27
|
+
if (err instanceof index_js_1.SensError)
|
|
28
|
+
throw err;
|
|
29
|
+
throw new index_js_1.SensError({
|
|
30
|
+
code: -1,
|
|
31
|
+
type: "SDK.UNKNOWN_ERROR",
|
|
32
|
+
message: err instanceof Error ? err.message : "unknown error",
|
|
33
|
+
});
|
|
28
34
|
}
|
|
29
35
|
}
|
|
30
36
|
}
|
|
@@ -37,20 +43,19 @@ const composeMiddleware = (middlewares, finalHandler) => {
|
|
|
37
43
|
};
|
|
38
44
|
const resolveCredentials = (config) => {
|
|
39
45
|
const { credentials } = config;
|
|
40
|
-
|
|
41
|
-
code:
|
|
42
|
-
type: "
|
|
43
|
-
message: "
|
|
44
|
-
invalidInputs: [{ type: "not-valid.args", message: "SensClient 초기화시 인증 정보를 확인해주세요" }],
|
|
46
|
+
const error = new index_js_1.SensError({
|
|
47
|
+
code: -1,
|
|
48
|
+
type: "SDK.AUTH_ERROR",
|
|
49
|
+
message: "SensClient 초기화시 인증 정보를 입력해주세요.",
|
|
45
50
|
});
|
|
46
51
|
if (!credentials)
|
|
47
52
|
throw error;
|
|
48
53
|
if (!credentials.accessKey) {
|
|
49
|
-
error.
|
|
54
|
+
error.message = "AccessKey 정보를 확인해주세요.";
|
|
50
55
|
throw error;
|
|
51
56
|
}
|
|
52
57
|
if (!credentials.secretKey) {
|
|
53
|
-
error.
|
|
58
|
+
error.message = "SecretKey 정보를 확인해주세요.";
|
|
54
59
|
throw error;
|
|
55
60
|
}
|
|
56
61
|
return {
|
|
@@ -59,24 +64,22 @@ const resolveCredentials = (config) => {
|
|
|
59
64
|
};
|
|
60
65
|
};
|
|
61
66
|
const resolveServiceId = (config) => {
|
|
62
|
-
var _a, _b
|
|
67
|
+
var _a, _b;
|
|
63
68
|
const { serviceId } = config;
|
|
64
|
-
|
|
65
|
-
code:
|
|
66
|
-
type: "
|
|
67
|
-
message: "
|
|
68
|
-
invalidInputs: [{ type: "not-valid.args", message: "SensClient 초기화시 서비스 정보를 확인해주세요" }],
|
|
69
|
+
const error = new index_js_1.SensError({
|
|
70
|
+
code: -1,
|
|
71
|
+
type: "SDK.GENERAL_ERROR",
|
|
72
|
+
message: "SensClient 초기화시 사용할 서비스 정보를 입력해주세요.",
|
|
69
73
|
});
|
|
70
74
|
if (!serviceId)
|
|
71
75
|
throw error;
|
|
72
76
|
const isReady = !!Object.values(serviceId).filter((d) => !!d).length;
|
|
73
77
|
if (!isReady) {
|
|
74
|
-
error.
|
|
78
|
+
error.message = "최소 1개 이상의 서비스 정보가 필요합니다";
|
|
75
79
|
throw error;
|
|
76
80
|
}
|
|
77
81
|
return {
|
|
78
82
|
kakao: (_a = serviceId.kakao) !== null && _a !== void 0 ? _a : "",
|
|
79
|
-
|
|
80
|
-
sms: (_c = serviceId.sms) !== null && _c !== void 0 ? _c : "",
|
|
83
|
+
sms: (_b = serviceId.sms) !== null && _b !== void 0 ? _b : "",
|
|
81
84
|
};
|
|
82
85
|
};
|
|
@@ -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 {
|
|
@@ -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;
|
|
@@ -47,18 +47,16 @@ const getMessageType = (input) => {
|
|
|
47
47
|
const contentLength = (0, constants_1.getContentLength)(input.content);
|
|
48
48
|
if (!contentLength) {
|
|
49
49
|
throw new index_js_1.SensError({
|
|
50
|
-
code:
|
|
51
|
-
type: "
|
|
50
|
+
code: -1,
|
|
51
|
+
type: "SDK.GENERAL_ERROR",
|
|
52
52
|
message: "메세지 내용이 없습니다.",
|
|
53
|
-
invalidInputs: [{ type: "not-valid.args", message: "메세지 내용을 확인해주세요." }],
|
|
54
53
|
});
|
|
55
54
|
}
|
|
56
55
|
if (contentLength > LMS_MAX_CONTENT_LENGTH) {
|
|
57
56
|
throw new index_js_1.SensError({
|
|
58
|
-
code:
|
|
59
|
-
type: "
|
|
60
|
-
message:
|
|
61
|
-
invalidInputs: [{ type: "not-valid.args", message: `메세지 최대 길이는 ${LMS_MAX_CONTENT_LENGTH}글자입니다` }],
|
|
57
|
+
code: -1,
|
|
58
|
+
type: "SDK.GENERAL_ERROR",
|
|
59
|
+
message: `메세지 최대 길이는 ${LMS_MAX_CONTENT_LENGTH}글자입니다`,
|
|
62
60
|
});
|
|
63
61
|
}
|
|
64
62
|
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-es/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);
|
|
@@ -5,7 +5,7 @@ const constants_js_1 = require("./constants.js");
|
|
|
5
5
|
const middlewareIngestkoreaMetadata = (next) => async (input, context) => {
|
|
6
6
|
input.request.headers = {
|
|
7
7
|
...input.request.headers,
|
|
8
|
-
[constants_js_1.INGESTKOREA_USER_AGENT]: "@ingestkorea/client-sens/1.
|
|
8
|
+
[constants_js_1.INGESTKOREA_USER_AGENT]: "@ingestkorea/client-sens/1.10.x",
|
|
9
9
|
};
|
|
10
10
|
return next(input, context);
|
|
11
11
|
};
|
|
@@ -1,60 +1,49 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.middlewareRetry = void 0;
|
|
4
|
-
const
|
|
4
|
+
const index_js_1 = require("../models/index.js");
|
|
5
5
|
const constants_js_1 = require("./constants.js");
|
|
6
6
|
const middlewareRetry = (next) => async (input, context) => {
|
|
7
|
-
var _a, _b;
|
|
8
|
-
var _c;
|
|
9
7
|
const MAX_RETRIES = 3;
|
|
8
|
+
const MIN_DELAY_MS = 300;
|
|
10
9
|
const BASE_DELAY_MS = 500;
|
|
11
10
|
const MAX_DELAY_MS = 5000;
|
|
12
11
|
let attempts = 0;
|
|
13
12
|
let totalRetryDelay = 0;
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
type: "Bad Request",
|
|
18
|
-
message: "Invalid Request",
|
|
19
|
-
description: { attempts, maxRetries: MAX_RETRIES, totalRetryDelay },
|
|
20
|
-
});
|
|
13
|
+
input.request.headers = {
|
|
14
|
+
...input.request.headers,
|
|
15
|
+
};
|
|
21
16
|
while (attempts < MAX_RETRIES) {
|
|
22
|
-
|
|
23
|
-
const requestLog = `attempt=${
|
|
17
|
+
attempts++;
|
|
18
|
+
const requestLog = `attempt=${attempts}; max=${MAX_RETRIES}; totalRetryDelay=${totalRetryDelay}`;
|
|
24
19
|
input.request.headers[constants_js_1.INGESTKOREA_REQUEST_LOG] = requestLog;
|
|
25
20
|
try {
|
|
26
21
|
const { response } = await next(input, context);
|
|
27
|
-
response.headers[constants_js_1.INGESTKOREA_RETRY] =
|
|
22
|
+
response.headers[constants_js_1.INGESTKOREA_RETRY] = attempts.toString();
|
|
28
23
|
response.headers[constants_js_1.INGESTKOREA_RETRY_DELAY] = totalRetryDelay.toString();
|
|
29
24
|
return { response };
|
|
30
25
|
}
|
|
31
26
|
catch (error) {
|
|
32
|
-
attempts++;
|
|
33
|
-
lastError.error.description = { attempts, maxRetries: MAX_RETRIES, totalRetryDelay };
|
|
34
|
-
if (error instanceof util_error_handler_1.IngestkoreaError) {
|
|
35
|
-
lastError.error.description = {
|
|
36
|
-
...lastError.error.description,
|
|
37
|
-
detail: error.error.description,
|
|
38
|
-
};
|
|
39
|
-
}
|
|
40
|
-
else {
|
|
41
|
-
lastError.error.description = {
|
|
42
|
-
...lastError.error.description,
|
|
43
|
-
detail: String((_b = error === null || error === void 0 ? void 0 : error.message) !== null && _b !== void 0 ? _b : error),
|
|
44
|
-
};
|
|
45
|
-
}
|
|
46
27
|
if (attempts >= MAX_RETRIES) {
|
|
47
|
-
|
|
48
|
-
|
|
28
|
+
throw new index_js_1.SensError({
|
|
29
|
+
code: -1,
|
|
30
|
+
type: "SDK.REQUEST_ERROR",
|
|
31
|
+
message: error instanceof Error ? error.message : requestLog,
|
|
32
|
+
});
|
|
49
33
|
}
|
|
50
34
|
const exp = BASE_DELAY_MS * 2 ** (attempts - 1);
|
|
51
35
|
const capped = Math.min(MAX_DELAY_MS, exp);
|
|
52
|
-
const
|
|
36
|
+
const baseWait = Math.max(MIN_DELAY_MS, Math.floor(capped / 2));
|
|
37
|
+
const jitter = Math.floor(Math.random() * (capped - baseWait));
|
|
38
|
+
const delay = baseWait + jitter;
|
|
53
39
|
totalRetryDelay += delay;
|
|
54
|
-
lastError.error.description = { attempts, maxRetries: MAX_RETRIES, totalRetryDelay };
|
|
55
40
|
await new Promise((resolve) => setTimeout(resolve, delay));
|
|
56
41
|
}
|
|
57
42
|
}
|
|
58
|
-
throw
|
|
43
|
+
throw new index_js_1.SensError({
|
|
44
|
+
code: -1,
|
|
45
|
+
type: "SDK.UNKNOWN_ERROR",
|
|
46
|
+
message: "Unexpected retry loop termination",
|
|
47
|
+
});
|
|
59
48
|
};
|
|
60
49
|
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-es/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);
|
|
@@ -19,80 +19,84 @@ const parseBody = async (output) => {
|
|
|
19
19
|
if (!isJsonResponse(headers["content-type"])) {
|
|
20
20
|
await (0, util_http_handler_1.destroyStream)(streamBody);
|
|
21
21
|
throw new index_js_1.SensError({
|
|
22
|
-
code:
|
|
23
|
-
type: "
|
|
22
|
+
code: -1,
|
|
23
|
+
type: "SDK.REQUEST_ERROR",
|
|
24
24
|
message: "response content-type is not application/json",
|
|
25
25
|
});
|
|
26
26
|
}
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
message: "parse response body error",
|
|
38
|
-
});
|
|
39
|
-
}
|
|
27
|
+
try {
|
|
28
|
+
const data = await (0, util_http_handler_1.collectBodyString)(streamBody);
|
|
29
|
+
return data.length ? JSON.parse(data) : {};
|
|
30
|
+
}
|
|
31
|
+
catch (e) {
|
|
32
|
+
throw new index_js_1.SensError({
|
|
33
|
+
code: -1,
|
|
34
|
+
type: "SDK.REQUEST_ERROR",
|
|
35
|
+
message: e instanceof Error ? e.message : "parse response body error",
|
|
36
|
+
});
|
|
40
37
|
}
|
|
41
|
-
return {};
|
|
42
38
|
};
|
|
43
39
|
exports.parseBody = parseBody;
|
|
44
40
|
const parseErrorBody = async (output) => {
|
|
45
41
|
const { statusCode, headers, body: streamBody } = output;
|
|
46
|
-
const error = new index_js_1.SensError({
|
|
47
|
-
code: statusCode !== null && statusCode !== void 0 ? statusCode : 400,
|
|
48
|
-
type: "Bad Request",
|
|
49
|
-
message: "something wrong",
|
|
50
|
-
});
|
|
51
42
|
if (!isJsonResponse(headers["content-type"])) {
|
|
52
43
|
await (0, util_http_handler_1.destroyStream)(streamBody);
|
|
53
|
-
|
|
54
|
-
|
|
44
|
+
throw new index_js_1.SensError({
|
|
45
|
+
code: -1,
|
|
46
|
+
type: "SDK.REQUEST_ERROR",
|
|
47
|
+
message: "response content-type is not application/json",
|
|
48
|
+
});
|
|
55
49
|
}
|
|
56
50
|
const data = await (0, util_http_handler_1.collectBodyString)(streamBody);
|
|
57
|
-
|
|
58
|
-
const
|
|
59
|
-
if (
|
|
60
|
-
|
|
51
|
+
const sensErrorBody = isSensErrorResponse(data);
|
|
52
|
+
const nCloudErrorBody = isNCloudErrorResponse(data);
|
|
53
|
+
if (sensErrorBody) {
|
|
54
|
+
const errorType = isSensErrorCode(sensErrorBody.status)
|
|
55
|
+
? index_js_1.SENS_ERROR_CODE_SPEC[sensErrorBody.status]
|
|
56
|
+
: "SDK.UNKNOWN_ERROR";
|
|
57
|
+
throw new index_js_1.SensError({
|
|
58
|
+
code: isSensErrorCode(sensErrorBody.status) ? sensErrorBody.status : statusCode,
|
|
59
|
+
type: errorType,
|
|
60
|
+
message: sensErrorBody.errorMessage || errorType,
|
|
61
|
+
...(sensErrorBody.errors &&
|
|
62
|
+
Array.isArray(sensErrorBody.errors) && {
|
|
63
|
+
errors: sensErrorBody.errors.filter((d) => !!d),
|
|
64
|
+
}),
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
if (nCloudErrorBody) {
|
|
68
|
+
const { errorCode } = nCloudErrorBody.error;
|
|
69
|
+
const errorType = isNCloudErrorCode(errorCode)
|
|
70
|
+
? index_js_1.NCLOUD_ERROR_CODE_SPEC[errorCode].type
|
|
71
|
+
: "SDK.UNKNOWN_ERROR";
|
|
72
|
+
throw new index_js_1.SensError({
|
|
73
|
+
code: isNCloudErrorCode(errorCode) ? index_js_1.NCLOUD_ERROR_CODE_SPEC[errorCode].status : statusCode,
|
|
74
|
+
type: errorType,
|
|
75
|
+
message: nCloudErrorBody.error.details || "Something wrong",
|
|
76
|
+
});
|
|
61
77
|
}
|
|
62
78
|
throw new index_js_1.SensError({
|
|
63
|
-
code:
|
|
64
|
-
type:
|
|
65
|
-
message:
|
|
66
|
-
|
|
67
|
-
Array.isArray(sensErrorBody.errors) && {
|
|
68
|
-
invalidInputs: sensErrorBody.errors.map((msg) => {
|
|
69
|
-
return { type: "not-valid.args", message: msg };
|
|
70
|
-
}),
|
|
71
|
-
}),
|
|
79
|
+
code: -1,
|
|
80
|
+
type: "SDK.UNKNOWN_ERROR",
|
|
81
|
+
message: "Invalid Naver Cloud Platform response format",
|
|
82
|
+
errors: [`${statusCode} - ${data.slice(0, 120)}...`],
|
|
72
83
|
});
|
|
73
84
|
};
|
|
74
85
|
exports.parseErrorBody = parseErrorBody;
|
|
75
|
-
const
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
429: "Too Many Requests",
|
|
81
|
-
500: "Internal Server Error",
|
|
86
|
+
const isSensErrorCode = (code) => {
|
|
87
|
+
return code in index_js_1.SENS_ERROR_CODE_SPEC;
|
|
88
|
+
};
|
|
89
|
+
const isNCloudErrorCode = (code) => {
|
|
90
|
+
return code in index_js_1.NCLOUD_ERROR_CODE_SPEC;
|
|
82
91
|
};
|
|
83
|
-
const
|
|
84
|
-
["status", "number", true],
|
|
85
|
-
["errorMessage", "string", false],
|
|
86
|
-
["errors", "object", false],
|
|
87
|
-
];
|
|
88
|
-
const isSensErrorBody = (input) => {
|
|
92
|
+
const isSensErrorResponse = (input) => {
|
|
89
93
|
if (typeof input !== "string")
|
|
90
94
|
return null;
|
|
91
95
|
try {
|
|
92
96
|
const data = JSON.parse(input);
|
|
93
97
|
if (!data || typeof data !== "object")
|
|
94
98
|
return null;
|
|
95
|
-
const isValid = SENS_ERROR_BODY_SPEC.every(([key, type, required]) => {
|
|
99
|
+
const isValid = index_js_1.SENS_ERROR_BODY_SPEC.every(([key, type, required]) => {
|
|
96
100
|
const value = data[key];
|
|
97
101
|
if (required && (value === undefined || value === null))
|
|
98
102
|
return false;
|
|
@@ -106,6 +110,27 @@ const isSensErrorBody = (input) => {
|
|
|
106
110
|
return null;
|
|
107
111
|
}
|
|
108
112
|
};
|
|
113
|
+
const isNCloudErrorResponse = (input) => {
|
|
114
|
+
if (typeof input !== "string")
|
|
115
|
+
return null;
|
|
116
|
+
try {
|
|
117
|
+
const data = JSON.parse(input);
|
|
118
|
+
if (!(data && typeof data == "object" && typeof data.error == "object"))
|
|
119
|
+
return null;
|
|
120
|
+
const isValid = index_js_1.NCLOUD_ERROR_BODY_SPEC.every(([key, type, required]) => {
|
|
121
|
+
const value = data.error[key];
|
|
122
|
+
if (required && (value === undefined || value === null))
|
|
123
|
+
return false;
|
|
124
|
+
if (value !== undefined && typeof value !== type)
|
|
125
|
+
return false;
|
|
126
|
+
return true;
|
|
127
|
+
});
|
|
128
|
+
return isValid ? data : null;
|
|
129
|
+
}
|
|
130
|
+
catch (_a) {
|
|
131
|
+
return null;
|
|
132
|
+
}
|
|
133
|
+
};
|
|
109
134
|
const compact = (obj) => {
|
|
110
135
|
if (Array.isArray(obj)) {
|
|
111
136
|
return obj.map((item) => (0, exports.compact)(item));
|
|
@@ -125,5 +150,6 @@ const convertToUtc = (input) => {
|
|
|
125
150
|
};
|
|
126
151
|
exports.convertToUtc = convertToUtc;
|
|
127
152
|
const isJsonResponse = (contentType) => {
|
|
128
|
-
|
|
153
|
+
var _a;
|
|
154
|
+
return (_a = contentType === null || contentType === void 0 ? void 0 : contentType.toLowerCase().includes("application/json")) !== null && _a !== void 0 ? _a : false;
|
|
129
155
|
};
|
package/dist-types/index.d.ts
CHANGED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { SesnResponseErrorType } from "./SensErrorInfo.js";
|
|
2
|
+
type SensClientErrorType = "SDK.AUTH_ERROR" | "SDK.GENERAL_ERROR" | "SDK.REQUEST_ERROR" | "SDK.UNKNOWN_ERROR";
|
|
3
|
+
export type SensErrorType = SesnResponseErrorType | SensClientErrorType;
|
|
4
|
+
export interface SensErrorInput {
|
|
5
|
+
code: number;
|
|
6
|
+
type: SensErrorType;
|
|
7
|
+
message: string;
|
|
8
|
+
errors?: string[];
|
|
9
|
+
}
|
|
10
|
+
export declare class SensError extends Error {
|
|
11
|
+
readonly code: number;
|
|
12
|
+
readonly type: SensErrorType;
|
|
13
|
+
readonly errors: string[];
|
|
14
|
+
readonly timestamp: string;
|
|
15
|
+
constructor(info: SensErrorInput);
|
|
16
|
+
}
|
|
17
|
+
export {};
|