From a18dada81c5fd434c0d5ee05e618a9ca4fdfc48b Mon Sep 17 00:00:00 2001 From: Kyle Corbelli Date: Fri, 15 Sep 2017 11:21:02 -0700 Subject: [PATCH] Extract and test a couple of helper functions --- src/actions.ts | 28 +++------------------------- src/services/auth.js | 32 -------------------------------- src/services/auth.test.ts | 33 +++++++++++++++++++++++++++++++-- src/services/auth.ts | 22 +++++++++++++++++++++- src/services/utility.test.ts | 17 +++++++++++++++++ src/services/utility.ts | 10 ++++++++++ src/types.ts | 4 ++++ 7 files changed, 86 insertions(+), 60 deletions(-) delete mode 100644 src/services/auth.js create mode 100644 src/services/utility.test.ts create mode 100644 src/services/utility.ts diff --git a/src/actions.ts b/src/actions.ts index f1ae3af..4e11246 100644 --- a/src/actions.ts +++ b/src/actions.ts @@ -37,10 +37,11 @@ import { SignOutRequestFailedAction, } from './types' import { - setAuthHeaders, deleteAuthHeaders, - persistAuthHeadersInLocalStorage, deleteAuthHeadersFromLocalStorage, + getUserAttributesFromResponse, + persistAuthHeadersInLocalStorage, + setAuthHeaders, } from './services/auth' // <- maybe this is where you pass in the platform paramter, specifying if it is for a browser or for React Native //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @@ -121,29 +122,6 @@ export const signOutRequestFailed = (): SignOutRequestFailedAction => ({ // }, // } -// extract this service somewhere and unit test it: -const invertHash = (hash: { [key: string]: any }) => { - const newHash = {} - for (let key in hash) { - const val = hash[key] - newHash[val] = key - } - return newHash -} - -// extract this service somewhere and unit test it: -const getUserAttributesFromResponse = (userAttributes: any, response: any) => { - const invertedUserAttributes = invertHash(userAttributes) - const userAttributesBackendKeys = Object.keys(invertedUserAttributes) - const userAttributesToReturn = {} - Object.keys(response.data.data).forEach((key: string) => { - if (userAttributesBackendKeys.indexOf(key) !== -1) { - userAttributesToReturn[invertedUserAttributes[key]] = response.data.data[key] - } - }) - return userAttributesToReturn -} - const generateAuthActions = (config: { [key: string]: any }): ActionsExport => { const { authUrl, diff --git a/src/services/auth.js b/src/services/auth.js deleted file mode 100644 index 4da1a04..0000000 --- a/src/services/auth.js +++ /dev/null @@ -1,32 +0,0 @@ -"use strict"; -exports.__esModule = true; -var axios_1 = require("axios"); -var authHeaderKeys = [ - 'access-token', - 'token-type', - 'client', - 'expiry', - 'uid', -]; -exports.setAuthHeaders = function (headers) { - authHeaderKeys.forEach(function (key) { - axios_1["default"].defaults.headers.common[key] = headers[key]; - }); -}; -// Will have to take a parameter from the package user to determine if this is for a browser or for React Native: -exports.persistAuthHeadersInLocalStorage = function (headers) { - authHeaderKeys.forEach(function (key) { - localStorage.setItem(key, headers[key]); - }); -}; -exports.deleteAuthHeaders = function () { - authHeaderKeys.forEach(function (key) { - delete axios_1["default"].defaults.headers.common[key]; - }); -}; -// Will have to take a parameter from the package user to determine if this is for a browser or for React Native: -exports.deleteAuthHeadersFromLocalStorage = function () { - authHeaderKeys.forEach(function (key) { - localStorage.removeItem(key); - }); -}; diff --git a/src/services/auth.test.ts b/src/services/auth.test.ts index b1deef9..ee71a56 100644 --- a/src/services/auth.test.ts +++ b/src/services/auth.test.ts @@ -1,8 +1,13 @@ import axios from 'axios' -import { AuthHeaders } from '../types' import { - setAuthHeaders, + AuthHeaders, + AuthResponse, + SingleLayerStringMap, +} from '../types' +import { deleteAuthHeaders, + getUserAttributesFromResponse, + setAuthHeaders, } from './auth' describe('auth service', () => { @@ -31,4 +36,28 @@ describe('auth service', () => { }) }) }) + + describe('getUserAttributesFromResponse', () => { + it('gets the values of the user attributes from the response, accounting for casing differences', () => { + const userAttributes: SingleLayerStringMap = { + firstName: 'first_name', + lastName: 'last_name', + } + const authResponse: AuthResponse = { + headers, + data: { + data: { + first_name: 'Rick', + last_name: 'Sanchez', + }, + }, + } + const result: SingleLayerStringMap = getUserAttributesFromResponse(userAttributes, authResponse) + const expectedResult: SingleLayerStringMap = { + firstName: 'Rick', + lastName: 'Sanchez', + } + expect(result).toEqual(expectedResult) + }) + }) }) diff --git a/src/services/auth.ts b/src/services/auth.ts index 24545be..6a49ad5 100644 --- a/src/services/auth.ts +++ b/src/services/auth.ts @@ -1,5 +1,10 @@ import axios from 'axios' -import { AuthHeaders } from '../types' +import { invertMapKeysAndValues } from './utility' +import { + AuthHeaders, + AuthResponse, + SingleLayerStringMap, +} from '../types' const authHeaderKeys: Array = [ 'access-token', @@ -34,3 +39,18 @@ export const deleteAuthHeadersFromLocalStorage = (): void => { localStorage.removeItem(key) }) } + +export const getUserAttributesFromResponse = ( + userAttributes: SingleLayerStringMap, + response: AuthResponse +): SingleLayerStringMap => { + const invertedUserAttributes: SingleLayerStringMap = invertMapKeysAndValues(userAttributes) + const userAttributesBackendKeys: string[] = Object.keys(invertedUserAttributes) + const userAttributesToReturn: SingleLayerStringMap = {} + Object.keys(response.data.data).forEach((key: string) => { + if (userAttributesBackendKeys.indexOf(key) !== -1) { + userAttributesToReturn[invertedUserAttributes[key]] = response.data.data[key] + } + }) + return userAttributesToReturn +} diff --git a/src/services/utility.test.ts b/src/services/utility.test.ts new file mode 100644 index 0000000..d4ccb15 --- /dev/null +++ b/src/services/utility.test.ts @@ -0,0 +1,17 @@ +import { SingleLayerStringMap } from '../types' +import { invertMapKeysAndValues } from './utility' + +describe('invertMapKeysAndValues', () => { + it('sets the values as the keys and the keys as the values', () => { + const stringMap: SingleLayerStringMap = { + firstName: 'first_name', + last_name: 'lastName', + } + const result: SingleLayerStringMap = invertMapKeysAndValues(stringMap) + const expectedResult: SingleLayerStringMap = { + first_name: 'firstName', + lastName: 'last_name', + } + expect(result).toEqual(expectedResult) + }) +}) diff --git a/src/services/utility.ts b/src/services/utility.ts new file mode 100644 index 0000000..cb6ab2c --- /dev/null +++ b/src/services/utility.ts @@ -0,0 +1,10 @@ +import { SingleLayerStringMap } from '../types' + +export const invertMapKeysAndValues = (stringMap: SingleLayerStringMap): SingleLayerStringMap => { + const newStringMap: SingleLayerStringMap = {} + for (let key in stringMap) { + const val = stringMap[key] + newStringMap[val] = key + } + return newStringMap +} diff --git a/src/types.ts b/src/types.ts index 975817a..f64c72a 100644 --- a/src/types.ts +++ b/src/types.ts @@ -173,3 +173,7 @@ export interface ActionsExport { } export type ActionsGeneratorExport = (config: { [key: string]: any }) => ActionsExport + +export interface SingleLayerStringMap { + [key: string]: string +}