Created
August 22, 2018 14:53
-
-
Save rgstephens/6dd796a8fd7a7dfba119f4e0f9335ef9 to your computer and use it in GitHub Desktop.
PartitionKeyGenerators.ts
This file contains hidden or 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
/* | |
* Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. | |
* Licensed under the Apache License, Version 2.0 (the "License"). | |
* You may not use this file except in compliance with the License. | |
* A copy of the License is located at | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* or in the "license" file accompanying this file. This file is distributed | |
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | |
* express or implied. See the License for the specific language governing | |
* permissions and limitations under the License. | |
*/ | |
'use strict'; | |
import { RequestEnvelope } from 'ask-sdk-model'; | |
import { createAskSdkError } from './AskSdkUtils'; | |
/** | |
* Type definition of function used by {@link DynamoDbPersistenceAdapter} to extract attributes id from {@link RequestEnvelope}. | |
*/ | |
export type PartitionKeyGenerator = (requestEnvelope : RequestEnvelope) => string; | |
/** | |
* Object containing implementations of {@link PartitionKeyGenerator}. | |
*/ | |
export const PartitionKeyGenerators = { | |
/** | |
* Gets attributes id using user id. | |
* @param {RequestEnvelope} requestEnvelope | |
* @returns {string} | |
*/ | |
userId(requestEnvelope : RequestEnvelope) : string { | |
if (!(requestEnvelope | |
&& requestEnvelope.context | |
&& requestEnvelope.context.System | |
&& requestEnvelope.context.System.user | |
&& requestEnvelope.context.System.user.userId)) { | |
throw createAskSdkError( | |
'PartitionKeyGenerators', | |
'Cannot retrieve user id from request envelope!', | |
); | |
} | |
return requestEnvelope.context.System.user.userId; | |
}, | |
/** | |
* Gets attributes id using device id. | |
* @param {RequestEnvelope} requestEnvelope | |
* @returns {string} | |
*/ | |
deviceId(requestEnvelope : RequestEnvelope) : string { | |
if (!(requestEnvelope | |
&& requestEnvelope.context | |
&& requestEnvelope.context.System | |
&& requestEnvelope.context.System.device | |
&& requestEnvelope.context.System.device.deviceId)) { | |
throw createAskSdkError( | |
'PartitionKeyGenerators', | |
'Cannot retrieve device id from request envelope!', | |
); | |
} | |
return requestEnvelope.context.System.device.deviceId; | |
}, | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment