@crosspost/types 0.1.2
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 +125 -0
- package/dist/index.cjs +964 -0
- package/dist/index.d.cts +7421 -0
- package/dist/index.d.ts +7421 -0
- package/dist/index.js +852 -0
- package/mod.ts +9 -0
- package/package.json +51 -0
- package/src/auth.ts +121 -0
- package/src/common.ts +33 -0
- package/src/errors/api-error.ts +153 -0
- package/src/errors/base-error.ts +13 -0
- package/src/errors/index.ts +7 -0
- package/src/errors/platform-error.ts +34 -0
- package/src/index.ts +20 -0
- package/src/leaderboard.ts +179 -0
- package/src/post.ts +347 -0
- package/src/rate-limit.ts +145 -0
- package/src/response.ts +297 -0
- package/src/user-profile.ts +43 -0
package/src/response.ts
ADDED
@@ -0,0 +1,297 @@
|
|
1
|
+
/**
|
2
|
+
* Enhanced Response Schemas and Types
|
3
|
+
* Defines schemas and types for the enhanced API response format
|
4
|
+
*/
|
5
|
+
|
6
|
+
import { z } from 'zod';
|
7
|
+
import type { PlatformName } from './common.ts';
|
8
|
+
|
9
|
+
/**
|
10
|
+
* Standard API response schema
|
11
|
+
*/
|
12
|
+
export const ApiResponseSchema = z.object({
|
13
|
+
data: z.any().describe('Response data'),
|
14
|
+
meta: z.object({
|
15
|
+
rateLimit: z.object({
|
16
|
+
remaining: z.number().describe('Number of requests remaining in the current window'),
|
17
|
+
limit: z.number().describe('Total number of requests allowed in the window'),
|
18
|
+
reset: z.number().describe('Timestamp when the rate limit resets (in seconds since epoch)'),
|
19
|
+
}).optional().describe('Rate limit information'),
|
20
|
+
pagination: z.object({
|
21
|
+
page: z.number().describe('Current page number'),
|
22
|
+
perPage: z.number().describe('Number of items per page'),
|
23
|
+
total: z.number().describe('Total number of items'),
|
24
|
+
totalPages: z.number().describe('Total number of pages'),
|
25
|
+
nextCursor: z.string().optional().describe('Next page cursor (if applicable)'),
|
26
|
+
prevCursor: z.string().optional().describe('Previous page cursor (if applicable)'),
|
27
|
+
}).optional().describe('Pagination information'),
|
28
|
+
}).optional().describe('Response metadata'),
|
29
|
+
}).describe('Standard API response');
|
30
|
+
|
31
|
+
/**
|
32
|
+
* Error response schema
|
33
|
+
*/
|
34
|
+
export const ErrorResponseSchema = z.object({
|
35
|
+
error: z.object({
|
36
|
+
type: z.string().describe('Error type'),
|
37
|
+
message: z.string().describe('Error message'),
|
38
|
+
code: z.string().optional().describe('Error code (if applicable)'),
|
39
|
+
details: z.any().optional().describe('Additional error details'),
|
40
|
+
}).describe('Error information'),
|
41
|
+
}).describe('Error response');
|
42
|
+
|
43
|
+
/**
|
44
|
+
* Schema for enhanced response metadata
|
45
|
+
*/
|
46
|
+
export const EnhancedResponseMetaSchema = z.object({
|
47
|
+
requestId: z.string().optional().describe('Unique request identifier'),
|
48
|
+
timestamp: z.string().optional().describe('Request timestamp'),
|
49
|
+
rateLimit: z.object({
|
50
|
+
remaining: z.number().describe('Number of requests remaining in the current window'),
|
51
|
+
limit: z.number().describe('Total number of requests allowed in the window'),
|
52
|
+
reset: z.number().describe('Timestamp when the rate limit resets (in seconds since epoch)'),
|
53
|
+
}).optional().describe('Rate limit information'),
|
54
|
+
pagination: z.object({
|
55
|
+
page: z.number().describe('Current page number'),
|
56
|
+
perPage: z.number().describe('Number of items per page'),
|
57
|
+
total: z.number().describe('Total number of items'),
|
58
|
+
totalPages: z.number().describe('Total number of pages'),
|
59
|
+
nextCursor: z.string().optional().describe('Next page cursor (if applicable)'),
|
60
|
+
prevCursor: z.string().optional().describe('Previous page cursor (if applicable)'),
|
61
|
+
}).optional().describe('Pagination information'),
|
62
|
+
}).optional().describe('Response metadata');
|
63
|
+
|
64
|
+
/**
|
65
|
+
* Schema for error details
|
66
|
+
*/
|
67
|
+
export const ErrorDetailSchema = z.object({
|
68
|
+
platform: z.string().optional().describe('Platform associated with the error (if applicable)'),
|
69
|
+
userId: z.string().optional().describe('User ID associated with the error (if applicable)'),
|
70
|
+
status: z.literal('error').describe('Error status'),
|
71
|
+
error: z.string().describe('Human-readable error message'),
|
72
|
+
errorCode: z.string().describe('Machine-readable error code'),
|
73
|
+
recoverable: z.boolean().describe('Whether the error is recoverable (can be retried)'),
|
74
|
+
details: z.record(z.any()).optional().describe('Additional error details (platform-specific)'),
|
75
|
+
}).describe('Error detail');
|
76
|
+
|
77
|
+
/**
|
78
|
+
* Schema for enhanced error response
|
79
|
+
*/
|
80
|
+
export const EnhancedErrorResponseSchema = z.object({
|
81
|
+
success: z.literal(false).describe('Success indicator (always false for error responses)'),
|
82
|
+
errors: z.array(ErrorDetailSchema).describe('Error information'),
|
83
|
+
}).describe('Enhanced error response');
|
84
|
+
|
85
|
+
/**
|
86
|
+
* Schema for success details
|
87
|
+
*/
|
88
|
+
export const SuccessDetailSchema = z.object({
|
89
|
+
platform: z.string().describe('Platform associated with the success'),
|
90
|
+
userId: z.string().describe('User ID associated with the success'),
|
91
|
+
status: z.literal('success').describe('Success status'),
|
92
|
+
postId: z.string().optional().describe('Post ID (if applicable)'),
|
93
|
+
postUrl: z.string().optional().describe('Post URL (if applicable)'),
|
94
|
+
}).catchall(z.any()).describe('Success detail');
|
95
|
+
|
96
|
+
/**
|
97
|
+
* Schema for multi-status response
|
98
|
+
*/
|
99
|
+
export const MultiStatusResponseSchema = z.object({
|
100
|
+
success: z.boolean().describe('Success indicator (true if at least one operation succeeded)'),
|
101
|
+
data: z.object({
|
102
|
+
summary: z.object({
|
103
|
+
total: z.number().describe('Total number of operations'),
|
104
|
+
succeeded: z.number().describe('Number of successful operations'),
|
105
|
+
failed: z.number().describe('Number of failed operations'),
|
106
|
+
}).describe('Summary of operations'),
|
107
|
+
results: z.array(SuccessDetailSchema).describe('Successful results'),
|
108
|
+
errors: z.array(ErrorDetailSchema).describe('Failed results'),
|
109
|
+
}).describe('Response data'),
|
110
|
+
}).describe('Multi-status response');
|
111
|
+
|
112
|
+
/**
|
113
|
+
* Function to create an enhanced response schema
|
114
|
+
* @param schema The schema to wrap
|
115
|
+
* @returns A schema for an enhanced response
|
116
|
+
*/
|
117
|
+
export function EnhancedResponseSchema<T extends z.ZodTypeAny>(schema: T) {
|
118
|
+
return z.object({
|
119
|
+
success: z.boolean().describe('Whether the request was successful'),
|
120
|
+
data: schema,
|
121
|
+
meta: EnhancedResponseMetaSchema,
|
122
|
+
});
|
123
|
+
}
|
124
|
+
|
125
|
+
// Derive TypeScript types from Zod schemas
|
126
|
+
export type EnhancedResponseMeta = z.infer<typeof EnhancedResponseMetaSchema>;
|
127
|
+
export type ErrorDetail = z.infer<typeof ErrorDetailSchema>;
|
128
|
+
export type EnhancedErrorResponse = z.infer<typeof EnhancedErrorResponseSchema>;
|
129
|
+
export type SuccessDetail = z.infer<typeof SuccessDetailSchema>;
|
130
|
+
export type MultiStatusResponse = z.infer<typeof MultiStatusResponseSchema>;
|
131
|
+
export type ApiResponse<T> = {
|
132
|
+
data: T;
|
133
|
+
meta?: {
|
134
|
+
rateLimit?: { remaining: number; limit: number; reset: number };
|
135
|
+
pagination?: {
|
136
|
+
page: number;
|
137
|
+
perPage: number;
|
138
|
+
total: number;
|
139
|
+
totalPages: number;
|
140
|
+
nextCursor?: string;
|
141
|
+
prevCursor?: string;
|
142
|
+
};
|
143
|
+
};
|
144
|
+
};
|
145
|
+
export type ErrorResponse = z.infer<typeof ErrorResponseSchema>;
|
146
|
+
|
147
|
+
/**
|
148
|
+
* Enhanced API response type
|
149
|
+
*/
|
150
|
+
export interface EnhancedApiResponse<T> {
|
151
|
+
success: boolean;
|
152
|
+
data: T;
|
153
|
+
meta?: EnhancedResponseMeta;
|
154
|
+
}
|
155
|
+
|
156
|
+
/**
|
157
|
+
* Helper function to create an enhanced API response
|
158
|
+
* @param data The response data
|
159
|
+
* @param meta Optional metadata
|
160
|
+
* @returns An enhanced API response
|
161
|
+
*/
|
162
|
+
export function createEnhancedApiResponse<T>(
|
163
|
+
data: T,
|
164
|
+
meta?: EnhancedResponseMeta,
|
165
|
+
): EnhancedApiResponse<T> {
|
166
|
+
return {
|
167
|
+
success: true,
|
168
|
+
data,
|
169
|
+
meta,
|
170
|
+
};
|
171
|
+
}
|
172
|
+
|
173
|
+
/**
|
174
|
+
* Create a standard API response
|
175
|
+
* @param data The response data
|
176
|
+
* @param meta The response metadata
|
177
|
+
* @returns A standard API response
|
178
|
+
*/
|
179
|
+
export function createApiResponse<T>(data: T, meta?: ApiResponse<T>['meta']): ApiResponse<T> {
|
180
|
+
return {
|
181
|
+
data,
|
182
|
+
meta,
|
183
|
+
};
|
184
|
+
}
|
185
|
+
|
186
|
+
/**
|
187
|
+
* Create an error response
|
188
|
+
* @param type The error type
|
189
|
+
* @param message The error message
|
190
|
+
* @param code The error code (if applicable)
|
191
|
+
* @param details Additional error details
|
192
|
+
* @returns An error response
|
193
|
+
*/
|
194
|
+
export function createErrorResponse(
|
195
|
+
type: string,
|
196
|
+
message: string,
|
197
|
+
code?: string,
|
198
|
+
details?: any,
|
199
|
+
): ErrorResponse {
|
200
|
+
return {
|
201
|
+
error: {
|
202
|
+
type,
|
203
|
+
message,
|
204
|
+
...(code ? { code } : {}),
|
205
|
+
...(details ? { details } : {}),
|
206
|
+
},
|
207
|
+
};
|
208
|
+
}
|
209
|
+
|
210
|
+
/**
|
211
|
+
* Helper function to create an enhanced error response
|
212
|
+
* @param errors Array of error details
|
213
|
+
* @returns An enhanced error response
|
214
|
+
*/
|
215
|
+
export function createEnhancedErrorResponse(errors: ErrorDetail[]): EnhancedErrorResponse {
|
216
|
+
return {
|
217
|
+
success: false,
|
218
|
+
errors,
|
219
|
+
};
|
220
|
+
}
|
221
|
+
|
222
|
+
/**
|
223
|
+
* Helper function to create an error detail
|
224
|
+
* @param error Error message
|
225
|
+
* @param errorCode Error code
|
226
|
+
* @param recoverable Whether the error is recoverable
|
227
|
+
* @param platform Optional platform
|
228
|
+
* @param userId Optional user ID
|
229
|
+
* @param details Optional additional details
|
230
|
+
* @returns An error detail
|
231
|
+
*/
|
232
|
+
export function createErrorDetail(
|
233
|
+
error: string,
|
234
|
+
errorCode: string,
|
235
|
+
recoverable: boolean,
|
236
|
+
platform?: PlatformName,
|
237
|
+
userId?: string,
|
238
|
+
details?: Record<string, any>,
|
239
|
+
): ErrorDetail {
|
240
|
+
return {
|
241
|
+
platform,
|
242
|
+
userId,
|
243
|
+
status: 'error',
|
244
|
+
error,
|
245
|
+
errorCode,
|
246
|
+
recoverable,
|
247
|
+
details,
|
248
|
+
};
|
249
|
+
}
|
250
|
+
|
251
|
+
/**
|
252
|
+
* Helper function to create a success detail
|
253
|
+
* @param platform Platform
|
254
|
+
* @param userId User ID
|
255
|
+
* @param additionalData Additional data
|
256
|
+
* @returns A success detail
|
257
|
+
*/
|
258
|
+
export function createSuccessDetail(
|
259
|
+
platform: PlatformName,
|
260
|
+
userId: string,
|
261
|
+
additionalData?: Record<string, any>,
|
262
|
+
): SuccessDetail {
|
263
|
+
return {
|
264
|
+
platform,
|
265
|
+
userId,
|
266
|
+
status: 'success',
|
267
|
+
...additionalData,
|
268
|
+
};
|
269
|
+
}
|
270
|
+
|
271
|
+
/**
|
272
|
+
* Helper function to create a multi-status response
|
273
|
+
* @param results Successful results
|
274
|
+
* @param errors Failed results
|
275
|
+
* @returns A multi-status response
|
276
|
+
*/
|
277
|
+
export function createMultiStatusResponse(
|
278
|
+
results: SuccessDetail[],
|
279
|
+
errors: ErrorDetail[],
|
280
|
+
): MultiStatusResponse {
|
281
|
+
const total = results.length + errors.length;
|
282
|
+
const succeeded = results.length;
|
283
|
+
const failed = errors.length;
|
284
|
+
|
285
|
+
return {
|
286
|
+
success: succeeded > 0,
|
287
|
+
data: {
|
288
|
+
summary: {
|
289
|
+
total,
|
290
|
+
succeeded,
|
291
|
+
failed,
|
292
|
+
},
|
293
|
+
results,
|
294
|
+
errors,
|
295
|
+
},
|
296
|
+
};
|
297
|
+
}
|
@@ -0,0 +1,43 @@
|
|
1
|
+
/**
|
2
|
+
* User Profile Schemas and Types
|
3
|
+
* Defines Zod schemas for user profile-related data
|
4
|
+
* TypeScript types are derived from Zod schemas for type safety
|
5
|
+
*/
|
6
|
+
|
7
|
+
import { z } from 'zod';
|
8
|
+
import { PlatformSchema } from './common.ts';
|
9
|
+
import { EnhancedResponseSchema } from './response.ts';
|
10
|
+
|
11
|
+
/**
|
12
|
+
* User profile schema
|
13
|
+
*/
|
14
|
+
export const UserProfileSchema = z.object({
|
15
|
+
userId: z.string().describe('User ID on the platform'),
|
16
|
+
username: z.string().describe('Username on the platform'),
|
17
|
+
url: z.string().url().optional().describe('URL to the user profile'),
|
18
|
+
profileImageUrl: z.string().describe('URL to the user profile image'),
|
19
|
+
isPremium: z.boolean().optional().describe('Whether the user has a premium account'),
|
20
|
+
platform: PlatformSchema.describe('The platform the user profile is from'),
|
21
|
+
lastUpdated: z.number().describe('Timestamp when the profile was last updated'),
|
22
|
+
}).describe('User profile');
|
23
|
+
|
24
|
+
/**
|
25
|
+
* Profile refresh result schema
|
26
|
+
*/
|
27
|
+
export const ProfileRefreshResultSchema = z.object({
|
28
|
+
success: z.boolean().describe('Whether the profile refresh was successful'),
|
29
|
+
profile: UserProfileSchema.optional().describe('The refreshed user profile (if successful)'),
|
30
|
+
error: z.string().optional().describe('Error message (if unsuccessful)'),
|
31
|
+
}).describe('Profile refresh result');
|
32
|
+
|
33
|
+
/**
|
34
|
+
* Profile refresh response schema
|
35
|
+
*/
|
36
|
+
export const ProfileRefreshResponseSchema = EnhancedResponseSchema(
|
37
|
+
ProfileRefreshResultSchema,
|
38
|
+
).describe('Profile refresh response');
|
39
|
+
|
40
|
+
// Derive TypeScript types from Zod schemas
|
41
|
+
export type UserProfile = z.infer<typeof UserProfileSchema>;
|
42
|
+
export type ProfileRefreshResult = z.infer<typeof ProfileRefreshResultSchema>;
|
43
|
+
export type ProfileRefreshResponse = z.infer<typeof ProfileRefreshResponseSchema>;
|