@crosspost/sdk 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.
@@ -0,0 +1,236 @@
1
+ import { NearAuthData } from 'near-sign-verify';
2
+ import { NearAuthorizationResponse, Platform, EnhancedApiResponse, AuthStatusResponse, AuthRevokeResponse, ConnectedAccountsResponse, CreatePostRequest, CreatePostResponse, RepostRequest, RepostResponse, QuotePostRequest, QuotePostResponse, ReplyToPostRequest, ReplyToPostResponse, LikePostRequest, LikePostResponse, UnlikePostRequest, UnlikePostResponse, DeletePostRequest, DeletePostResponse, ApiError, PlatformError } from '@crosspost/types';
3
+ export * from '@crosspost/types';
4
+
5
+ /**
6
+ * Options for making a request to the API
7
+ */
8
+ interface RequestOptions {
9
+ /**
10
+ * Base URL for the API
11
+ */
12
+ baseUrl: string;
13
+ /**
14
+ * NEAR authentication data for generating auth tokens
15
+ * Can be undefined if not authorize yet
16
+ */
17
+ signature?: NearAuthData;
18
+ /**
19
+ * Request timeout in milliseconds
20
+ */
21
+ timeout: number;
22
+ /**
23
+ * Number of retries for failed requests
24
+ */
25
+ retries: number;
26
+ }
27
+
28
+ /**
29
+ * Authentication-related API operations
30
+ */
31
+ declare class AuthApi {
32
+ private options;
33
+ /**
34
+ * Creates an instance of AuthApi
35
+ * @param options Request options
36
+ */
37
+ constructor(options: RequestOptions);
38
+ /**
39
+ * Authorizes the NEAR account associated with the provided nearAuthData with the Crosspost service.
40
+ * @returns A promise resolving with the authorization response.
41
+ */
42
+ authorizeNearAccount(): Promise<NearAuthorizationResponse>;
43
+ /**
44
+ * Checks the authorization status of the NEAR account with the Crosspost service.
45
+ * @returns A promise resolving with the authorization status response.
46
+ */
47
+ getNearAuthorizationStatus(): Promise<NearAuthorizationResponse>;
48
+ /**
49
+ * Initiates the login process for a specific platform.
50
+ * The service handles the OAuth flow; this method triggers it.
51
+ * @param platform The target platform.
52
+ * @param options Optional success and error redirect URLs.
53
+ * @returns A promise resolving with the response from the service (might indicate success/failure or redirect info).
54
+ */
55
+ loginToPlatform(platform: Platform, options?: {
56
+ successUrl?: string;
57
+ errorUrl?: string;
58
+ }): Promise<EnhancedApiResponse<any>>;
59
+ /**
60
+ * Refreshes the authentication token for the specified platform.
61
+ * @param platform The target platform.
62
+ * @returns A promise resolving with the refresh response.
63
+ */
64
+ refreshToken(platform: Platform): Promise<EnhancedApiResponse<any>>;
65
+ /**
66
+ * Refreshes the user's profile information from the specified platform.
67
+ * @param platform The target platform.
68
+ * @returns A promise resolving with the profile refresh response.
69
+ */
70
+ refreshProfile(platform: Platform): Promise<EnhancedApiResponse<any>>;
71
+ /**
72
+ * Gets the authentication status for the specified platform.
73
+ * @param platform The target platform.
74
+ * @returns A promise resolving with the authentication status response.
75
+ */
76
+ getAuthStatus(platform: Platform): Promise<AuthStatusResponse>;
77
+ /**
78
+ * Revokes the authentication token for the specified platform.
79
+ * @param platform The target platform.
80
+ * @returns A promise resolving with the revocation response.
81
+ */
82
+ revokeAuth(platform: Platform): Promise<AuthRevokeResponse>;
83
+ /**
84
+ * Lists all accounts connected to the NEAR account.
85
+ * @returns A promise resolving with the list of connected accounts.
86
+ */
87
+ getConnectedAccounts(): Promise<ConnectedAccountsResponse>;
88
+ }
89
+
90
+ /**
91
+ * Post-related API operations
92
+ */
93
+ declare class PostApi {
94
+ private options;
95
+ /**
96
+ * Creates an instance of PostApi
97
+ * @param options Request options
98
+ */
99
+ constructor(options: RequestOptions);
100
+ /**
101
+ * Creates a new post on the specified target platforms.
102
+ * @param request The post creation request details.
103
+ * @returns A promise resolving with the post creation response.
104
+ */
105
+ createPost(request: CreatePostRequest): Promise<CreatePostResponse>;
106
+ /**
107
+ * Reposts an existing post on the specified target platforms.
108
+ * @param request The repost request details.
109
+ * @returns A promise resolving with the repost response.
110
+ */
111
+ repost(request: RepostRequest): Promise<RepostResponse>;
112
+ /**
113
+ * Quotes an existing post on the specified target platforms.
114
+ * @param request The quote post request details.
115
+ * @returns A promise resolving with the quote post response.
116
+ */
117
+ quotePost(request: QuotePostRequest): Promise<QuotePostResponse>;
118
+ /**
119
+ * Replies to an existing post on the specified target platforms.
120
+ * @param request The reply request details.
121
+ * @returns A promise resolving with the reply response.
122
+ */
123
+ replyToPost(request: ReplyToPostRequest): Promise<ReplyToPostResponse>;
124
+ /**
125
+ * Likes a post on the specified target platforms.
126
+ * @param request The like request details.
127
+ * @returns A promise resolving with the like response.
128
+ */
129
+ likePost(request: LikePostRequest): Promise<LikePostResponse>;
130
+ /**
131
+ * Unlikes a post on the specified target platforms.
132
+ * @param request The unlike request details.
133
+ * @returns A promise resolving with the unlike response.
134
+ */
135
+ unlikePost(request: UnlikePostRequest): Promise<UnlikePostResponse>;
136
+ /**
137
+ * Deletes one or more posts.
138
+ * @param request The delete request details.
139
+ * @returns A promise resolving with the delete response.
140
+ */
141
+ deletePost(request: DeletePostRequest): Promise<DeletePostResponse>;
142
+ }
143
+
144
+ /**
145
+ * Configuration options for the CrosspostClient
146
+ */
147
+ interface CrosspostClientConfig {
148
+ /**
149
+ * Base URL for the Crosspost API
150
+ * @default 'https://api.opencrosspost.com'
151
+ */
152
+ baseUrl?: string;
153
+ /**
154
+ * NEAR authentication data obtained from near-sign-verify
155
+ */
156
+ signature?: NearAuthData;
157
+ /**
158
+ * Request timeout in milliseconds
159
+ * @default 30000
160
+ */
161
+ timeout?: number;
162
+ /**
163
+ * Number of retries for failed requests (specifically for network errors or 5xx status codes)
164
+ * @default 2
165
+ */
166
+ retries?: number;
167
+ }
168
+
169
+ /**
170
+ * Main client for interacting with the Crosspost API service.
171
+ */
172
+ declare class CrosspostClient {
173
+ /**
174
+ * Authentication-related API operations
175
+ */
176
+ readonly auth: AuthApi;
177
+ /**
178
+ * Post-related API operations
179
+ */
180
+ readonly post: PostApi;
181
+ private readonly options;
182
+ /**
183
+ * Creates an instance of CrosspostClient.
184
+ * @param config Configuration options for the client.
185
+ */
186
+ constructor(config?: CrosspostClientConfig);
187
+ /**
188
+ * Sets the authentication data (signature) for the client and stores it in a cookie
189
+ * @param signature The NEAR authentication data
190
+ */
191
+ setAuthentication(signature: NearAuthData): Promise<void>;
192
+ }
193
+
194
+ /**
195
+ * Handles error responses from the API and converts them to appropriate error objects.
196
+ *
197
+ * @param data The error response data
198
+ * @param status The HTTP status code
199
+ * @returns An ApiError or PlatformError instance
200
+ */
201
+ declare function handleErrorResponse(data: any, status: number): ApiError | PlatformError;
202
+ /**
203
+ * Creates a network error with appropriate details
204
+ *
205
+ * @param error The original error
206
+ * @param url The request URL
207
+ * @param timeout The request timeout
208
+ * @returns An ApiError instance
209
+ */
210
+ declare function createNetworkError(error: unknown, url: string, timeout: number): ApiError;
211
+
212
+ declare const AUTH_COOKIE_NAME = "__crosspost_auth";
213
+ declare const CSRF_COOKIE_NAME = "XSRF-TOKEN";
214
+ declare const CSRF_HEADER_NAME = "X-CSRF-Token";
215
+ declare const AUTH_COOKIE_OPTIONS: Cookies.CookieAttributes;
216
+ /**
217
+ * Gets authentication data from the cookie
218
+ * @returns The NearAuthData object or undefined if not found
219
+ */
220
+ declare function getAuthFromCookie(): NearAuthData | undefined;
221
+ /**
222
+ * Stores authentication data in a secure cookie
223
+ * @param authData The NearAuthData object to store
224
+ */
225
+ declare function storeAuthInCookie(authData: NearAuthData): void;
226
+ /**
227
+ * Clears the authentication cookie
228
+ */
229
+ declare function clearAuthCookie(): void;
230
+ /**
231
+ * Gets the CSRF token from the cookie
232
+ * @returns The CSRF token or undefined if not found
233
+ */
234
+ declare function getCsrfToken(): string | undefined;
235
+
236
+ export { AUTH_COOKIE_NAME, AUTH_COOKIE_OPTIONS, AuthApi, CSRF_COOKIE_NAME, CSRF_HEADER_NAME, CrosspostClient, type CrosspostClientConfig, PostApi, clearAuthCookie, createNetworkError, getAuthFromCookie, getCsrfToken, handleErrorResponse, storeAuthInCookie };
@@ -0,0 +1,236 @@
1
+ import { NearAuthData } from 'near-sign-verify';
2
+ import { NearAuthorizationResponse, Platform, EnhancedApiResponse, AuthStatusResponse, AuthRevokeResponse, ConnectedAccountsResponse, CreatePostRequest, CreatePostResponse, RepostRequest, RepostResponse, QuotePostRequest, QuotePostResponse, ReplyToPostRequest, ReplyToPostResponse, LikePostRequest, LikePostResponse, UnlikePostRequest, UnlikePostResponse, DeletePostRequest, DeletePostResponse, ApiError, PlatformError } from '@crosspost/types';
3
+ export * from '@crosspost/types';
4
+
5
+ /**
6
+ * Options for making a request to the API
7
+ */
8
+ interface RequestOptions {
9
+ /**
10
+ * Base URL for the API
11
+ */
12
+ baseUrl: string;
13
+ /**
14
+ * NEAR authentication data for generating auth tokens
15
+ * Can be undefined if not authorize yet
16
+ */
17
+ signature?: NearAuthData;
18
+ /**
19
+ * Request timeout in milliseconds
20
+ */
21
+ timeout: number;
22
+ /**
23
+ * Number of retries for failed requests
24
+ */
25
+ retries: number;
26
+ }
27
+
28
+ /**
29
+ * Authentication-related API operations
30
+ */
31
+ declare class AuthApi {
32
+ private options;
33
+ /**
34
+ * Creates an instance of AuthApi
35
+ * @param options Request options
36
+ */
37
+ constructor(options: RequestOptions);
38
+ /**
39
+ * Authorizes the NEAR account associated with the provided nearAuthData with the Crosspost service.
40
+ * @returns A promise resolving with the authorization response.
41
+ */
42
+ authorizeNearAccount(): Promise<NearAuthorizationResponse>;
43
+ /**
44
+ * Checks the authorization status of the NEAR account with the Crosspost service.
45
+ * @returns A promise resolving with the authorization status response.
46
+ */
47
+ getNearAuthorizationStatus(): Promise<NearAuthorizationResponse>;
48
+ /**
49
+ * Initiates the login process for a specific platform.
50
+ * The service handles the OAuth flow; this method triggers it.
51
+ * @param platform The target platform.
52
+ * @param options Optional success and error redirect URLs.
53
+ * @returns A promise resolving with the response from the service (might indicate success/failure or redirect info).
54
+ */
55
+ loginToPlatform(platform: Platform, options?: {
56
+ successUrl?: string;
57
+ errorUrl?: string;
58
+ }): Promise<EnhancedApiResponse<any>>;
59
+ /**
60
+ * Refreshes the authentication token for the specified platform.
61
+ * @param platform The target platform.
62
+ * @returns A promise resolving with the refresh response.
63
+ */
64
+ refreshToken(platform: Platform): Promise<EnhancedApiResponse<any>>;
65
+ /**
66
+ * Refreshes the user's profile information from the specified platform.
67
+ * @param platform The target platform.
68
+ * @returns A promise resolving with the profile refresh response.
69
+ */
70
+ refreshProfile(platform: Platform): Promise<EnhancedApiResponse<any>>;
71
+ /**
72
+ * Gets the authentication status for the specified platform.
73
+ * @param platform The target platform.
74
+ * @returns A promise resolving with the authentication status response.
75
+ */
76
+ getAuthStatus(platform: Platform): Promise<AuthStatusResponse>;
77
+ /**
78
+ * Revokes the authentication token for the specified platform.
79
+ * @param platform The target platform.
80
+ * @returns A promise resolving with the revocation response.
81
+ */
82
+ revokeAuth(platform: Platform): Promise<AuthRevokeResponse>;
83
+ /**
84
+ * Lists all accounts connected to the NEAR account.
85
+ * @returns A promise resolving with the list of connected accounts.
86
+ */
87
+ getConnectedAccounts(): Promise<ConnectedAccountsResponse>;
88
+ }
89
+
90
+ /**
91
+ * Post-related API operations
92
+ */
93
+ declare class PostApi {
94
+ private options;
95
+ /**
96
+ * Creates an instance of PostApi
97
+ * @param options Request options
98
+ */
99
+ constructor(options: RequestOptions);
100
+ /**
101
+ * Creates a new post on the specified target platforms.
102
+ * @param request The post creation request details.
103
+ * @returns A promise resolving with the post creation response.
104
+ */
105
+ createPost(request: CreatePostRequest): Promise<CreatePostResponse>;
106
+ /**
107
+ * Reposts an existing post on the specified target platforms.
108
+ * @param request The repost request details.
109
+ * @returns A promise resolving with the repost response.
110
+ */
111
+ repost(request: RepostRequest): Promise<RepostResponse>;
112
+ /**
113
+ * Quotes an existing post on the specified target platforms.
114
+ * @param request The quote post request details.
115
+ * @returns A promise resolving with the quote post response.
116
+ */
117
+ quotePost(request: QuotePostRequest): Promise<QuotePostResponse>;
118
+ /**
119
+ * Replies to an existing post on the specified target platforms.
120
+ * @param request The reply request details.
121
+ * @returns A promise resolving with the reply response.
122
+ */
123
+ replyToPost(request: ReplyToPostRequest): Promise<ReplyToPostResponse>;
124
+ /**
125
+ * Likes a post on the specified target platforms.
126
+ * @param request The like request details.
127
+ * @returns A promise resolving with the like response.
128
+ */
129
+ likePost(request: LikePostRequest): Promise<LikePostResponse>;
130
+ /**
131
+ * Unlikes a post on the specified target platforms.
132
+ * @param request The unlike request details.
133
+ * @returns A promise resolving with the unlike response.
134
+ */
135
+ unlikePost(request: UnlikePostRequest): Promise<UnlikePostResponse>;
136
+ /**
137
+ * Deletes one or more posts.
138
+ * @param request The delete request details.
139
+ * @returns A promise resolving with the delete response.
140
+ */
141
+ deletePost(request: DeletePostRequest): Promise<DeletePostResponse>;
142
+ }
143
+
144
+ /**
145
+ * Configuration options for the CrosspostClient
146
+ */
147
+ interface CrosspostClientConfig {
148
+ /**
149
+ * Base URL for the Crosspost API
150
+ * @default 'https://api.opencrosspost.com'
151
+ */
152
+ baseUrl?: string;
153
+ /**
154
+ * NEAR authentication data obtained from near-sign-verify
155
+ */
156
+ signature?: NearAuthData;
157
+ /**
158
+ * Request timeout in milliseconds
159
+ * @default 30000
160
+ */
161
+ timeout?: number;
162
+ /**
163
+ * Number of retries for failed requests (specifically for network errors or 5xx status codes)
164
+ * @default 2
165
+ */
166
+ retries?: number;
167
+ }
168
+
169
+ /**
170
+ * Main client for interacting with the Crosspost API service.
171
+ */
172
+ declare class CrosspostClient {
173
+ /**
174
+ * Authentication-related API operations
175
+ */
176
+ readonly auth: AuthApi;
177
+ /**
178
+ * Post-related API operations
179
+ */
180
+ readonly post: PostApi;
181
+ private readonly options;
182
+ /**
183
+ * Creates an instance of CrosspostClient.
184
+ * @param config Configuration options for the client.
185
+ */
186
+ constructor(config?: CrosspostClientConfig);
187
+ /**
188
+ * Sets the authentication data (signature) for the client and stores it in a cookie
189
+ * @param signature The NEAR authentication data
190
+ */
191
+ setAuthentication(signature: NearAuthData): Promise<void>;
192
+ }
193
+
194
+ /**
195
+ * Handles error responses from the API and converts them to appropriate error objects.
196
+ *
197
+ * @param data The error response data
198
+ * @param status The HTTP status code
199
+ * @returns An ApiError or PlatformError instance
200
+ */
201
+ declare function handleErrorResponse(data: any, status: number): ApiError | PlatformError;
202
+ /**
203
+ * Creates a network error with appropriate details
204
+ *
205
+ * @param error The original error
206
+ * @param url The request URL
207
+ * @param timeout The request timeout
208
+ * @returns An ApiError instance
209
+ */
210
+ declare function createNetworkError(error: unknown, url: string, timeout: number): ApiError;
211
+
212
+ declare const AUTH_COOKIE_NAME = "__crosspost_auth";
213
+ declare const CSRF_COOKIE_NAME = "XSRF-TOKEN";
214
+ declare const CSRF_HEADER_NAME = "X-CSRF-Token";
215
+ declare const AUTH_COOKIE_OPTIONS: Cookies.CookieAttributes;
216
+ /**
217
+ * Gets authentication data from the cookie
218
+ * @returns The NearAuthData object or undefined if not found
219
+ */
220
+ declare function getAuthFromCookie(): NearAuthData | undefined;
221
+ /**
222
+ * Stores authentication data in a secure cookie
223
+ * @param authData The NearAuthData object to store
224
+ */
225
+ declare function storeAuthInCookie(authData: NearAuthData): void;
226
+ /**
227
+ * Clears the authentication cookie
228
+ */
229
+ declare function clearAuthCookie(): void;
230
+ /**
231
+ * Gets the CSRF token from the cookie
232
+ * @returns The CSRF token or undefined if not found
233
+ */
234
+ declare function getCsrfToken(): string | undefined;
235
+
236
+ export { AUTH_COOKIE_NAME, AUTH_COOKIE_OPTIONS, AuthApi, CSRF_COOKIE_NAME, CSRF_HEADER_NAME, CrosspostClient, type CrosspostClientConfig, PostApi, clearAuthCookie, createNetworkError, getAuthFromCookie, getCsrfToken, handleErrorResponse, storeAuthInCookie };