@codybrom/denim 1.3.2 → 1.3.3
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/deno.json +1 -1
- package/mod.ts +23 -14
- package/package.json +1 -1
package/deno.json
CHANGED
package/mod.ts
CHANGED
|
@@ -58,7 +58,7 @@ export interface ThreadsPostRequest {
|
|
|
58
58
|
*/
|
|
59
59
|
export async function createThreadsContainer(
|
|
60
60
|
request: ThreadsPostRequest
|
|
61
|
-
): Promise<{ containerId:
|
|
61
|
+
): Promise<{ containerId: number; mediaType: string }> {
|
|
62
62
|
// Input validation
|
|
63
63
|
validateRequest(request);
|
|
64
64
|
|
|
@@ -111,7 +111,7 @@ export async function createThreadsContainer(
|
|
|
111
111
|
|
|
112
112
|
try {
|
|
113
113
|
const data = JSON.parse(responseText);
|
|
114
|
-
return { containerId: data.id, mediaType: request.mediaType };
|
|
114
|
+
return { containerId: parseInt(data.id, 10), mediaType: request.mediaType };
|
|
115
115
|
} catch (error) {
|
|
116
116
|
console.error(`Failed to parse response JSON: ${error}`);
|
|
117
117
|
throw new Error(`Invalid response from Threads API: ${responseText}`);
|
|
@@ -272,7 +272,7 @@ async function checkContainerStatus(
|
|
|
272
272
|
*
|
|
273
273
|
* @param userId - The user ID of the Threads account
|
|
274
274
|
* @param accessToken - The access token for authentication
|
|
275
|
-
* @param containerId - The ID of the container to publish
|
|
275
|
+
* @param containerId - The ID of the container to publish (as a number)
|
|
276
276
|
* @param mediaType - The type of media being published
|
|
277
277
|
* @returns A Promise that resolves to the published container ID
|
|
278
278
|
* @throws Will throw an error if the API request fails, if publishing times out, or if the container is not ready (for videos)
|
|
@@ -280,24 +280,27 @@ async function checkContainerStatus(
|
|
|
280
280
|
* @example
|
|
281
281
|
* ```typescript
|
|
282
282
|
* const { containerId, mediaType } = await createThreadsContainer(request);
|
|
283
|
-
* const publishedId = await publishThreadsContainer("123456", "your_access_token", containerId, mediaType);
|
|
283
|
+
* const publishedId = await publishThreadsContainer("123456", "your_access_token", parseInt(containerId, 10), mediaType);
|
|
284
284
|
* ```
|
|
285
285
|
*/
|
|
286
286
|
export async function publishThreadsContainer(
|
|
287
287
|
userId: string,
|
|
288
288
|
accessToken: string,
|
|
289
|
-
containerId:
|
|
289
|
+
containerId: number,
|
|
290
290
|
mediaType: string
|
|
291
|
-
): Promise<
|
|
291
|
+
): Promise<number> {
|
|
292
292
|
if (mediaType === "VIDEO") {
|
|
293
293
|
// Check container status before publishing for videos
|
|
294
|
-
let status = await checkContainerStatus(
|
|
294
|
+
let status = await checkContainerStatus(
|
|
295
|
+
containerId.toString(),
|
|
296
|
+
accessToken
|
|
297
|
+
);
|
|
295
298
|
let attempts = 0;
|
|
296
299
|
const maxAttempts = 10; // Increased from 5 to 10 for videos
|
|
297
300
|
|
|
298
301
|
while (status !== "FINISHED" && attempts < maxAttempts) {
|
|
299
302
|
await new Promise((resolve) => setTimeout(resolve, 30000)); // Wait for 30 seconds
|
|
300
|
-
status = await checkContainerStatus(containerId, accessToken);
|
|
303
|
+
status = await checkContainerStatus(containerId.toString(), accessToken);
|
|
301
304
|
attempts++;
|
|
302
305
|
console.log(
|
|
303
306
|
`Video container status check attempt ${attempts}: ${status}`
|
|
@@ -314,7 +317,7 @@ export async function publishThreadsContainer(
|
|
|
314
317
|
const publishUrl = `${THREADS_API_BASE_URL}/${userId}/threads_publish`;
|
|
315
318
|
const publishBody = new URLSearchParams({
|
|
316
319
|
access_token: accessToken,
|
|
317
|
-
creation_id: containerId,
|
|
320
|
+
creation_id: containerId.toString(), // Convert to string for URLSearchParams, but it will be sent as a number
|
|
318
321
|
});
|
|
319
322
|
|
|
320
323
|
const publishResponse = await fetch(publishUrl, {
|
|
@@ -336,7 +339,10 @@ export async function publishThreadsContainer(
|
|
|
336
339
|
|
|
337
340
|
// For non-video posts, we still need to check if the post was actually published
|
|
338
341
|
if (mediaType !== "VIDEO") {
|
|
339
|
-
let status = await checkContainerStatus(
|
|
342
|
+
let status = await checkContainerStatus(
|
|
343
|
+
containerId.toString(),
|
|
344
|
+
accessToken
|
|
345
|
+
);
|
|
340
346
|
let attempts = 0;
|
|
341
347
|
const maxAttempts = 5;
|
|
342
348
|
|
|
@@ -346,7 +352,7 @@ export async function publishThreadsContainer(
|
|
|
346
352
|
attempts < maxAttempts
|
|
347
353
|
) {
|
|
348
354
|
await new Promise((resolve) => setTimeout(resolve, 60000)); // Wait for 1 minute
|
|
349
|
-
status = await checkContainerStatus(containerId, accessToken);
|
|
355
|
+
status = await checkContainerStatus(containerId.toString(), accessToken);
|
|
350
356
|
attempts++;
|
|
351
357
|
}
|
|
352
358
|
|
|
@@ -414,9 +420,12 @@ export function serveRequests() {
|
|
|
414
420
|
mediaType
|
|
415
421
|
);
|
|
416
422
|
|
|
417
|
-
return new Response(
|
|
418
|
-
|
|
419
|
-
|
|
423
|
+
return new Response(
|
|
424
|
+
JSON.stringify({ success: true, publishedId: publishedId.toString() }),
|
|
425
|
+
{
|
|
426
|
+
headers: { "Content-Type": "application/json" },
|
|
427
|
+
}
|
|
428
|
+
);
|
|
420
429
|
} catch (error) {
|
|
421
430
|
console.error("Error posting to Threads:", error);
|
|
422
431
|
return new Response(
|