Skip to content

Commit

Permalink
feat(tailor): make possible to add custom event during template fetching
Browse files Browse the repository at this point in the history
- by adding tailor object (event emitter) as third parameter of  function

fix zalando#347
  • Loading branch information
AdelineCaro committed Jul 26, 2022
1 parent 985a8e4 commit f0ce0e5
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 4 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ server.listen(process.env.PORT || 8080);
## Options

* `fetchContext(request)` - Function that returns a promise of the context, that is an object that maps fragment id to fragment url, to be able to override urls of the fragments on the page, defaults to `Promise.resolve({})`
* `fetchTemplate(request, parseTemplate)` - Function that should fetch the template, call `parseTemplate` and return a promise of the result. Useful to implement your own way to retrieve and cache the templates, e.g. from s3.
Default implementation [`lib/fetch-template.js`](https://github.com/zalando/tailor/blob/master/lib/fetch-template.js) fetches the template from the file system
* `fetchTemplate(request, parseTemplate, eventEmitter)` - Function that should fetch the template, call `parseTemplate` and return a promise of the result. Useful to implement your own way to retrieve and cache the templates, e.g. from s3.
Default implementation [`lib/fetch-template.js`](https://github.com/zalando/tailor/blob/master/lib/fetch-template.js) fetches the template from the file system.
`eventEmitter` allow you to emit your own events that you can catch like native [Tailor events](https://github.com/zalando/tailor/blob/master/docs/Events.md).
* `templatesPath` - To specify the path where the templates are stored locally, Defaults to `/templates/`
* `fragmentTag` - Name of the fragment tag, defaults to `fragment`
* `handledTags` - An array of custom tags, check [`tests/handle-tag`](https://github.com/zalando/tailor/blob/master/tests/handle-tag.js) for more info
Expand Down
14 changes: 14 additions & 0 deletions docs/Events.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,19 @@ Events may be used for logging and monitoring. Check [perf/benchmark.js](https:/
* Error: `fragment:error(request, fragment.attributes, error)` in case of socket error, timeout, 50x
* Fallback: `fragment:fallback(request, fragment.attributes, error)` in case of timeout/error from the fragment if the *fallback-src* is specified

## Custom events

It is possible to emit custom event during template fetching. You need to override `fetchTemplate` Tailor option. `fetchTemplate` is Tailor event emitter that you can use to emit your own events like this :

```js
const fetchTemplate = (request, parseTemplate, eventEmitter) => {
// [Your logic]

eventEmitter.emit('myEvent', {foo: 'bar' });

parseTemplate();
}
```


**Note:** `fragment:response`, `fragment:fallback` and `fragment:error` are mutually exclusive. `fragment:end` happens only in case of successful response.
2 changes: 1 addition & 1 deletion lib/request-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ module.exports = function processRequest(options, request, response) {
this.emit('context:error', request, err);
return {};
});
const templatePromise = fetchTemplate(request, parseTemplate);
const templatePromise = fetchTemplate(request, parseTemplate, this);
const responseHeaders = {
// Disable cache in browsers and proxies
'Cache-Control': 'no-cache, no-store, must-revalidate',
Expand Down
21 changes: 20 additions & 1 deletion tests/tailor.events.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ describe('Tailor events', () => {
tailor = new Tailor({
fetchContext: mockContext,
pipeDefinition: () => Buffer.from(''),
fetchTemplate: (request, parseTemplate) => {
fetchTemplate: (request, parseTemplate, tailor) => {
const template = mockTemplate(request);
tailor.emit('customEvent', 'bar');
if (template) {
return parseTemplate(template);
} else {
Expand Down Expand Up @@ -159,4 +160,22 @@ describe('Tailor events', () => {
response.on('end', done);
});
});

it('emits a custom event on fetching Template', done => {
const onCustomEvent = sinon.spy();
nock('https://fragment')
.get('/')
.reply(200, 'hello');
mockTemplate.returns('<fragment src="https://fragment">');
tailor.on('customEvent', onCustomEvent);
http.get('http://localhost:8080/template', response => {
response.resume();
response.on('end', () => {
const foo = onCustomEvent.args[0][0];
assert.equal(foo, 'bar');
assert.equal(onCustomEvent.callCount, 1);
done();
});
});
});
});

0 comments on commit f0ce0e5

Please sign in to comment.