Skip to content

Commit

Permalink
fix: general fix on some typo and lint issues (#633)
Browse files Browse the repository at this point in the history

Co-authored-by: Shen Chen <shench@amazon.com>
  • Loading branch information
ShenChen93 and Shen Chen committed Jul 22, 2020
1 parent dffba0f commit 11ce407
Show file tree
Hide file tree
Showing 13 changed files with 159 additions and 13 deletions.
2 changes: 1 addition & 1 deletion ask-sdk-core/lib/attributes/AttributesManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export interface AttributesManager {
* @param {boolean} [useSessionCache=true]
* @returns {Promise<Object.<string, any>>}
*/
getPersistentAttributes(useSessionCache? : boolean, defaultAttribuets?: {[key: string]: any}) : Promise<{[key : string] : any}>;
getPersistentAttributes(useSessionCache? : boolean, defaultAttributes ? : {[key : string] : any}) : Promise<{[key : string] : any}>;
/**
* Overwrites the request attributes value.
* @param {Object.<string, any>} requestAttributes
Expand Down
4 changes: 2 additions & 2 deletions ask-sdk-core/lib/attributes/AttributesManagerFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export class AttributesManagerFactory {

return thisSessionAttributes as T;
},
async getPersistentAttributes(useSessionCache : boolean = true, defaultAttributes?: {[key: string]: any}) : Promise<{[key : string] : any}> {
async getPersistentAttributes(useSessionCache : boolean = true, defaultAttributes? : {[key : string] : any}) : Promise<{[key : string] : any}> {
if (!options.persistenceAdapter) {
throw createAskSdkError(
'AttributesManager',
Expand All @@ -65,7 +65,7 @@ export class AttributesManagerFactory {
persistentAttributesSet = true;
}
if (defaultAttributes && (!thisPersistentAttributes || Object.keys(thisPersistentAttributes).length < 1)) {
thisPersistentAttributes = defaultAttributes
thisPersistentAttributes = defaultAttributes;
}

return thisPersistentAttributes;
Expand Down
2 changes: 2 additions & 0 deletions ask-sdk-core/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,12 @@ export {
getDeviceId,
getUserId,
getDialogState,
getSlotValueV2,
getIntentName,
getLocale,
getRequest,
getRequestType,
getSimpleSlotValues,
getSlot,
getSlotValue,
getSupportedInterfaces,
Expand Down
6 changes: 6 additions & 0 deletions ask-sdk-core/lib/response/ResponseBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,12 @@ export interface ResponseBuilder {
* @returns {ResponseBuilder}
*/
addDirective(directive : Directive) : this;
/**
* Helper method for adding Api response in the response
* @param {any} apiResponse
* @returns {ResponseBuilder}
*/
withApiResponse(apiResponse : any) : this;
/**
* Returns the response object
* @returns {Response}
Expand Down
5 changes: 5 additions & 0 deletions ask-sdk-core/lib/response/ResponseFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,11 @@ export class ResponseFactory {

return this;
},
withApiResponse(apiResponse : any) : ResponseBuilder {
response.apiResponse = apiResponse;

return this;
},
getResponse() : Response {
return response;
},
Expand Down
46 changes: 46 additions & 0 deletions ask-sdk-core/lib/util/RequestEnvelopeUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@

import {
IntentRequest,
ListSlotValue,
Request,
RequestEnvelope,
Session,
SimpleSlotValue,
Slot,
SlotValue,
SupportedInterfaces,
} from 'ask-sdk-model';
import { createAskSdkError } from 'ask-sdk-runtime';
Expand Down Expand Up @@ -213,6 +216,49 @@ export function getSlotValue(requestEnvelope : RequestEnvelope, slotName : strin
`Expecting request type of IntentRequest but got ${getRequestType(requestEnvelope)}.`);
}

/**
* Returns the SlotValue from the given {@link Slot} in the request.
*
* SlotValue will exist for slots using multiple slot value feature. And this method attempts to retrieve the requested {@link Slot}'s SlotValue from the incoming request.
* If the slot or slot.slotValue does not exist in the request, null will be returned.
*
* @param {RequestEnvelope} requestEnvelope
* @param {string} slotName
* @return {SlotValue}
*/
export function getSlotValueV2(requestEnvelope : RequestEnvelope, slotName : string) : SlotValue {
const slot = getSlot(requestEnvelope, slotName);
if (slot && slot.slotValue) {
return slot.slotValue;
}

return null;
}

/**
* Returns all the SimpleSlotValues from the given {@link SlotValue}.
* @param {SlotValue} slotValue
* @return {SimpleSlotValue[]}
*/
export function getSimpleSlotValues(slotValue : SlotValue) : SimpleSlotValue[] {

// If the given slotValue type is SimpleSlotValue, directly return slotValue in an array
if (slotValue.type === 'Simple') {
return [slotValue as SimpleSlotValue];
}

// If the given slotValue type is ListSlotValue
// Loop all the SlotValues and retrieve simpleSlotValues recursively
if (slotValue.type === 'List' && slotValue.values) {
return slotValue.values.reduce(
(simpleSlotValues, value) => simpleSlotValues.concat(getSimpleSlotValues(value)),
[],
);
}

return [];
}

/**
* Retrieves the {@link SupportedInterfaces} from the request.
*
Expand Down
4 changes: 2 additions & 2 deletions ask-sdk-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@
"ask-sdk-runtime": "^2.8.0"
},
"peerDependencies": {
"ask-sdk-model": "^1.9.0"
"ask-sdk-model": "^1.29.0"
},
"devDependencies": {
"@types/chai": "^4.1.2",
"@types/mocha": "^5.0.0",
"@types/node": "^9.6.1",
"@types/sinon": "^7.0.13",
"ask-sdk-model": "^1.9.0",
"ask-sdk-model": "^1.29.0",
"chai": "^4.1.2",
"del": "^3.0.0",
"gulp": "^4.0.0",
Expand Down
23 changes: 19 additions & 4 deletions ask-sdk-core/tst/response/ResponseFactory.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ describe('ResponseFactory', () => {
const textContent = new RichTextContentHelper()
.withPrimaryText('primary text')
.withSecondaryText('secondary text')
.withTertiaryText('teritiary text')
.withTertiaryText('tertiary text')
.getTextContent();

const displayTemplate : BodyTemplate1 = {
Expand Down Expand Up @@ -531,7 +531,7 @@ describe('ResponseFactory', () => {
type : 'RichText',
},
tertiaryText : {
text : 'teritiary text',
text : 'tertiary text',
type : 'RichText',
},
},
Expand Down Expand Up @@ -742,7 +742,7 @@ describe('ResponseFactory', () => {
const textContent = new RichTextContentHelper()
.withPrimaryText('primary text')
.withSecondaryText('secondary text')
.withTertiaryText('teritiary text')
.withTertiaryText('tertiary text')
.getTextContent();

const displayTemplate : BodyTemplate1 = {
Expand Down Expand Up @@ -790,7 +790,7 @@ describe('ResponseFactory', () => {
type : 'RichText',
},
tertiaryText : {
text : 'teritiary text',
text : 'tertiary text',
type : 'RichText',
},
},
Expand Down Expand Up @@ -832,4 +832,19 @@ describe('ResponseFactory', () => {
.reprompt(speechOutput)
.getResponse()).to.deep.equals(expectResponse);
});

it('should build response with apiResponse', () => {
const responseBuilder : ResponseBuilder = ResponseFactory.init();
const apiResponse = {
test: {
title: 'this is a test apiResponse',
},
};
const expectResponse = {
apiResponse,
};
expect(responseBuilder
.withApiResponse(apiResponse)
.getResponse()).to.deep.equals(expectResponse);
});
});
72 changes: 72 additions & 0 deletions ask-sdk-core/tst/util/RequestEnvelopeUtil.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@

import {
IntentRequest,
ListSlotValue,
RequestEnvelope,
SimpleSlotValue,
Slot,
} from 'ask-sdk-model';
import { expect } from 'chai';
Expand All @@ -26,8 +28,10 @@ import {
getLocale,
getRequest,
getRequestType,
getSimpleSlotValues,
getSlot,
getSlotValue,
getSlotValueV2,
getSupportedInterfaces,
getUserId,
isNewSession,
Expand Down Expand Up @@ -70,6 +74,27 @@ describe('RequestEnvelopeUtils', () => {
const requestEnvelopeWithNoDevice : RequestEnvelope = JsonProvider.requestEnvelope();
delete requestEnvelopeWithNoDevice.context.System.device;

const nestedSimpleSlotValue : SimpleSlotValue = {
type: 'Simple',
value: 'nested simple slot',
};
const nestedListSlotValue : ListSlotValue = { type: 'List', values: [nestedSimpleSlotValue] };
const simpleSlot : Slot = JsonProvider.slot();
simpleSlot.value = 'mockSlotValue';
simpleSlot.name = 'mockSlot';
simpleSlot.slotValue = {
type: 'Simple',
value: 'test',
};

const listSlot : Slot = JsonProvider.slot();
listSlot.value = 'mockSlotValue';
listSlot.name = 'mockSlot';
listSlot.slotValue = {
type: 'List',
values: [simpleSlot.slotValue, nestedListSlotValue],
};

it('should be able to get locale', () => {
expect(getLocale(requestEnvelope)).eq('en-US');
});
Expand Down Expand Up @@ -174,6 +199,53 @@ describe('RequestEnvelopeUtils', () => {
expect(getSlotValue(intentRequestEnvelope, 'mockSlot')).eq('mockSlotValue');
});

it('getSimpleSlotValues should be able to get single SimpleSlotValues', () => {

const slotValues = getSimpleSlotValues(simpleSlot.slotValue);
expect(slotValues).deep.eq([simpleSlot.slotValue]);
});

it('getSimpleSlotValues should return empty array when slotValue.values is null', () => {

const emptyListSlotValue : ListSlotValue = { type: 'List', values: null };
const slotValues = getSimpleSlotValues(emptyListSlotValue);
expect(slotValues).deep.eq([]);
});

it('getSimpleSlotValues should return empty array when slotValue.type is invalid', () => {

const emptyListSlotValue = { type: 'Invalid', values: [simpleSlot.slotValue] };
const slotValues = getSimpleSlotValues(emptyListSlotValue as ListSlotValue);
expect(slotValues).deep.eq([]);
});

it('getSimpleSlotValues should be able to get all nested SimpleSlotValues', () => {
const requestWithSimpleSlotValue = { ...intentRequestEnvelope };
(requestWithSimpleSlotValue.request as IntentRequest).intent.slots = {
mockSlot: listSlot,
};
const slotValues = getSimpleSlotValues(listSlot.slotValue);
expect(slotValues).deep.eq([simpleSlot.slotValue, nestedSimpleSlotValue]);
});

it('getSlotValueV2 should return null when slotName is not match', () => {
const requestWithSimpleSlotValue = { ...intentRequestEnvelope };
(requestWithSimpleSlotValue.request as IntentRequest).intent.slots = {
mockSlot: listSlot,
};
const slotValue = getSlotValueV2(intentRequestEnvelope, 'invalidSlotName');
expect(slotValue).deep.eq(null);
});

it('getSlotValueV2 should be able to get SlotValue object', () => {
const requestWithSimpleSlotValue = { ...intentRequestEnvelope };
(requestWithSimpleSlotValue.request as IntentRequest).intent.slots = {
mockSlot: listSlot,
};
const slotValue = getSlotValueV2(intentRequestEnvelope, 'mockSlot');
expect(slotValue).deep.eq(listSlot.slotValue);
});

it('should throw an error if trying to get slot value of non existent slot', () => {
expect(getSlotValue(intentRequestEnvelope, 'non-existentSlot')).eq(null);
});
Expand Down
2 changes: 1 addition & 1 deletion ask-sdk-dynamodb-persistence-adapter/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"@types/mocha": "^5.0.0",
"@types/node": "^9.6.1",
"ask-sdk-core": "^2.8.0",
"ask-sdk-model": "^1.24.0",
"ask-sdk-model": "^1.29.0",
"aws-sdk-mock": "^4.1.0",
"chai": "^4.1.2",
"del": "^3.0.0",
Expand Down
2 changes: 1 addition & 1 deletion ask-sdk-express-adapter/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"@types/sinon": "^7.0.13",
"@types/supertest": "^2.0.7",
"ask-sdk-core": "^2.7.0",
"ask-sdk-model": "^1.9.0",
"ask-sdk-model": "^1.29.0",
"chai": "^4.1.2",
"del": "^3.0.0",
"express": "^4.16.4",
Expand Down
2 changes: 1 addition & 1 deletion ask-sdk-s3-persistence-adapter/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"@types/mocha": "^5.0.0",
"@types/node": "^9.6.1",
"ask-sdk-core": "^2.8.0",
"ask-sdk-model": "^1.9.0",
"ask-sdk-model": "^1.29.0",
"aws-sdk-mock": "^4.1.0",
"chai": "^4.1.2",
"del": "^3.0.0",
Expand Down
2 changes: 1 addition & 1 deletion ask-sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"dependencies": {
"ask-sdk-core": "^2.8.0",
"ask-sdk-dynamodb-persistence-adapter": "^2.8.0",
"ask-sdk-model": "^1.9.0"
"ask-sdk-model": "^1.29.0"
},
"devDependencies": {
"@types/chai": "^4.1.2",
Expand Down

0 comments on commit 11ce407

Please sign in to comment.