Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Strongly-typed promise-based client library for Facebook's Graph API using TypeScript
Strongly-typed promise-based client library for Facebook's Graph API using TypeScript.
TypeScript is recommended to get all the benefits of this package, but you can also use plain JavaScript.
To install this package in a Node.js project, run this command in the project root:
npm install -S fbsdk-ts
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';
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.
Once your app is initialized, you can read different nodes on the Nodes
object:
// Get information about a page
app.Nodes.Page('{SOME-PAGE-ID}')
.read('{PAGE-ACCESS-TOKEN}')
.then((pageInfo) => {
// do stuff with page info
});
A node may or may not also contain Edges
, which will return a collection of nodes related to the root node:
// Get conversations on page
app.Nodes.Page('{SOME-PAGE-ID}')
.Edges.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.
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.Nodes.Page('{SOME-PAGE-ID}')
.read('{PAGE-ACCESS-TOKEN}', ['category', 'followers_count'])
.then((pageInfo) => {
// do stuff with page info
});
If you are using TypeScript, you can limit the fields of the result type by duplicating your fields array as a type parameter on the .read
method:
...
.read<['category', 'followers_count']>('{PAGE-ACCESS-TOKEN}', ['category', 'followers_count'])
...
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.Nodes.Page('{SOME-PAGE-ID}')
.Edges.Conversations.read('{PAGE-ACCESS-TOKEN}', undefined, { folder: 'spam' })
.then((spamConversations) => {
// do stuff with spam conversations
});
This package is made to be extensible. You can create your own custom class like FacebookApp
with its own methods to suit your needs.
To install this package in a Node.js project, run this command in the project root:
npm install -S fbsdk-ts
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';
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 object on .Nodes
, while FacebookAppNoExposedNodes
keeps the nodes object on the projected member _Nodes
. If you want to make a subclass that does not expose the nodes object, you can extend FacebookAppNoExposedNodes
:
// make a class that can only return page data
class FacebookPageGetter extends FacebookAppNoExposedNodes {
public getPageInfo(pageId, accessToken) {
return await this._Nodes.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.
NOTE: TypeScript is required for these steps. The instructions assume you're familiar with TypeScript, specifically with interfaces, generic types, and union types.
To install this package in a Node.js project, run this command in the project root:
npm install -S fbsdk-ts
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.
Import the FacebookAppBase
class like so:
import { FacebookAppBase } from 'fbsdk-ts';
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.
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, { Edge } from 'fbsdk-ts/dist/api-spec/node';
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>;
}
/*
* Each key of the nodes object must be a function of the type: (id: string) => Node
* The second parameter of the Node constructor is an implementation of the edges definition
* The first parameter of the Edge constructor must the the API path to the edge
*
* The above requirements will by type-checked by the compiler.
*/
class CustomFacebookApp extends FacebookAppBase<CustomAPISpec> {
protected _Nodes: APISpecNodeCollection<CustomAPISpec> = {
SomeNodeName: (id: string) =>
new Node(
this.GraphAPI,
{
SomeEdge: new Edge('things', this.GraphAPI, id),
},
id
),
SomeEdgelessNodeName: (id: string) => new Node(this.GraphAPI, {}, id),
};
}
If you want to expose the nodes as a public member variable (as the default FacebookApp
does), you can simply add this inside your class:
...
public Nodes = this._Nodes;
...
This class can also be used as described in how to extend / subclass.
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.
FAQs
Strongly-typed promise-based client library for Facebook's Graph API using TypeScript
We found that fbsdk-ts demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.