forked from aws-amplify/amplify-js
-
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.
refactor: Updated pinpoint provider (#5)
refactor: Updated pinpoint provider * Removed race condition on initialization * Added private API for updating endpoint * Updated to use pinpoint client more directly without need to add client abstraction layer * Added shared endpoint util * Refactored and renamed cached endpoint util * Add types to cachedUuid functions * Add types to cachedUuid function parameters Co-authored-by: Chris Fang <chrfang@amazon.com>
- Loading branch information
Showing
9 changed files
with
147 additions
and
157 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
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,5 @@ | ||
{ | ||
"ignore_dirs": [ | ||
"node_modules" | ||
] | ||
} |
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,37 @@ | ||
import Cache from '../AsyncStorageCache'; | ||
import { v1 as uuid } from 'uuid'; | ||
|
||
const uuids = {}; | ||
const promises = {}; | ||
|
||
export const getCachedUuid = (cacheKey: string): Promise<string> => { | ||
// if uuid for a key has been created, just resolve | ||
if (uuids[cacheKey]) { | ||
return Promise.resolve(uuids[cacheKey]); | ||
} | ||
// if uuid for a key has not been created, ensure only one creation process is running | ||
if (!promises[cacheKey]) { | ||
promises[cacheKey] = getUuid(cacheKey); | ||
} | ||
return promises[cacheKey]; | ||
}; | ||
|
||
const getUuid = (cacheKey: string): Promise<string> => | ||
new Promise(async (resolve, reject) => { | ||
try { | ||
const cachedUuid = await Cache.getItem(cacheKey); | ||
if (cachedUuid) { | ||
uuids[cacheKey] = cachedUuid; | ||
resolve(uuids[cacheKey]); | ||
return; | ||
} | ||
const generatedUuid = uuid(); | ||
await Cache.setItem(cacheKey, generatedUuid); | ||
uuids[cacheKey] = generatedUuid; | ||
resolve(uuids[cacheKey]); | ||
} catch (err) { | ||
reject(err); | ||
} finally { | ||
delete promises[cacheKey]; | ||
} | ||
}); |
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
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 |
---|---|---|
@@ -1,47 +1,22 @@ | ||
import { InAppNotificationsResponse } from '../../types'; | ||
require('aws-sdk/lib/node_loader'); | ||
|
||
import PinpointMessagesClient from './pinpointClient'; | ||
const AWS = require('aws-sdk/lib/core'); | ||
const Service = AWS.Service; | ||
const apiLoader = AWS.apiLoader; | ||
|
||
// Gamma URL | ||
// const url = 'https://714kasriok.execute-api.us-east-1.amazonaws.com'; | ||
// Prod | ||
const url = 'https://pinpoint.us-east-1.amazonaws.com/'; | ||
const PINPOINT = 'pinpoint'; | ||
|
||
export function getInAppMessages({ appId, credentials, endpointId, region }) { | ||
return getInAppCampaigns({ | ||
appId, | ||
credentials, | ||
endpointId, | ||
region, | ||
}); | ||
} | ||
apiLoader.services[PINPOINT] = {}; | ||
AWS.Pinpoint = Service.defineService('pinpoint', ['2016-12-01']); | ||
Object.defineProperty(apiLoader.services[PINPOINT], '2016-12-01', { | ||
get: function get() { | ||
const model = require('./pinpoint-2016-12-01.min.json'); | ||
return model; | ||
}, | ||
enumerable: true, | ||
configurable: true, | ||
}); | ||
|
||
export async function getInAppCampaigns({ | ||
appId, | ||
credentials, | ||
endpointId, | ||
region, | ||
}) { | ||
const options = { | ||
endpoint: url, | ||
region, | ||
...credentials, | ||
}; | ||
const pinpointInternal = new PinpointMessagesClient(options); | ||
const PinpointClient = AWS.Pinpoint; | ||
|
||
return pinpointInternal | ||
.getInAppMessages(getInAppMessagesRequest(appId, endpointId)) | ||
.promise() | ||
.then( | ||
(response: InAppNotificationsResponse) => | ||
response.InAppMessagesResponse.InAppMessageCampaigns | ||
) | ||
.catch(err => console.warn('Error: ' + err)); | ||
} | ||
|
||
function getInAppMessagesRequest(appId: string, endpointId: string) { | ||
return { | ||
ApplicationId: appId, | ||
EndpointId: endpointId, | ||
}; | ||
} | ||
export default PinpointClient; |
Oops, something went wrong.