@cstar.help/js 0.12.1 → 0.13.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/dist/auth/index.cjs +217 -3
- package/dist/auth/index.js +215 -3
- package/dist/chat/index.cjs +46 -26
- package/dist/chat/index.d.cts +28 -24
- package/dist/chat/index.d.ts +28 -24
- package/dist/chat/index.js +46 -26
- package/dist/community/index.cjs +233 -9
- package/dist/community/index.d.cts +11 -0
- package/dist/community/index.d.ts +11 -0
- package/dist/community/index.js +231 -9
- package/dist/index.cjs +268 -73
- package/dist/index.d.cts +3259 -0
- package/dist/index.d.ts +3259 -0
- package/dist/index.js +267 -73
- package/dist/library/index.cjs +254 -9
- package/dist/library/index.d.cts +26 -3
- package/dist/library/index.d.ts +26 -3
- package/dist/library/index.js +252 -9
- package/dist/quickhelp/index.cjs +176 -2
- package/dist/quickhelp/index.d.cts +11 -0
- package/dist/quickhelp/index.d.ts +11 -0
- package/dist/quickhelp/index.js +174 -2
- package/dist/webhook/index.cjs +0 -18
- package/dist/webhook/index.d.cts +440 -0
- package/dist/webhook/index.d.ts +440 -0
- package/dist/webhook/index.js +0 -23
- package/dist/webhook/node.cjs +57 -0
- package/dist/webhook/node.d.cts +440 -0
- package/dist/webhook/node.d.ts +440 -0
- package/dist/webhook/node.js +30 -0
- package/package.json +14 -2
package/dist/auth/index.cjs
CHANGED
|
@@ -23,6 +23,221 @@ __export(auth_exports, {
|
|
|
23
23
|
AuthClient: () => AuthClient
|
|
24
24
|
});
|
|
25
25
|
module.exports = __toCommonJS(auth_exports);
|
|
26
|
+
|
|
27
|
+
// src/errors.ts
|
|
28
|
+
var ErrorType = {
|
|
29
|
+
AUTHENTICATION_ERROR: "authentication_error",
|
|
30
|
+
PERMISSION_ERROR: "permission_error",
|
|
31
|
+
INVALID_REQUEST_ERROR: "invalid_request_error",
|
|
32
|
+
NOT_FOUND_ERROR: "not_found_error",
|
|
33
|
+
CONFLICT_ERROR: "conflict_error",
|
|
34
|
+
RATE_LIMIT_ERROR: "rate_limit_error",
|
|
35
|
+
FEATURE_NOT_CONFIGURED: "feature_not_configured",
|
|
36
|
+
API_ERROR: "api_error",
|
|
37
|
+
SERVER_ERROR: "server_error"
|
|
38
|
+
};
|
|
39
|
+
var DEFAULT_DOC_URL = "https://cstar.help/developers/api/errors";
|
|
40
|
+
function normalizeArgs(messageOrStatus, maybeBody, defaults) {
|
|
41
|
+
if (typeof messageOrStatus === "string") {
|
|
42
|
+
return {
|
|
43
|
+
statusCode: defaults?.code === "rate_limit_error" ? 429 : 0,
|
|
44
|
+
body: {
|
|
45
|
+
type: defaults?.type ?? ErrorType.API_ERROR,
|
|
46
|
+
code: defaults?.code ?? "sdk_error",
|
|
47
|
+
message: messageOrStatus,
|
|
48
|
+
doc_url: defaults?.doc_url ?? DEFAULT_DOC_URL,
|
|
49
|
+
...defaults?.feature ? { feature: defaults.feature } : {},
|
|
50
|
+
...defaults?.enable_url ? { enable_url: defaults.enable_url } : {}
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
return {
|
|
55
|
+
statusCode: messageOrStatus,
|
|
56
|
+
body: maybeBody ?? {
|
|
57
|
+
type: defaults?.type ?? ErrorType.API_ERROR,
|
|
58
|
+
code: defaults?.code ?? "unknown_error",
|
|
59
|
+
message: defaults?.message ?? `Request failed with status ${messageOrStatus}`,
|
|
60
|
+
doc_url: defaults?.doc_url ?? DEFAULT_DOC_URL
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
var CStarError = class extends Error {
|
|
65
|
+
constructor(messageOrStatus, body) {
|
|
66
|
+
const { statusCode, body: resolved } = normalizeArgs(messageOrStatus, body);
|
|
67
|
+
super(resolved.message);
|
|
68
|
+
this.name = "CStarError";
|
|
69
|
+
this.type = resolved.type;
|
|
70
|
+
this.code = resolved.code;
|
|
71
|
+
this.param = resolved.param;
|
|
72
|
+
this.docUrl = resolved.doc_url;
|
|
73
|
+
this.requestId = resolved.request_id;
|
|
74
|
+
this.statusCode = statusCode;
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
var CStarAuthenticationError = class extends CStarError {
|
|
78
|
+
constructor(messageOrStatus, body) {
|
|
79
|
+
const { statusCode, body: resolved } = normalizeArgs(messageOrStatus, body, {
|
|
80
|
+
type: ErrorType.AUTHENTICATION_ERROR,
|
|
81
|
+
code: "authentication_failed",
|
|
82
|
+
doc_url: `${DEFAULT_DOC_URL}#authentication-error`
|
|
83
|
+
});
|
|
84
|
+
super(statusCode, resolved);
|
|
85
|
+
this.name = "CStarAuthenticationError";
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
var CStarRateLimitError = class extends CStarError {
|
|
89
|
+
constructor(messageOrStatus, body, retryAfter) {
|
|
90
|
+
const { statusCode, body: resolved } = normalizeArgs(messageOrStatus, body, {
|
|
91
|
+
type: ErrorType.RATE_LIMIT_ERROR,
|
|
92
|
+
code: "rate_limit_exceeded",
|
|
93
|
+
doc_url: `${DEFAULT_DOC_URL}#rate-limit-error`
|
|
94
|
+
});
|
|
95
|
+
super(statusCode === 0 ? 429 : statusCode, resolved);
|
|
96
|
+
this.name = "CStarRateLimitError";
|
|
97
|
+
this.retryAfter = retryAfter ?? 60;
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
var CStarNotFoundError = class extends CStarError {
|
|
101
|
+
constructor(messageOrStatus, body) {
|
|
102
|
+
const { statusCode, body: resolved } = normalizeArgs(messageOrStatus, body, {
|
|
103
|
+
type: ErrorType.NOT_FOUND_ERROR,
|
|
104
|
+
code: "resource_not_found",
|
|
105
|
+
doc_url: `${DEFAULT_DOC_URL}#not-found-error`
|
|
106
|
+
});
|
|
107
|
+
super(statusCode, resolved);
|
|
108
|
+
this.name = "CStarNotFoundError";
|
|
109
|
+
}
|
|
110
|
+
};
|
|
111
|
+
var CStarValidationError = class extends CStarError {
|
|
112
|
+
constructor(messageOrStatus, body) {
|
|
113
|
+
const { statusCode, body: resolved } = normalizeArgs(messageOrStatus, body, {
|
|
114
|
+
type: ErrorType.INVALID_REQUEST_ERROR,
|
|
115
|
+
code: "validation_error",
|
|
116
|
+
doc_url: `${DEFAULT_DOC_URL}#validation-error`
|
|
117
|
+
});
|
|
118
|
+
super(statusCode, resolved);
|
|
119
|
+
this.name = "CStarValidationError";
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
var CStarPermissionError = class extends CStarError {
|
|
123
|
+
constructor(messageOrStatus, body) {
|
|
124
|
+
const { statusCode, body: resolved } = normalizeArgs(messageOrStatus, body, {
|
|
125
|
+
type: ErrorType.PERMISSION_ERROR,
|
|
126
|
+
code: "permission_denied",
|
|
127
|
+
doc_url: `${DEFAULT_DOC_URL}#permission-error`
|
|
128
|
+
});
|
|
129
|
+
super(statusCode, resolved);
|
|
130
|
+
this.name = "CStarPermissionError";
|
|
131
|
+
}
|
|
132
|
+
};
|
|
133
|
+
var CStarConflictError = class extends CStarError {
|
|
134
|
+
constructor(messageOrStatus, body) {
|
|
135
|
+
const { statusCode, body: resolved } = normalizeArgs(messageOrStatus, body, {
|
|
136
|
+
type: ErrorType.CONFLICT_ERROR,
|
|
137
|
+
code: "conflict",
|
|
138
|
+
doc_url: `${DEFAULT_DOC_URL}#conflict-error`
|
|
139
|
+
});
|
|
140
|
+
super(statusCode, resolved);
|
|
141
|
+
this.name = "CStarConflictError";
|
|
142
|
+
}
|
|
143
|
+
};
|
|
144
|
+
var CStarServerError = class extends CStarError {
|
|
145
|
+
constructor(messageOrStatus, body) {
|
|
146
|
+
const { statusCode, body: resolved } = normalizeArgs(messageOrStatus, body, {
|
|
147
|
+
type: ErrorType.SERVER_ERROR,
|
|
148
|
+
code: "server_error",
|
|
149
|
+
doc_url: `${DEFAULT_DOC_URL}#server-error`
|
|
150
|
+
});
|
|
151
|
+
super(statusCode, resolved);
|
|
152
|
+
this.name = "CStarServerError";
|
|
153
|
+
}
|
|
154
|
+
};
|
|
155
|
+
var CStarFeatureDisabledError = class extends CStarError {
|
|
156
|
+
constructor(messageOrStatus, body) {
|
|
157
|
+
const { statusCode, body: resolved } = normalizeArgs(messageOrStatus, body, {
|
|
158
|
+
type: ErrorType.FEATURE_NOT_CONFIGURED,
|
|
159
|
+
code: "feature_not_configured",
|
|
160
|
+
doc_url: `${DEFAULT_DOC_URL}#feature-not-configured`
|
|
161
|
+
});
|
|
162
|
+
super(statusCode, resolved);
|
|
163
|
+
this.name = "CStarFeatureDisabledError";
|
|
164
|
+
this.feature = resolved.feature ?? "unknown";
|
|
165
|
+
this.enableUrl = resolved.enable_url ?? "";
|
|
166
|
+
}
|
|
167
|
+
};
|
|
168
|
+
function parsePublicError(status, body, fallbackMessage) {
|
|
169
|
+
const obj = body && typeof body === "object" ? body : null;
|
|
170
|
+
const errVal = obj?.error;
|
|
171
|
+
let message = fallbackMessage;
|
|
172
|
+
let structured = null;
|
|
173
|
+
if (typeof errVal === "string") {
|
|
174
|
+
message = errVal;
|
|
175
|
+
} else if (errVal && typeof errVal === "object") {
|
|
176
|
+
const e = errVal;
|
|
177
|
+
if (typeof e.message === "string") message = e.message;
|
|
178
|
+
structured = {
|
|
179
|
+
type: typeof e.type === "string" ? e.type : ErrorType.API_ERROR,
|
|
180
|
+
code: typeof e.code === "string" ? e.code : "api_error",
|
|
181
|
+
message,
|
|
182
|
+
param: typeof e.param === "string" ? e.param : void 0,
|
|
183
|
+
doc_url: typeof e.doc_url === "string" ? e.doc_url : DEFAULT_DOC_URL,
|
|
184
|
+
request_id: typeof e.request_id === "string" ? e.request_id : void 0,
|
|
185
|
+
feature: typeof e.feature === "string" ? e.feature : void 0,
|
|
186
|
+
enable_url: typeof e.enable_url === "string" ? e.enable_url : void 0
|
|
187
|
+
};
|
|
188
|
+
} else if (typeof obj?.message === "string") {
|
|
189
|
+
message = obj.message;
|
|
190
|
+
}
|
|
191
|
+
const errorBody = structured ?? {
|
|
192
|
+
type: ErrorType.API_ERROR,
|
|
193
|
+
code: status >= 500 ? "server_error" : "request_failed",
|
|
194
|
+
message,
|
|
195
|
+
doc_url: DEFAULT_DOC_URL
|
|
196
|
+
};
|
|
197
|
+
return parseError(status, { error: errorBody });
|
|
198
|
+
}
|
|
199
|
+
var STATUS_TO_CLASS = {
|
|
200
|
+
400: CStarValidationError,
|
|
201
|
+
401: CStarAuthenticationError,
|
|
202
|
+
403: CStarPermissionError,
|
|
203
|
+
404: CStarNotFoundError,
|
|
204
|
+
409: CStarConflictError,
|
|
205
|
+
422: CStarValidationError,
|
|
206
|
+
429: CStarRateLimitError
|
|
207
|
+
};
|
|
208
|
+
var TYPE_TO_CLASS = {
|
|
209
|
+
[ErrorType.AUTHENTICATION_ERROR]: CStarAuthenticationError,
|
|
210
|
+
[ErrorType.PERMISSION_ERROR]: CStarPermissionError,
|
|
211
|
+
[ErrorType.RATE_LIMIT_ERROR]: CStarRateLimitError,
|
|
212
|
+
[ErrorType.NOT_FOUND_ERROR]: CStarNotFoundError,
|
|
213
|
+
[ErrorType.INVALID_REQUEST_ERROR]: CStarValidationError,
|
|
214
|
+
[ErrorType.CONFLICT_ERROR]: CStarConflictError,
|
|
215
|
+
[ErrorType.SERVER_ERROR]: CStarServerError,
|
|
216
|
+
[ErrorType.FEATURE_NOT_CONFIGURED]: CStarFeatureDisabledError
|
|
217
|
+
};
|
|
218
|
+
function parseError(statusCode, body, retryAfter) {
|
|
219
|
+
const errorBody = body?.error ?? {
|
|
220
|
+
type: ErrorType.API_ERROR,
|
|
221
|
+
code: "unknown_error",
|
|
222
|
+
message: `Request failed with status ${statusCode}`,
|
|
223
|
+
doc_url: DEFAULT_DOC_URL
|
|
224
|
+
};
|
|
225
|
+
if (statusCode === 429) {
|
|
226
|
+
return new CStarRateLimitError(statusCode, errorBody, retryAfter ?? 60);
|
|
227
|
+
}
|
|
228
|
+
const StatusCls = STATUS_TO_CLASS[statusCode];
|
|
229
|
+
if (StatusCls) {
|
|
230
|
+
return new StatusCls(statusCode, errorBody);
|
|
231
|
+
}
|
|
232
|
+
const TypeCls = TYPE_TO_CLASS[errorBody.type];
|
|
233
|
+
if (TypeCls) {
|
|
234
|
+
return new TypeCls(statusCode, errorBody);
|
|
235
|
+
}
|
|
236
|
+
if (statusCode >= 500) return new CStarServerError(statusCode, errorBody);
|
|
237
|
+
return new CStarError(statusCode, errorBody);
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
// src/auth/index.ts
|
|
26
241
|
var DEFAULT_BASE_URL = "https://www.cstar.help";
|
|
27
242
|
var AuthClient = class {
|
|
28
243
|
constructor(config) {
|
|
@@ -114,9 +329,8 @@ var AuthClient = class {
|
|
|
114
329
|
body: options.body ? JSON.stringify(options.body) : void 0
|
|
115
330
|
});
|
|
116
331
|
if (!response.ok) {
|
|
117
|
-
const errorBody = await response.json().catch(() =>
|
|
118
|
-
|
|
119
|
-
throw new Error(message);
|
|
332
|
+
const errorBody = await response.json().catch(() => null);
|
|
333
|
+
throw parsePublicError(response.status, errorBody, `Auth API error: ${response.status}`);
|
|
120
334
|
}
|
|
121
335
|
return response.json();
|
|
122
336
|
}
|
package/dist/auth/index.js
CHANGED
|
@@ -1,3 +1,216 @@
|
|
|
1
|
+
// src/errors.ts
|
|
2
|
+
var ErrorType = {
|
|
3
|
+
AUTHENTICATION_ERROR: "authentication_error",
|
|
4
|
+
PERMISSION_ERROR: "permission_error",
|
|
5
|
+
INVALID_REQUEST_ERROR: "invalid_request_error",
|
|
6
|
+
NOT_FOUND_ERROR: "not_found_error",
|
|
7
|
+
CONFLICT_ERROR: "conflict_error",
|
|
8
|
+
RATE_LIMIT_ERROR: "rate_limit_error",
|
|
9
|
+
FEATURE_NOT_CONFIGURED: "feature_not_configured",
|
|
10
|
+
API_ERROR: "api_error",
|
|
11
|
+
SERVER_ERROR: "server_error"
|
|
12
|
+
};
|
|
13
|
+
var DEFAULT_DOC_URL = "https://cstar.help/developers/api/errors";
|
|
14
|
+
function normalizeArgs(messageOrStatus, maybeBody, defaults) {
|
|
15
|
+
if (typeof messageOrStatus === "string") {
|
|
16
|
+
return {
|
|
17
|
+
statusCode: defaults?.code === "rate_limit_error" ? 429 : 0,
|
|
18
|
+
body: {
|
|
19
|
+
type: defaults?.type ?? ErrorType.API_ERROR,
|
|
20
|
+
code: defaults?.code ?? "sdk_error",
|
|
21
|
+
message: messageOrStatus,
|
|
22
|
+
doc_url: defaults?.doc_url ?? DEFAULT_DOC_URL,
|
|
23
|
+
...defaults?.feature ? { feature: defaults.feature } : {},
|
|
24
|
+
...defaults?.enable_url ? { enable_url: defaults.enable_url } : {}
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
return {
|
|
29
|
+
statusCode: messageOrStatus,
|
|
30
|
+
body: maybeBody ?? {
|
|
31
|
+
type: defaults?.type ?? ErrorType.API_ERROR,
|
|
32
|
+
code: defaults?.code ?? "unknown_error",
|
|
33
|
+
message: defaults?.message ?? `Request failed with status ${messageOrStatus}`,
|
|
34
|
+
doc_url: defaults?.doc_url ?? DEFAULT_DOC_URL
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
var CStarError = class extends Error {
|
|
39
|
+
constructor(messageOrStatus, body) {
|
|
40
|
+
const { statusCode, body: resolved } = normalizeArgs(messageOrStatus, body);
|
|
41
|
+
super(resolved.message);
|
|
42
|
+
this.name = "CStarError";
|
|
43
|
+
this.type = resolved.type;
|
|
44
|
+
this.code = resolved.code;
|
|
45
|
+
this.param = resolved.param;
|
|
46
|
+
this.docUrl = resolved.doc_url;
|
|
47
|
+
this.requestId = resolved.request_id;
|
|
48
|
+
this.statusCode = statusCode;
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
var CStarAuthenticationError = class extends CStarError {
|
|
52
|
+
constructor(messageOrStatus, body) {
|
|
53
|
+
const { statusCode, body: resolved } = normalizeArgs(messageOrStatus, body, {
|
|
54
|
+
type: ErrorType.AUTHENTICATION_ERROR,
|
|
55
|
+
code: "authentication_failed",
|
|
56
|
+
doc_url: `${DEFAULT_DOC_URL}#authentication-error`
|
|
57
|
+
});
|
|
58
|
+
super(statusCode, resolved);
|
|
59
|
+
this.name = "CStarAuthenticationError";
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
var CStarRateLimitError = class extends CStarError {
|
|
63
|
+
constructor(messageOrStatus, body, retryAfter) {
|
|
64
|
+
const { statusCode, body: resolved } = normalizeArgs(messageOrStatus, body, {
|
|
65
|
+
type: ErrorType.RATE_LIMIT_ERROR,
|
|
66
|
+
code: "rate_limit_exceeded",
|
|
67
|
+
doc_url: `${DEFAULT_DOC_URL}#rate-limit-error`
|
|
68
|
+
});
|
|
69
|
+
super(statusCode === 0 ? 429 : statusCode, resolved);
|
|
70
|
+
this.name = "CStarRateLimitError";
|
|
71
|
+
this.retryAfter = retryAfter ?? 60;
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
var CStarNotFoundError = class extends CStarError {
|
|
75
|
+
constructor(messageOrStatus, body) {
|
|
76
|
+
const { statusCode, body: resolved } = normalizeArgs(messageOrStatus, body, {
|
|
77
|
+
type: ErrorType.NOT_FOUND_ERROR,
|
|
78
|
+
code: "resource_not_found",
|
|
79
|
+
doc_url: `${DEFAULT_DOC_URL}#not-found-error`
|
|
80
|
+
});
|
|
81
|
+
super(statusCode, resolved);
|
|
82
|
+
this.name = "CStarNotFoundError";
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
var CStarValidationError = class extends CStarError {
|
|
86
|
+
constructor(messageOrStatus, body) {
|
|
87
|
+
const { statusCode, body: resolved } = normalizeArgs(messageOrStatus, body, {
|
|
88
|
+
type: ErrorType.INVALID_REQUEST_ERROR,
|
|
89
|
+
code: "validation_error",
|
|
90
|
+
doc_url: `${DEFAULT_DOC_URL}#validation-error`
|
|
91
|
+
});
|
|
92
|
+
super(statusCode, resolved);
|
|
93
|
+
this.name = "CStarValidationError";
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
var CStarPermissionError = class extends CStarError {
|
|
97
|
+
constructor(messageOrStatus, body) {
|
|
98
|
+
const { statusCode, body: resolved } = normalizeArgs(messageOrStatus, body, {
|
|
99
|
+
type: ErrorType.PERMISSION_ERROR,
|
|
100
|
+
code: "permission_denied",
|
|
101
|
+
doc_url: `${DEFAULT_DOC_URL}#permission-error`
|
|
102
|
+
});
|
|
103
|
+
super(statusCode, resolved);
|
|
104
|
+
this.name = "CStarPermissionError";
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
var CStarConflictError = class extends CStarError {
|
|
108
|
+
constructor(messageOrStatus, body) {
|
|
109
|
+
const { statusCode, body: resolved } = normalizeArgs(messageOrStatus, body, {
|
|
110
|
+
type: ErrorType.CONFLICT_ERROR,
|
|
111
|
+
code: "conflict",
|
|
112
|
+
doc_url: `${DEFAULT_DOC_URL}#conflict-error`
|
|
113
|
+
});
|
|
114
|
+
super(statusCode, resolved);
|
|
115
|
+
this.name = "CStarConflictError";
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
var CStarServerError = class extends CStarError {
|
|
119
|
+
constructor(messageOrStatus, body) {
|
|
120
|
+
const { statusCode, body: resolved } = normalizeArgs(messageOrStatus, body, {
|
|
121
|
+
type: ErrorType.SERVER_ERROR,
|
|
122
|
+
code: "server_error",
|
|
123
|
+
doc_url: `${DEFAULT_DOC_URL}#server-error`
|
|
124
|
+
});
|
|
125
|
+
super(statusCode, resolved);
|
|
126
|
+
this.name = "CStarServerError";
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
var CStarFeatureDisabledError = class extends CStarError {
|
|
130
|
+
constructor(messageOrStatus, body) {
|
|
131
|
+
const { statusCode, body: resolved } = normalizeArgs(messageOrStatus, body, {
|
|
132
|
+
type: ErrorType.FEATURE_NOT_CONFIGURED,
|
|
133
|
+
code: "feature_not_configured",
|
|
134
|
+
doc_url: `${DEFAULT_DOC_URL}#feature-not-configured`
|
|
135
|
+
});
|
|
136
|
+
super(statusCode, resolved);
|
|
137
|
+
this.name = "CStarFeatureDisabledError";
|
|
138
|
+
this.feature = resolved.feature ?? "unknown";
|
|
139
|
+
this.enableUrl = resolved.enable_url ?? "";
|
|
140
|
+
}
|
|
141
|
+
};
|
|
142
|
+
function parsePublicError(status, body, fallbackMessage) {
|
|
143
|
+
const obj = body && typeof body === "object" ? body : null;
|
|
144
|
+
const errVal = obj?.error;
|
|
145
|
+
let message = fallbackMessage;
|
|
146
|
+
let structured = null;
|
|
147
|
+
if (typeof errVal === "string") {
|
|
148
|
+
message = errVal;
|
|
149
|
+
} else if (errVal && typeof errVal === "object") {
|
|
150
|
+
const e = errVal;
|
|
151
|
+
if (typeof e.message === "string") message = e.message;
|
|
152
|
+
structured = {
|
|
153
|
+
type: typeof e.type === "string" ? e.type : ErrorType.API_ERROR,
|
|
154
|
+
code: typeof e.code === "string" ? e.code : "api_error",
|
|
155
|
+
message,
|
|
156
|
+
param: typeof e.param === "string" ? e.param : void 0,
|
|
157
|
+
doc_url: typeof e.doc_url === "string" ? e.doc_url : DEFAULT_DOC_URL,
|
|
158
|
+
request_id: typeof e.request_id === "string" ? e.request_id : void 0,
|
|
159
|
+
feature: typeof e.feature === "string" ? e.feature : void 0,
|
|
160
|
+
enable_url: typeof e.enable_url === "string" ? e.enable_url : void 0
|
|
161
|
+
};
|
|
162
|
+
} else if (typeof obj?.message === "string") {
|
|
163
|
+
message = obj.message;
|
|
164
|
+
}
|
|
165
|
+
const errorBody = structured ?? {
|
|
166
|
+
type: ErrorType.API_ERROR,
|
|
167
|
+
code: status >= 500 ? "server_error" : "request_failed",
|
|
168
|
+
message,
|
|
169
|
+
doc_url: DEFAULT_DOC_URL
|
|
170
|
+
};
|
|
171
|
+
return parseError(status, { error: errorBody });
|
|
172
|
+
}
|
|
173
|
+
var STATUS_TO_CLASS = {
|
|
174
|
+
400: CStarValidationError,
|
|
175
|
+
401: CStarAuthenticationError,
|
|
176
|
+
403: CStarPermissionError,
|
|
177
|
+
404: CStarNotFoundError,
|
|
178
|
+
409: CStarConflictError,
|
|
179
|
+
422: CStarValidationError,
|
|
180
|
+
429: CStarRateLimitError
|
|
181
|
+
};
|
|
182
|
+
var TYPE_TO_CLASS = {
|
|
183
|
+
[ErrorType.AUTHENTICATION_ERROR]: CStarAuthenticationError,
|
|
184
|
+
[ErrorType.PERMISSION_ERROR]: CStarPermissionError,
|
|
185
|
+
[ErrorType.RATE_LIMIT_ERROR]: CStarRateLimitError,
|
|
186
|
+
[ErrorType.NOT_FOUND_ERROR]: CStarNotFoundError,
|
|
187
|
+
[ErrorType.INVALID_REQUEST_ERROR]: CStarValidationError,
|
|
188
|
+
[ErrorType.CONFLICT_ERROR]: CStarConflictError,
|
|
189
|
+
[ErrorType.SERVER_ERROR]: CStarServerError,
|
|
190
|
+
[ErrorType.FEATURE_NOT_CONFIGURED]: CStarFeatureDisabledError
|
|
191
|
+
};
|
|
192
|
+
function parseError(statusCode, body, retryAfter) {
|
|
193
|
+
const errorBody = body?.error ?? {
|
|
194
|
+
type: ErrorType.API_ERROR,
|
|
195
|
+
code: "unknown_error",
|
|
196
|
+
message: `Request failed with status ${statusCode}`,
|
|
197
|
+
doc_url: DEFAULT_DOC_URL
|
|
198
|
+
};
|
|
199
|
+
if (statusCode === 429) {
|
|
200
|
+
return new CStarRateLimitError(statusCode, errorBody, retryAfter ?? 60);
|
|
201
|
+
}
|
|
202
|
+
const StatusCls = STATUS_TO_CLASS[statusCode];
|
|
203
|
+
if (StatusCls) {
|
|
204
|
+
return new StatusCls(statusCode, errorBody);
|
|
205
|
+
}
|
|
206
|
+
const TypeCls = TYPE_TO_CLASS[errorBody.type];
|
|
207
|
+
if (TypeCls) {
|
|
208
|
+
return new TypeCls(statusCode, errorBody);
|
|
209
|
+
}
|
|
210
|
+
if (statusCode >= 500) return new CStarServerError(statusCode, errorBody);
|
|
211
|
+
return new CStarError(statusCode, errorBody);
|
|
212
|
+
}
|
|
213
|
+
|
|
1
214
|
// src/auth/index.ts
|
|
2
215
|
var DEFAULT_BASE_URL = "https://www.cstar.help";
|
|
3
216
|
var AuthClient = class {
|
|
@@ -90,9 +303,8 @@ var AuthClient = class {
|
|
|
90
303
|
body: options.body ? JSON.stringify(options.body) : void 0
|
|
91
304
|
});
|
|
92
305
|
if (!response.ok) {
|
|
93
|
-
const errorBody = await response.json().catch(() =>
|
|
94
|
-
|
|
95
|
-
throw new Error(message);
|
|
306
|
+
const errorBody = await response.json().catch(() => null);
|
|
307
|
+
throw parsePublicError(response.status, errorBody, `Auth API error: ${response.status}`);
|
|
96
308
|
}
|
|
97
309
|
return response.json();
|
|
98
310
|
}
|
package/dist/chat/index.cjs
CHANGED
|
@@ -289,37 +289,45 @@ var PresenceManager = class {
|
|
|
289
289
|
};
|
|
290
290
|
|
|
291
291
|
// src/chat/errors.ts
|
|
292
|
+
function normalizeChatArgs(arg, fallbackCode) {
|
|
293
|
+
if (typeof arg === "string") {
|
|
294
|
+
return { message: arg, code: fallbackCode, statusCode: 0, requestId: null };
|
|
295
|
+
}
|
|
296
|
+
return { ...arg, code: arg.code ?? fallbackCode };
|
|
297
|
+
}
|
|
292
298
|
var ChatClientError = class extends Error {
|
|
293
|
-
constructor(
|
|
294
|
-
|
|
299
|
+
constructor(arg) {
|
|
300
|
+
const opts = normalizeChatArgs(arg, "UNKNOWN");
|
|
301
|
+
super(opts.message);
|
|
295
302
|
this.name = "ChatClientError";
|
|
296
303
|
this.code = opts.code ?? "UNKNOWN";
|
|
297
|
-
this.statusCode = opts.statusCode;
|
|
304
|
+
this.statusCode = opts.statusCode ?? 0;
|
|
298
305
|
this.requestId = opts.requestId ?? null;
|
|
299
306
|
}
|
|
300
307
|
};
|
|
301
308
|
var ChatClientAuthError = class extends ChatClientError {
|
|
302
|
-
constructor(
|
|
303
|
-
super(
|
|
309
|
+
constructor(arg) {
|
|
310
|
+
super(normalizeChatArgs(arg, "AUTH_ERROR"));
|
|
304
311
|
this.name = "ChatClientAuthError";
|
|
305
312
|
}
|
|
306
313
|
};
|
|
307
314
|
var ChatClientRateLimitError = class extends ChatClientError {
|
|
308
|
-
constructor(
|
|
309
|
-
|
|
315
|
+
constructor(arg) {
|
|
316
|
+
const opts = typeof arg === "string" ? { message: arg, code: "RATE_LIMIT", statusCode: 429, requestId: null, retryAfter: null } : { ...arg, code: arg.code ?? "RATE_LIMIT" };
|
|
317
|
+
super(opts);
|
|
310
318
|
this.name = "ChatClientRateLimitError";
|
|
311
319
|
this.retryAfter = opts.retryAfter ?? null;
|
|
312
320
|
}
|
|
313
321
|
};
|
|
314
322
|
var ChatClientServerError = class extends ChatClientError {
|
|
315
|
-
constructor(
|
|
316
|
-
super(
|
|
323
|
+
constructor(arg) {
|
|
324
|
+
super(normalizeChatArgs(arg, "SERVER_ERROR"));
|
|
317
325
|
this.name = "ChatClientServerError";
|
|
318
326
|
}
|
|
319
327
|
};
|
|
320
328
|
var ChatClientNetworkError = class extends ChatClientError {
|
|
321
329
|
constructor(opts) {
|
|
322
|
-
super(opts.message,
|
|
330
|
+
super({ message: opts.message, code: "NETWORK_ERROR", statusCode: 0, requestId: null });
|
|
323
331
|
this.name = "ChatClientNetworkError";
|
|
324
332
|
this.originalError = opts.originalError;
|
|
325
333
|
}
|
|
@@ -374,6 +382,7 @@ var ChatClient = class {
|
|
|
374
382
|
this.sessionToken = response.data.sessionToken;
|
|
375
383
|
this.teamId = response.data.teamId;
|
|
376
384
|
this.customerId = response.data.customer.id;
|
|
385
|
+
this.authMode = "hmac";
|
|
377
386
|
if (this.realtimeEnabled) {
|
|
378
387
|
this.realtimeInitPromise = this.initRealtime(
|
|
379
388
|
response.data.customer.id,
|
|
@@ -571,7 +580,8 @@ var ChatClient = class {
|
|
|
571
580
|
requestId
|
|
572
581
|
});
|
|
573
582
|
}
|
|
574
|
-
throw new ChatClientError(
|
|
583
|
+
throw new ChatClientError({
|
|
584
|
+
message,
|
|
575
585
|
code,
|
|
576
586
|
statusCode: response.status,
|
|
577
587
|
requestId
|
|
@@ -709,7 +719,7 @@ var TicketsApi = class {
|
|
|
709
719
|
/** List tickets for the identified customer. */
|
|
710
720
|
async list(params) {
|
|
711
721
|
const teamId = this.chat.getTeamId();
|
|
712
|
-
if (!teamId) throw new
|
|
722
|
+
if (!teamId) throw new ChatClientAuthError("Not identified. Call chat.identify() first.");
|
|
713
723
|
const response = await this.chat.fetch("/api/widget/conversations", {
|
|
714
724
|
method: "GET",
|
|
715
725
|
query: {
|
|
@@ -727,7 +737,7 @@ var TicketsApi = class {
|
|
|
727
737
|
/** Create a new ticket. */
|
|
728
738
|
async create(params) {
|
|
729
739
|
const teamId = this.chat.getTeamId();
|
|
730
|
-
if (!teamId) throw new
|
|
740
|
+
if (!teamId) throw new ChatClientAuthError("Not identified. Call chat.identify() first.");
|
|
731
741
|
const response = await this.chat.fetch("/api/widget/tickets", {
|
|
732
742
|
method: "POST",
|
|
733
743
|
body: {
|
|
@@ -736,15 +746,25 @@ var TicketsApi = class {
|
|
|
736
746
|
message: params.message
|
|
737
747
|
}
|
|
738
748
|
});
|
|
749
|
+
const messages = (response.data.messages ?? []).map((m) => ({
|
|
750
|
+
id: m.id,
|
|
751
|
+
content: m.content,
|
|
752
|
+
sender: m.sender,
|
|
753
|
+
sender_name: m.sender_name,
|
|
754
|
+
created_at: m.created_at,
|
|
755
|
+
read_at: m.read_at,
|
|
756
|
+
attachments: m.attachments ?? []
|
|
757
|
+
}));
|
|
739
758
|
return {
|
|
740
759
|
id: response.data.id,
|
|
741
|
-
ticketNumber:
|
|
760
|
+
ticketNumber: null,
|
|
742
761
|
subject: response.data.subject,
|
|
743
762
|
status: response.data.status,
|
|
744
|
-
messageCount:
|
|
763
|
+
messageCount: messages.length,
|
|
745
764
|
createdAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
746
765
|
updatedAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
747
|
-
closedAt: null
|
|
766
|
+
closedAt: null,
|
|
767
|
+
messages
|
|
748
768
|
};
|
|
749
769
|
}
|
|
750
770
|
};
|
|
@@ -755,7 +775,7 @@ var ChatCommunityApi = class {
|
|
|
755
775
|
/** Create a new community post as the identified customer. */
|
|
756
776
|
async createPost(params) {
|
|
757
777
|
const teamId = this.chat.getTeamId();
|
|
758
|
-
if (!teamId) throw new
|
|
778
|
+
if (!teamId) throw new ChatClientAuthError("Not identified. Call chat.identify() first.");
|
|
759
779
|
const response = await this.chat.fetch("/api/widget/community", {
|
|
760
780
|
method: "POST",
|
|
761
781
|
body: {
|
|
@@ -772,7 +792,7 @@ var ChatCommunityApi = class {
|
|
|
772
792
|
/** Update a post. Only the original author can edit. */
|
|
773
793
|
async updatePost(postId, params) {
|
|
774
794
|
const teamId = this.chat.getTeamId();
|
|
775
|
-
if (!teamId) throw new
|
|
795
|
+
if (!teamId) throw new ChatClientAuthError("Not identified. Call chat.identify() first.");
|
|
776
796
|
const response = await this.chat.fetch("/api/widget/community", {
|
|
777
797
|
method: "POST",
|
|
778
798
|
body: {
|
|
@@ -789,7 +809,7 @@ var ChatCommunityApi = class {
|
|
|
789
809
|
/** Delete a post (soft delete). Only the original author can delete. */
|
|
790
810
|
async deletePost(postId) {
|
|
791
811
|
const teamId = this.chat.getTeamId();
|
|
792
|
-
if (!teamId) throw new
|
|
812
|
+
if (!teamId) throw new ChatClientAuthError("Not identified. Call chat.identify() first.");
|
|
793
813
|
await this.chat.fetch("/api/widget/community", {
|
|
794
814
|
method: "POST",
|
|
795
815
|
body: { teamId, action: "deletePost", postId }
|
|
@@ -798,7 +818,7 @@ var ChatCommunityApi = class {
|
|
|
798
818
|
/** Update a comment. Only the original author can edit. */
|
|
799
819
|
async updateComment(postId, commentId, params) {
|
|
800
820
|
const teamId = this.chat.getTeamId();
|
|
801
|
-
if (!teamId) throw new
|
|
821
|
+
if (!teamId) throw new ChatClientAuthError("Not identified. Call chat.identify() first.");
|
|
802
822
|
const response = await this.chat.fetch("/api/widget/community", {
|
|
803
823
|
method: "POST",
|
|
804
824
|
body: {
|
|
@@ -814,7 +834,7 @@ var ChatCommunityApi = class {
|
|
|
814
834
|
/** Delete a comment (soft delete). Only the original author can delete. */
|
|
815
835
|
async deleteComment(postId, commentId) {
|
|
816
836
|
const teamId = this.chat.getTeamId();
|
|
817
|
-
if (!teamId) throw new
|
|
837
|
+
if (!teamId) throw new ChatClientAuthError("Not identified. Call chat.identify() first.");
|
|
818
838
|
await this.chat.fetch("/api/widget/community", {
|
|
819
839
|
method: "POST",
|
|
820
840
|
body: { teamId, action: "deleteComment", postId, commentId }
|
|
@@ -823,7 +843,7 @@ var ChatCommunityApi = class {
|
|
|
823
843
|
/** Toggle vote on a post. Returns new vote state and count. */
|
|
824
844
|
async vote(postId) {
|
|
825
845
|
const teamId = this.chat.getTeamId();
|
|
826
|
-
if (!teamId) throw new
|
|
846
|
+
if (!teamId) throw new ChatClientAuthError("Not identified. Call chat.identify() first.");
|
|
827
847
|
const response = await this.chat.fetch("/api/widget/community", {
|
|
828
848
|
method: "POST",
|
|
829
849
|
body: { teamId, action: "vote", postId }
|
|
@@ -833,7 +853,7 @@ var ChatCommunityApi = class {
|
|
|
833
853
|
/** Add a comment to a post. Supports 1-level nesting via parentCommentId. */
|
|
834
854
|
async comment(postId, params) {
|
|
835
855
|
const teamId = this.chat.getTeamId();
|
|
836
|
-
if (!teamId) throw new
|
|
856
|
+
if (!teamId) throw new ChatClientAuthError("Not identified. Call chat.identify() first.");
|
|
837
857
|
const response = await this.chat.fetch("/api/widget/community", {
|
|
838
858
|
method: "POST",
|
|
839
859
|
body: {
|
|
@@ -849,7 +869,7 @@ var ChatCommunityApi = class {
|
|
|
849
869
|
/** Toggle follow on a post. */
|
|
850
870
|
async follow(postId) {
|
|
851
871
|
const teamId = this.chat.getTeamId();
|
|
852
|
-
if (!teamId) throw new
|
|
872
|
+
if (!teamId) throw new ChatClientAuthError("Not identified. Call chat.identify() first.");
|
|
853
873
|
const response = await this.chat.fetch("/api/widget/community", {
|
|
854
874
|
method: "POST",
|
|
855
875
|
body: { teamId, action: "follow", postId }
|
|
@@ -859,7 +879,7 @@ var ChatCommunityApi = class {
|
|
|
859
879
|
/** Check if the current user has voted on a post. */
|
|
860
880
|
async getVoteStatus(postId) {
|
|
861
881
|
const teamId = this.chat.getTeamId();
|
|
862
|
-
if (!teamId) throw new
|
|
882
|
+
if (!teamId) throw new ChatClientAuthError("Not identified. Call chat.identify() first.");
|
|
863
883
|
const response = await this.chat.fetch("/api/widget/community", {
|
|
864
884
|
method: "GET",
|
|
865
885
|
query: { teamId, postId }
|
|
@@ -870,7 +890,7 @@ var ChatCommunityApi = class {
|
|
|
870
890
|
async bulkVoteStatus(postIds) {
|
|
871
891
|
if (postIds.length === 0) return {};
|
|
872
892
|
const teamId = this.chat.getTeamId();
|
|
873
|
-
if (!teamId) throw new
|
|
893
|
+
if (!teamId) throw new ChatClientAuthError("Not identified. Call chat.identify() first.");
|
|
874
894
|
const response = await this.chat.fetch("/api/widget/community", {
|
|
875
895
|
method: "GET",
|
|
876
896
|
query: { teamId, postIds: postIds.join(",") }
|