threads-graph-api
Advanced tools
Comparing version 0.0.4 to 0.0.5
import { z } from 'zod'; | ||
import { CreateMediaContainerParams, CreateMediaContainerResponse, ExchangeAuthenticationCodeResponse, GetAccountMetricsParams, GetAccountMetricsResponse, GetConversationParams, GetConversationResponse, GetMediaMetricsParams, GetMediaMetricsResponse, GetMediaObjectParams, GetMediaObjectResponse, GetRepliesParams, GetRepliesResponse, GetUserProfileParams, GetUserProfileResponse, GetUserThreadsParams, GetUserThreadsPublishingLimitParams, GetUserThreadsPublishingLimitResponse, GetUserThreadsResponse, ManageReplyParams, ManageReplyResponse, PublishParams, PublishResponse } from './types'; | ||
import { CreateMediaContainerParams, CreateMediaContainerResponse, ErrorResponse, ExchangeAuthorizationCodeResponse, GetAccountMetricsParams, GetAccountMetricsResponse, GetConversationParams, GetConversationResponse, GetMediaMetricsParams, GetMediaMetricsResponse, GetMediaObjectParams, GetMediaObjectResponse, GetRepliesParams, GetRepliesResponse, GetUserProfileParams, GetUserProfileResponse, GetUserThreadsParams, GetUserThreadsPublishingLimitParams, GetUserThreadsPublishingLimitResponse, GetUserThreadsResponse, ManageReplyParams, ManageReplyResponse, PublishParams, PublishResponse } from './types'; | ||
export declare class ThreadsApiError extends Error { | ||
private readonly _error?; | ||
constructor(error?: ErrorResponse); | ||
getThreadsError(): { | ||
error: { | ||
code?: number | undefined; | ||
message?: string | undefined; | ||
type?: string | undefined; | ||
fbtrace_id?: string | undefined; | ||
}; | ||
} | undefined; | ||
} | ||
export declare class ThreadsPublicApiClient { | ||
@@ -10,3 +22,3 @@ private readonly _baseUrl; | ||
createAuthorizationUrl(clientId: string, redirectUri: string, scope: string[], state?: string, baseUrl?: string): string; | ||
exchangeAuthorizationCode(clientId: string, clientSecret: string, redirectUri: string, code: string): Promise<ExchangeAuthenticationCodeResponse>; | ||
exchangeAuthorizationCode(clientId: string, clientSecret: string, redirectUri: string, code: string): Promise<ExchangeAuthorizationCodeResponse>; | ||
} | ||
@@ -13,0 +25,0 @@ export declare class ThreadsAuthenticatedApiClient extends ThreadsPublicApiClient { |
@@ -74,3 +74,25 @@ var __extends = (this && this.__extends) || (function () { | ||
}; | ||
import { CreateMediaContainerResponseSchema, ExchangeAuthenticationCodeResponseSchema, GetAccountMetricsResponseSchema, GetConversationResponseSchema, GetMediaMetricsResponseSchema, GetMediaObjectResponseSchema, GetRepliesResponseSchema, GetUserProfileResponseSchema, GetUserThreadsPublishingLimitResponseSchema, GetUserThreadsResponseSchema, ManageReplyResponseSchema, PublishResponseSchema, } from './types'; | ||
import { CreateMediaContainerResponseSchema, ErrorResponseSchema, ExchangeAuthorizationCodeResponseSchema, GetAccountMetricsResponseSchema, GetConversationResponseSchema, GetMediaMetricsResponseSchema, GetMediaObjectResponseSchema, GetRepliesResponseSchema, GetUserProfileResponseSchema, GetUserThreadsPublishingLimitResponseSchema, GetUserThreadsResponseSchema, ManageReplyResponseSchema, PublishResponseSchema, } from './types'; | ||
var ThreadsApiError = /** @class */ (function (_super) { | ||
__extends(ThreadsApiError, _super); | ||
function ThreadsApiError(error) { | ||
var _newTarget = this.constructor; | ||
var _this = _super.call(this, (error === null || error === void 0 ? void 0 : error.error.message) || 'An unknown error occurred') || this; | ||
_this._error = error; | ||
var actualProto = _newTarget.prototype; | ||
if (Object.setPrototypeOf) { | ||
Object.setPrototypeOf(_this, actualProto); | ||
} | ||
else { | ||
// @ts-ignore | ||
_this.__proto__ = actualProto; | ||
} | ||
return _this; | ||
} | ||
ThreadsApiError.prototype.getThreadsError = function () { | ||
return this._error; | ||
}; | ||
return ThreadsApiError; | ||
}(Error)); | ||
export { ThreadsApiError }; | ||
var ThreadsPublicApiClient = /** @class */ (function () { | ||
@@ -86,3 +108,3 @@ function ThreadsPublicApiClient(baseUrl) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var filteredParams, apiUrl, response, json; | ||
var filteredParams, apiUrl, response, json, error; | ||
return __generator(this, function (_a) { | ||
@@ -105,2 +127,6 @@ switch (_a.label) { | ||
json = _a.sent(); | ||
if (json.error) { | ||
error = ErrorResponseSchema.safeParse(json); | ||
throw new ThreadsApiError(error.success ? error.data : undefined); | ||
} | ||
return [2 /*return*/, responseSchema.parse(json)]; | ||
@@ -113,3 +139,3 @@ } | ||
return __awaiter(this, void 0, void 0, function () { | ||
var apiUrl, body, response, json; | ||
var apiUrl, body, response, json, error; | ||
return __generator(this, function (_a) { | ||
@@ -120,5 +146,3 @@ switch (_a.label) { | ||
body = new FormData(); | ||
Object.keys(params).forEach(function (key) { | ||
return body.append(key, params[key]); | ||
}); | ||
Object.keys(params).forEach(function (key) { return params[key] && body.append(key, params[key]); }); | ||
return [4 /*yield*/, fetch(apiUrl, { | ||
@@ -133,2 +157,6 @@ method: 'POST', | ||
json = _a.sent(); | ||
if (json.error) { | ||
error = ErrorResponseSchema.safeParse(json); | ||
throw new ThreadsApiError(error.success ? error.data : undefined); | ||
} | ||
return [2 /*return*/, responseSchema.parse(json)]; | ||
@@ -167,3 +195,3 @@ } | ||
json = _a.sent(); | ||
return [2 /*return*/, ExchangeAuthenticationCodeResponseSchema.parse(json)]; | ||
return [2 /*return*/, ExchangeAuthorizationCodeResponseSchema.parse(json)]; | ||
} | ||
@@ -170,0 +198,0 @@ }); |
@@ -26,3 +26,36 @@ import { z } from 'zod'; | ||
export type SuccessResponse = z.infer<typeof SuccessResponseSchema>; | ||
export declare const ExchangeAuthenticationCodeResponseSchema: z.ZodObject<{ | ||
export declare const ErrorResponseSchema: z.ZodObject<{ | ||
error: z.ZodObject<{ | ||
code: z.ZodOptional<z.ZodNumber>; | ||
fbtrace_id: z.ZodOptional<z.ZodString>; | ||
message: z.ZodOptional<z.ZodString>; | ||
type: z.ZodOptional<z.ZodString>; | ||
}, "strip", z.ZodTypeAny, { | ||
code?: number | undefined; | ||
message?: string | undefined; | ||
type?: string | undefined; | ||
fbtrace_id?: string | undefined; | ||
}, { | ||
code?: number | undefined; | ||
message?: string | undefined; | ||
type?: string | undefined; | ||
fbtrace_id?: string | undefined; | ||
}>; | ||
}, "strip", z.ZodTypeAny, { | ||
error: { | ||
code?: number | undefined; | ||
message?: string | undefined; | ||
type?: string | undefined; | ||
fbtrace_id?: string | undefined; | ||
}; | ||
}, { | ||
error: { | ||
code?: number | undefined; | ||
message?: string | undefined; | ||
type?: string | undefined; | ||
fbtrace_id?: string | undefined; | ||
}; | ||
}>; | ||
export type ErrorResponse = z.infer<typeof ErrorResponseSchema>; | ||
export declare const ExchangeAuthorizationCodeResponseSchema: z.ZodObject<{ | ||
access_token: z.ZodString; | ||
@@ -37,3 +70,3 @@ user_id: z.ZodString; | ||
}>; | ||
export type ExchangeAuthenticationCodeResponse = z.infer<typeof ExchangeAuthenticationCodeResponseSchema>; | ||
export type ExchangeAuthorizationCodeResponse = z.infer<typeof ExchangeAuthorizationCodeResponseSchema>; | ||
export type CreateMediaContainerParams = { | ||
@@ -40,0 +73,0 @@ replyToId?: string; |
@@ -16,3 +16,11 @@ import { z } from 'zod'; | ||
}); | ||
export var ExchangeAuthenticationCodeResponseSchema = z.object({ | ||
export var ErrorResponseSchema = z.object({ | ||
error: z.object({ | ||
code: z.number().optional(), | ||
fbtrace_id: z.string().optional(), | ||
message: z.string().optional(), | ||
type: z.string().optional(), | ||
}), | ||
}); | ||
export var ExchangeAuthorizationCodeResponseSchema = z.object({ | ||
access_token: z.string(), | ||
@@ -27,16 +35,2 @@ user_id: z.coerce.string(), | ||
}); | ||
// | ||
// | 'id' | ||
// | 'media_product_type' | ||
// | 'media_type' | ||
// | 'media_url' | ||
// | 'permalink' | ||
// | 'owner' | ||
// | 'username' | ||
// | 'text' | ||
// | 'timestamp' | ||
// | 'shortcode' | ||
// | 'thumbnail_url' | ||
// | 'children' | ||
// | 'is_quote_post'; | ||
export var ThreadsMediaObjectSchema = z.object({ | ||
@@ -43,0 +37,0 @@ id: z.string().optional(), |
{ | ||
"name": "threads-graph-api", | ||
"repository": "github:spoolappio/threads-graph-api", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"description": "A library to interface with the official Instagram Threads API", | ||
@@ -6,0 +6,0 @@ "main": "dist/src/index.js", |
@@ -26,3 +26,3 @@ # threads-graph-api | ||
## Usage | ||
`threads-graph-api` follows the [official Threads API documentation](https://developers.facebook.com/docs/threads/reference) for endpoints and parameters. | ||
`threads-graph-api` follows the [official Threads API documentation](https://developers.facebook.com/docs/threads/reference) for endpoints and parameters. See [types.ts](src/types.ts) for all request and response schemas. | ||
@@ -99,3 +99,3 @@ ### Initializing the Client | ||
### Authenticated API | ||
### Using the API | ||
@@ -246,2 +246,17 @@ #### Creating a media container | ||
### Handling errors | ||
The `ThreadsApiError` class is thrown when the Threads API returns an error. The `message` field of the returned error is accessible from the `error` field on the `ThreadsApiError` object. To access other fields, use `getThreadsError()` to retrieve the full error object returned by the Threads API: | ||
```typescript | ||
try { | ||
const response = await authenticatedClient.getUserProfile(params); | ||
} catch (error) { | ||
if (error instanceof ThreadsApiError) { | ||
console.error('Threads API Error:', error.message, error.getThreadsError()); | ||
} else { | ||
console.error('Unexpected Error:', error); | ||
} | ||
} | ||
``` | ||
## Contributing | ||
@@ -248,0 +263,0 @@ |
@@ -8,4 +8,4 @@ import {z} from 'zod'; | ||
ErrorResponseSchema, | ||
ExchangeAuthenticationCodeResponse, | ||
ExchangeAuthenticationCodeResponseSchema, | ||
ExchangeAuthorizationCodeResponse, | ||
ExchangeAuthorizationCodeResponseSchema, | ||
GetAccountMetricsParams, | ||
@@ -150,3 +150,3 @@ GetAccountMetricsResponse, | ||
code: string, | ||
): Promise<ExchangeAuthenticationCodeResponse> { | ||
): Promise<ExchangeAuthorizationCodeResponse> { | ||
const formData = new FormData(); | ||
@@ -164,3 +164,3 @@ formData.append('client_id', clientId); | ||
const json = await response.json(); | ||
return ExchangeAuthenticationCodeResponseSchema.parse(json); | ||
return ExchangeAuthorizationCodeResponseSchema.parse(json); | ||
} | ||
@@ -167,0 +167,0 @@ } |
@@ -49,8 +49,8 @@ import {z} from 'zod'; | ||
export const ExchangeAuthenticationCodeResponseSchema = z.object({ | ||
export const ExchangeAuthorizationCodeResponseSchema = z.object({ | ||
access_token: z.string(), | ||
user_id: z.coerce.string(), | ||
}); | ||
export type ExchangeAuthenticationCodeResponse = z.infer< | ||
typeof ExchangeAuthenticationCodeResponseSchema | ||
export type ExchangeAuthorizationCodeResponse = z.infer< | ||
typeof ExchangeAuthorizationCodeResponseSchema | ||
>; | ||
@@ -57,0 +57,0 @@ |
103266
2627
266