[ASK SDK] Create Request Interceptor by TypeScript
When using ASK SDK, we can intercept the request / response by Interceptor. Requirement If you want to create […]
広告ここから
広告ここまで
目次
When using ASK SDK, we can intercept the
Requirement
If you want to create a Interceptor, we have to instal the following packages.
- ask-
sdk -core: Import theHandlerInput
type - ask-
sdk -runtime: Import theRequestIntereptor
interface
$ yarn -D ask-sdk-core ask-sdk-runtime
How to make it
We can make a Interceptor object by the following code.
import { HandlerInput } from 'ask-sdk-core';
import { RequestInterceptor } from 'ask-sdk-runtime'
const Interceptor: RequestInterceptor<HandlerInput> = {
async process(input: HandlerInput) {
console.log(`Input ${JSON.stringify(input)}`)
}
}
If you want to do something, we can do it.
Example: Set a Invokation count
import { HandlerInput } from 'ask-sdk-core';
import { RequestInterceptor } from 'ask-sdk-runtime'
const Interceptor: RequestInterceptor<HandlerInput> = {
async process(input: HandlerInput) {
const { attributesManager } = input
const data = await attributesManager.getPersistentAttributes()
console.log('Before: %j', data)
if (input.context.session && input.context.session.isNewSession) return
console.log('is new session')
const newData = {
...data,
count: data.count || 0 + 1
}
console.log('Before: %j', newData)
attributesManager.setPersistentAttributes(newData);
await attributesManager.savePersistentAttributes();
}
}