Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added basic Typescript definitions. #46

Closed
wants to merge 10 commits into from
61 changes: 61 additions & 0 deletions fb.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import Promise = require("any-promise");
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can remove this line. Promise can be used directly without importing it as it is part of ES6.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While that is true, that doesn't mean that everyone is using an ES6 environment, and additionally this library uses any-promise as the Promise implementation.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's how i have seen it used in most well written typings I have used. If you are not targeting ES6, it is up to the consumer to provide the typings for Promise. Example from a fairy recent declaration https://github.com/facebook/dataloader/blob/master/src/index.d.ts

Copy link

@ibratoev ibratoev Feb 8, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Additionally, 2.0 of this library supports only NodeJS environments that support Promise natively ;)


type HTTPMethods = "get" | "post" | "delete";

export interface FacebookOptions {
accessToken?: string;
appId?: string;
appSecret?: string;
version?: string;
proxy?: string;
timeout?: number;
scope?: string;
redirectUri?: string;
Promise?: any;
}

export interface LoginUrlOptions {
appId?: string;
client_id?: string;
redirectUri?: string;
redirect_uri?: string;
scope?: string;
display?: string;
state?: string;
responseType?: string;
response_type?: string;
}

export class Facebook {
api(path: string): Promise<any>;
api(path: string, callback: (response: any) => void): void;
api(path: string, params: any): Promise<any>;
api(path: string, params: any, callback: (response: any) => void): void;
api(path: string, method: HTTPMethods): Promise<any>;
api(path: string, method: HTTPMethods, callback: (response: any) => void): void;
api(path: string, method: HTTPMethods, params: any): Promise<any>;
api(path: string, method: HTTPMethods, params: any, callback: (response: any) => void): void;
napi(path: string, callback?: (error: any, response: any) => void): void;
napi(path: string, params: any, callback?: (error: any, response: any) => void): void;
napi(path: string, method: HTTPMethods, callback?: (error: any, response: any) => void): void;
napi(path: string, method: HTTPMethods, params: any, callback?: (error: any, response: any) => void): void;
getAccessToken(): string;
setAccessToken(token: string): void;
parseSignedRequest(signedRequest: string, appSecret?: string): any;
getLoginUrl(options?: LoginUrlOptions): string;
options(): FacebookOptions;
options(optionName: string): any;
options(optionsDict: FacebookOptions): void;
extend(options: FacebookOptions): Facebook;
withAccessToken(token: string): Facebook;
}

export default new Facebook;
export var FB: Facebook;
export var version: string;

export class FacebookApiException {
name: string;
message: string;
response: any;
}
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,15 @@
"lodash.omit": "^4.3.0",
"mocha": "^2.4.5",
"mocha-eslint": "^2.0.2",
"nock": "^8.0.0"
"nock": "^8.0.0",
"typescript": "^1.8.10",
"typescript-definition-tester": "0.0.4"
},
"engines": {
"node": ">=4"
},
"publishConfig": {
"tag": "next"
}
},
"typings": "fb.d.ts"
}
46 changes: 46 additions & 0 deletions test/typings/import.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// so happy this relative import seems to work the same as ' import FB from "fb" '
import FB from "../../";
import {Facebook} from "../../";

// the 3 options overloads
FB.options().appId;
FB.options("appId");
FB.options({
appId: "alskjdfaslkdjfalsdfjk"
});
// extend returns a Facebook
FB.extend({
accessToken: "loremipsum"
}).options("accessToken");
// withAccessToken returns a Facebook
FB.withAccessToken("foobar").options("accessToken");
// setAccessToken works, getAccessToken returns a string
FB.setAccessToken("foobar");
FB.getAccessToken().charAt(0);

// All api calls have the documented parameters/returns
FB.api("./").then((response) => {
});
FB.api("./", (response) => {});
FB.api("./", {scope: "foo"}).then((response) => {
});
FB.api("./", {scope: "foo"}, (response) => {});
FB.api("./", "get").then((response) => {
});
FB.api("./", "post", (response) => {});
FB.api("./", "delete", {scope: "foo"}).then((response) => {
});
FB.api("./", "get", {scope: "foo"}, (response) => {});
// All napi calls have the coumented parameters
FB.napi("./");
FB.napi("./", (error, response) => {});
FB.napi("./", {scope: "foo"});
FB.napi("./", {scope: "foo"}, (error, response) => {});
FB.napi("./", "post");
FB.napi("./", "post", (error, response) => {});
FB.napi("./", "delete", {scope: "foo"});
FB.napi("./", "delete", {scope: "foo"}, (error, response) => {});

// can instantiate Facebook class
let fb = new Facebook();
fb.api("./");
3 changes: 3 additions & 0 deletions test/typings/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"compileOnSave": false
}
18 changes: 18 additions & 0 deletions test/typings/typings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';

var tt = require('typescript-definition-tester');

describe('typescript definition tests', function() {
this.slow(1800);
this.timeout(2000);
it('should compile examples successfully against fb.d.ts', (done) => {
tt.compileDirectory(
__dirname,
function(fileName) {
return fileName.indexOf('.ts') > -1;
},
function() {
done();
});
});
});