@codybrom/denim 1.3.0 → 1.3.1

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 +23 -57
  3. package/package.json +1 -1
  4. package/readme.md +4 -4
package/deno.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@codybrom/denim",
3
- "version": "1.3.0",
3
+ "version": "1.3.1",
4
4
  "description": "A Deno function for posting to Threads.",
5
5
  "entry": "./mod.ts",
6
6
  "exports": {
package/mod.ts CHANGED
@@ -70,7 +70,6 @@ export async function createThreadsContainer(
70
70
 
71
71
  // Add common optional parameters
72
72
  if (request.text) body.append("text", request.text);
73
- if (request.altText) body.append("alt_text", request.altText);
74
73
  if (request.replyControl) body.append("reply_control", request.replyControl);
75
74
  if (request.allowlistedCountryCodes) {
76
75
  body.append(
@@ -80,16 +79,29 @@ export async function createThreadsContainer(
80
79
  }
81
80
 
82
81
  // Handle media type specific parameters
83
- if (request.mediaType === "VIDEO") {
84
- const videoItemId = await createVideoItemContainer(request);
85
- body.set("media_type", "CAROUSEL");
86
- body.append("children", videoItemId);
87
- } else if (request.mediaType === "IMAGE" && request.imageUrl) {
88
- body.append("image_url", request.imageUrl);
89
- } else if (request.mediaType === "TEXT" && request.linkAttachment) {
90
- body.append("link_attachment", request.linkAttachment);
91
- } else if (request.mediaType === "CAROUSEL" && request.children) {
92
- body.append("children", request.children.join(","));
82
+ switch (request.mediaType) {
83
+ case "VIDEO":
84
+ if (!request.videoUrl)
85
+ throw new Error("videoUrl is required for VIDEO media type");
86
+ body.append("video_url", request.videoUrl);
87
+ if (request.altText) body.append("alt_text", request.altText);
88
+ break;
89
+ case "IMAGE":
90
+ if (!request.imageUrl)
91
+ throw new Error("imageUrl is required for IMAGE media type");
92
+ body.append("image_url", request.imageUrl);
93
+ if (request.altText) body.append("alt_text", request.altText);
94
+ break;
95
+ case "TEXT":
96
+ if (request.linkAttachment)
97
+ body.append("link_attachment", request.linkAttachment);
98
+ break;
99
+ case "CAROUSEL":
100
+ if (!request.children || request.children.length < 2) {
101
+ throw new Error("CAROUSEL media type requires at least 2 children");
102
+ }
103
+ body.append("children", request.children.join(","));
104
+ break;
93
105
  }
94
106
 
95
107
  console.log(`Sending request to: ${url}`);
@@ -149,9 +161,6 @@ function validateRequest(request: ThreadsPostRequest): void {
149
161
  if (request.mediaType !== "IMAGE" && request.imageUrl) {
150
162
  throw new Error("imageUrl can only be used with IMAGE media type");
151
163
  }
152
- if (request.mediaType !== "VIDEO" && request.videoUrl) {
153
- throw new Error("videoUrl can only be used with VIDEO media type");
154
- }
155
164
  if (request.mediaType !== "TEXT" && request.linkAttachment) {
156
165
  throw new Error("linkAttachment can only be used with TEXT media type");
157
166
  }
@@ -166,49 +175,6 @@ function validateRequest(request: ThreadsPostRequest): void {
166
175
  }
167
176
  }
168
177
 
169
- /**
170
- * Creates a video item container for Threads.
171
- * @param request - The ThreadsPostRequest object containing video post details
172
- * @returns A Promise that resolves to the video item container ID
173
- * @throws Will throw an error if the API request fails
174
- */
175
- async function createVideoItemContainer(
176
- request: ThreadsPostRequest
177
- ): Promise<string> {
178
- const url = `${THREADS_API_BASE_URL}/${request.userId}/threads`;
179
- const body = new URLSearchParams({
180
- access_token: request.accessToken,
181
- is_carousel_item: "true",
182
- media_type: "VIDEO",
183
- video_url: request.videoUrl!,
184
- ...(request.altText && { alt_text: request.altText }),
185
- });
186
-
187
- const response = await fetch(url, {
188
- method: "POST",
189
- body: body,
190
- headers: {
191
- "Content-Type": "application/x-www-form-urlencoded",
192
- },
193
- });
194
-
195
- const responseText = await response.text();
196
-
197
- if (!response.ok) {
198
- throw new Error(
199
- `Failed to create video item container: ${response.statusText}. Details: ${responseText}`
200
- );
201
- }
202
-
203
- try {
204
- const data = JSON.parse(responseText);
205
- return data.id;
206
- } catch (error) {
207
- console.error(`Failed to parse response JSON: ${error}`);
208
- throw new Error(`Invalid response from Threads API: ${responseText}`);
209
- }
210
- }
211
-
212
178
  /**
213
179
  * Creates a carousel item for a Threads carousel post.
214
180
  *
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@codybrom/denim",
3
- "version": "1.3.0",
3
+ "version": "1.3.1",
4
4
  "description": "Typescript/Deno module to simplify posting to Threads with text, images, or videos",
5
5
  "main": "mod.ts",
6
6
  "directories": {
package/readme.md CHANGED
@@ -32,13 +32,13 @@ This will add the latest version of Denim to your project's dependencies.
32
32
  To import straight from JSR:
33
33
 
34
34
  ```typescript
35
- import { ThreadsPostRequest, createThreadsContainer, publishThreadsContainer } from 'jsr:@codybrom/denim@^1.3.0';
35
+ import { ThreadsPostRequest, createThreadsContainer, publishThreadsContainer } from 'jsr:@codybrom/denim';
36
36
  ```
37
37
 
38
38
  ### Basic Usage
39
39
 
40
40
  ```typescript
41
- import { createThreadsContainer, publishThreadsContainer, ThreadsPostRequest } from "jsr:@codybrom/denim@^1.3.0";
41
+ import { createThreadsContainer, publishThreadsContainer, ThreadsPostRequest } from "jsr:@codybrom/denim";
42
42
 
43
43
  const request: ThreadsPostRequest = {
44
44
  userId: "YOUR_USER_ID",
@@ -61,7 +61,7 @@ console.log(`Post published with ID: ${publishedId}`);
61
61
  #### Retrieving Publishing Rate Limit
62
62
 
63
63
  ```typescript
64
- import { getPublishingLimit } from "jsr:@codybrom/denim@^1.3.0";
64
+ import { getPublishingLimit } from "jsr:@codybrom/denim";
65
65
 
66
66
  const userId = "YOUR_USER_ID";
67
67
  const accessToken = "YOUR_ACCESS_TOKEN";
@@ -144,7 +144,7 @@ const videoRequest: ThreadsPostRequest = {
144
144
  #### Carousel Post
145
145
 
146
146
  ```typescript
147
- import { createCarouselItem, createThreadsContainer, publishThreadsContainer, ThreadsPostRequest } from "jsr:@codybrom/denim@^1.0.4";
147
+ import { createCarouselItem, createThreadsContainer, publishThreadsContainer, ThreadsPostRequest } from "jsr:@codybrom/denim";
148
148
 
149
149
  // First, create carousel items
150
150
  const item1Id = await createCarouselItem({