@crosspost/sdk 0.1.5 → 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 +51 -6
- package/dist/index.d.cts +2 -10
- package/dist/index.d.ts +2 -10
- package/dist/index.js +50 -6
- package/package.json +1 -1
- 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");
|
@@ -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");
|
@@ -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
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(
|