-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #93 from IABTechLab/sas-uid2-3835-store-js-sdk-config
store config when javascript sdk is initialized
- Loading branch information
Showing
5 changed files
with
216 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import { ProductDetails } from './product'; | ||
import { SdkOptions } from './sdkOptions'; | ||
|
||
type storedConfig = Pick< | ||
SdkOptions, | ||
'baseUrl' | 'useCookie' | 'refreshRetryPeriod' | 'cookiePath' | 'cookieDomain' | ||
>; | ||
|
||
const getConfigFromSdkOptions = (options: SdkOptions): storedConfig => { | ||
const config: storedConfig = { | ||
refreshRetryPeriod: options.refreshRetryPeriod, | ||
baseUrl: options.baseUrl, | ||
useCookie: options.useCookie, | ||
cookiePath: options.cookiePath, | ||
cookieDomain: options.cookieDomain, | ||
}; | ||
return config; | ||
}; | ||
|
||
export const storeConfig = (options: SdkOptions, productDetails: ProductDetails) => { | ||
if (options.useCookie) { | ||
setConfigCookie(options, productDetails); | ||
} else { | ||
setConfigToLocalStorage(options, productDetails); | ||
} | ||
}; | ||
|
||
export const loadConfig = ( | ||
options: SdkOptions, | ||
productDetails: ProductDetails | ||
): storedConfig | null => { | ||
if (options.useCookie) { | ||
return loadConfigFromCookie(productDetails); | ||
} else { | ||
return loadConfigFromLocalStorage(productDetails); | ||
} | ||
}; | ||
|
||
const setConfigCookie = (options: SdkOptions, productDetails: ProductDetails) => { | ||
const cookieDomain = options.cookieDomain; | ||
const path = options.cookiePath ?? '/'; | ||
const value = JSON.stringify(getConfigFromSdkOptions(options)); | ||
let cookie = | ||
productDetails.cookieName + '_config' + '=' + encodeURIComponent(value) + ' ;path=' + path; | ||
if (typeof cookieDomain !== 'undefined') { | ||
cookie += ';domain=' + cookieDomain; | ||
} | ||
document.cookie = cookie; | ||
}; | ||
|
||
const removeConfigCookie = (productDetails: ProductDetails) => { | ||
document.cookie = | ||
productDetails.cookieName + '_config' + '=;expires=Tue, 1 Jan 1980 23:59:59 GMT'; | ||
}; | ||
|
||
const getConfigCookie = (productDetails: ProductDetails) => { | ||
const docCookie = document.cookie; | ||
if (docCookie) { | ||
const payload = docCookie | ||
.split('; ') | ||
.find((row) => row.startsWith(productDetails.cookieName + '_config' + '=')); | ||
if (payload) { | ||
return decodeURIComponent(payload.split('=')[1]); | ||
} | ||
} | ||
}; | ||
|
||
const loadConfigFromCookie = (productDetails: ProductDetails): storedConfig | null => { | ||
const cookieData = getConfigCookie(productDetails); | ||
if (cookieData) { | ||
const result = JSON.parse(cookieData) as storedConfig; | ||
return result; | ||
} | ||
return null; | ||
}; | ||
|
||
const setConfigToLocalStorage = (options: SdkOptions, productDetails: ProductDetails) => { | ||
const value = JSON.stringify(getConfigFromSdkOptions(options)); | ||
localStorage.setItem(productDetails.localStorageKey + '_config', value); | ||
}; | ||
|
||
const removeConfigFromLocalStorage = (productDetails: ProductDetails) => { | ||
localStorage.removeItem(productDetails.localStorageKey + '_config'); | ||
}; | ||
|
||
const loadConfigFromLocalStorage = (productDetails: ProductDetails): storedConfig | null => { | ||
const data = localStorage.getItem(productDetails.localStorageKey + '_config'); | ||
if (data) { | ||
const result = JSON.parse(data) as storedConfig; | ||
return result; | ||
} | ||
return null; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters