From 6545309f7708608a5c3bc65d94cebd0402eea985 Mon Sep 17 00:00:00 2001 From: Michael Randolph Date: Tue, 31 Oct 2017 17:59:30 -0400 Subject: [PATCH] Added types for passport-facebook-token --- types/passport-facebook-token/index.d.ts | 78 +++++++++++------- .../passport-facebook-token-tests.ts | 51 +++++++----- types/passport-facebook-token/tsconfig.json | 2 +- types/passport-facebook-token/tslint.json | 80 +------------------ 4 files changed, 84 insertions(+), 127 deletions(-) diff --git a/types/passport-facebook-token/index.d.ts b/types/passport-facebook-token/index.d.ts index 3077beca0e..9013910ee1 100644 --- a/types/passport-facebook-token/index.d.ts +++ b/types/passport-facebook-token/index.d.ts @@ -1,38 +1,58 @@ -// Type definitions for passport-facebook-token 0.4.0 +// Type definitions for passport-facebook-token 0.4 // Project: https://github.com/drudge/passport-facebook-token -// Definitions by: Ray Martone +// Definitions by: Ray Martone , Michael Randolph // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.3 -/// - - - -import passport = require('passport'); -import express = require('express'); - -interface Profile extends passport.Profile { - gender: string; - profileUrl: string; -} - -interface StrategyOptions { - clientID: string; - clientSecret: string; - authorizationURL?: string; - tokenURL?: string; - scopeSeparator?: string; - passReqToCallback?: Function; - enableProof?: boolean; - profileFields?: any[]; -} +import * as passport from 'passport'; +import * as express from 'express'; declare class Strategy implements passport.Strategy { - constructor(options: StrategyOptions, - verify: (accessToken: string, - refreshToken: string, - profile: Profile, - done: (err: any, user?: any) => void) => void); + constructor(options: Strategy.StrategyOptionsWithRequest, verify: Strategy.VerifyFunctionWithRequest) + constructor(options: Strategy.StrategyOptions, verify: Strategy.VerifyFunction); + name: string; authenticate: (req: express.Request, options?: any) => void; } + +declare namespace Strategy { + interface ValueObject { + value: string; + } + + interface Profile extends passport.Profile { + provider: string; + id: string; + displayName: string; + name: { + familyName: string; + givenName: string; + middleName: string; + }; + gender: string; + emails: ValueObject[]; + photos: ValueObject[]; + _raw: string; + _json: any; + } + + interface StrategyOptions { + clientID: string; + clientSecret: string; + authorizationURL?: string; + tokenURL?: string; + scopeSeparator?: string; + enableProof?: boolean; + profileFields?: string[]; + } + + interface StrategyOptionsWithRequest extends StrategyOptions { + passReqToCallback: true; + } + + type VerifyFunction = (accessToken: string, refreshToken: string, profile: Profile, done: (error: any, user?: any, info?: any) => void) => void; + + type VerifyFunctionWithRequest = (req: express.Request, accessToken: string, refreshToken: string, profile: Profile, done: (error: any, user?: any, info?: any) => void) => void; +} + +export = Strategy; diff --git a/types/passport-facebook-token/passport-facebook-token-tests.ts b/types/passport-facebook-token/passport-facebook-token-tests.ts index e44100ca7d..cb2f078e88 100644 --- a/types/passport-facebook-token/passport-facebook-token-tests.ts +++ b/types/passport-facebook-token/passport-facebook-token-tests.ts @@ -1,28 +1,43 @@ +import * as express from 'express'; +import * as passport from 'passport'; +import * as FacebookStrategy from 'passport-facebook-token'; +// tslint:disable-next-line:no-duplicate-imports +import { StrategyOptions, StrategyOptionsWithRequest, VerifyFunction, VerifyFunctionWithRequest, Profile } from 'passport-facebook-token'; -import passport = require('passport'); -import facebook = require('passport-facebook-token'); - -var User = { +const User = { findOrCreate(id: string, provider: string, callback: (err: any, user: any) => void): void { - callback(null, {username: 'ray'}); + callback(null, { username: 'ray' }); } -} - -var options: facebook.StrategyOptions = { - clientID: process.env.PASSPORT_FACEBOOK_CLIENT_ID, - clientSecret: process.env.PASSPORT_FACEBOOK_CLIENT_SECRET }; -function verify(accessToken: string, - refreshToken: string, - profile: facebook.Profile, - done: (err: any, user?: any) => void) { - User.findOrCreate(profile.id, profile.provider, function (err, user) { +const options: StrategyOptions = { + clientID: 'TEST_CLIENT_ID', + clientSecret: 'TEST_CLIENT_SECRET' +}; + +const optionsWithRequest: StrategyOptionsWithRequest = { + clientID: 'TEST_CLIENT_ID', + clientSecret: 'TEST_CLIENT_SECRET', + passReqToCallback: true +}; + +const verify: VerifyFunction = (accessToken: string, refreshToken: string, profile: Profile, done: (err: any, user?: any, info?: any) => void) => { + User.findOrCreate(profile.id, profile.provider, (err, user) => { if (err) { - return done(err); + done(err); } done(null, user); }); -} +}; -passport.use(new facebook.Strategy(options, verify)); +const verifyWithRequest: VerifyFunctionWithRequest = (req: express.Request, accessToken: string, refreshToken: string, profile: Profile, done: (err: any, user?: any, info?: any) => void) => { + User.findOrCreate(profile.id, profile.provider, (err, user) => { + if (err) { + done(err); + } + done(null, user); + }); +}; + +passport.use(new FacebookStrategy(options, verify)); +passport.use(new FacebookStrategy(optionsWithRequest, verifyWithRequest)); diff --git a/types/passport-facebook-token/tsconfig.json b/types/passport-facebook-token/tsconfig.json index a5f4d17112..e1d020177a 100644 --- a/types/passport-facebook-token/tsconfig.json +++ b/types/passport-facebook-token/tsconfig.json @@ -6,7 +6,7 @@ ], "noImplicitAny": true, "noImplicitThis": true, - "strictNullChecks": false, + "strictNullChecks": true, "strictFunctionTypes": true, "baseUrl": "../", "typeRoots": [ diff --git a/types/passport-facebook-token/tslint.json b/types/passport-facebook-token/tslint.json index a41bf5d19a..3db14f85ea 100644 --- a/types/passport-facebook-token/tslint.json +++ b/types/passport-facebook-token/tslint.json @@ -1,79 +1 @@ -{ - "extends": "dtslint/dt.json", - "rules": { - "adjacent-overload-signatures": false, - "array-type": false, - "arrow-return-shorthand": false, - "ban-types": false, - "callable-types": false, - "comment-format": false, - "dt-header": false, - "eofline": false, - "export-just-namespace": false, - "import-spacing": false, - "interface-name": false, - "interface-over-type-literal": false, - "jsdoc-format": false, - "max-line-length": false, - "member-access": false, - "new-parens": false, - "no-any-union": false, - "no-boolean-literal-compare": false, - "no-conditional-assignment": false, - "no-consecutive-blank-lines": false, - "no-construct": false, - "no-declare-current-package": false, - "no-duplicate-imports": false, - "no-duplicate-variable": false, - "no-empty-interface": false, - "no-for-in-array": false, - "no-inferrable-types": false, - "no-internal-module": false, - "no-irregular-whitespace": false, - "no-mergeable-namespace": false, - "no-misused-new": false, - "no-namespace": false, - "no-object-literal-type-assertion": false, - "no-padding": false, - "no-redundant-jsdoc": false, - "no-redundant-jsdoc-2": false, - "no-redundant-undefined": false, - "no-reference-import": false, - "no-relative-import-in-test": false, - "no-self-import": false, - "no-single-declare-module": false, - "no-string-throw": false, - "no-unnecessary-callback-wrapper": false, - "no-unnecessary-class": false, - "no-unnecessary-generics": false, - "no-unnecessary-qualifier": false, - "no-unnecessary-type-assertion": false, - "no-useless-files": false, - "no-var-keyword": false, - "no-var-requires": false, - "no-void-expression": false, - "no-trailing-whitespace": false, - "object-literal-key-quotes": false, - "object-literal-shorthand": false, - "one-line": false, - "one-variable-per-declaration": false, - "only-arrow-functions": false, - "prefer-conditional-expression": false, - "prefer-const": false, - "prefer-declare-function": false, - "prefer-for-of": false, - "prefer-method-signature": false, - "prefer-template": false, - "radix": false, - "semicolon": false, - "space-before-function-paren": false, - "space-within-parens": false, - "strict-export-declare-modifiers": false, - "trim-file": false, - "triple-equals": false, - "typedef-whitespace": false, - "unified-signatures": false, - "void-return": false, - "whitespace": false - } -} +{ "extends": "dtslint/dt.json" }