1.3.3 • Published 3 years ago

fbsdk-ts v1.3.3

Weekly downloads
16
License
MIT
Repository
github
Last release
3 years ago

fbsdk-ts

Strongly-typed promise-based client library for Facebook's Graph API using TypeScript.

Where do I start?

How to use:

TypeScript is recommended to get all the benefits of this package, but you can also use plain JavaScript.

Step 0: Installing

To install this package in a Node.js project, run this command in the project root:

npm install -S fbsdk-ts

Step 1: Importing

If you are using plain JavaScript, import the FacebookApp class like so:

const { FacebookApp } = require('fbsdk-ts');

If you are using TypesScript, import the FacebookApp class like so:

import { FacebookApp } from 'fbsdk-ts';

Step 2: Initialization

FacebookApp is a class can be initialized like so:

const app = new FacebookApp({
    appId: '{YOUR-APP-ID}',
    appSecret: '{YOUR-APP-SECRET}',
});

*NOTE: Never use your app secret directly in your code, as this is a security risk! Environment variables are recommended for storing your app secret.*

Step 3: Reading Nodes and Edges

Once your app is initialized, you can read different nodes using the Node function:

// Get information about a page

app.Node('Page', '{SOME-PAGE-ID}')
    .read('{PAGE-ACCESS-TOKEN}')
    .then((pageInfo) => {
        // do stuff with page info
    });

A node may or may not also contain edges that can be accessed using the Edge function:

// Get conversations on page

app.Node('Page', '{SOME-PAGE-ID}')
    .Edge('Conversations').read('{PAGE-ACCESS-TOKEN}')
    .then((conversations) => {
        // do stuff with conversations
    });

*NOTE: As with your app secret, never use your access tokens directly in your code. Environment variables are again recommended for storing your access tokens.*

Step 4: Selecting Fields

By default, not all fields on a node are returned when it is read (however, the id field is always included). To select the fields that should be returned (in addition to id), you can pass in an array of field keys as the second parameter of the .read method:

// Get the category and follower count of a page

app.Node('Page', '{SOME-PAGE-ID}')
    .read('{PAGE-ACCESS-TOKEN}', ['category', 'followers_count'])
    .then((pageInfo) => {
        // do stuff with page info
    });

Step 5: Using Read Parameters

