npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

operand-facebook-sdk

v1.1.0

Published

## Get Started

Downloads

268

Readme

Welcome to the best SDK for communicating with api's meta

Get Started

First, install lib:

  npm install operand-facebook-sdk@latest

Next, import with ES6 or CommonJS:

  • ES6
import { MetaPage } from 'operand-facebook-sdk';

(async () => {
	const page = new MetaPage({
		apiVersion: 'v21.0',
		pageId: 'your-page-id',
		pageAccessToken: 'your-page-access-token',
	})

	const pageData = await page.getAllPosts()

	console.log({ pageData });
})()
  • CommonJS
const OPSDK = require("operand-facebook-sdk");

(async () => {
	const page = new OPSDK.MetaPage({
		apiVersion: 'v21.0',
		pageId: 'your-page-id',
		pageAccessToken: 'your-page-access-token',
	})

	const pageData = await page.getAllPosts()

	console.log({ pageData });
})()

Documentation

Classes

MetaAuth

Provides authentication for generating access tokens for the Meta API.

Methods

  • createAccessToken(options: CreateMetaAuth): Promise<string>
    • Description: Generates an access token for accessing Meta API resources.
    • Parameters:
      • client_id: The application’s client ID.
      • client_secret: The application’s client secret.
      • redirect_uri: Redirect URI after the user grants permissions.
      • apiVersion: API version to use.
      • code: The authorization code from the OAuth process.
    • Returns: A Promise that resolves with the access token as a string.

MetaPage

Handles operations for managing Meta pages, including posting, updating, and deleting posts, as well as creating stories.

Constructor
  • Parameters:
    • pageAccessToken: Access token for the page.
    • pageId: ID of the page to manage.
    • apiVersion: Version of the API to use.
Methods
  • getAccounts(options: GetAccounts): Promise<any>

    • Description: Retrieves accounts associated with the page.
    • Parameters:
      • fields: Array of fields to include in the response.
    • Returns: An Array of account objects.
  • getAllPosts(): Promise<PagePost[]>

    • Description: Fetches all posts on the page.
    • Returns: A list of PagePost objects.
  • getPostUrlById(postId: string): string

    • Description: Generates a URL for a post by its ID.
    • Parameters:
      • postId: ID of the post.
    • Returns: The URL of the post.
  • createPost(post: CreatePost): Promise<string>

    • Description: Creates a post on the page, either text, photo, or video.
    • Parameters:
      • post: Post configuration object, including message, publication time, and media type.
    • Returns: The ID of the created post.
  • updatePost(postId: string, message: string): Promise<boolean>

    • Description: Updates the content of a post.
    • Parameters:
      • postId: ID of the post to update.
      • message: New content for the post.
    • Returns: Boolean indicating success.
  • deletePost(postId: string): Promise<boolean>

    • Description: Deletes a post by ID.
    • Parameters:
      • postId: ID of the post to delete.
    • Returns: Boolean indicating success.
  • createStories(story: CreateStories): Promise<string>

    • Description: Creates a story for the page, either photo or video.
    • Parameters:
      • story: Story configuration including media type and source.
    • Returns: The ID of the created story.
  • createReels(reel: CreateReels): Promise<string>

    • Description: Creates a reels for the page, only video.
    • Parameters:
      • reel: Reel configuration including media mediaSource to video.
    • Returns: The ID of the created reel.
Private Methods
  • fileTypesPermitted(file: "video" | "photo", type: string): boolean

    • Description: Checks if the file type is permitted.
  • verifyPhotoSize(value: string | Buffer, isBuffer: boolean): Promise<boolean>

    • Description: Checks if the photo size is within allowed limits.
  • validatePublishDate(datePublish: Date): boolean

    • Description: Validates that the publish date is within 10 minutes to 6 months from now.
  • savePhotoInMetaStorageByUrl(url: string): Promise<string>

    • Description: Saves a photo in Meta’s storage using a URL.
  • savePhotoInMetaStorageByPath(path: string): Promise<string>

    • Description: Saves a photo in Meta’s storage using a file path.
  • saveVideoInMetaStorageMomentaryByUrl(video: string): Promise<string>

    • Description: Saves a video momentarily in Meta’s storage by URL.
  • uploadPhotos(photos: Array<{ source: string; value: string }>): Promise<string[]>

    • Description: Uploads multiple photos to Meta’s storage.
  • createTextPost(message: string, publishNow: boolean, datePublish?: Date): Promise<string>

    • Description: Creates a text-only post on the page.
  • createMediaPost(message: string, mediaIds: string[], publishNow: boolean, datePublish?: Date): Promise<string>

    • Description: Creates a media post on the page with specified media IDs.
  • uploadVideo(video: { source: string; value: string }, message: string, publishNow: boolean, datePublish?: Date): Promise<string>

    • Description: Uploads a video to Meta’s storage, supporting both immediate and scheduled publishing.
  • createPhotoStory(story: CreateStories): Promise<string>

    • Description: Creates a photo story on the page.
  • createVideoStory(story: CreateStories): Promise<string>

    • Description: Creates a video story on the page.

