@crosspost/sdk 0.1.7 → 0.1.8

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 CHANGED
@@ -36,6 +36,7 @@ __export(index_exports, {
36
36
  AuthCallbackResponseSchema: () => AuthCallbackResponseSchema,
37
37
  AuthInitRequestSchema: () => AuthInitRequestSchema,
38
38
  AuthRevokeResponseSchema: () => AuthRevokeResponseSchema,
39
+ AuthStatusParamsSchema: () => AuthStatusParamsSchema,
39
40
  AuthStatusResponseSchema: () => AuthStatusResponseSchema,
40
41
  AuthTokenRequestSchema: () => AuthTokenRequestSchema,
41
42
  AuthUrlResponseSchema: () => AuthUrlResponseSchema,
@@ -49,6 +50,7 @@ __export(index_exports, {
49
50
  DeleteResultSchema: () => DeleteResultSchema,
50
51
  EndpointRateLimitResponseSchema: () => EndpointRateLimitResponseSchema,
51
52
  ErrorDetailSchema: () => ErrorDetailSchema,
53
+ HealthStatusSchema: () => HealthStatusSchema,
52
54
  LikePostRequestSchema: () => LikePostRequestSchema,
53
55
  LikePostResponseSchema: () => LikePostResponseSchema,
54
56
  LikeResultSchema: () => LikeResultSchema,
@@ -58,6 +60,7 @@ __export(index_exports, {
58
60
  NearAuthorizationRequestSchema: () => NearAuthorizationRequestSchema,
59
61
  NearAuthorizationResponseSchema: () => NearAuthorizationResponseSchema,
60
62
  NearAuthorizationStatusResponseSchema: () => NearAuthorizationStatusResponseSchema,
63
+ NearUnauthorizationResponseSchema: () => NearUnauthorizationResponseSchema,
61
64
  Platform: () => Platform,
62
65
  PlatformActivitySchema: () => PlatformActivitySchema,
63
66
  PlatformParamSchema: () => PlatformParamSchema,
@@ -4324,6 +4327,11 @@ var SuccessDetailSchema = z.object({
4324
4327
  additionalData: z.any().optional(),
4325
4328
  status: z.literal("success")
4326
4329
  }).catchall(z.any());
4330
+ var HealthStatusSchema = z.object({
4331
+ status: z.string().describe("Health status of the API"),
4332
+ version: z.string().optional().describe("API version"),
4333
+ timestamp: z.string().datetime().describe("Current server time")
4334
+ }).describe("Health status response");
4327
4335
  var MultiStatusDataSchema = z.object({
4328
4336
  summary: z.object({
4329
4337
  total: z.number().int().nonnegative(),
@@ -4356,6 +4364,14 @@ var AuthCallbackResponseSchema = z.object({
4356
4364
  userId: z.string().describe("User ID"),
4357
4365
  redirectUrl: z.string().optional().describe("URL to redirect the user to after authentication")
4358
4366
  }).describe("Auth callback response");
4367
+ var AuthStatusParamsSchema = z.object({
4368
+ platform: z.string().describe("Social media platform"),
4369
+ userId: z.string().describe("User ID on the platform")
4370
+ }).describe("Token status parameters");
4371
+ var NearUnauthorizationResponseSchema = z.object({
4372
+ success: z.boolean().describe("Whether the unauthorized operation was successful"),
4373
+ nearAccount: z.string().describe("NEAR account ID that was unauthorized")
4374
+ }).describe("NEAR unauthorized response");
4359
4375
  var AuthStatusResponseSchema = z.object({
4360
4376
  platform: PlatformSchema,
4361
4377
  userId: z.string().describe("User ID"),
@@ -4952,7 +4968,7 @@ function createNetworkError(error, url, timeout) {
4952
4968
  // src/core/request.ts
4953
4969
  async function makeRequest(method, path, options, data, query) {
4954
4970
  let url = `${options.baseUrl}${path.startsWith("/") ? path : `/${path}`}`;
4955
- if (query && Object.keys(query).length > 0) {
4971
+ if (query && typeof query === "object" && Object.keys(query).length > 0) {
4956
4972
  const queryParams = new URLSearchParams();
4957
4973
  for (const [key, value] of Object.entries(query)) {
4958
4974
  if (value !== void 0 && value !== null) {
@@ -4978,9 +4994,28 @@ async function makeRequest(method, path, options, data, query) {
4978
4994
  try {
4979
4995
  const headers = {
4980
4996
  "Content-Type": "application/json",
4981
- "Accept": "application/json",
4982
- "Authorization": `Bearer ${(0, import_near_sign_verify.createAuthToken)(options.nearAuthData)}`
4997
+ "Accept": "application/json"
4983
4998
  };
4999
+ if (method === "GET") {
5000
+ const nearAccount = options.nearAccount || options.nearAuthData?.account_id;
5001
+ if (!nearAccount) {
5002
+ throw new CrosspostError(
5003
+ "No NEAR account provided for GET request",
5004
+ ApiErrorCode.UNAUTHORIZED,
5005
+ 401
5006
+ );
5007
+ }
5008
+ headers["X-Near-Account"] = nearAccount;
5009
+ } else {
5010
+ if (!options.nearAuthData) {
5011
+ throw new CrosspostError(
5012
+ "NEAR authentication data required for non-GET request",
5013
+ ApiErrorCode.UNAUTHORIZED,
5014
+ 401
5015
+ );
5016
+ }
5017
+ headers["Authorization"] = `Bearer ${(0, import_near_sign_verify.createAuthToken)(options.nearAuthData)}`;
5018
+ }
4984
5019
  const requestOptions = {
4985
5020
  method,
4986
5021
  headers,
@@ -5152,25 +5187,28 @@ var AuthApi = class {
5152
5187
  /**
5153
5188
  * Refreshes the authentication token for the specified platform.
5154
5189
  * @param platform The target platform.
5155
- * @returns A promise resolving with the refresh response.
5190
+ * @returns A promise resolving with the refresh response containing updated auth details.
5156
5191
  */
5157
- async refreshToken(platform) {
5192
+ async refreshToken(platform, userId) {
5158
5193
  return makeRequest(
5159
5194
  "POST",
5160
5195
  `/auth/${platform}/refresh`,
5161
- this.options
5196
+ this.options,
5197
+ { userId }
5162
5198
  );
5163
5199
  }
5164
5200
  /**
5165
5201
  * Refreshes the user's profile information from the specified platform.
5166
5202
  * @param platform The target platform.
5167
- * @returns A promise resolving with the profile refresh response.
5203
+ * @param userId The user ID on the platform
5204
+ * @returns A promise resolving with the updated account profile information.
5168
5205
  */
5169
- async refreshProfile(platform) {
5206
+ async refreshProfile(platform, userId) {
5170
5207
  return makeRequest(
5171
5208
  "POST",
5172
5209
  `/auth/${platform}/refresh-profile`,
5173
- this.options
5210
+ this.options,
5211
+ { userId }
5174
5212
  );
5175
5213
  }
5176
5214
  /**
@@ -5178,11 +5216,25 @@ var AuthApi = class {
5178
5216
  * @param platform The target platform.
5179
5217
  * @returns A promise resolving with the authentication status response.
5180
5218
  */
5181
- async getAuthStatus(platform) {
5219
+ async getAuthStatus(platform, userId) {
5182
5220
  return makeRequest(
5183
5221
  "GET",
5184
- `/auth/${platform}/status`,
5185
- this.options
5222
+ `/auth/${platform}/status/${userId}`,
5223
+ this.options,
5224
+ void 0,
5225
+ { platform, userId }
5226
+ );
5227
+ }
5228
+ /**
5229
+ * Unauthorizes a NEAR account from using the service
5230
+ * @returns A promise resolving with the unauthorized response
5231
+ */
5232
+ async unauthorizeNear() {
5233
+ return makeRequest(
5234
+ "DELETE",
5235
+ "/auth/unauthorize/near",
5236
+ this.options,
5237
+ {}
5186
5238
  );
5187
5239
  }
5188
5240
  /**
@@ -5190,11 +5242,12 @@ var AuthApi = class {
5190
5242
  * @param platform The target platform.
5191
5243
  * @returns A promise resolving with the revocation response.
5192
5244
  */
5193
- async revokeAuth(platform) {
5245
+ async revokeAuth(platform, userId) {
5194
5246
  return makeRequest(
5195
5247
  "DELETE",
5196
5248
  `/auth/${platform}/revoke`,
5197
- this.options
5249
+ this.options,
5250
+ { userId }
5198
5251
  );
5199
5252
  }
5200
5253
  /**
@@ -5341,7 +5394,9 @@ var SystemApi = class {
5341
5394
  return makeRequest(
5342
5395
  "GET",
5343
5396
  `/api/rate-limit/${endpoint}`,
5344
- this.options
5397
+ this.options,
5398
+ void 0,
5399
+ { endpoint }
5345
5400
  );
5346
5401
  }
5347
5402
  /**
@@ -5388,18 +5443,52 @@ var CrosspostClient = class {
5388
5443
  }
5389
5444
  /**
5390
5445
  * Sets the authentication data (signature) for the client
5446
+ * Required for non-GET requests
5391
5447
  * @param nearAuthData The NEAR authentication data
5392
5448
  */
5393
5449
  setAuthentication(nearAuthData) {
5394
5450
  this.options.nearAuthData = nearAuthData;
5451
+ if (!this.options.nearAccount) {
5452
+ this.options.nearAccount = nearAuthData.account_id;
5453
+ }
5454
+ }
5455
+ /**
5456
+ * Sets the NEAR account ID for simplified GET request authentication
5457
+ * If not set, will use account_id from nearAuthData
5458
+ * @param nearAccount The NEAR account ID
5459
+ */
5460
+ setNearAccount(nearAccount) {
5461
+ this.options.nearAccount = nearAccount;
5462
+ }
5463
+ /**
5464
+ * Gets the current NEAR account ID being used for authentication
5465
+ * @returns The NEAR account ID from nearAccount or nearAuthData
5466
+ */
5467
+ getNearAccount() {
5468
+ return this.options.nearAccount || this.options.nearAuthData?.account_id;
5395
5469
  }
5396
5470
  /**
5397
5471
  * Checks if authentication data (signature) exists on client
5398
- * @param signature The NEAR authentication data
5472
+ * @returns true if nearAuthData is set (required for non-GET requests)
5399
5473
  */
5400
5474
  isAuthenticated() {
5401
5475
  return !!this.options.nearAuthData;
5402
5476
  }
5477
+ /**
5478
+ * Checks if a NEAR account is set for GET request authentication
5479
+ * @returns true if either nearAccount or nearAuthData.account_id is set
5480
+ */
5481
+ hasNearAccount() {
5482
+ return !!(this.options.nearAccount || this.options.nearAuthData?.account_id);
5483
+ }
5484
+ /**
5485
+ * Clears all authentication data from the client
5486
+ * This will prevent all requests from working until new authentication is set
5487
+ */
5488
+ clear() {
5489
+ this.options.nearAuthData = void 0;
5490
+ this.options.nearAccount = void 0;
5491
+ }
5403
5492
  };
5404
5493
  // Annotate the CommonJS export names for ESM import in node:
5405
5494
  0 && (module.exports = {
@@ -5419,6 +5508,7 @@ var CrosspostClient = class {
5419
5508
  AuthCallbackResponseSchema,
5420
5509
  AuthInitRequestSchema,
5421
5510
  AuthRevokeResponseSchema,
5511
+ AuthStatusParamsSchema,
5422
5512
  AuthStatusResponseSchema,
5423
5513
  AuthTokenRequestSchema,
5424
5514
  AuthUrlResponseSchema,
@@ -5432,6 +5522,7 @@ var CrosspostClient = class {
5432
5522
  DeleteResultSchema,
5433
5523
  EndpointRateLimitResponseSchema,
5434
5524
  ErrorDetailSchema,
5525
+ HealthStatusSchema,
5435
5526
  LikePostRequestSchema,
5436
5527
  LikePostResponseSchema,
5437
5528
  LikeResultSchema,
@@ -5441,6 +5532,7 @@ var CrosspostClient = class {
5441
5532
  NearAuthorizationRequestSchema,
5442
5533
  NearAuthorizationResponseSchema,
5443
5534
  NearAuthorizationStatusResponseSchema,
5535
+ NearUnauthorizationResponseSchema,
5444
5536
  Platform,
5445
5537
  PlatformActivitySchema,
5446
5538
  PlatformParamSchema,
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, ApiResponse, AuthStatusResponse, AuthRevokeResponse, ConnectedAccountsResponse, CreatePostRequest, CreatePostResponse, RepostRequest, RepostResponse, QuotePostRequest, QuotePostResponse, ReplyToPostRequest, ReplyToPostResponse, LikePostRequest, LikePostResponse, UnlikePostRequest, UnlikePostResponse, DeletePostRequest, DeletePostResponse, RateLimitResponse, EndpointRateLimitResponse, ApiErrorCode, StatusCode, ErrorDetails } from '@crosspost/types';
2
+ import { ActivityLeaderboardQuery, ActivityLeaderboardResponse, AccountActivityQuery, AccountActivityResponse, AccountPostsQuery, AccountPostsResponse, NearAuthorizationResponse, Platform, AuthInitRequest, AuthUrlResponse, AuthCallbackResponse, ConnectedAccount, AuthStatusResponse, NearUnauthorizationResponse, AuthRevokeResponse, ConnectedAccountsResponse, CreatePostRequest, CreatePostResponse, RepostRequest, RepostResponse, QuotePostRequest, QuotePostResponse, ReplyToPostRequest, ReplyToPostResponse, LikePostRequest, LikePostResponse, UnlikePostRequest, UnlikePostResponse, DeletePostRequest, DeletePostResponse, RateLimitResponse, EndpointRateLimitResponse, HealthStatus, ApiErrorCode, StatusCode, ErrorDetails } from '@crosspost/types';
3
3
  export * from '@crosspost/types';
4
4
 
5
5
  /**
@@ -12,9 +12,14 @@ interface RequestOptions {
12
12
  baseUrl: string;
13
13
  /**
14
14
  * NEAR authentication data for generating auth tokens
15
- * Can be undefined if not authorize yet
15
+ * Required for non-GET requests, optional for GET requests
16
16
  */
17
17
  nearAuthData?: NearAuthData;
18
+ /**
19
+ * NEAR account ID for simplified GET request authentication
20
+ * If not provided, will use account_id from nearAuthData
21
+ */
22
+ nearAccount?: string;
18
23
  /**
19
24
  * Request timeout in milliseconds
20
25
  */
@@ -84,34 +89,37 @@ declare class AuthApi {
84
89
  * @param options Optional success and error redirect URLs.
85
90
  * @returns A promise resolving with the response from the service (might indicate success/failure or redirect info).
86
91
  */
87
- loginToPlatform(platform: Platform, options?: {
88
- successUrl?: string;
89
- errorUrl?: string;
90
- }): Promise<ApiResponse<any>>;
92
+ loginToPlatform(platform: Platform, options?: AuthInitRequest): Promise<AuthUrlResponse>;
91
93
  /**
92
94
  * Refreshes the authentication token for the specified platform.
93
95
  * @param platform The target platform.
94
- * @returns A promise resolving with the refresh response.
96
+ * @returns A promise resolving with the refresh response containing updated auth details.
95
97
  */
96
- refreshToken(platform: Platform): Promise<ApiResponse<any>>;
98
+ refreshToken(platform: Platform, userId: string): Promise<AuthCallbackResponse>;
97
99
  /**
98
100
  * Refreshes the user's profile information from the specified platform.
99
101
  * @param platform The target platform.
100
- * @returns A promise resolving with the profile refresh response.
102
+ * @param userId The user ID on the platform
103
+ * @returns A promise resolving with the updated account profile information.
101
104
  */
102
- refreshProfile(platform: Platform): Promise<ApiResponse<any>>;
105
+ refreshProfile(platform: Platform, userId: string): Promise<ConnectedAccount>;
103
106
  /**
104
107
  * Gets the authentication status for the specified platform.
105
108
  * @param platform The target platform.
106
109
  * @returns A promise resolving with the authentication status response.
107
110
  */
108
- getAuthStatus(platform: Platform): Promise<AuthStatusResponse>;
111
+ getAuthStatus(platform: Platform, userId: string): Promise<AuthStatusResponse>;
112
+ /**
113
+ * Unauthorizes a NEAR account from using the service
114
+ * @returns A promise resolving with the unauthorized response
115
+ */
116
+ unauthorizeNear(): Promise<NearUnauthorizationResponse>;
109
117
  /**
110
118
  * Revokes the authentication token for the specified platform.
111
119
  * @param platform The target platform.
112
120
  * @returns A promise resolving with the revocation response.
113
121
  */
114
- revokeAuth(platform: Platform): Promise<AuthRevokeResponse>;
122
+ revokeAuth(platform: Platform, userId: string): Promise<AuthRevokeResponse>;
115
123
  /**
116
124
  * Lists all accounts connected to the NEAR account.
117
125
  * @returns A promise resolving with the list of connected accounts.
@@ -199,9 +207,7 @@ declare class SystemApi {
199
207
  * Gets the health status of the API
200
208
  * @returns A promise resolving with the health status
201
209
  */
202
- getHealthStatus(): Promise<ApiResponse<{
203
- status: string;
204
- }>>;
210
+ getHealthStatus(): Promise<HealthStatus>;
205
211
  }
206
212
 
207
213
  /**
@@ -245,14 +251,36 @@ declare class CrosspostClient {
245
251
  constructor(config?: CrosspostClientConfig);
246
252
  /**
247
253
  * Sets the authentication data (signature) for the client
254
+ * Required for non-GET requests
248
255
  * @param nearAuthData The NEAR authentication data
249
256
  */
250
257
  setAuthentication(nearAuthData: NearAuthData): void;
258
+ /**
259
+ * Sets the NEAR account ID for simplified GET request authentication
260
+ * If not set, will use account_id from nearAuthData
261
+ * @param nearAccount The NEAR account ID
262
+ */
263
+ setNearAccount(nearAccount: string): void;
264
+ /**
265
+ * Gets the current NEAR account ID being used for authentication
266
+ * @returns The NEAR account ID from nearAccount or nearAuthData
267
+ */
268
+ getNearAccount(): string | undefined;
251
269
  /**
252
270
  * Checks if authentication data (signature) exists on client
253
- * @param signature The NEAR authentication data
271
+ * @returns true if nearAuthData is set (required for non-GET requests)
254
272
  */
255
273
  isAuthenticated(): boolean;
274
+ /**
275
+ * Checks if a NEAR account is set for GET request authentication
276
+ * @returns true if either nearAccount or nearAuthData.account_id is set
277
+ */
278
+ hasNearAccount(): boolean;
279
+ /**
280
+ * Clears all authentication data from the client
281
+ * This will prevent all requests from working until new authentication is set
282
+ */
283
+ clear(): void;
256
284
  }
257
285
 
258
286
  /**
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, ApiResponse, AuthStatusResponse, AuthRevokeResponse, ConnectedAccountsResponse, CreatePostRequest, CreatePostResponse, RepostRequest, RepostResponse, QuotePostRequest, QuotePostResponse, ReplyToPostRequest, ReplyToPostResponse, LikePostRequest, LikePostResponse, UnlikePostRequest, UnlikePostResponse, DeletePostRequest, DeletePostResponse, RateLimitResponse, EndpointRateLimitResponse, ApiErrorCode, StatusCode, ErrorDetails } from '@crosspost/types';
2
+ import { ActivityLeaderboardQuery, ActivityLeaderboardResponse, AccountActivityQuery, AccountActivityResponse, AccountPostsQuery, AccountPostsResponse, NearAuthorizationResponse, Platform, AuthInitRequest, AuthUrlResponse, AuthCallbackResponse, ConnectedAccount, AuthStatusResponse, NearUnauthorizationResponse, AuthRevokeResponse, ConnectedAccountsResponse, CreatePostRequest, CreatePostResponse, RepostRequest, RepostResponse, QuotePostRequest, QuotePostResponse, ReplyToPostRequest, ReplyToPostResponse, LikePostRequest, LikePostResponse, UnlikePostRequest, UnlikePostResponse, DeletePostRequest, DeletePostResponse, RateLimitResponse, EndpointRateLimitResponse, HealthStatus, ApiErrorCode, StatusCode, ErrorDetails } from '@crosspost/types';
3
3
  export * from '@crosspost/types';
4
4
 
5
5
  /**
@@ -12,9 +12,14 @@ interface RequestOptions {
12
12
  baseUrl: string;
13
13
  /**
14
14
  * NEAR authentication data for generating auth tokens
15
- * Can be undefined if not authorize yet
15
+ * Required for non-GET requests, optional for GET requests
16
16
  */
17
17
  nearAuthData?: NearAuthData;
18
+ /**
19
+ * NEAR account ID for simplified GET request authentication
20
+ * If not provided, will use account_id from nearAuthData
21
+ */
22
+ nearAccount?: string;
18
23
  /**
19
24
  * Request timeout in milliseconds
20
25
  */
@@ -84,34 +89,37 @@ declare class AuthApi {
84
89
  * @param options Optional success and error redirect URLs.
85
90
  * @returns A promise resolving with the response from the service (might indicate success/failure or redirect info).
86
91
  */
87
- loginToPlatform(platform: Platform, options?: {
88
- successUrl?: string;
89
- errorUrl?: string;
90
- }): Promise<ApiResponse<any>>;
92
+ loginToPlatform(platform: Platform, options?: AuthInitRequest): Promise<AuthUrlResponse>;
91
93
  /**
92
94
  * Refreshes the authentication token for the specified platform.
93
95
  * @param platform The target platform.
94
- * @returns A promise resolving with the refresh response.
96
+ * @returns A promise resolving with the refresh response containing updated auth details.
95
97
  */
96
- refreshToken(platform: Platform): Promise<ApiResponse<any>>;
98
+ refreshToken(platform: Platform, userId: string): Promise<AuthCallbackResponse>;
97
99
  /**
98
100
  * Refreshes the user's profile information from the specified platform.
99
101
  * @param platform The target platform.
100
- * @returns A promise resolving with the profile refresh response.
102
+ * @param userId The user ID on the platform
103
+ * @returns A promise resolving with the updated account profile information.
101
104
  */
102
- refreshProfile(platform: Platform): Promise<ApiResponse<any>>;
105
+ refreshProfile(platform: Platform, userId: string): Promise<ConnectedAccount>;
103
106
  /**
104
107
  * Gets the authentication status for the specified platform.
105
108
  * @param platform The target platform.
106
109
  * @returns A promise resolving with the authentication status response.
107
110
  */
108
- getAuthStatus(platform: Platform): Promise<AuthStatusResponse>;
111
+ getAuthStatus(platform: Platform, userId: string): Promise<AuthStatusResponse>;
112
+ /**
113
+ * Unauthorizes a NEAR account from using the service
114
+ * @returns A promise resolving with the unauthorized response
115
+ */
116
+ unauthorizeNear(): Promise<NearUnauthorizationResponse>;
109
117
  /**
110
118
  * Revokes the authentication token for the specified platform.
111
119
  * @param platform The target platform.
112
120
  * @returns A promise resolving with the revocation response.
113
121
  */
114
- revokeAuth(platform: Platform): Promise<AuthRevokeResponse>;
122
+ revokeAuth(platform: Platform, userId: string): Promise<AuthRevokeResponse>;
115
123
  /**
116
124
  * Lists all accounts connected to the NEAR account.
117
125
  * @returns A promise resolving with the list of connected accounts.
@@ -199,9 +207,7 @@ declare class SystemApi {
199
207
  * Gets the health status of the API
200
208
  * @returns A promise resolving with the health status
201
209
  */
202
- getHealthStatus(): Promise<ApiResponse<{
203
- status: string;
204
- }>>;
210
+ getHealthStatus(): Promise<HealthStatus>;
205
211
  }
206
212
 
207
213
  /**
@@ -245,14 +251,36 @@ declare class CrosspostClient {
245
251
  constructor(config?: CrosspostClientConfig);
246
252
  /**
247
253
  * Sets the authentication data (signature) for the client
254
+ * Required for non-GET requests
248
255
  * @param nearAuthData The NEAR authentication data
249
256
  */
250
257
  setAuthentication(nearAuthData: NearAuthData): void;
258
+ /**
259
+ * Sets the NEAR account ID for simplified GET request authentication
260
+ * If not set, will use account_id from nearAuthData
261
+ * @param nearAccount The NEAR account ID
262
+ */
263
+ setNearAccount(nearAccount: string): void;
264
+ /**
265
+ * Gets the current NEAR account ID being used for authentication
266
+ * @returns The NEAR account ID from nearAccount or nearAuthData
267
+ */
268
+ getNearAccount(): string | undefined;
251
269
  /**
252
270
  * Checks if authentication data (signature) exists on client
253
- * @param signature The NEAR authentication data
271
+ * @returns true if nearAuthData is set (required for non-GET requests)
254
272
  */
255
273
  isAuthenticated(): boolean;
274
+ /**
275
+ * Checks if a NEAR account is set for GET request authentication
276
+ * @returns true if either nearAccount or nearAuthData.account_id is set
277
+ */
278
+ hasNearAccount(): boolean;
279
+ /**
280
+ * Clears all authentication data from the client
281
+ * This will prevent all requests from working until new authentication is set
282
+ */
283
+ clear(): void;
256
284
  }
257
285
 
258
286
  /**
package/dist/index.js CHANGED
@@ -4207,6 +4207,11 @@ var SuccessDetailSchema = z.object({
4207
4207
  additionalData: z.any().optional(),
4208
4208
  status: z.literal("success")
4209
4209
  }).catchall(z.any());
4210
+ var HealthStatusSchema = z.object({
4211
+ status: z.string().describe("Health status of the API"),
4212
+ version: z.string().optional().describe("API version"),
4213
+ timestamp: z.string().datetime().describe("Current server time")
4214
+ }).describe("Health status response");
4210
4215
  var MultiStatusDataSchema = z.object({
4211
4216
  summary: z.object({
4212
4217
  total: z.number().int().nonnegative(),
@@ -4239,6 +4244,14 @@ var AuthCallbackResponseSchema = z.object({
4239
4244
  userId: z.string().describe("User ID"),
4240
4245
  redirectUrl: z.string().optional().describe("URL to redirect the user to after authentication")
4241
4246
  }).describe("Auth callback response");
4247
+ var AuthStatusParamsSchema = z.object({
4248
+ platform: z.string().describe("Social media platform"),
4249
+ userId: z.string().describe("User ID on the platform")
4250
+ }).describe("Token status parameters");
4251
+ var NearUnauthorizationResponseSchema = z.object({
4252
+ success: z.boolean().describe("Whether the unauthorized operation was successful"),
4253
+ nearAccount: z.string().describe("NEAR account ID that was unauthorized")
4254
+ }).describe("NEAR unauthorized response");
4242
4255
  var AuthStatusResponseSchema = z.object({
4243
4256
  platform: PlatformSchema,
4244
4257
  userId: z.string().describe("User ID"),
@@ -4835,7 +4848,7 @@ function createNetworkError(error, url, timeout) {
4835
4848
  // src/core/request.ts
4836
4849
  async function makeRequest(method, path, options, data, query) {
4837
4850
  let url = `${options.baseUrl}${path.startsWith("/") ? path : `/${path}`}`;
4838
- if (query && Object.keys(query).length > 0) {
4851
+ if (query && typeof query === "object" && Object.keys(query).length > 0) {
4839
4852
  const queryParams = new URLSearchParams();
4840
4853
  for (const [key, value] of Object.entries(query)) {
4841
4854
  if (value !== void 0 && value !== null) {
@@ -4861,9 +4874,28 @@ async function makeRequest(method, path, options, data, query) {
4861
4874
  try {
4862
4875
  const headers = {
4863
4876
  "Content-Type": "application/json",
4864
- "Accept": "application/json",
4865
- "Authorization": `Bearer ${createAuthToken(options.nearAuthData)}`
4877
+ "Accept": "application/json"
4866
4878
  };
4879
+ if (method === "GET") {
4880
+ const nearAccount = options.nearAccount || options.nearAuthData?.account_id;
4881
+ if (!nearAccount) {
4882
+ throw new CrosspostError(
4883
+ "No NEAR account provided for GET request",
4884
+ ApiErrorCode.UNAUTHORIZED,
4885
+ 401
4886
+ );
4887
+ }
4888
+ headers["X-Near-Account"] = nearAccount;
4889
+ } else {
4890
+ if (!options.nearAuthData) {
4891
+ throw new CrosspostError(
4892
+ "NEAR authentication data required for non-GET request",
4893
+ ApiErrorCode.UNAUTHORIZED,
4894
+ 401
4895
+ );
4896
+ }
4897
+ headers["Authorization"] = `Bearer ${createAuthToken(options.nearAuthData)}`;
4898
+ }
4867
4899
  const requestOptions = {
4868
4900
  method,
4869
4901
  headers,
@@ -5035,25 +5067,28 @@ var AuthApi = class {
5035
5067
  /**
5036
5068
  * Refreshes the authentication token for the specified platform.
5037
5069
  * @param platform The target platform.
5038
- * @returns A promise resolving with the refresh response.
5070
+ * @returns A promise resolving with the refresh response containing updated auth details.
5039
5071
  */
5040
- async refreshToken(platform) {
5072
+ async refreshToken(platform, userId) {
5041
5073
  return makeRequest(
5042
5074
  "POST",
5043
5075
  `/auth/${platform}/refresh`,
5044
- this.options
5076
+ this.options,
5077
+ { userId }
5045
5078
  );
5046
5079
  }
5047
5080
  /**
5048
5081
  * Refreshes the user's profile information from the specified platform.
5049
5082
  * @param platform The target platform.
5050
- * @returns A promise resolving with the profile refresh response.
5083
+ * @param userId The user ID on the platform
5084
+ * @returns A promise resolving with the updated account profile information.
5051
5085
  */
5052
- async refreshProfile(platform) {
5086
+ async refreshProfile(platform, userId) {
5053
5087
  return makeRequest(
5054
5088
  "POST",
5055
5089
  `/auth/${platform}/refresh-profile`,
5056
- this.options
5090
+ this.options,
5091
+ { userId }
5057
5092
  );
5058
5093
  }
5059
5094
  /**
@@ -5061,11 +5096,25 @@ var AuthApi = class {
5061
5096
  * @param platform The target platform.
5062
5097
  * @returns A promise resolving with the authentication status response.
5063
5098
  */
5064
- async getAuthStatus(platform) {
5099
+ async getAuthStatus(platform, userId) {
5065
5100
  return makeRequest(
5066
5101
  "GET",
5067
- `/auth/${platform}/status`,
5068
- this.options
5102
+ `/auth/${platform}/status/${userId}`,
5103
+ this.options,
5104
+ void 0,
5105
+ { platform, userId }
5106
+ );
5107
+ }
5108
+ /**
5109
+ * Unauthorizes a NEAR account from using the service
5110
+ * @returns A promise resolving with the unauthorized response
5111
+ */
5112
+ async unauthorizeNear() {
5113
+ return makeRequest(
5114
+ "DELETE",
5115
+ "/auth/unauthorize/near",
5116
+ this.options,
5117
+ {}
5069
5118
  );
5070
5119
  }
5071
5120
  /**
@@ -5073,11 +5122,12 @@ var AuthApi = class {
5073
5122
  * @param platform The target platform.
5074
5123
  * @returns A promise resolving with the revocation response.
5075
5124
  */
5076
- async revokeAuth(platform) {
5125
+ async revokeAuth(platform, userId) {
5077
5126
  return makeRequest(
5078
5127
  "DELETE",
5079
5128
  `/auth/${platform}/revoke`,
5080
- this.options
5129
+ this.options,
5130
+ { userId }
5081
5131
  );
5082
5132
  }
5083
5133
  /**
@@ -5224,7 +5274,9 @@ var SystemApi = class {
5224
5274
  return makeRequest(
5225
5275
  "GET",
5226
5276
  `/api/rate-limit/${endpoint}`,
5227
- this.options
5277
+ this.options,
5278
+ void 0,
5279
+ { endpoint }
5228
5280
  );
5229
5281
  }
5230
5282
  /**
@@ -5271,18 +5323,52 @@ var CrosspostClient = class {
5271
5323
  }
5272
5324
  /**
5273
5325
  * Sets the authentication data (signature) for the client
5326
+ * Required for non-GET requests
5274
5327
  * @param nearAuthData The NEAR authentication data
5275
5328
  */
5276
5329
  setAuthentication(nearAuthData) {
5277
5330
  this.options.nearAuthData = nearAuthData;
5331
+ if (!this.options.nearAccount) {
5332
+ this.options.nearAccount = nearAuthData.account_id;
5333
+ }
5334
+ }
5335
+ /**
5336
+ * Sets the NEAR account ID for simplified GET request authentication
5337
+ * If not set, will use account_id from nearAuthData
5338
+ * @param nearAccount The NEAR account ID
5339
+ */
5340
+ setNearAccount(nearAccount) {
5341
+ this.options.nearAccount = nearAccount;
5342
+ }
5343
+ /**
5344
+ * Gets the current NEAR account ID being used for authentication
5345
+ * @returns The NEAR account ID from nearAccount or nearAuthData
5346
+ */
5347
+ getNearAccount() {
5348
+ return this.options.nearAccount || this.options.nearAuthData?.account_id;
5278
5349
  }
5279
5350
  /**
5280
5351
  * Checks if authentication data (signature) exists on client
5281
- * @param signature The NEAR authentication data
5352
+ * @returns true if nearAuthData is set (required for non-GET requests)
5282
5353
  */
5283
5354
  isAuthenticated() {
5284
5355
  return !!this.options.nearAuthData;
5285
5356
  }
5357
+ /**
5358
+ * Checks if a NEAR account is set for GET request authentication
5359
+ * @returns true if either nearAccount or nearAuthData.account_id is set
5360
+ */
5361
+ hasNearAccount() {
5362
+ return !!(this.options.nearAccount || this.options.nearAuthData?.account_id);
5363
+ }
5364
+ /**
5365
+ * Clears all authentication data from the client
5366
+ * This will prevent all requests from working until new authentication is set
5367
+ */
5368
+ clear() {
5369
+ this.options.nearAuthData = void 0;
5370
+ this.options.nearAccount = void 0;
5371
+ }
5286
5372
  };
5287
5373
  export {
5288
5374
  AccountActivityEntrySchema,
@@ -5301,6 +5387,7 @@ export {
5301
5387
  AuthCallbackResponseSchema,
5302
5388
  AuthInitRequestSchema,
5303
5389
  AuthRevokeResponseSchema,
5390
+ AuthStatusParamsSchema,
5304
5391
  AuthStatusResponseSchema,
5305
5392
  AuthTokenRequestSchema,
5306
5393
  AuthUrlResponseSchema,
@@ -5314,6 +5401,7 @@ export {
5314
5401
  DeleteResultSchema,
5315
5402
  EndpointRateLimitResponseSchema,
5316
5403
  ErrorDetailSchema,
5404
+ HealthStatusSchema,
5317
5405
  LikePostRequestSchema,
5318
5406
  LikePostResponseSchema,
5319
5407
  LikeResultSchema,
@@ -5323,6 +5411,7 @@ export {
5323
5411
  NearAuthorizationRequestSchema,
5324
5412
  NearAuthorizationResponseSchema,
5325
5413
  NearAuthorizationStatusResponseSchema,
5414
+ NearUnauthorizationResponseSchema,
5326
5415
  Platform,
5327
5416
  PlatformActivitySchema,
5328
5417
  PlatformParamSchema,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@crosspost/sdk",
3
- "version": "0.1.7",
3
+ "version": "0.1.8",
4
4
  "description": "SDK for interacting with the Crosspost API",
5
5
  "type": "module",
6
6
  "main": "dist/index.cjs",
@@ -28,7 +28,7 @@ export class ActivityApi {
28
28
  * @returns A promise resolving with the leaderboard response
29
29
  */
30
30
  async getLeaderboard(query?: ActivityLeaderboardQuery): Promise<ActivityLeaderboardResponse> {
31
- return makeRequest<ActivityLeaderboardResponse>(
31
+ return makeRequest<ActivityLeaderboardResponse, never, ActivityLeaderboardQuery>(
32
32
  'GET',
33
33
  '/api/activity',
34
34
  this.options,
@@ -47,7 +47,7 @@ export class ActivityApi {
47
47
  signerId: string,
48
48
  query?: AccountActivityQuery,
49
49
  ): Promise<AccountActivityResponse> {
50
- return makeRequest<AccountActivityResponse>(
50
+ return makeRequest<AccountActivityResponse, never, AccountActivityQuery>(
51
51
  'GET',
52
52
  `/api/activity/${signerId}`,
53
53
  this.options,
@@ -66,7 +66,7 @@ export class ActivityApi {
66
66
  signerId: string,
67
67
  query?: AccountPostsQuery,
68
68
  ): Promise<AccountPostsResponse> {
69
- return makeRequest<AccountPostsResponse>(
69
+ return makeRequest<AccountPostsResponse, never, AccountPostsQuery>(
70
70
  'GET',
71
71
  `/api/activity/${signerId}/posts`,
72
72
  this.options,
package/src/api/auth.ts CHANGED
@@ -1,9 +1,16 @@
1
1
  import type {
2
- ApiResponse,
2
+ AuthCallbackResponse,
3
+ AuthInitRequest,
3
4
  AuthRevokeResponse,
5
+ AuthStatusParams,
4
6
  AuthStatusResponse,
7
+ AuthTokenRequest,
8
+ AuthUrlResponse,
9
+ ConnectedAccount,
5
10
  ConnectedAccountsResponse,
11
+ NearAuthorizationRequest,
6
12
  NearAuthorizationResponse,
13
+ NearUnauthorizationResponse,
7
14
  Platform,
8
15
  } from '@crosspost/types';
9
16
  import { makeRequest, type RequestOptions } from '../core/request.ts';
@@ -27,7 +34,7 @@ export class AuthApi {
27
34
  * @returns A promise resolving with the authorization response.
28
35
  */
29
36
  async authorizeNearAccount(): Promise<NearAuthorizationResponse> {
30
- return makeRequest<NearAuthorizationResponse>(
37
+ return makeRequest<NearAuthorizationResponse, NearAuthorizationRequest>(
31
38
  'POST',
32
39
  '/auth/authorize/near',
33
40
  this.options,
@@ -40,7 +47,7 @@ export class AuthApi {
40
47
  * @returns A promise resolving with the authorization status response.
41
48
  */
42
49
  async getNearAuthorizationStatus(): Promise<NearAuthorizationResponse> {
43
- return makeRequest<NearAuthorizationResponse>(
50
+ return makeRequest<NearAuthorizationResponse, never>(
44
51
  'GET',
45
52
  '/auth/authorize/near/status',
46
53
  this.options,
@@ -56,9 +63,9 @@ export class AuthApi {
56
63
  */
57
64
  async loginToPlatform(
58
65
  platform: Platform,
59
- options?: { successUrl?: string; errorUrl?: string },
60
- ): Promise<ApiResponse<any>> { // TODO: Refine response type based on actual API
61
- return makeRequest<ApiResponse<any>>(
66
+ options?: AuthInitRequest,
67
+ ): Promise<AuthUrlResponse> {
68
+ return makeRequest<AuthUrlResponse, AuthInitRequest>(
62
69
  'POST',
63
70
  `/auth/${platform}/login`,
64
71
  this.options,
@@ -69,26 +76,29 @@ export class AuthApi {
69
76
  /**
70
77
  * Refreshes the authentication token for the specified platform.
71
78
  * @param platform The target platform.
72
- * @returns A promise resolving with the refresh response.
79
+ * @returns A promise resolving with the refresh response containing updated auth details.
73
80
  */
74
- async refreshToken(platform: Platform): Promise<ApiResponse<any>> { // TODO: Refine response type
75
- return makeRequest<ApiResponse<any>>(
81
+ async refreshToken(platform: Platform, userId: string): Promise<AuthCallbackResponse> {
82
+ return makeRequest<AuthCallbackResponse, AuthTokenRequest>(
76
83
  'POST',
77
84
  `/auth/${platform}/refresh`,
78
85
  this.options,
86
+ { userId },
79
87
  );
80
88
  }
81
89
 
82
90
  /**
83
91
  * Refreshes the user's profile information from the specified platform.
84
92
  * @param platform The target platform.
85
- * @returns A promise resolving with the profile refresh response.
93
+ * @param userId The user ID on the platform
94
+ * @returns A promise resolving with the updated account profile information.
86
95
  */
87
- async refreshProfile(platform: Platform): Promise<ApiResponse<any>> { // TODO: Refine response type
88
- return makeRequest<ApiResponse<any>>(
96
+ async refreshProfile(platform: Platform, userId: string): Promise<ConnectedAccount> {
97
+ return makeRequest<ConnectedAccount, AuthTokenRequest>(
89
98
  'POST',
90
99
  `/auth/${platform}/refresh-profile`,
91
100
  this.options,
101
+ { userId },
92
102
  );
93
103
  }
94
104
 
@@ -97,11 +107,26 @@ export class AuthApi {
97
107
  * @param platform The target platform.
98
108
  * @returns A promise resolving with the authentication status response.
99
109
  */
100
- async getAuthStatus(platform: Platform): Promise<AuthStatusResponse> {
101
- return makeRequest<AuthStatusResponse>(
110
+ async getAuthStatus(platform: Platform, userId: string): Promise<AuthStatusResponse> {
111
+ return makeRequest<AuthStatusResponse, never, AuthStatusParams>(
102
112
  'GET',
103
- `/auth/${platform}/status`,
113
+ `/auth/${platform}/status/${userId}`,
104
114
  this.options,
115
+ undefined,
116
+ { platform, userId },
117
+ );
118
+ }
119
+
120
+ /**
121
+ * Unauthorizes a NEAR account from using the service
122
+ * @returns A promise resolving with the unauthorized response
123
+ */
124
+ async unauthorizeNear(): Promise<NearUnauthorizationResponse> {
125
+ return makeRequest<NearUnauthorizationResponse, NearAuthorizationRequest>(
126
+ 'DELETE',
127
+ '/auth/unauthorize/near',
128
+ this.options,
129
+ {},
105
130
  );
106
131
  }
107
132
 
@@ -110,11 +135,12 @@ export class AuthApi {
110
135
  * @param platform The target platform.
111
136
  * @returns A promise resolving with the revocation response.
112
137
  */
113
- async revokeAuth(platform: Platform): Promise<AuthRevokeResponse> {
114
- return makeRequest<AuthRevokeResponse>(
138
+ async revokeAuth(platform: Platform, userId: string): Promise<AuthRevokeResponse> {
139
+ return makeRequest<AuthRevokeResponse, AuthTokenRequest>(
115
140
  'DELETE',
116
141
  `/auth/${platform}/revoke`,
117
142
  this.options,
143
+ { userId },
118
144
  );
119
145
  }
120
146
 
@@ -123,7 +149,7 @@ export class AuthApi {
123
149
  * @returns A promise resolving with the list of connected accounts.
124
150
  */
125
151
  async getConnectedAccounts(): Promise<ConnectedAccountsResponse> {
126
- return makeRequest<ConnectedAccountsResponse>(
152
+ return makeRequest<ConnectedAccountsResponse, never>(
127
153
  'GET',
128
154
  '/auth/accounts',
129
155
  this.options,
package/src/api/post.ts CHANGED
@@ -36,7 +36,7 @@ export class PostApi {
36
36
  * @returns A promise resolving with the post creation response.
37
37
  */
38
38
  async createPost(request: CreatePostRequest): Promise<CreatePostResponse> {
39
- return makeRequest<CreatePostResponse>(
39
+ return makeRequest<CreatePostResponse, CreatePostRequest>(
40
40
  'POST',
41
41
  '/api/post',
42
42
  this.options,
@@ -50,7 +50,7 @@ export class PostApi {
50
50
  * @returns A promise resolving with the repost response.
51
51
  */
52
52
  async repost(request: RepostRequest): Promise<RepostResponse> {
53
- return makeRequest<RepostResponse>(
53
+ return makeRequest<RepostResponse, RepostRequest>(
54
54
  'POST',
55
55
  '/api/post/repost',
56
56
  this.options,
@@ -64,7 +64,7 @@ export class PostApi {
64
64
  * @returns A promise resolving with the quote post response.
65
65
  */
66
66
  async quotePost(request: QuotePostRequest): Promise<QuotePostResponse> {
67
- return makeRequest<QuotePostResponse>(
67
+ return makeRequest<QuotePostResponse, QuotePostRequest>(
68
68
  'POST',
69
69
  '/api/post/quote',
70
70
  this.options,
@@ -78,7 +78,7 @@ export class PostApi {
78
78
  * @returns A promise resolving with the reply response.
79
79
  */
80
80
  async replyToPost(request: ReplyToPostRequest): Promise<ReplyToPostResponse> {
81
- return makeRequest<ReplyToPostResponse>(
81
+ return makeRequest<ReplyToPostResponse, ReplyToPostRequest>(
82
82
  'POST',
83
83
  '/api/post/reply',
84
84
  this.options,
@@ -92,7 +92,7 @@ export class PostApi {
92
92
  * @returns A promise resolving with the like response.
93
93
  */
94
94
  async likePost(request: LikePostRequest): Promise<LikePostResponse> {
95
- return makeRequest<LikePostResponse>(
95
+ return makeRequest<LikePostResponse, LikePostRequest>(
96
96
  'POST',
97
97
  `/api/post/like`,
98
98
  this.options,
@@ -106,7 +106,7 @@ export class PostApi {
106
106
  * @returns A promise resolving with the unlike response.
107
107
  */
108
108
  async unlikePost(request: UnlikePostRequest): Promise<UnlikePostResponse> {
109
- return makeRequest<UnlikePostResponse>(
109
+ return makeRequest<UnlikePostResponse, UnlikePostRequest>(
110
110
  'DELETE',
111
111
  `/api/post/like`,
112
112
  this.options,
@@ -120,7 +120,7 @@ export class PostApi {
120
120
  * @returns A promise resolving with the delete response.
121
121
  */
122
122
  async deletePost(request: DeletePostRequest): Promise<DeletePostResponse> {
123
- return makeRequest<DeletePostResponse>(
123
+ return makeRequest<DeletePostResponse, DeletePostRequest>(
124
124
  'DELETE',
125
125
  `/api/post`,
126
126
  this.options,
package/src/api/system.ts CHANGED
@@ -1,4 +1,9 @@
1
- import type { ApiResponse, EndpointRateLimitResponse, RateLimitResponse } from '@crosspost/types';
1
+ import type {
2
+ EndpointRateLimitResponse,
3
+ HealthStatus,
4
+ RateLimitEndpointParam,
5
+ RateLimitResponse,
6
+ } from '@crosspost/types';
2
7
  import { makeRequest, type RequestOptions } from '../core/request.ts';
3
8
 
4
9
  /**
@@ -21,7 +26,7 @@ export class SystemApi {
21
26
  * @returns A promise resolving with the rate limit response
22
27
  */
23
28
  async getRateLimits(): Promise<RateLimitResponse> {
24
- return makeRequest<RateLimitResponse>(
29
+ return makeRequest<RateLimitResponse, never>(
25
30
  'GET',
26
31
  '/api/rate-limit',
27
32
  this.options,
@@ -34,10 +39,12 @@ export class SystemApi {
34
39
  * @returns A promise resolving with the endpoint rate limit response
35
40
  */
36
41
  async getEndpointRateLimit(endpoint: string): Promise<EndpointRateLimitResponse> {
37
- return makeRequest<EndpointRateLimitResponse>(
42
+ return makeRequest<EndpointRateLimitResponse, never, RateLimitEndpointParam>(
38
43
  'GET',
39
44
  `/api/rate-limit/${endpoint}`,
40
45
  this.options,
46
+ undefined,
47
+ { endpoint },
41
48
  );
42
49
  }
43
50
 
@@ -45,8 +52,8 @@ export class SystemApi {
45
52
  * Gets the health status of the API
46
53
  * @returns A promise resolving with the health status
47
54
  */
48
- async getHealthStatus(): Promise<ApiResponse<{ status: string }>> {
49
- return makeRequest<ApiResponse<{ status: string }>>(
55
+ async getHealthStatus(): Promise<HealthStatus> {
56
+ return makeRequest<HealthStatus, never>(
50
57
  'GET',
51
58
  '/health',
52
59
  this.options,
@@ -43,17 +43,56 @@ export class CrosspostClient {
43
43
 
44
44
  /**
45
45
  * Sets the authentication data (signature) for the client
46
+ * Required for non-GET requests
46
47
  * @param nearAuthData The NEAR authentication data
47
48
  */
48
49
  public setAuthentication(nearAuthData: NearAuthData): void {
49
50
  this.options.nearAuthData = nearAuthData;
51
+ // Also set nearAccount from nearAuthData if not already set
52
+ if (!this.options.nearAccount) {
53
+ this.options.nearAccount = nearAuthData.account_id;
54
+ }
55
+ }
56
+
57
+ /**
58
+ * Sets the NEAR account ID for simplified GET request authentication
59
+ * If not set, will use account_id from nearAuthData
60
+ * @param nearAccount The NEAR account ID
61
+ */
62
+ public setNearAccount(nearAccount: string): void {
63
+ this.options.nearAccount = nearAccount;
64
+ }
65
+
66
+ /**
67
+ * Gets the current NEAR account ID being used for authentication
68
+ * @returns The NEAR account ID from nearAccount or nearAuthData
69
+ */
70
+ public getNearAccount(): string | undefined {
71
+ return this.options.nearAccount || this.options.nearAuthData?.account_id;
50
72
  }
51
73
 
52
74
  /**
53
75
  * Checks if authentication data (signature) exists on client
54
- * @param signature The NEAR authentication data
76
+ * @returns true if nearAuthData is set (required for non-GET requests)
55
77
  */
56
78
  public isAuthenticated(): boolean {
57
79
  return !!this.options.nearAuthData;
58
80
  }
81
+
82
+ /**
83
+ * Checks if a NEAR account is set for GET request authentication
84
+ * @returns true if either nearAccount or nearAuthData.account_id is set
85
+ */
86
+ public hasNearAccount(): boolean {
87
+ return !!(this.options.nearAccount || this.options.nearAuthData?.account_id);
88
+ }
89
+
90
+ /**
91
+ * Clears all authentication data from the client
92
+ * This will prevent all requests from working until new authentication is set
93
+ */
94
+ public clear(): void {
95
+ this.options.nearAuthData = undefined;
96
+ this.options.nearAccount = undefined;
97
+ }
59
98
  }
@@ -17,9 +17,14 @@ export interface RequestOptions {
17
17
  baseUrl: string;
18
18
  /**
19
19
  * NEAR authentication data for generating auth tokens
20
- * Can be undefined if not authorize yet
20
+ * Required for non-GET requests, optional for GET requests
21
21
  */
22
22
  nearAuthData?: NearAuthData;
23
+ /**
24
+ * NEAR account ID for simplified GET request authentication
25
+ * If not provided, will use account_id from nearAuthData
26
+ */
27
+ nearAccount?: string;
23
28
  /**
24
29
  * Request timeout in milliseconds
25
30
  */
@@ -39,17 +44,21 @@ export interface RequestOptions {
39
44
  * @param data Optional request data
40
45
  * @returns A promise resolving with the response data
41
46
  */
42
- export async function makeRequest<T>(
47
+ export async function makeRequest<
48
+ TResponse,
49
+ TRequest = unknown,
50
+ TQuery = unknown,
51
+ >(
43
52
  method: string,
44
53
  path: string,
45
54
  options: RequestOptions,
46
- data?: any,
47
- query?: Record<string, any>,
48
- ): Promise<T> {
55
+ data?: TRequest,
56
+ query?: TQuery,
57
+ ): Promise<TResponse> {
49
58
  let url = `${options.baseUrl}${path.startsWith('/') ? path : `/${path}`}`;
50
59
 
51
60
  // Add query parameters if provided
52
- if (query && Object.keys(query).length > 0) {
61
+ if (query && typeof query === 'object' && Object.keys(query).length > 0) {
53
62
  const queryParams = new URLSearchParams();
54
63
  for (const [key, value] of Object.entries(query)) {
55
64
  if (value !== undefined && value !== null) {
@@ -81,9 +90,31 @@ export async function makeRequest<T>(
81
90
  const headers: Record<string, string> = {
82
91
  'Content-Type': 'application/json',
83
92
  'Accept': 'application/json',
84
- 'Authorization': `Bearer ${createAuthToken(options.nearAuthData!)}`,
85
93
  };
86
94
 
95
+ // For GET requests, use X-Near-Account header if available
96
+ if (method === 'GET') {
97
+ const nearAccount = options.nearAccount || options.nearAuthData?.account_id;
98
+ if (!nearAccount) {
99
+ throw new CrosspostError(
100
+ 'No NEAR account provided for GET request',
101
+ ApiErrorCode.UNAUTHORIZED,
102
+ 401,
103
+ );
104
+ }
105
+ headers['X-Near-Account'] = nearAccount;
106
+ } else {
107
+ // For non-GET requests, require nearAuthData
108
+ if (!options.nearAuthData) {
109
+ throw new CrosspostError(
110
+ 'NEAR authentication data required for non-GET request',
111
+ ApiErrorCode.UNAUTHORIZED,
112
+ 401,
113
+ );
114
+ }
115
+ headers['Authorization'] = `Bearer ${createAuthToken(options.nearAuthData)}`;
116
+ }
117
+
87
118
  const requestOptions: RequestInit = {
88
119
  method,
89
120
  headers,
@@ -133,7 +164,7 @@ export async function makeRequest<T>(
133
164
  if (responseData && typeof responseData === 'object' && 'success' in responseData) {
134
165
  if (responseData.success) {
135
166
  // Success response - return the data
136
- return responseData.data as T;
167
+ return responseData.data as TResponse;
137
168
  } else {
138
169
  // Error response - handle with our error utilities
139
170
  lastError = handleErrorResponse(responseData, response.status);