Sometimes, a node or edge may have parameters that can be used with a read operation. These may be passed in as the third parameter in the .read method (undefined may be used for the second parameter if you don't want to specify fields):

// Get a page's spam folder

app.Node('Page', '{SOME-PAGE-ID}')
    .Edge('Conversations').read('{PAGE-ACCESS-TOKEN}', undefined, {
        folder: 'spam',
    })
    .then((spamConversations) => {
        // do stuff with spam conversations
    });

How to extend / subclass:

This package is made to be extensible. You can create your own custom class like FacebookApp with its own methods to suit your needs.

Step 0: Installing

To install this package in a Node.js project, run this command in the project root:

npm install -S fbsdk-ts

Step 1: Importing

If you are using plain JavaScript, import the FacebookAppNoExposedNodes class like so:

const { FacebookAppNoExposedNodes } = require('fbsdk-ts');

If you are using TypesScript, import the FacebookAppNoExposedNodes class like so:

import { FacebookAppNoExposedNodes } from 'fbsdk-ts';

Step 2: Sub-classing

FacebookAppNoExposedNodes is an abstract class, and the parent class of the FacebookApp class used in the previous examples. Both classes are virtually identical, except that FacebookApp exposes the nodes function on .Node, while FacebookAppNoExposedNodes keeps the nodes function on the protected member _Node. If you want to make a subclass that does not expose the nodes function, you can extend FacebookAppNoExposedNodes:

// make a class that can only return page data

class FacebookPageGetter extends FacebookAppNoExposedNodes {
    public async getPageInfo(pageId, accessToken) {
        return await this._Node('Page', pageId).read(accessToken);
    }
}

*NOTE: Don't return or expose single node objects as this will expose its edges object. Instead only return the data, and make any member node objects private.*


How to add nodes / edges:

*NOTE: TypeScript is required for these steps. The instructions assume you're familiar with TypeScript, specifically with interfaces, generic types, and union types.*

Step 0: Installation

To install this package in a Node.js project, run this command in the project root:

npm install -S fbsdk-ts

Step 0.5: Creating tsconfig.json

If you haven't already, create a tsconfig.json file in your project root. It does not have to contain anything, but you may fill it with your preferred configuration options.

Step 1: Importing

Import the FacebookAppBase class like so:

import { FacebookAppBase } from 'fbsdk-ts';

Step 2: Making an APISpec definition

FacebookAppBase is an abstract generic class, and requires an APISpec type parameter. You can declare an interface that extends APISpec and start defining nodes and their edges:

import { FacebookAppBase } from 'fbsdk-ts';
import { APISpec } from 'fbsdk-ts/dist/api-spec';

interface CustomAPISpec extends APISpec {
    SomeNodeName: {
        node: {
            read_return: SomeNode; // the type that defines the shape of the node
        };
        edges: {
            SomeEdge: {
                read_return: EdgeResult; // The type that defines the shape of the nodes returned by the edge
                edge: 'things'; // The API path for the edge
            };
        };
    };
}

These definitions can be further simplified by helper types:

import { FacebookAppBase } from 'fbsdk-ts';
import { APISpec, NodeSpec, EdgeSpec } from 'fbsdk-ts/dist/api-spec';

interface CustomAPISpec extends APISpec {
    SomeNodeName: {
        node: NodeSpec<SomeNode>;
        edges: {
            SomeEdge: EdgeSpec<EdgeResult, 'things'>;
        };
    };
}

If a node doesn't have any edges, its definition can be further shortened with another helper type:

import { FacebookAppBase } from 'fbsdk-ts';
import {
    APISpec,
    NodeSpec,
    EdgeSpec,
    EdgelessNodeSpec,
} from 'fbsdk-ts/dist/api-spec';

interface CustomAPISpec extends APISpec {
    SomeNodeName: {
        node: NodeSpec<SomeNode>;
        edges: {
            SomeEdge: EdgeSpec<EdgeResult, 'things'>;
        };
    };
    SomeEdgelessNodeName: EdgelessNodeSpec<SomeEdgelessNode>;
}

Nodes and edges can have read parameters defined like so:

import { FacebookAppBase } from 'fbsdk-ts';
import {
    APISpec,
    NodeSpec,
    EdgeSpec,
    EdgelessNodeSpec,
} from 'fbsdk-ts/dist/api-spec';

interface CustomAPISpec extends APISpec {
    SomeNodeName: {
        node: NodeSpec<SomeNode> & {
            read_params: {
                additional_token: string;
            };
        };
        edges: {
            SomeEdge: EdgeSpec<EdgeResult, 'things'> & {
                read_params: {
                    include_specific_kind: boolean;
                };
            };
        };
    };
    SomeEdgelessNodeName: EdgelessNodeSpec<SomeEdgelessNode>;
}

For more specific code examples, see the v9 APISpec definition. If you want to contribute more nodes/edges to this project, please add onto the type definition in that file.

Step 3: Implementing the definition

Import the Node and Edge classes from fbsdk-ts/dist/api-spec/node. Your custom APISpec definition can now be implemented in a subclass of FacebookAppBase:

import { FacebookAppBase } from 'fbsdk-ts';
import {
    APISpec,
    NodeSpec,
    EdgeSpec,
    EdgelessNodeSpec,
    APISpecNodeCollection,
} from 'fbsdk-ts/dist/api-spec';
import Node from 'fbsdk-ts/dist/api-spec/node';
import { KnownKeys } from 'fbsdk-ts/dist/util';

interface CustomAPISpec extends APISpec {
    SomeNodeName: {
        node: NodeSpec<SomeNode> & {
            read_params: {
                additional_token: string;
            };
        };
        edges: {
            SomeEdge: EdgeSpec<EdgeResult, 'things'> & {
                read_params: {
                    include_specific_kind: boolean;
                };
            };
        };
    };
    SomeEdgelessNodeName: EdgelessNodeSpec<SomeEdgelessNode>;
}

/*
 * The below function simply implements the node function 
 */

class CustomFacebookApp extends FacebookAppBase<CustomAPISpec> {
    protected _Node<NodeType extends KnownKeys<CustomAPISpec>>(
        node: NodeType,
        id?: string,
    ) {
        return new Node<CustomAPISpec[NodeType]['node'], CustomAPISpec[NodeType]['edges']>(
            this.GraphAPI,
            id,
        );
    }
}

If you want to expose the nodes function as a public member variable (as the default FacebookApp does), you can simply add this inside your class:

...
public Node = this._Node;
...

This class can also be used as described in how to extend / subclass.


How to access the node types

The nodes can be imported from fbsdk-ts/dist/graph-api/types like so:

import { Page, User, Conversation } from 'fbsdk-ts/dist/graph-api/types';

Please check the source file for the names of all the currently available types.

1.3.3

3 years ago

2.0.2

3 years ago

2.0.1

3 years ago

2.0.0

3 years ago

1.3.2

3 years ago

1.3.1

3 years ago

1.3.0

3 years ago

1.2.4

3 years ago

1.2.3

3 years ago

1.2.2

3 years ago

1.2.1

3 years ago

1.2.0

3 years ago

1.1.0

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago