1.0.4 • Published 3 months ago

facebook-js-sdk v1.0.4

Weekly downloads
125
License
MIT
Repository
github
Last release
3 months ago

Facebook JS SDK

npm version Build Status License: MIT

A simple npm package to interact with Facebook API.

Installation

npm install facebook-js-sdk

Usage

JavaScript

const Facebook = require('facebook-js-sdk');

const fb = new Facebook({
  appId: 'your-app-id',
  appSecret: 'your-app-secret',
  redirectUrl: 'your-redirect-url'
});

// Get login URL
const url = fb.getLoginUrl(['email', 'public_profile']);

// Exchange code for token
fb.callback('code-from-facebook')
  .then(response => {
    fb.setAccessToken(response.data.access_token);
  });

// Make API calls
fb.get('/me')
  .then(response => console.log(response.data));

TypeScript

import Facebook from 'facebook-js-sdk';

interface UserProfile {
  id: string;
  name: string;
  email?: string;
}

const fb = new Facebook({
  appId: 'your-app-id',
  appSecret: 'your-app-secret',
  redirectUrl: 'your-redirect-url'
});

// Get login URL with type-safe permissions
const url = fb.getLoginUrl(['email', 'public_profile']);

// Exchange code for token with type safety
const response = await fb.callback('code-from-facebook');
fb.setAccessToken(response.data.access_token);

// Make API calls with type safety
const profile = await fb.get<UserProfile>('/me');
console.log(profile.data.name);

API Reference

Constructor

new Facebook({
  appId?: string;
  appSecret?: string;
  redirectUrl?: string;
  graphVersion?: string; // defaults to 'v20.0'
  accessToken?: string;
})

You must provide either:

  • accessToken for direct API access, or
  • appId, appSecret, and redirectUrl for OAuth flow

Methods

getLoginUrl(permissions: string[]): string

Generate a Facebook login URL with the specified permissions.

callback(code: string): Promise<{ access_token: string, ... }>

Exchange an OAuth code for an access token.

getAccessToken(): string | undefined

Get the current access token.

setAccessToken(accessToken: string): void

Set the access token for API calls.

get<T>(path: string, accessToken?: string): Promise<{ data: T }>

Make a GET request to the Facebook API.

post<T>(path: string, options: object, accessToken?: string): Promise<{ data: T }>

Make a POST request to the Facebook API.

delete<T>(path: string, accessToken?: string): Promise<{ data: T }>

Make a DELETE request to the Facebook API.

Development

# Install dependencies
npm install

# Run tests
npm test

# Run linter
npm run lint

# Build
npm run build

Contributing

Please read CONTRIBUTING.md for details on our code of conduct and the process for submitting pull requests.

License

This project is licensed under the MIT License - see the LICENSE file for details.

1.0.8

3 months ago

1.0.7

3 months ago

1.0.6

3 months ago

1.0.5

3 months ago

1.0.2

9 months ago

1.0.1

9 months ago

1.0.0

9 months ago

1.0.4

9 months ago

1.0.3

9 months ago

0.3.5

9 months ago

0.3.4

5 years ago

0.2.4

5 years ago

0.1.4

5 years ago

0.1.2

5 years ago

0.1.3

5 years ago

0.1.1

5 years ago

0.0.1

5 years ago