@yoctol/kurator
Advanced tools
Comparing version 0.5.1 to 0.5.3
{ | ||
"name": "@yoctol/kurator", | ||
"license": "UNLICENSED", | ||
"version": "0.5.1", | ||
"version": "0.5.3", | ||
"main": "src/index.js", | ||
@@ -6,0 +6,0 @@ "files": [ |
@@ -11,2 +11,6 @@ const BottenderTransformer = require('../BottenderTransformer'); | ||
}, | ||
kurator: { | ||
getLocalAction: jest.fn(), | ||
getRemoteAction: jest.fn(), | ||
}, | ||
}; | ||
@@ -1094,2 +1098,82 @@ } | ||
}); | ||
it('should support rpc', async () => { | ||
const { context, kurator } = setup(); | ||
kurator.getLocalAction.mockReturnValue(async _context => { | ||
await _context.sendText('world'); | ||
}); | ||
const action = BottenderTransformer.createFromActionDefinition( | ||
{ | ||
actions: [ | ||
{ | ||
type: 'text', | ||
descriptor: { | ||
text: 'hello', | ||
}, | ||
}, | ||
{ | ||
type: 'rpc', | ||
descriptor: { | ||
name: 'world', | ||
}, | ||
}, | ||
], | ||
}, | ||
{ | ||
kurator, | ||
} | ||
); | ||
await action(context); | ||
expect(kurator.getLocalAction).toBeCalledWith('world', { | ||
byReference: true, | ||
}); | ||
expect(context.sendText).toHaveBeenCalledTimes(2); | ||
expect(context.sendText.mock.calls[0][0]).toBe('hello'); | ||
expect(context.sendText.mock.calls[1][0]).toBe('world'); | ||
}); | ||
it('should support bridge', async () => { | ||
const { context, kurator } = setup(); | ||
kurator.getRemoteAction.mockReturnValue(async _context => { | ||
await _context.sendText('world'); | ||
}); | ||
const action = BottenderTransformer.createFromActionDefinition( | ||
{ | ||
actions: [ | ||
{ | ||
type: 'text', | ||
descriptor: { | ||
text: 'hello', | ||
}, | ||
}, | ||
{ | ||
type: 'bridge', | ||
descriptor: { | ||
bridgeActionId: '202468246467646540', | ||
}, | ||
}, | ||
], | ||
}, | ||
{ | ||
kurator, | ||
} | ||
); | ||
await action(context); | ||
expect(kurator.getRemoteAction).toBeCalledWith('202468246467646540', { | ||
byReference: true, | ||
}); | ||
expect(context.sendText).toHaveBeenCalledTimes(2); | ||
expect(context.sendText.mock.calls[0][0]).toBe('hello'); | ||
expect(context.sendText.mock.calls[1][0]).toBe('world'); | ||
}); | ||
}); |
@@ -77,3 +77,3 @@ const B = require('bottender-compose'); | ||
probabilities, | ||
actions.map(toBottenderComposeActionWithOptions(options)) // eslint-disable-line no-use-before-define | ||
actions.map(createBottenderComposeActionFactory(options)) // eslint-disable-line no-use-before-define | ||
) | ||
@@ -83,3 +83,3 @@ ); | ||
// eslint-disable-next-line no-use-before-define | ||
return B.random(actions.map(toBottenderComposeActionWithOptions(options))); | ||
return B.random(actions.map(createBottenderComposeActionFactory(options))); | ||
} | ||
@@ -102,3 +102,6 @@ | ||
function toBottenderComposeActionWithOptions(options = {}) { | ||
function createBottenderComposeActionFactory({ | ||
kurator, | ||
...otherOptions | ||
} = {}) { | ||
return (action, index, actions) => { | ||
@@ -108,3 +111,3 @@ const nextAction = actions[index + 1]; | ||
const messageOptions = { | ||
...options, | ||
...otherOptions, | ||
}; | ||
@@ -125,2 +128,10 @@ | ||
return toCarouselAction(action, messageOptions); | ||
case 'rpc': | ||
return kurator.getLocalAction(action.descriptor.name, { | ||
byReference: true, | ||
}); | ||
case 'bridge': | ||
return kurator.getRemoteAction(action.descriptor.bridgeActionId, { | ||
byReference: true, | ||
}); | ||
default: | ||
@@ -132,8 +143,8 @@ } | ||
function toBottenderComposeAction(action) { | ||
return toBottenderComposeActionWithOptions()(action, 0, [action]); | ||
return createBottenderComposeActionFactory()(action, 0, [action]); | ||
} | ||
function createFromActionDefinition(def) { | ||
function createFromActionDefinition(def, { kurator } = {}) { | ||
const validActions = def.actions | ||
.map(toBottenderComposeActionWithOptions()) | ||
.map(createBottenderComposeActionFactory({ kurator })) | ||
.filter(action => typeof action === 'function'); | ||
@@ -140,0 +151,0 @@ return B.series(validActions); |
@@ -45,3 +45,5 @@ const B = require('bottender-compose'); | ||
actionDefinition.name, | ||
BottenderTransformer.createFromActionDefinition(actionDefinition) | ||
BottenderTransformer.createFromActionDefinition(actionDefinition, { | ||
kurator: this, | ||
}) | ||
) | ||
@@ -87,8 +89,38 @@ ); | ||
getRemoteAction(actionId) { | ||
getRemoteAction(actionId, { byReference = false, nullable = false } = {}) { | ||
if (byReference) { | ||
return (...args) => { | ||
const action = this._remoteActions[actionId]; | ||
if (!action) { | ||
if (nullable) return; | ||
return B.warn(`Remote Action ID: ${actionId} is not yet implemented`)( | ||
...args | ||
); | ||
} | ||
return action(...args); | ||
}; | ||
} | ||
return this._remoteActions[actionId]; | ||
} | ||
getLocalAction(actionId) { | ||
return this._localActions[actionId]; | ||
getLocalAction(name, { byReference = false, nullable = false } = {}) { | ||
if (byReference) { | ||
return (...args) => { | ||
const action = this._localActions[name]; | ||
if (!action) { | ||
if (nullable) return; | ||
return B.warn(`Local Action Name: ${name} is not yet implemented`)( | ||
...args | ||
); | ||
} | ||
return action(...args); | ||
}; | ||
} | ||
return this._localActions[name]; | ||
} | ||
@@ -95,0 +127,0 @@ |
45744
1565