Skip to content

Commit

Permalink
fix: update attributes persistence behavior of ask-sdk-v1adapter
Browse files Browse the repository at this point in the history
This commit contains the following changes:
- fix a bug where persistent attributes will be wipped out when using
v1 adapter
- update tsconfig.json
- add tsconfig.json to root directory

fixes issue #414
  • Loading branch information
Zhang authored and tianrenz committed Jun 22, 2018
1 parent 8d6bd6f commit 51432df
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 23 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
dist/
node_modules/
package-lock.json
lerna-debug.log
7 changes: 4 additions & 3 deletions ask-sdk-core/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
"rootDir": "lib",
"outDir" : "dist",
"sourceMap" : true,
"inlineSources" : false,
"alwaysStrict" : true,
"removeComments" : false,
"declaration" : true,
"lib": ["es2017", "dom"]
"lib": [
"es2017",
"dom"
]
},
"include": ["lib"]
}
7 changes: 4 additions & 3 deletions ask-sdk-dynamodb-persistence-adapter/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
"rootDir": "lib",
"outDir" : "dist",
"sourceMap" : true,
"inlineSources" : false,
"alwaysStrict" : true,
"removeComments" : false,
"declaration" : true,
"lib": ["es2017", "dom"]
"lib": [
"es2017",
"dom"
]
},
"include": ["lib"]
}
20 changes: 18 additions & 2 deletions ask-sdk-v1adapter/lib/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ import {
RequestHandler,
SkillBuilders,
} from 'ask-sdk';
import { RequestEnvelope, Response, ResponseEnvelope } from 'ask-sdk-model';
import {
RequestEnvelope,
Response,
ResponseEnvelope,
} from 'ask-sdk-model';
import { DynamoDB } from 'aws-sdk';
import { EventEmitter } from 'events';
import * as i18n from 'i18next';
Expand Down Expand Up @@ -50,6 +54,18 @@ export class Adapter extends EventEmitter {

constructor(event : RequestEnvelope, context : any, callback? : (err : Error, result? : any) => void) {
super();
if (!event.session) {
event.session = {
new : undefined,
sessionId : undefined,
user : undefined,
application : undefined,
attributes : {},
};
} else if (!event.session.attributes) {
event.session.attributes = {};
}

this._event = event;
this._context = context;
this._callback = callback;
Expand Down Expand Up @@ -149,7 +165,7 @@ function ValidateRequest() : void {
}
}

if (this.dynamoDBTableName && (!this._event.session || this._event.session.new)) {
if (this.dynamoDBTableName && (!this._event.session.sessionId || this._event.session.new)) {
if (!dynamoDbPersistenceAdapter) {
dynamoDbPersistenceAdapter = new DynamoDbPersistenceAdapter({
createTable : true,
Expand Down
6 changes: 1 addition & 5 deletions ask-sdk-v1adapter/lib/responseHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

import { DynamoDbPersistenceAdapter } from 'ask-sdk';
import { Intent } from 'ask-sdk-model';
import { DynamoDB } from 'aws-sdk';
import { V1Handler } from './v1Handler';

let dynamoDbPersistenceAdapter : DynamoDbPersistenceAdapter;
Expand Down Expand Up @@ -198,10 +197,7 @@ export const ResponseHandlers : V1Handler = {
if (!dynamoDbPersistenceAdapter) {
dynamoDbPersistenceAdapter = new DynamoDbPersistenceAdapter({
createTable : true,
dynamoDBClient : this.handler.dynamoDBClient ? this.handler.dynamoDBClient : new DynamoDB({
apiVersion: '2012-08-10',
region: 'us-east-1',
}),
dynamoDBClient : this.handler.dynamoDBClient,
partitionKeyName : 'userId',
attributesName : 'mapAttr',
tableName : this.handler.dynamoDBTableName,
Expand Down
7 changes: 4 additions & 3 deletions ask-sdk-v1adapter/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
"rootDir": "lib",
"outDir" : "dist",
"sourceMap" : true,
"inlineSources" : false,
"alwaysStrict" : true,
"removeComments" : false,
"declaration" : true,
"lib": ["es2017", "dom"]
"lib": [
"es2017",
"dom"
]
},
"include": ["lib"]
}
18 changes: 15 additions & 3 deletions ask-sdk-v1adapter/tst/adapter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@

'use strict';

import { RequestEnvelope, ResponseEnvelope, ui } from 'ask-sdk-model';
import { ResponseEnvelope } from 'ask-sdk-model';
import { expect } from 'chai';
import * as sinon from 'sinon';
import { Adapter } from '../lib/adapter';
import { LaunchRequest, RecipeIntentRequest } from './mock/mockSampleRequest';
import { LaunchRequest, PlaybackControllerRequest } from './mock/mockSampleRequest';
import { mockV2Requesthandler } from './mock/mockV2RequestHandler';

const mockContext = {
Expand All @@ -30,13 +30,25 @@ const mockContext = {
};
/* tslint:disable */
describe('Adapter', () => {
it('should pass lambda inputs to adapter by handler function', () => {
it('should pass lambda inputs /to adapter by handler function', () => {
const adapter = new Adapter(LaunchRequest, mockContext);

expect(adapter._event).to.deep.equal(LaunchRequest);
expect(adapter._context).to.deep.equal(mockContext);
});

it('should add missing session object', () => {
const adapter = new Adapter(PlaybackControllerRequest, mockContext);

expect(adapter._event.session).to.deep.equal({
application: undefined,
attributes: {},
new: undefined,
sessionId: undefined,
user: undefined,
})
});

it('should be able to add V2 style request handlers', () => {
const adapter = new Adapter(LaunchRequest, mockContext);

Expand Down
7 changes: 4 additions & 3 deletions ask-sdk/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
"rootDir": "lib",
"outDir" : "dist",
"sourceMap" : true,
"inlineSources" : false,
"alwaysStrict" : true,
"removeComments" : false,
"declaration" : true,
"lib": ["es2017", "dom"]
"lib": [
"es2017",
"dom"
]
},
"include": ["lib"]
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
"clean-all": "npm run clean-build && npm run clean-node_modules"
},
"devDependencies": {
"lerna": "^2.10.2"
"lerna": "^2.11.0"
}
}
24 changes: 24 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"compilerOptions" : {
"target" : "es5",
"module" : "commonjs",
"moduleResolution": "node",
"outDir" : "dist",
"sourceMap" : true,
"alwaysStrict" : true,
"declaration" : true,
"lib": [
"es2017",
"dom"
]
},
"include" : [
"ask-sdk/lib",
"ask-sdk-core/lib",
"ask-sdk-dynamodb-persistence-adapter/lib",
"ask-sdk-v1adapter/lib"
],
"exclude" : [
"node_modules"
]
}

0 comments on commit 51432df

Please sign in to comment.