2021-05-05 15:06:19 +00:00
|
|
|
/*
|
|
|
|
For internal API, we will generate an api Key that must be provided as
|
|
|
|
GET parameter for every API call.
|
|
|
|
*/
|
|
|
|
|
2021-12-07 09:29:20 +00:00
|
|
|
async function _getKey ({ storageManager }: RegisterServerOptions, key: string): Promise<string> {
|
|
|
|
let value: string = await storageManager.getData(key)
|
2021-05-05 15:06:19 +00:00
|
|
|
if (!value) {
|
|
|
|
value = Math.random().toString(36).slice(2, 12)
|
2021-12-07 09:29:20 +00:00
|
|
|
await storageManager.storeData(key, value)
|
2021-05-05 15:06:19 +00:00
|
|
|
}
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
|
2021-12-07 09:29:20 +00:00
|
|
|
async function getAPIKey (options: RegisterServerOptions): Promise<string> {
|
|
|
|
return _getKey(options, 'APIKEY')
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getExternalComponentKey (options: RegisterServerOptions, componentName: string): Promise<string> {
|
|
|
|
if (!/^[A-Z]+$/.test(componentName)) {
|
|
|
|
throw new Error('Invalid component name: ' + componentName)
|
|
|
|
}
|
|
|
|
const key = 'EXTERNALCOMPONENTKEY_' + componentName
|
|
|
|
return _getKey(options, key)
|
|
|
|
}
|
|
|
|
|
2021-05-05 15:06:19 +00:00
|
|
|
export {
|
2021-12-07 09:29:20 +00:00
|
|
|
getAPIKey,
|
|
|
|
getExternalComponentKey
|
2021-05-05 15:06:19 +00:00
|
|
|
}
|