diff --git a/mod.ts b/mod.ts index 761f249..d0fc473 100644 --- a/mod.ts +++ b/mod.ts @@ -114,17 +114,9 @@ export async function createThreadsContainer( } const data = JSON.parse(responseText); + console.log(`Created container: ${data}`); - // If getPermalink is true, fetch the permalink - if (request.getPermalink) { - const threadData = await getSingleThread(data.id, request.accessToken); - return { - id: data.id, - permalink: threadData.permalink || "", - }; - } else { - return data.id; - } + return data.id; } catch (error) { // Access error message safely const errorMessage = @@ -385,22 +377,6 @@ export async function publishThreadsContainer( const publishData = await publishResponse.json(); - if (getPermalink) { - const mediaId = publishData.id; - const permalinkUrl = `${THREADS_API_BASE_URL}/${mediaId}?fields=permalink&access_token=${accessToken}`; - const permalinkResponse = await fetch(permalinkUrl); - - if (permalinkResponse.ok) { - const permalinkData = await permalinkResponse.json(); - return { - id: mediaId, - permalink: permalinkData.permalink, - }; - } else { - throw new Error("Failed to fetch permalink"); - } - } - // Check container status let status = await checkContainerStatus(containerId, accessToken); let attempts = 0; @@ -426,6 +402,14 @@ export async function publishThreadsContainer( ); } + if (getPermalink) { + const threadData = await getSingleThread(publishData.id, accessToken); + return { + id: publishData.id, + permalink: threadData.permalink || "", + }; + } + return publishData.id; } catch (error) { if (error instanceof Error) { @@ -434,6 +418,7 @@ export async function publishThreadsContainer( throw error; } } + /** * Serves HTTP requests to create and publish Threads posts. * diff --git a/mod_test.ts b/mod_test.ts index 990dcbb..995e319 100644 --- a/mod_test.ts +++ b/mod_test.ts @@ -250,6 +250,57 @@ Deno.test("Threads API", async (t) => { ); teardownMockAPI(); }); + + await t.step("should return permalink when requested", async () => { + setupMockAPI(); + const userId = "12345"; + const accessToken = "token"; + const containerId = await createThreadsContainer({ + userId, + accessToken, + mediaType: "TEXT", + text: "Test post with permalink", + }); + + const result = await publishThreadsContainer( + userId, + accessToken, + typeof containerId === "string" ? containerId : containerId.id, + true // Request permalink + ); + + if (typeof result === "string") { + throw new Error("Expected an object with permalink, but got a string"); + } else { + assertEquals(typeof result, "object"); + assertEquals(typeof result.id, "string"); + assertEquals(typeof result.permalink, "string"); + assertEquals(result.permalink.startsWith("https://"), true); + } + teardownMockAPI(); + }); + + await t.step("should not return permalink when not requested", async () => { + setupMockAPI(); + const userId = "12345"; + const accessToken = "token"; + const containerId = await createThreadsContainer({ + userId, + accessToken, + mediaType: "TEXT", + text: "Test post without permalink", + }); + + const result = await publishThreadsContainer( + userId, + accessToken, + typeof containerId === "string" ? containerId : containerId.id, + false // Don't request permalink + ); + + assertEquals(typeof result, "string"); + teardownMockAPI(); + }); }); await t.step("createCarouselItem", async (t) => {