-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
55 lines (41 loc) · 1.57 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
'use strict';
const fastifyPlugin = require('fastify-plugin');
const { renderModuleFactory } = require('@angular/platform-server');
function fastifyNgUniversal(fastify, opts, next) {
// set default value for the fastify plugin extraProviders option
opts.extraProviders = opts.extraProviders || [];
// add a reply decorator
fastify.decorateReply('renderNg', function (url, options = {}) {
const serverModule = options.serverModule || opts.serverModule;
const documentTemplate = options.document || opts.document;
let extraProviders = [];
// set default value for the reply decorator extraProviders option
options.extraProviders = options.extraProviders || [];
// append custom extra providers if there is any
extraProviders = extraProviders.concat(opts.extraProviders, options.extraProviders);
// check if the server module has value or not
if (!serverModule) {
this.send(new Error('Missing Angular Server module to render.'));
return;
}
// check if the document template has value or not
if (!documentTemplate) {
this.send(new Error('Missing template where the Angular app will be rendered.'));
return;
}
// assemble the options
const renderOpts = {
document: documentTemplate,
url: url,
extraProviders
};
// render the angular application
renderModuleFactory(serverModule, renderOpts)
.then(html => {
this.header('Content-Type', 'text/html').send(html);
})
;
});
next();
}
module.exports = fastifyPlugin(fastifyNgUniversal, '>=1.5.0');