@crosspost/types
Version:
Shared type definitions for Crosspost API
108 lines (89 loc) • 3.28 kB
text/typescript
import { z } from 'zod';
import type { ResponseMeta } from './response.ts';
import type { PlatformName } from './common.ts';
import type { StatusCode } from './index.ts';
export enum ApiErrorCode {
// General errors
UNKNOWN_ERROR = 'UNKNOWN_ERROR',
INTERNAL_ERROR = 'INTERNAL_ERROR',
// Authentication/Authorization errors
UNAUTHORIZED = 'UNAUTHORIZED',
FORBIDDEN = 'FORBIDDEN',
// Validation errors
VALIDATION_ERROR = 'VALIDATION_ERROR',
INVALID_REQUEST = 'INVALID_REQUEST',
// Rate limiting
RATE_LIMITED = 'RATE_LIMITED',
// Resource errors
NOT_FOUND = 'NOT_FOUND',
// Platform-specific errors
PLATFORM_ERROR = 'PLATFORM_ERROR',
PLATFORM_UNAVAILABLE = 'PLATFORM_UNAVAILABLE',
// Content errors
CONTENT_POLICY_VIOLATION = 'CONTENT_POLICY_VIOLATION',
DUPLICATE_CONTENT = 'DUPLICATE_CONTENT',
// Media errors
MEDIA_UPLOAD_FAILED = 'MEDIA_UPLOAD_FAILED',
// Post errors
MULTI_STATUS = 'MULTI_STATUS',
POST_CREATION_FAILED = 'POST_CREATION_FAILED',
THREAD_CREATION_FAILED = 'THREAD_CREATION_FAILED',
POST_DELETION_FAILED = 'POST_DELETION_FAILED',
POST_INTERACTION_FAILED = 'POST_INTERACTION_FAILED',
// Network errors
NETWORK_ERROR = 'NETWORK_ERROR',
// Response format errors
INVALID_RESPONSE = 'INVALID_RESPONSE',
// Refresh errors
TOKEN_REFRESH_FAILED = 'TOKEN_REFRESH_FAILED',
PROFILE_REFRESH_FAILED = 'PROFILE_REFRESH_FAILED',
}
export const ApiErrorCodeSchema = z.enum(Object.values(ApiErrorCode) as [string, ...string[]]);
/**
* Map of API error codes to HTTP status codes
* This ensures consistent HTTP status codes across the application
*/
export const errorCodeToStatusCode: Record<ApiErrorCode, StatusCode> = {
[]: 207,
[]: 500,
[]: 500,
[]: 400,
[]: 400,
[]: 404,
[]: 401,
[]: 403,
[]: 429,
[]: 502,
[]: 503,
[]: 400,
[]: 400,
[]: 400,
[]: 500,
[]: 500,
[]: 500,
[]: 500,
[]: 503,
[]: 500,
[]: 500,
[]: 500,
};
/**
* Common error details that can be included in any error
*/
export interface ErrorDetails {
platform?: PlatformName;
userId?: string;
[]: unknown;
}
export const ErrorDetailSchema = z.object({
message: z.string().describe('Human-readable error message'),
code: ApiErrorCodeSchema.describe('Machine-readable error code'),
recoverable: z.boolean().describe('Whether the error can be recovered from'),
details: z.record(z.unknown()).optional().describe('Additional error details'),
});
export type ErrorDetail = z.infer<typeof ErrorDetailSchema>;
export interface ApiErrorResponse {
success: false;
errors: ErrorDetail[];
meta: ResponseMeta;
}