@crosspost/sdk 0.1.4 → 0.1.6
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/index.cjs +60 -15
- package/dist/index.d.cts +2 -10
- package/dist/index.d.ts +2 -10
- package/dist/index.js +59 -15
- package/package.json +2 -2
- package/src/utils/error.ts +70 -14
package/dist/index.cjs
CHANGED
@@ -43,6 +43,7 @@ __export(index_exports, {
|
|
43
43
|
AuthStatusResponseSchema: () => AuthStatusResponseSchema,
|
44
44
|
AuthUrlResponseSchema: () => AuthUrlResponseSchema,
|
45
45
|
BaseError: () => BaseError,
|
46
|
+
CompositeApiError: () => CompositeApiError,
|
46
47
|
ConnectedAccountSchema: () => ConnectedAccountSchema,
|
47
48
|
ConnectedAccountsResponseSchema: () => ConnectedAccountsResponseSchema,
|
48
49
|
CreatePostRequestSchema: () => CreatePostRequestSchema,
|
@@ -4387,6 +4388,7 @@ var ApiErrorCode = /* @__PURE__ */ ((ApiErrorCode2) => {
|
|
4387
4388
|
ApiErrorCode2["POST_DELETION_FAILED"] = "POST_DELETION_FAILED";
|
4388
4389
|
ApiErrorCode2["POST_INTERACTION_FAILED"] = "POST_INTERACTION_FAILED";
|
4389
4390
|
ApiErrorCode2["NETWORK_ERROR"] = "NETWORK_ERROR";
|
4391
|
+
ApiErrorCode2["MULTI_STATUS"] = "MULTI_STATUS";
|
4390
4392
|
return ApiErrorCode2;
|
4391
4393
|
})(ApiErrorCode || {});
|
4392
4394
|
var ApiError = class _ApiError extends BaseError {
|
@@ -4483,6 +4485,18 @@ var PlatformError = class extends Error {
|
|
4483
4485
|
this.details = details;
|
4484
4486
|
}
|
4485
4487
|
};
|
4488
|
+
var CompositeApiError = class extends ApiError {
|
4489
|
+
constructor(message, errors, status = 207, details, recoverable = false) {
|
4490
|
+
super(
|
4491
|
+
message,
|
4492
|
+
"MULTI_STATUS",
|
4493
|
+
status,
|
4494
|
+
{ ...details, errors },
|
4495
|
+
recoverable
|
4496
|
+
);
|
4497
|
+
this.errors = errors;
|
4498
|
+
}
|
4499
|
+
};
|
4486
4500
|
var PlatformParamSchema = z.object({
|
4487
4501
|
platform: z.string().describe("Social media platform")
|
4488
4502
|
}).describe("Platform parameter");
|
@@ -4820,15 +4834,15 @@ var EndpointRateLimitResponseSchema = z.object({
|
|
4820
4834
|
signerId: z.string().describe("NEAR account ID")
|
4821
4835
|
}).describe("Endpoint rate limit response");
|
4822
4836
|
var TimePeriod = /* @__PURE__ */ ((TimePeriod2) => {
|
4823
|
-
TimePeriod2["
|
4824
|
-
TimePeriod2["YEARLY"] = "
|
4825
|
-
TimePeriod2["MONTHLY"] = "
|
4826
|
-
TimePeriod2["WEEKLY"] = "
|
4827
|
-
TimePeriod2["DAILY"] = "
|
4837
|
+
TimePeriod2["ALL"] = "all";
|
4838
|
+
TimePeriod2["YEARLY"] = "year";
|
4839
|
+
TimePeriod2["MONTHLY"] = "month";
|
4840
|
+
TimePeriod2["WEEKLY"] = "week";
|
4841
|
+
TimePeriod2["DAILY"] = "day";
|
4828
4842
|
return TimePeriod2;
|
4829
4843
|
})(TimePeriod || {});
|
4830
4844
|
var ActivityLeaderboardQuerySchema = z.object({
|
4831
|
-
timeframe: z.
|
4845
|
+
timeframe: z.nativeEnum(TimePeriod).optional().describe(
|
4832
4846
|
"Timeframe for the leaderboard"
|
4833
4847
|
),
|
4834
4848
|
limit: z.string().optional().transform((val) => val ? parseInt(val, 10) : void 0).pipe(z.number().min(1).max(100).optional()).describe("Maximum number of results to return (1-100)"),
|
@@ -4847,7 +4861,7 @@ var AccountActivityEntrySchema = z.object({
|
|
4847
4861
|
}).describe("Account activity entry");
|
4848
4862
|
var ActivityLeaderboardResponseSchema = EnhancedResponseSchema(
|
4849
4863
|
z.object({
|
4850
|
-
timeframe: z.
|
4864
|
+
timeframe: z.nativeEnum(TimePeriod).describe("Timeframe for the leaderboard"),
|
4851
4865
|
entries: z.array(AccountActivityEntrySchema).describe("Leaderboard entries"),
|
4852
4866
|
total: z.number().describe("Total number of entries in the leaderboard"),
|
4853
4867
|
limit: z.number().describe("Maximum number of results returned"),
|
@@ -4859,7 +4873,7 @@ var AccountActivityParamsSchema = z.object({
|
|
4859
4873
|
signerId: z.string().describe("NEAR account ID")
|
4860
4874
|
}).describe("Account activity params");
|
4861
4875
|
var AccountActivityQuerySchema = z.object({
|
4862
|
-
timeframe: z.
|
4876
|
+
timeframe: z.nativeEnum(TimePeriod).optional().describe(
|
4863
4877
|
"Timeframe for the activity"
|
4864
4878
|
)
|
4865
4879
|
}).describe("Account activity query");
|
@@ -4876,7 +4890,7 @@ var PlatformActivitySchema = z.object({
|
|
4876
4890
|
var AccountActivityResponseSchema = EnhancedResponseSchema(
|
4877
4891
|
z.object({
|
4878
4892
|
signerId: z.string().describe("NEAR account ID"),
|
4879
|
-
timeframe: z.
|
4893
|
+
timeframe: z.nativeEnum(TimePeriod).describe("Timeframe for the activity"),
|
4880
4894
|
totalPosts: z.number().describe("Total number of posts across all platforms"),
|
4881
4895
|
totalLikes: z.number().describe("Total number of likes across all platforms"),
|
4882
4896
|
totalReposts: z.number().describe("Total number of reposts across all platforms"),
|
@@ -5113,6 +5127,38 @@ async function apiWrapper(apiCall, context) {
|
|
5113
5127
|
}
|
5114
5128
|
}
|
5115
5129
|
function handleErrorResponse(data, status) {
|
5130
|
+
if (data?.errors && Array.isArray(data.errors)) {
|
5131
|
+
if (data.errors.length === 1) {
|
5132
|
+
const errorDetail = data.errors[0];
|
5133
|
+
if (errorDetail.platform && Object.values(Platform).includes(errorDetail.platform)) {
|
5134
|
+
return new PlatformError(
|
5135
|
+
errorDetail.message,
|
5136
|
+
errorDetail.platform,
|
5137
|
+
errorDetail.code,
|
5138
|
+
errorDetail.recoverable ?? false,
|
5139
|
+
void 0,
|
5140
|
+
status,
|
5141
|
+
errorDetail.userId,
|
5142
|
+
errorDetail.details
|
5143
|
+
);
|
5144
|
+
} else {
|
5145
|
+
return new ApiError(
|
5146
|
+
errorDetail.message,
|
5147
|
+
errorDetail.code,
|
5148
|
+
status,
|
5149
|
+
errorDetail.details,
|
5150
|
+
errorDetail.recoverable ?? false
|
5151
|
+
);
|
5152
|
+
}
|
5153
|
+
} else if (data.errors.length > 1) {
|
5154
|
+
return new CompositeApiError(
|
5155
|
+
"Multiple errors occurred",
|
5156
|
+
data.errors,
|
5157
|
+
status,
|
5158
|
+
{ originalResponse: data }
|
5159
|
+
);
|
5160
|
+
}
|
5161
|
+
}
|
5116
5162
|
const errorData = data?.error || {};
|
5117
5163
|
const message = errorData?.message || data?.message || "An API error occurred";
|
5118
5164
|
const codeString = errorData?.code || data?.code || ApiErrorCode.UNKNOWN_ERROR;
|
@@ -5129,19 +5175,17 @@ function handleErrorResponse(data, status) {
|
|
5129
5175
|
message,
|
5130
5176
|
platform,
|
5131
5177
|
code,
|
5132
|
-
|
5178
|
+
recoverable,
|
5179
|
+
void 0,
|
5133
5180
|
status,
|
5134
|
-
|
5135
|
-
enhancedDetails
|
5136
|
-
recoverable
|
5181
|
+
void 0,
|
5182
|
+
enhancedDetails
|
5137
5183
|
);
|
5138
5184
|
} else {
|
5139
5185
|
return new ApiError(
|
5140
5186
|
message,
|
5141
5187
|
code,
|
5142
|
-
// Use the parsed code
|
5143
5188
|
status,
|
5144
|
-
// Cast status
|
5145
5189
|
enhancedDetails,
|
5146
5190
|
recoverable
|
5147
5191
|
);
|
@@ -5650,6 +5694,7 @@ var CrosspostClient = class {
|
|
5650
5694
|
AuthStatusResponseSchema,
|
5651
5695
|
AuthUrlResponseSchema,
|
5652
5696
|
BaseError,
|
5697
|
+
CompositeApiError,
|
5653
5698
|
ConnectedAccountSchema,
|
5654
5699
|
ConnectedAccountsResponseSchema,
|
5655
5700
|
CreatePostRequestSchema,
|
package/dist/index.d.cts
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
import { NearAuthData } from 'near-sign-verify';
|
2
|
-
import { ActivityLeaderboardQuery, ActivityLeaderboardResponse, AccountActivityQuery, AccountActivityResponse, AccountPostsQuery, AccountPostsResponse, NearAuthorizationResponse, Platform, EnhancedApiResponse, AuthStatusResponse, AuthRevokeResponse, ConnectedAccountsResponse, CreatePostRequest, CreatePostResponse, RepostRequest, RepostResponse, QuotePostRequest, QuotePostResponse, ReplyToPostRequest, ReplyToPostResponse, LikePostRequest, LikePostResponse, UnlikePostRequest, UnlikePostResponse, DeletePostRequest, DeletePostResponse, RateLimitResponse, EndpointRateLimitResponse, ApiError, ApiErrorCode, PlatformError } from '@crosspost/types';
|
2
|
+
import { ActivityLeaderboardQuery, ActivityLeaderboardResponse, AccountActivityQuery, AccountActivityResponse, AccountPostsQuery, AccountPostsResponse, NearAuthorizationResponse, Platform, EnhancedApiResponse, AuthStatusResponse, AuthRevokeResponse, ConnectedAccountsResponse, CreatePostRequest, CreatePostResponse, RepostRequest, RepostResponse, QuotePostRequest, QuotePostResponse, ReplyToPostRequest, ReplyToPostResponse, LikePostRequest, LikePostResponse, UnlikePostRequest, UnlikePostResponse, DeletePostRequest, DeletePostResponse, RateLimitResponse, EndpointRateLimitResponse, ApiError, ApiErrorCode, PlatformError, CompositeApiError } from '@crosspost/types';
|
3
3
|
export * from '@crosspost/types';
|
4
4
|
|
5
5
|
/**
|
@@ -378,15 +378,7 @@ declare function apiWrapper<T>(apiCall: () => Promise<T>, context?: Record<strin
|
|
378
378
|
* @param status The HTTP status code
|
379
379
|
* @returns An ApiError or PlatformError instance
|
380
380
|
*/
|
381
|
-
declare function handleErrorResponse(data: any, status: number): ApiError | PlatformError;
|
382
|
-
/**
|
383
|
-
* Creates a network error with appropriate details
|
384
|
-
*
|
385
|
-
* @param error The original error
|
386
|
-
* @param url The request URL
|
387
|
-
* @param timeout The request timeout
|
388
|
-
* @returns An ApiError instance
|
389
|
-
*/
|
381
|
+
declare function handleErrorResponse(data: any, status: number): ApiError | PlatformError | CompositeApiError;
|
390
382
|
declare function createNetworkError(error: unknown, url: string, timeout: number): ApiError;
|
391
383
|
|
392
384
|
export { ActivityApi, AuthApi, CrosspostClient, type CrosspostClientConfig, ERROR_CATEGORIES, PostApi, SystemApi, apiWrapper, createNetworkError, enrichErrorWithContext, getErrorDetails, getErrorMessage, handleErrorResponse, isAuthError, isContentError, isErrorOfCategory, isMediaError, isNetworkError, isPlatformError, isPostError, isRateLimitError, isRecoverableError, isValidationError };
|
package/dist/index.d.ts
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
import { NearAuthData } from 'near-sign-verify';
|
2
|
-
import { ActivityLeaderboardQuery, ActivityLeaderboardResponse, AccountActivityQuery, AccountActivityResponse, AccountPostsQuery, AccountPostsResponse, NearAuthorizationResponse, Platform, EnhancedApiResponse, AuthStatusResponse, AuthRevokeResponse, ConnectedAccountsResponse, CreatePostRequest, CreatePostResponse, RepostRequest, RepostResponse, QuotePostRequest, QuotePostResponse, ReplyToPostRequest, ReplyToPostResponse, LikePostRequest, LikePostResponse, UnlikePostRequest, UnlikePostResponse, DeletePostRequest, DeletePostResponse, RateLimitResponse, EndpointRateLimitResponse, ApiError, ApiErrorCode, PlatformError } from '@crosspost/types';
|
2
|
+
import { ActivityLeaderboardQuery, ActivityLeaderboardResponse, AccountActivityQuery, AccountActivityResponse, AccountPostsQuery, AccountPostsResponse, NearAuthorizationResponse, Platform, EnhancedApiResponse, AuthStatusResponse, AuthRevokeResponse, ConnectedAccountsResponse, CreatePostRequest, CreatePostResponse, RepostRequest, RepostResponse, QuotePostRequest, QuotePostResponse, ReplyToPostRequest, ReplyToPostResponse, LikePostRequest, LikePostResponse, UnlikePostRequest, UnlikePostResponse, DeletePostRequest, DeletePostResponse, RateLimitResponse, EndpointRateLimitResponse, ApiError, ApiErrorCode, PlatformError, CompositeApiError } from '@crosspost/types';
|
3
3
|
export * from '@crosspost/types';
|
4
4
|
|
5
5
|
/**
|
@@ -378,15 +378,7 @@ declare function apiWrapper<T>(apiCall: () => Promise<T>, context?: Record<strin
|
|
378
378
|
* @param status The HTTP status code
|
379
379
|
* @returns An ApiError or PlatformError instance
|
380
380
|
*/
|
381
|
-
declare function handleErrorResponse(data: any, status: number): ApiError | PlatformError;
|
382
|
-
/**
|
383
|
-
* Creates a network error with appropriate details
|
384
|
-
*
|
385
|
-
* @param error The original error
|
386
|
-
* @param url The request URL
|
387
|
-
* @param timeout The request timeout
|
388
|
-
* @returns An ApiError instance
|
389
|
-
*/
|
381
|
+
declare function handleErrorResponse(data: any, status: number): ApiError | PlatformError | CompositeApiError;
|
390
382
|
declare function createNetworkError(error: unknown, url: string, timeout: number): ApiError;
|
391
383
|
|
392
384
|
export { ActivityApi, AuthApi, CrosspostClient, type CrosspostClientConfig, ERROR_CATEGORIES, PostApi, SystemApi, apiWrapper, createNetworkError, enrichErrorWithContext, getErrorDetails, getErrorMessage, handleErrorResponse, isAuthError, isContentError, isErrorOfCategory, isMediaError, isNetworkError, isPlatformError, isPostError, isRateLimitError, isRecoverableError, isValidationError };
|
package/dist/index.js
CHANGED
@@ -4250,6 +4250,7 @@ var ApiErrorCode = /* @__PURE__ */ ((ApiErrorCode2) => {
|
|
4250
4250
|
ApiErrorCode2["POST_DELETION_FAILED"] = "POST_DELETION_FAILED";
|
4251
4251
|
ApiErrorCode2["POST_INTERACTION_FAILED"] = "POST_INTERACTION_FAILED";
|
4252
4252
|
ApiErrorCode2["NETWORK_ERROR"] = "NETWORK_ERROR";
|
4253
|
+
ApiErrorCode2["MULTI_STATUS"] = "MULTI_STATUS";
|
4253
4254
|
return ApiErrorCode2;
|
4254
4255
|
})(ApiErrorCode || {});
|
4255
4256
|
var ApiError = class _ApiError extends BaseError {
|
@@ -4346,6 +4347,18 @@ var PlatformError = class extends Error {
|
|
4346
4347
|
this.details = details;
|
4347
4348
|
}
|
4348
4349
|
};
|
4350
|
+
var CompositeApiError = class extends ApiError {
|
4351
|
+
constructor(message, errors, status = 207, details, recoverable = false) {
|
4352
|
+
super(
|
4353
|
+
message,
|
4354
|
+
"MULTI_STATUS",
|
4355
|
+
status,
|
4356
|
+
{ ...details, errors },
|
4357
|
+
recoverable
|
4358
|
+
);
|
4359
|
+
this.errors = errors;
|
4360
|
+
}
|
4361
|
+
};
|
4349
4362
|
var PlatformParamSchema = z.object({
|
4350
4363
|
platform: z.string().describe("Social media platform")
|
4351
4364
|
}).describe("Platform parameter");
|
@@ -4683,15 +4696,15 @@ var EndpointRateLimitResponseSchema = z.object({
|
|
4683
4696
|
signerId: z.string().describe("NEAR account ID")
|
4684
4697
|
}).describe("Endpoint rate limit response");
|
4685
4698
|
var TimePeriod = /* @__PURE__ */ ((TimePeriod2) => {
|
4686
|
-
TimePeriod2["
|
4687
|
-
TimePeriod2["YEARLY"] = "
|
4688
|
-
TimePeriod2["MONTHLY"] = "
|
4689
|
-
TimePeriod2["WEEKLY"] = "
|
4690
|
-
TimePeriod2["DAILY"] = "
|
4699
|
+
TimePeriod2["ALL"] = "all";
|
4700
|
+
TimePeriod2["YEARLY"] = "year";
|
4701
|
+
TimePeriod2["MONTHLY"] = "month";
|
4702
|
+
TimePeriod2["WEEKLY"] = "week";
|
4703
|
+
TimePeriod2["DAILY"] = "day";
|
4691
4704
|
return TimePeriod2;
|
4692
4705
|
})(TimePeriod || {});
|
4693
4706
|
var ActivityLeaderboardQuerySchema = z.object({
|
4694
|
-
timeframe: z.
|
4707
|
+
timeframe: z.nativeEnum(TimePeriod).optional().describe(
|
4695
4708
|
"Timeframe for the leaderboard"
|
4696
4709
|
),
|
4697
4710
|
limit: z.string().optional().transform((val) => val ? parseInt(val, 10) : void 0).pipe(z.number().min(1).max(100).optional()).describe("Maximum number of results to return (1-100)"),
|
@@ -4710,7 +4723,7 @@ var AccountActivityEntrySchema = z.object({
|
|
4710
4723
|
}).describe("Account activity entry");
|
4711
4724
|
var ActivityLeaderboardResponseSchema = EnhancedResponseSchema(
|
4712
4725
|
z.object({
|
4713
|
-
timeframe: z.
|
4726
|
+
timeframe: z.nativeEnum(TimePeriod).describe("Timeframe for the leaderboard"),
|
4714
4727
|
entries: z.array(AccountActivityEntrySchema).describe("Leaderboard entries"),
|
4715
4728
|
total: z.number().describe("Total number of entries in the leaderboard"),
|
4716
4729
|
limit: z.number().describe("Maximum number of results returned"),
|
@@ -4722,7 +4735,7 @@ var AccountActivityParamsSchema = z.object({
|
|
4722
4735
|
signerId: z.string().describe("NEAR account ID")
|
4723
4736
|
}).describe("Account activity params");
|
4724
4737
|
var AccountActivityQuerySchema = z.object({
|
4725
|
-
timeframe: z.
|
4738
|
+
timeframe: z.nativeEnum(TimePeriod).optional().describe(
|
4726
4739
|
"Timeframe for the activity"
|
4727
4740
|
)
|
4728
4741
|
}).describe("Account activity query");
|
@@ -4739,7 +4752,7 @@ var PlatformActivitySchema = z.object({
|
|
4739
4752
|
var AccountActivityResponseSchema = EnhancedResponseSchema(
|
4740
4753
|
z.object({
|
4741
4754
|
signerId: z.string().describe("NEAR account ID"),
|
4742
|
-
timeframe: z.
|
4755
|
+
timeframe: z.nativeEnum(TimePeriod).describe("Timeframe for the activity"),
|
4743
4756
|
totalPosts: z.number().describe("Total number of posts across all platforms"),
|
4744
4757
|
totalLikes: z.number().describe("Total number of likes across all platforms"),
|
4745
4758
|
totalReposts: z.number().describe("Total number of reposts across all platforms"),
|
@@ -4976,6 +4989,38 @@ async function apiWrapper(apiCall, context) {
|
|
4976
4989
|
}
|
4977
4990
|
}
|
4978
4991
|
function handleErrorResponse(data, status) {
|
4992
|
+
if (data?.errors && Array.isArray(data.errors)) {
|
4993
|
+
if (data.errors.length === 1) {
|
4994
|
+
const errorDetail = data.errors[0];
|
4995
|
+
if (errorDetail.platform && Object.values(Platform).includes(errorDetail.platform)) {
|
4996
|
+
return new PlatformError(
|
4997
|
+
errorDetail.message,
|
4998
|
+
errorDetail.platform,
|
4999
|
+
errorDetail.code,
|
5000
|
+
errorDetail.recoverable ?? false,
|
5001
|
+
void 0,
|
5002
|
+
status,
|
5003
|
+
errorDetail.userId,
|
5004
|
+
errorDetail.details
|
5005
|
+
);
|
5006
|
+
} else {
|
5007
|
+
return new ApiError(
|
5008
|
+
errorDetail.message,
|
5009
|
+
errorDetail.code,
|
5010
|
+
status,
|
5011
|
+
errorDetail.details,
|
5012
|
+
errorDetail.recoverable ?? false
|
5013
|
+
);
|
5014
|
+
}
|
5015
|
+
} else if (data.errors.length > 1) {
|
5016
|
+
return new CompositeApiError(
|
5017
|
+
"Multiple errors occurred",
|
5018
|
+
data.errors,
|
5019
|
+
status,
|
5020
|
+
{ originalResponse: data }
|
5021
|
+
);
|
5022
|
+
}
|
5023
|
+
}
|
4979
5024
|
const errorData = data?.error || {};
|
4980
5025
|
const message = errorData?.message || data?.message || "An API error occurred";
|
4981
5026
|
const codeString = errorData?.code || data?.code || ApiErrorCode.UNKNOWN_ERROR;
|
@@ -4992,19 +5037,17 @@ function handleErrorResponse(data, status) {
|
|
4992
5037
|
message,
|
4993
5038
|
platform,
|
4994
5039
|
code,
|
4995
|
-
|
5040
|
+
recoverable,
|
5041
|
+
void 0,
|
4996
5042
|
status,
|
4997
|
-
|
4998
|
-
enhancedDetails
|
4999
|
-
recoverable
|
5043
|
+
void 0,
|
5044
|
+
enhancedDetails
|
5000
5045
|
);
|
5001
5046
|
} else {
|
5002
5047
|
return new ApiError(
|
5003
5048
|
message,
|
5004
5049
|
code,
|
5005
|
-
// Use the parsed code
|
5006
5050
|
status,
|
5007
|
-
// Cast status
|
5008
5051
|
enhancedDetails,
|
5009
5052
|
recoverable
|
5010
5053
|
);
|
@@ -5512,6 +5555,7 @@ export {
|
|
5512
5555
|
AuthStatusResponseSchema,
|
5513
5556
|
AuthUrlResponseSchema,
|
5514
5557
|
BaseError,
|
5558
|
+
CompositeApiError,
|
5515
5559
|
ConnectedAccountSchema,
|
5516
5560
|
ConnectedAccountsResponseSchema,
|
5517
5561
|
CreatePostRequestSchema,
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@crosspost/sdk",
|
3
|
-
"version": "0.1.
|
3
|
+
"version": "0.1.6",
|
4
4
|
"description": "SDK for interacting with the Crosspost API",
|
5
5
|
"type": "module",
|
6
6
|
"main": "dist/index.cjs",
|
@@ -36,7 +36,7 @@
|
|
36
36
|
"author": "crosspost.near",
|
37
37
|
"license": "MIT",
|
38
38
|
"dependencies": {
|
39
|
-
"near-sign-verify": "^0.1.
|
39
|
+
"near-sign-verify": "^0.1.3"
|
40
40
|
},
|
41
41
|
"devDependencies": {
|
42
42
|
"@types/node": "^20.10.5",
|
package/src/utils/error.ts
CHANGED
@@ -1,4 +1,12 @@
|
|
1
|
-
import {
|
1
|
+
import {
|
2
|
+
ApiError,
|
3
|
+
ApiErrorCode,
|
4
|
+
CompositeApiError,
|
5
|
+
type ErrorDetail,
|
6
|
+
Platform,
|
7
|
+
PlatformError,
|
8
|
+
} from '@crosspost/types';
|
9
|
+
import type { StatusCode } from 'hono/utils/http-status';
|
2
10
|
|
3
11
|
/**
|
4
12
|
* Error categories grouped by type
|
@@ -307,17 +315,55 @@ export async function apiWrapper<T>(
|
|
307
315
|
* @param status The HTTP status code
|
308
316
|
* @returns An ApiError or PlatformError instance
|
309
317
|
*/
|
310
|
-
export function handleErrorResponse(
|
311
|
-
|
318
|
+
export function handleErrorResponse(
|
319
|
+
data: any,
|
320
|
+
status: number,
|
321
|
+
): ApiError | PlatformError | CompositeApiError {
|
322
|
+
// Check for enhanced error response format with multiple errors
|
323
|
+
if (data?.errors && Array.isArray(data.errors)) {
|
324
|
+
if (data.errors.length === 1) {
|
325
|
+
// Single error case - convert to standard error
|
326
|
+
const errorDetail = data.errors[0];
|
327
|
+
if (
|
328
|
+
errorDetail.platform && Object.values(Platform).includes(errorDetail.platform as Platform)
|
329
|
+
) {
|
330
|
+
return new PlatformError(
|
331
|
+
errorDetail.message,
|
332
|
+
errorDetail.platform as Platform,
|
333
|
+
errorDetail.code,
|
334
|
+
errorDetail.recoverable ?? false,
|
335
|
+
undefined,
|
336
|
+
status as StatusCode,
|
337
|
+
errorDetail.userId,
|
338
|
+
errorDetail.details,
|
339
|
+
);
|
340
|
+
} else {
|
341
|
+
return new ApiError(
|
342
|
+
errorDetail.message,
|
343
|
+
errorDetail.code,
|
344
|
+
status as StatusCode,
|
345
|
+
errorDetail.details,
|
346
|
+
errorDetail.recoverable ?? false,
|
347
|
+
);
|
348
|
+
}
|
349
|
+
} else if (data.errors.length > 1) {
|
350
|
+
// Multiple errors case - return composite error
|
351
|
+
return new CompositeApiError(
|
352
|
+
'Multiple errors occurred',
|
353
|
+
data.errors as ErrorDetail[],
|
354
|
+
status as StatusCode,
|
355
|
+
{ originalResponse: data },
|
356
|
+
);
|
357
|
+
}
|
358
|
+
}
|
359
|
+
|
360
|
+
// Fall back to legacy error format handling
|
312
361
|
const errorData = data?.error || {};
|
313
362
|
const message = errorData?.message || data?.message || 'An API error occurred';
|
314
|
-
|
315
|
-
// Ensure code is a valid ApiErrorCode or default
|
316
363
|
const codeString = errorData?.code || data?.code || ApiErrorCode.UNKNOWN_ERROR;
|
317
364
|
const code = Object.values(ApiErrorCode).includes(codeString as ApiErrorCode)
|
318
365
|
? codeString as ApiErrorCode
|
319
366
|
: ApiErrorCode.UNKNOWN_ERROR;
|
320
|
-
|
321
367
|
const details = errorData?.details || data?.details || {};
|
322
368
|
const recoverable = errorData?.recoverable ?? data?.recoverable ?? false;
|
323
369
|
const platform = errorData?.platform || data?.platform;
|
@@ -325,25 +371,25 @@ export function handleErrorResponse(data: any, status: number): ApiError | Platf
|
|
325
371
|
// Add original response data to details if not already present
|
326
372
|
const enhancedDetails = { ...details };
|
327
373
|
if (typeof enhancedDetails === 'object' && !enhancedDetails.originalResponse) {
|
328
|
-
enhancedDetails.originalResponse = data;
|
374
|
+
enhancedDetails.originalResponse = data;
|
329
375
|
}
|
330
376
|
|
331
377
|
if (platform && Object.values(Platform).includes(platform as Platform)) {
|
332
|
-
// If platform is specified and valid, it's a PlatformError
|
333
378
|
return new PlatformError(
|
334
379
|
message,
|
335
380
|
platform as Platform,
|
336
|
-
code,
|
337
|
-
status as any, // Cast status
|
338
|
-
enhancedDetails,
|
381
|
+
code,
|
339
382
|
recoverable,
|
383
|
+
undefined,
|
384
|
+
status as StatusCode,
|
385
|
+
undefined,
|
386
|
+
enhancedDetails,
|
340
387
|
);
|
341
388
|
} else {
|
342
|
-
// Otherwise, it's a general ApiError
|
343
389
|
return new ApiError(
|
344
390
|
message,
|
345
|
-
code,
|
346
|
-
status as
|
391
|
+
code,
|
392
|
+
status as StatusCode,
|
347
393
|
enhancedDetails,
|
348
394
|
recoverable,
|
349
395
|
);
|
@@ -358,6 +404,16 @@ export function handleErrorResponse(data: any, status: number): ApiError | Platf
|
|
358
404
|
* @param timeout The request timeout
|
359
405
|
* @returns An ApiError instance
|
360
406
|
*/
|
407
|
+
/**
|
408
|
+
* Check if an error is a composite error containing multiple error details
|
409
|
+
*
|
410
|
+
* @param error The error to check
|
411
|
+
* @returns True if the error is a composite error, false otherwise
|
412
|
+
*/
|
413
|
+
export function isCompositeApiError(error: unknown): error is CompositeApiError {
|
414
|
+
return error instanceof CompositeApiError;
|
415
|
+
}
|
416
|
+
|
361
417
|
export function createNetworkError(error: unknown, url: string, timeout: number): ApiError {
|
362
418
|
if (error instanceof DOMException && error.name === 'AbortError') {
|
363
419
|
return new ApiError(
|