@codybrom/denim 1.3.5 → 1.3.6

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.
Files changed (4) hide show
  1. package/deno.json +1 -1
  2. package/mod.ts +11 -26
  3. package/mod_test.ts +51 -0
  4. package/package.json +1 -1
package/deno.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@codybrom/denim",
3
- "version": "1.3.5",
3
+ "version": "1.3.6",
4
4
  "description": "A Deno function for posting to Threads.",
5
5
  "entry": "./mod.ts",
6
6
  "exports": {
package/mod.ts CHANGED
@@ -114,17 +114,9 @@ export async function createThreadsContainer(
114
114
  }
115
115
 
116
116
  const data = JSON.parse(responseText);
117
+ console.log(`Created container: ${data}`);
117
118
 
118
- // If getPermalink is true, fetch the permalink
119
- if (request.getPermalink) {
120
- const threadData = await getSingleThread(data.id, request.accessToken);
121
- return {
122
- id: data.id,
123
- permalink: threadData.permalink || "",
124
- };
125
- } else {
126
- return data.id;
127
- }
119
+ return data.id;
128
120
  } catch (error) {
129
121
  // Access error message safely
130
122
  const errorMessage =
@@ -385,22 +377,6 @@ export async function publishThreadsContainer(
385
377
 
386
378
  const publishData = await publishResponse.json();
387
379
 
388
- if (getPermalink) {
389
- const mediaId = publishData.id;
390
- const permalinkUrl = `${THREADS_API_BASE_URL}/${mediaId}?fields=permalink&access_token=${accessToken}`;
391
- const permalinkResponse = await fetch(permalinkUrl);
392
-
393
- if (permalinkResponse.ok) {
394
- const permalinkData = await permalinkResponse.json();
395
- return {
396
- id: mediaId,
397
- permalink: permalinkData.permalink,
398
- };
399
- } else {
400
- throw new Error("Failed to fetch permalink");
401
- }
402
- }
403
-
404
380
  // Check container status
405
381
  let status = await checkContainerStatus(containerId, accessToken);
406
382
  let attempts = 0;
@@ -426,6 +402,14 @@ export async function publishThreadsContainer(
426
402
  );
427
403
  }
428
404
 
405
+ if (getPermalink) {
406
+ const threadData = await getSingleThread(publishData.id, accessToken);
407
+ return {
408
+ id: publishData.id,
409
+ permalink: threadData.permalink || "",
410
+ };
411
+ }
412
+
429
413
  return publishData.id;
430
414
  } catch (error) {
431
415
  if (error instanceof Error) {
@@ -434,6 +418,7 @@ export async function publishThreadsContainer(
434
418
  throw error;
435
419
  }
436
420
  }
421
+
437
422
  /**
438
423
  * Serves HTTP requests to create and publish Threads posts.
439
424
  *
package/mod_test.ts CHANGED
@@ -250,6 +250,57 @@ Deno.test("Threads API", async (t) => {
250
250
  );
251
251
  teardownMockAPI();
252
252
  });
253
+
254
+ await t.step("should return permalink when requested", async () => {
255
+ setupMockAPI();
256
+ const userId = "12345";
257
+ const accessToken = "token";
258
+ const containerId = await createThreadsContainer({
259
+ userId,
260
+ accessToken,
261
+ mediaType: "TEXT",
262
+ text: "Test post with permalink",
263
+ });
264
+
265
+ const result = await publishThreadsContainer(
266
+ userId,
267
+ accessToken,
268
+ typeof containerId === "string" ? containerId : containerId.id,
269
+ true // Request permalink
270
+ );
271
+
272
+ if (typeof result === "string") {
273
+ throw new Error("Expected an object with permalink, but got a string");
274
+ } else {
275
+ assertEquals(typeof result, "object");
276
+ assertEquals(typeof result.id, "string");
277
+ assertEquals(typeof result.permalink, "string");
278
+ assertEquals(result.permalink.startsWith("https://"), true);
279
+ }
280
+ teardownMockAPI();
281
+ });
282
+
283
+ await t.step("should not return permalink when not requested", async () => {
284
+ setupMockAPI();
285
+ const userId = "12345";
286
+ const accessToken = "token";
287
+ const containerId = await createThreadsContainer({
288
+ userId,
289
+ accessToken,
290
+ mediaType: "TEXT",
291
+ text: "Test post without permalink",
292
+ });
293
+
294
+ const result = await publishThreadsContainer(
295
+ userId,
296
+ accessToken,
297
+ typeof containerId === "string" ? containerId : containerId.id,
298
+ false // Don't request permalink
299
+ );
300
+
301
+ assertEquals(typeof result, "string");
302
+ teardownMockAPI();
303
+ });
253
304
  });
254
305
 
255
306
  await t.step("createCarouselItem", async (t) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@codybrom/denim",
3
- "version": "1.3.5",
3
+ "version": "1.3.6",
4
4
  "description": "Typescript/Deno module to simplify posting to Threads",
5
5
  "main": "mod.ts",
6
6
  "directories": {