Interfaces

Meta

export interface PagePost {
  created_time: string;
  message: string;
  id: string;
}

export interface PaginationCursors {
  before: string;
  after: string;
}

export interface PaginationInfo {
  cursors: PaginationCursors;
}

export interface GetPagePostsResponse {
  data: PagePost[];
  paging: PaginationInfo;
}

export interface CreatePagePostResponse {
  id: string;
  post_id?: string;
}

export interface UpdatePagePostResponse {
  success: boolean;
}

export interface DeletePagePostResponse {
  success: boolean;
}

export interface SaveMediaStorageResponse {
  id: string;
}

export interface CreatePhotoStoriesResponse {
  success: boolean;
  post_id: string;
}

export interface CreateAccessTokenResponse {
  access_token: string;
  token_type: string;
}

export interface GetPageAccountsResponse {
  data: Array<{
    id: string;
    name?: string;
    about?: string;
    category?: string;
    category_list?: Array<{ id: string; name: string }>;
    location?: {
      city?: string;
      country?: string;
      latitude?: number;
      longitude?: number;
      street?: string;
      zip?: string;
    };
    fan_count?: number;
    access_token?: string;
    tasks?: string[];
    picture?: {
      data: {
        url: string;
        width?: number;
        height?: number;
        is_silhouette?: boolean;
      };
    };
    cover?: {
      id: string;
      source: string;
      offset_y?: number;
      offset_x?: number;
    };
    photos?: {
      data: Array<{
        id: string;
        name?: string;
        created_time?: string;
        picture?: string;
      }>;
    };
    videos?: {
      data: Array<{
        id: string;
        title?: string;
        description?: string;
        picture?: string;
        source?: string;
      }>;
    };
    engagement?: {
      count?: number;
      social_sentence?: string;
    };
    is_published?: boolean;
    is_verified?: boolean;
    verification_status?: string;
    website?: string;
    emails?: string[];
    phone?: string;
    instagram_business_account?: { id: string };
    hours?: {
      monday?: string;
      tuesday?: string;
      wednesday?: string;
      thursday?: string;
      friday?: string;
      saturday?: string;
      sunday?: string;
    };
    created_time?: string;
    bio?: string;
    link?: string;
    business?: {
      id: string;
      name: string;
    };
  }>;
}

export interface CreateStartVideoUploadResponse {
  video_id: string;
  upload_url: string;
}

export interface CreateLoadingVideoUploadResponse {
  success: boolean;
}

export interface CreateFinishVideoUploadResponse {
  success: boolean;
  message: string;
  post_id: string;
}

Main

import { ApiVersion } from "./meta-auth";
import { PagePost } from "./meta-response";

export type ConstructorMain = {
  pageId: string;
  pageAccessToken: string;
  apiVersion: ApiVersion;
};

type PhotoMediaItem = {
  source: "url" | "path";
  value: string;
};

type VideoMediaItem = {
  source: "url" | "path";
  value: string;
};

export type CreatePost = {
  mediaType?: "photo" | "video";
  message?: string;
  publishNow: boolean;
  datePublish?: Date;
  photos?: PhotoMediaItem[];
  video?: VideoMediaItem;
};

type CreateStoriesPath = {
  mediaSource: "local";
  mediaType: "photo" | "video";
  path: string;
};

type CreateStoriesUrl = {
  mediaSource: "url";
  mediaType: "photo" | "video";
  url: string;
};

export type CreateStories = CreateStoriesPath | CreateStoriesUrl;

type CreateReelsPath = {
  mediaSource: "local";
  path: string;
  title?: string;
  description?: string;
};

type CreateReelsUrl = {
  mediaSource: "url";
  url: string;
  title?: string;
  description?: string;
};

export type CreateReels = CreateReelsPath | CreateReelsUrl;

export interface IMetaPage {
  getAllPosts(): Promise<PagePost[]>;
  getPostUrlById(postId: string): string;
  createPost(data: CreatePost): Promise<string>;
  updatePost(postId: string, message: string): Promise<boolean>;
  deletePost(postId: string): Promise<boolean>;
  createStories(data: CreateStories): Promise<string>;
}

Auth

export type ApiVersion = "v17.0" | "v18.0" | "v19.0" | "v20.0" | "v21.0";

export type CreateMetaAuth = {
  client_id: string;
  client_secret: string;
  redirect_uri?: string;
  apiVersion: ApiVersion;
  code: string;
};

export type FieldsPage = Array<
  | "id"
  | "name"
  | "about"
  | "category"
  | "category_list"
  | "location"
  | "fan_count"
  | "access_token"
  | "tasks"
  | "picture"
  | "cover"
  | "photos"
  | "videos"
  | "engagement"
  | "is_published"
  | "is_verified"
  | "verification_status"
  | "website"
  | "emails"
  | "phone"
  | "instagram_business_account"
  | "hours"
  | "created_time"
  | "bio"
  | "link"
  | "business"
  | string
>;

export type GetAccounts = {
  fields: FieldsPage;
  accessToken: string;
};