Skip to content

Commit

Permalink
Replace Promise with $q
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Lewis committed Jul 19, 2016
1 parent 4ee574c commit d64c1b3
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 26 deletions.
7 changes: 4 additions & 3 deletions src/oauth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@ import OAuth2 from './oauth2';

export default class OAuth {
static $inject = ['$http', 'SatellizerConfig', 'SatellizerShared', 'SatellizerOAuth1', 'SatellizerOAuth2'];

constructor(private $http: angular.IHttpService,
private $q: angular.IQService,
private SatellizerConfig: Config,
private SatellizerShared: Shared,
private SatellizerOAuth1: OAuth1,
private SatellizerOAuth2: OAuth2) {
}

authenticate(name: string, userData: any): Promise<any> {
return new Promise((resolve, reject) => {
authenticate(name: string, userData: any): angular.IPromise<any> {
return this.$q((resolve, reject) => {
const provider = this.SatellizerConfig.providers[name];
const initialize: any = provider.oauthType === '1.0' ? this.SatellizerOAuth1.init(provider, userData) : this.SatellizerOAuth2.init(provider, userData);

Expand Down
4 changes: 2 additions & 2 deletions src/oauth1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { IOAuth1Options } from './oauth1';
export interface IOAuth1 {
init(options: any, data: any): angular.IPromise<any>;
getRequestToken(): angular.IHttpPromise<any>;
openPopup(options: IOAuth1Options, response: angular.IHttpPromiseCallbackArg<any>): Promise<any>;
openPopup(options: IOAuth1Options, response: angular.IHttpPromiseCallbackArg<any>): angular.IPromise<any>;
exchangeForToken(oauthData: any, userData: any): angular.IHttpPromise<any>;
buildQueryString(obj: any): string;
}
Expand Down Expand Up @@ -63,7 +63,7 @@ export default class OAuth1 implements IOAuth1 {
});
}

openPopup(options: IOAuth1Options, response: angular.IHttpPromiseCallbackArg<any>): Promise<any> {
openPopup(options: IOAuth1Options, response: angular.IHttpPromiseCallbackArg<any>): angular.IPromise<any> {
const popupUrl = [options.authorizationEndpoint, this.buildQueryString(response.data)].join('?');

if (this.$window['cordova']) {
Expand Down
7 changes: 4 additions & 3 deletions src/oauth2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export default class OAuth2 {
constructor(private $http: angular.IHttpService,
private $window: angular.IWindowService,
private $timeout: angular.ITimeoutService,
private $q: angular.IQService,
private SatellizerConfig: Config,
private SatellizerPopup: Popup,
private SatellizerStorage: Storage) {
Expand All @@ -65,8 +66,8 @@ export default class OAuth2 {
};
}

init(options: IOAuth2Options, userData: any): Promise<any> {
return new Promise((resolve, reject) => {
init(options: IOAuth2Options, userData: any): angular.IPromise<any> {
return this.$q((resolve, reject) => {
angular.extend(this.defaults, options);

this.$timeout(() => {
Expand All @@ -82,7 +83,7 @@ export default class OAuth2 {

this.SatellizerPopup.open(url, name, popupOptions);

this.SatellizerPopup.polling(redirectUri).then((oauth: any): void|Promise<any>|angular.IHttpPromise<any> => {
this.SatellizerPopup.polling(redirectUri).then((oauth: any): void|angular.IPromise<any>|angular.IHttpPromise<any> => {

if (responseType === 'token' || !url) {
return resolve(oauth);
Expand Down
15 changes: 8 additions & 7 deletions src/popup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { parseQueryString, getFullUrlPath } from './utils';
export interface IPopup {
open(url: string, name: string, popupOptions: { width: number, height: number }): void;
stringifyOptions (options: any): string;
polling(redirectUri: string): Promise<any>;
eventListener(redirectUri: string): Promise<any>;
polling(redirectUri: string): angular.IPromise<any>;
eventListener(redirectUri: string): angular.IPromise<any>;
}

export default class Popup implements IPopup {
Expand All @@ -15,7 +15,8 @@ export default class Popup implements IPopup {
private defaults: { redirectUri: string };

constructor(private $interval: angular.IIntervalService,
private $window: angular.IWindowService) {
private $window: angular.IWindowService,
private $q: angular.IQService) {
this.popup = null;
this.url = 'about:blank'; // TODO remove
this.defaults = {
Expand Down Expand Up @@ -59,8 +60,8 @@ export default class Popup implements IPopup {
// }
}

polling(redirectUri: string): Promise<any> {
return new Promise((resolve, reject) => {
polling(redirectUri: string): angular.IPromise<any> {
return this.$q((resolve, reject) => {
const redirectUriParser = document.createElement('a');
redirectUriParser.href = redirectUri;
const redirectUriPath = getFullUrlPath(redirectUriParser);
Expand Down Expand Up @@ -104,8 +105,8 @@ export default class Popup implements IPopup {
});
}

eventListener(redirectUri): Promise<any> {
return new Promise((resolve, reject) => {
eventListener(redirectUri): angular.IPromise<any> {
return this.$q((resolve, reject) => {
this.popup.addEventListener('loadstart', (event) => {
if (!event.url.includes(redirectUri)) {
return;
Expand Down
6 changes: 3 additions & 3 deletions test/authProvider.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ describe('AuthProvider', () => {
httpBackend = $httpBackend;
storage = new Storage($window, config);
shared = new Shared($q, $window, $log, config, storage);
popup = new Popup($interval, $window);
popup = new Popup($interval, $window, $q);
oauth1 = new OAuth1($http, $window, config, popup);
oauth2 = new OAuth2($http, $window, $timeout, config, popup, storage);
oauth = new OAuth($http, config, shared, oauth1, oauth2);
oauth2 = new OAuth2($http, $window, $timeout, $q, config, popup, storage);
oauth = new OAuth($http, $q, config, shared, oauth1, oauth2);
local = new Local($http, config, shared);
auth = authProvider.$get(shared, local, oauth);
}));
Expand Down
6 changes: 3 additions & 3 deletions test/oauth.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ describe('OAuth', () => {
config = new Config();
storage = new Storage($window, config);
shared = new Shared($q, $window, $log, config, storage);
popup = new Popup($interval, $window);
popup = new Popup($interval, $window, $q);
oauth1 = new OAuth1($http, $window, config, popup);
oauth2 = new OAuth2($http, $window, $timeout, config, popup, storage);
oauth = new OAuth($http, config, shared, oauth1, oauth2);
oauth2 = new OAuth2($http, $window, $timeout, $q, config, popup, storage);
oauth = new OAuth($http, $q, config, shared, oauth1, oauth2);
}));

afterEach(() => {
Expand Down
2 changes: 1 addition & 1 deletion test/oauth1.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe('OAuth1', () => {
config = new Config();
storage = new Storage($window, config);
shared = new Shared($q, $window, $log, config, storage);
popup = new Popup($interval, $window);
popup = new Popup($interval, $window, $q);
oauth1 = new OAuth1($http, $window, config, popup);
}));

Expand Down
4 changes: 2 additions & 2 deletions test/oauth2.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ describe('OAuth2', () => {
config = new Config();
storage = new Storage($window, config);
shared = new Shared($q, $window, $log, config, storage);
popup = new Popup($interval, $window);
oauth2 = new OAuth2($http, $window, $timeout, config, popup, storage);
popup = new Popup($interval, $window, $q);
oauth2 = new OAuth2($http, $window, $timeout, $q, config, popup, storage);
}));

afterEach(() => {
Expand Down
4 changes: 2 additions & 2 deletions test/popup.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ let popup;

describe('Popup', () => {

beforeEach(angular.mock.inject(($interval, $window) => {
beforeEach(angular.mock.inject(($interval, $window, $q) => {
interval = $interval;
window = $window;
popup = new Popup($interval, $window);
popup = new Popup($interval, $window, $q);
}));

it('should be defined', () => {
Expand Down

0 comments on commit d64c1b3

Please sign in to comment.