11// @flow
22import events from 'events'
3- import https from 'https'
3+ import { request } from 'https'
44import url from 'url'
55
66// @flow
7- type User = { } ;
7+ type User = {
8+ options : { } ,
9+ access_token : string ,
10+ } ;
811
9- const fromJSON = ( json : string ) => {
10- const data = JSON . parse ( json ) ;
12+ const fromJSON = ( json : string ) : User => {
13+ const data : { } = JSON . parse ( json ) ;
14+ if ( ! data . access_token && typeof data . access_token !== 'string' ) {
15+ throw new Error ( 'access_token not present' ) ;
16+ }
1117 return {
1218 options : data ,
13- access_token : data . access_token
19+ access_token : new String ( data . access_token ) . toString ( )
1420 } ;
1521} ;
1622
@@ -27,16 +33,32 @@ export class AuthError extends Error {
2733 }
2834}
2935
36+ /**
37+ * Client for creating and authenticating Simperium.com user accounts.
38+ */
3039export class Auth extends EventEmitter {
3140 appId : string
3241 appSecret : string
3342
43+ /**
44+ * Creates an instance of the Auth client
45+ *
46+ * @param {string } appId - Simperium.com application ID
47+ * @param {string } appSecret - Simperium.com application secret
48+ */
3449 constructor ( appId : string , appSecret : string ) {
3550 super ( ) ;
3651 this . appId = appId ;
3752 this . appSecret = appSecret ;
3853 }
3954
55+ /**
56+ * Authorizes a user account with username and password
57+ *
58+ * @param {string } username account username
59+ * @param {string } password account password
60+ * @returns {Promise<User> } user account data
61+ */
4062 authorize ( username : string , password : string ) {
4163 const body = JSON . stringify ( { username : username , password : password } ) ;
4264 return this . request ( 'authorize/' , body ) ;
@@ -62,7 +84,7 @@ export class Auth extends EventEmitter {
6284
6385 request ( endpoint : string , body : string ) : Promise < User > {
6486 return new Promise ( ( resolve , reject ) => {
65- const req = https . request ( this . getUrlOptions ( endpoint ) , ( res ) => {
87+ const req = request ( this . getUrlOptions ( endpoint ) , ( res ) => {
6688 let responseData = '' ;
6789
6890 res . on ( 'data' , ( data ) => {
0 commit comments