@podium/podlet 5.0.0 → 5.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +14 -0
- package/README.md +4 -4
- package/lib/podlet.js +675 -124
- package/package.json +23 -20
- package/types/podlet.d.ts +663 -0
- package/index.d.ts +0 -114
|
@@ -0,0 +1,663 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @typedef {(...args: any) => void} LogFunction
|
|
3
|
+
* @typedef {{ trace: LogFunction, debug: LogFunction, info: LogFunction, warn: LogFunction, error: LogFunction, fatal: LogFunction }} AbsLogger
|
|
4
|
+
*
|
|
5
|
+
* @typedef {Object} PodletOptions
|
|
6
|
+
* @property {string} name - (required) podlet name
|
|
7
|
+
* @property {string} version - (required) podlet version
|
|
8
|
+
* @property {string} pathname - (required) podlet pathname
|
|
9
|
+
* @property {string} [manifest] - path where the podlet manifest file is served from (default '/manifest.json')
|
|
10
|
+
* @property {string} [content] - path where the podlet content HTML markup is served from (default '/')
|
|
11
|
+
* @property {string} [fallback] - path where the podlet fallback HTML markup is served from (default '/fallback')
|
|
12
|
+
* @property {boolean} [development] - a boolean flag that, when true, enables additional development setup (default false)
|
|
13
|
+
* @property {Console | AbsLogger} [logger] - a logger to use when provided. Can be the console object if console logging is desired but can also be any Log4j compatible logging object as well. Nothing is logged if no logger is provided. (default null)
|
|
14
|
+
* @property {import("@podium/proxy").default.PodiumProxyOptions} proxy - options that can be provided to configure the @podium/proxy instance used by the podlet. See that module for details.
|
|
15
|
+
*
|
|
16
|
+
* @typedef {{ debug: 'true' | 'false', locale: string, deviceType: string, requestedBy: string, mountOrigin: string, mountPathname: string, publicPathname: string }} PodletContext
|
|
17
|
+
* @typedef {{ as?: string | false | null, crossorigin?: string | null | boolean, disabled?: boolean | '' | null, hreflang?: string | false | null, title?: string | false | null, media?: string | false | null, rel?: string | false | null, type?: string | false | null, value: string | false | null, data?: Array<{ key: string; value: string }>, strategy?: "beforeInteractive" | "afterInteractive" | "lazy", scope?: "content" | "fallback" | "all", [key: string]: any }} AssetCssLike
|
|
18
|
+
* @typedef {{ value: string | null, crossorigin?: string | null | boolean, type?: string | null | false, integrity?: string | null | false, referrerpolicy?: string | null | false, nomodule?: boolean | null | '', async?: boolean | null | '', defer?: boolean | null | '', data?: Array<{ key: string; value: string }>, strategy?: "beforeInteractive" | "afterInteractive" | "lazy", scope?: "content" | "fallback" | "all", [key: string]: any }} AssetJsLike
|
|
19
|
+
*/
|
|
20
|
+
export default class PodiumPodlet {
|
|
21
|
+
/**
|
|
22
|
+
* Creates a new instance of a Podium podlet which can be used in conjunction with your framework of choice to build podlet server apps.
|
|
23
|
+
* `name`, `version` and `pathname` constructor arguments are required. All other options are optional.
|
|
24
|
+
*
|
|
25
|
+
* * `name` - podlet name (**required**)
|
|
26
|
+
* * `version` - podlet version (**required**)
|
|
27
|
+
* * `pathname` - podlet pathname (**required**)
|
|
28
|
+
* * `manifest` - path where the podlet manifest file is served from (**default** `'/manifest.json'`)
|
|
29
|
+
* * `content` - path where the podlet content HTML markup is served from (**default** `'/'`)
|
|
30
|
+
* * `fallback` - path where the podlet fallback HTML markup is served from (**default** `'/fallback'`)
|
|
31
|
+
* * `development` - a boolean flag that, when true, enables additional development setup (**default** `false`)
|
|
32
|
+
* * `logger` - a logger to use when provided. Can be the console object if console logging is desired but can also be any Log4j compatible logging object as well. Nothing is logged if no logger is provided. (**default** `null`)
|
|
33
|
+
* * `proxy` - options that can be provided to configure the @podium/proxy instance used by the podlet. See that module for details. (**default**: `{}`)
|
|
34
|
+
*
|
|
35
|
+
* @see https://podium-lib.io/docs/api/podlet/#constructor
|
|
36
|
+
* @see https://podium-lib.io/docs/podlet/getting_started
|
|
37
|
+
*
|
|
38
|
+
* @param {PodletOptions} options
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* ```
|
|
42
|
+
* const podlet = new Podlet({ name: 'foo', version: '1.0.0', pathname: '/' });
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
constructor({ name, version, pathname, manifest, fallback, content, logger, development, proxy, }: PodletOptions);
|
|
46
|
+
/**
|
|
47
|
+
* The name that the podlet identifies itself by. (set in the constructor) This is used internally for things like metrics but can also be used by a layout server.
|
|
48
|
+
* This value must be in camelCase.
|
|
49
|
+
*
|
|
50
|
+
* @see https://podium-lib.io/docs/api/podlet/#name
|
|
51
|
+
*
|
|
52
|
+
* @example ```js
|
|
53
|
+
* const podlet = new Podlet({ name: 'foo', ... });
|
|
54
|
+
* podlet.name // foo
|
|
55
|
+
* ```
|
|
56
|
+
*/
|
|
57
|
+
name: string;
|
|
58
|
+
/**
|
|
59
|
+
* The current version of the podlet. (set in the constructor)
|
|
60
|
+
* It is important that this value be updated when a new version of the podlet is deployed since the page (layout)
|
|
61
|
+
* that the podlet is displayed in uses this value to know whether to refresh the podlet's manifest and fallback content or not.
|
|
62
|
+
*
|
|
63
|
+
* @see https://podium-lib.io/docs/api/podlet/#version
|
|
64
|
+
*
|
|
65
|
+
* @example ```js
|
|
66
|
+
* const podlet = new Podlet({ version: '1.0.0', ... });
|
|
67
|
+
* podlet.version // 1.0.0
|
|
68
|
+
* ```
|
|
69
|
+
*/
|
|
70
|
+
version: string;
|
|
71
|
+
/**
|
|
72
|
+
* The podlet development property (set in the constructor)
|
|
73
|
+
* Used to make podlet development simple without the need to run a layout server locally
|
|
74
|
+
* Do not run a podlet in development mode when deploying to production.
|
|
75
|
+
*
|
|
76
|
+
* @see https://podium-lib.io/docs/api/podlet/#development
|
|
77
|
+
* @see https://podium-lib.io/docs/api/podlet/#development-mode
|
|
78
|
+
*
|
|
79
|
+
* @example ```js
|
|
80
|
+
* const podlet = new Podlet({ development: true, ... });
|
|
81
|
+
* podlet.development // true
|
|
82
|
+
* ```
|
|
83
|
+
*/
|
|
84
|
+
development: boolean;
|
|
85
|
+
/**
|
|
86
|
+
* A logger. The abstract logger "Abslog" is used to make it possible to provide different kinds of loggers.
|
|
87
|
+
* The logger can be provided via the 'logger' constructor argument.
|
|
88
|
+
*
|
|
89
|
+
* @see https://www.npmjs.com/package/abslog
|
|
90
|
+
*
|
|
91
|
+
* @example ```js
|
|
92
|
+
* const podlet = new Podlet({ logger: console, ... });
|
|
93
|
+
* podlet.log.trace('trace log to the console')
|
|
94
|
+
* podlet.log.debug('debug log to the console')
|
|
95
|
+
* podlet.log.info('info log to the console')
|
|
96
|
+
* podlet.log.warn('warn log to the console')
|
|
97
|
+
* podlet.log.error('error log to the console')
|
|
98
|
+
* podlet.log.fatal('fatal log to the console')
|
|
99
|
+
* ```
|
|
100
|
+
*
|
|
101
|
+
* @type {AbsLogger}
|
|
102
|
+
*/
|
|
103
|
+
log: AbsLogger;
|
|
104
|
+
/**
|
|
105
|
+
* An instance of the `@podium/proxy` module
|
|
106
|
+
* @see https://github.com/podium-lib/proxy
|
|
107
|
+
*/
|
|
108
|
+
httpProxy: Proxy;
|
|
109
|
+
/**
|
|
110
|
+
* The pathname for the manifest of the podlet. Defaults to /manifest.json. (set in the constructor)
|
|
111
|
+
* The value should be relative to the value set on the pathname argument.
|
|
112
|
+
* In other words if a podlet is mounted into an HTTP server at /foo and the manifest is at /foo/component.json, pathname will be /foo and manifestRoute will be /component.json
|
|
113
|
+
*
|
|
114
|
+
* @see https://podium-lib.io/docs/api/podlet/#manifest
|
|
115
|
+
*
|
|
116
|
+
* @example ```js
|
|
117
|
+
* const podlet = new Podlet({ manifest: '/manifest.json', ... });
|
|
118
|
+
* podlet.manifestRoute // /manifest.json
|
|
119
|
+
* ```
|
|
120
|
+
*/
|
|
121
|
+
manifestRoute: string;
|
|
122
|
+
/**
|
|
123
|
+
* The pathname for the content route of the podlet. Defaults to /. (set in the constructor)
|
|
124
|
+
* The value should be relative to the value set on the pathname argument.
|
|
125
|
+
* In other words if a podlet is mounted into an HTTP server at /foo and the content is at /foo/content, pathname will be /foo and contentRoute will be /content
|
|
126
|
+
*
|
|
127
|
+
* @see https://podium-lib.io/docs/api/podlet/#content
|
|
128
|
+
*
|
|
129
|
+
* @example ```js
|
|
130
|
+
* const podlet = new Podlet({ content: '/foo', ... });
|
|
131
|
+
* podlet.contentRoute // /foo
|
|
132
|
+
* ```
|
|
133
|
+
*/
|
|
134
|
+
contentRoute: string;
|
|
135
|
+
/**
|
|
136
|
+
* The pathname for the fallback route of the podlet. Defaults to /fallback. (set in the constructor)
|
|
137
|
+
* The value should be relative to the value set on the pathname argument.
|
|
138
|
+
* In other words if a podlet is mounted into an HTTP server at /foo and the fallback is at /foo/fallback, pathname will be /foo and fallbackRoute will be /fallback
|
|
139
|
+
*
|
|
140
|
+
* @see https://podium-lib.io/docs/api/podlet/#fallback
|
|
141
|
+
*
|
|
142
|
+
* @example ```js
|
|
143
|
+
* const podlet = new Podlet({ fallback: '/fallback', ... });
|
|
144
|
+
* podlet.fallbackRoute // /fallback
|
|
145
|
+
* ```
|
|
146
|
+
*/
|
|
147
|
+
fallbackRoute: string;
|
|
148
|
+
/**
|
|
149
|
+
* An object that holds information about defined proxy routes. Proxy routes are defined using the podlet.proxy(...) method and up to 4 proxy routes
|
|
150
|
+
* may be defined per podlet.
|
|
151
|
+
*
|
|
152
|
+
* @see https://podium-lib.io/docs/api/podlet#proxy-target-name-
|
|
153
|
+
* @see https://podium-lib.io/docs/podlet/proxying
|
|
154
|
+
*
|
|
155
|
+
* @example ```js
|
|
156
|
+
* const podlet = new Podlet({ ... });
|
|
157
|
+
* podlet.proxy({ target: '/api', name: 'api' })
|
|
158
|
+
* podlet.proxyRoutes // { api: '/api' }
|
|
159
|
+
* ```
|
|
160
|
+
*
|
|
161
|
+
* @type {Record<string, string>}
|
|
162
|
+
*/
|
|
163
|
+
proxyRoutes: Record<string, string>;
|
|
164
|
+
/**
|
|
165
|
+
* An object containing a set of Podium context values configured for podlet development.
|
|
166
|
+
* This is necessary when the podlet is in development mode because requests do not come from a layout (which is what normally sends the context information)
|
|
167
|
+
* These base context values can be overridden by providing a default context using the podlet.defaults() method
|
|
168
|
+
* in which case the baseContext and the defaultContext will be merged together to provide the development context object.
|
|
169
|
+
* This is not used at all when the podlet is not in development mode or when it is in development mode but the request to the podlet comes from a Podium layout.
|
|
170
|
+
*
|
|
171
|
+
* @see https://podium-lib.io/docs/podlet/context
|
|
172
|
+
* @see https://podium-lib.io/docs/api/podlet#development-mode
|
|
173
|
+
* @see https://podium-lib.io/docs/api/podlet#defaultscontext
|
|
174
|
+
*
|
|
175
|
+
* @example ```js
|
|
176
|
+
* const podlet = new Podlet({ name: 'foo', pathname: '/bar', ... });
|
|
177
|
+
* podlet.baseContext; // { debug: 'false', locale: 'en-US', deviceType: 'desktop', requestedBy: this.name, mountOrigin: '', mountPathname: '/bar', publicPathname: '/bar/podium-resource/foo' }
|
|
178
|
+
* ```
|
|
179
|
+
*
|
|
180
|
+
* @type {PodletContext}
|
|
181
|
+
*/
|
|
182
|
+
baseContext: PodletContext;
|
|
183
|
+
/**
|
|
184
|
+
* An object containing a set of Podium context values configured for podlet development.
|
|
185
|
+
* This is necessary when the podlet is in development mode because requests do not come from a layout (which is what normally sends the context information)
|
|
186
|
+
* These default context values override the `baseContext` values set by the package and can be set using the podlet.defaults() method
|
|
187
|
+
* in which case the baseContext and the defaultContext will be merged together to provide the development context object.
|
|
188
|
+
* This is not used at all when the podlet is not in development mode or when it is in development mode but the request to the podlet comes from a Podium layout.
|
|
189
|
+
*
|
|
190
|
+
* @see https://podium-lib.io/docs/podlet/context
|
|
191
|
+
* @see https://podium-lib.io/docs/api/podlet#development-mode
|
|
192
|
+
* @see https://podium-lib.io/docs/api/podlet#defaultscontext
|
|
193
|
+
*
|
|
194
|
+
* @example ```js
|
|
195
|
+
* const podlet = new Podlet({ name: 'foo', pathname: '/bar', ... });
|
|
196
|
+
* podlet.defaults({ debug: 'true', locale: 'nb' });
|
|
197
|
+
* podlet.baseContext; // { debug: 'true', locale: 'nb', deviceType: 'desktop', requestedBy: this.name, mountOrigin: '', mountPathname: '/bar', publicPathname: '/bar/podium-resource/foo' }
|
|
198
|
+
* ```
|
|
199
|
+
*
|
|
200
|
+
* @type {PodletContext}
|
|
201
|
+
*/
|
|
202
|
+
defaultContext: PodletContext;
|
|
203
|
+
/**
|
|
204
|
+
* Property that holds the podlet's CSS asset references. Objects in the array are AssetCss instances. Asset references can be added using the podlet.css() method.
|
|
205
|
+
*
|
|
206
|
+
* @see https://podium-lib.io/docs/api/podlet/#cssoptionsoptions
|
|
207
|
+
* @see https://podium-lib.io/docs/api/assets#assetcss
|
|
208
|
+
* @see https://podium-lib.io/docs/api/assets
|
|
209
|
+
*
|
|
210
|
+
* @example ```js
|
|
211
|
+
* const podlet = new Podlet({ ... });
|
|
212
|
+
* podlet.css({ value: 'https://my.assets.com/styles.css' });
|
|
213
|
+
* podlet.cssRoute // [ AssetCss{ value: 'https://my.assets.com/styles.css' } ]
|
|
214
|
+
* ```
|
|
215
|
+
*
|
|
216
|
+
* @type {AssetCss[]}
|
|
217
|
+
*/
|
|
218
|
+
cssRoute: AssetCss[];
|
|
219
|
+
/**
|
|
220
|
+
* Property that holds the podlet's JS asset references. Objects in the array are AssetJs instances. Asset references can be added using the podlet.js() method.
|
|
221
|
+
*
|
|
222
|
+
* @see https://podium-lib.io/docs/api/podlet/#jsoptionsoptions
|
|
223
|
+
* @see https://podium-lib.io/docs/api/assets#assetjs
|
|
224
|
+
* @see https://podium-lib.io/docs/api/assets
|
|
225
|
+
*
|
|
226
|
+
* @example ```js
|
|
227
|
+
* const podlet = new Podlet({ ... });
|
|
228
|
+
* podlet.js({ value: 'https://my.assets.com/scripts.js' });
|
|
229
|
+
* podlet.jsRoute // [ AssetJs{ value: 'https://my.assets.com/scripts.js' } ]
|
|
230
|
+
* ```
|
|
231
|
+
*
|
|
232
|
+
* @type {AssetJs[]}
|
|
233
|
+
*/
|
|
234
|
+
jsRoute: AssetJs[];
|
|
235
|
+
/**
|
|
236
|
+
* Metrics client stream object that can be used to consume metrics out of a Podium podlet.
|
|
237
|
+
* @see https://www.npmjs.com/package/@metrics/client for detailed documentation
|
|
238
|
+
*
|
|
239
|
+
* @example
|
|
240
|
+
* ```js
|
|
241
|
+
* const podlet = new Podlet(...);
|
|
242
|
+
* podlet.metrics.pipe(...);
|
|
243
|
+
* // or
|
|
244
|
+
* podlet.metrics.on('data', chunk => { ... });
|
|
245
|
+
* ```
|
|
246
|
+
*/
|
|
247
|
+
metrics: Metrics;
|
|
248
|
+
/**
|
|
249
|
+
* Method that returns the pathname for where a podlet is mounted in an HTTP server.
|
|
250
|
+
* It is important that this value matches where the entry point of a route is in an HTTP server since
|
|
251
|
+
* this value is used to define where the manifest is for the podlet.
|
|
252
|
+
* (set in the constructor)
|
|
253
|
+
*
|
|
254
|
+
* @see https://podium-lib.io/docs/api/podlet/#pathname
|
|
255
|
+
*
|
|
256
|
+
* @example
|
|
257
|
+
* The method returns the value of `pathname` as defined in the podlet constructor
|
|
258
|
+
* ```js
|
|
259
|
+
* const podlet = new Podlet({ pathname: '/foo', ... });
|
|
260
|
+
* podlet.pathname() // /foo
|
|
261
|
+
* ```
|
|
262
|
+
*
|
|
263
|
+
* @example
|
|
264
|
+
* This method is typically used when defining routes to ensure the pathname is prepended to any routes
|
|
265
|
+
* ```js
|
|
266
|
+
* const podlet = new Podlet({ pathname: '/foo', content: '/bar', ... });
|
|
267
|
+
* app.get(podlet.pathname() + '/bar', (req, res) => res.podiumSend(...));
|
|
268
|
+
* ```
|
|
269
|
+
*/
|
|
270
|
+
pathname(): string;
|
|
271
|
+
/**
|
|
272
|
+
* Method that returns the pathname for where a podlet's manifest route is to be mounted.
|
|
273
|
+
* By default the podlet's pathname value is prepended to the manifest value.
|
|
274
|
+
*
|
|
275
|
+
* @example
|
|
276
|
+
* Prefix is true by default which will prepend the pathname (/foo) in this example
|
|
277
|
+
* ```js
|
|
278
|
+
* const podlet = new Podlet({ pathname: '/foo', manifest: '/manifest.json', ... });
|
|
279
|
+
* podlet.manifest() // /foo/manifest.json
|
|
280
|
+
* podlet.manifest({ prefix: false }) // /manifest.json
|
|
281
|
+
* ```
|
|
282
|
+
*
|
|
283
|
+
* @example
|
|
284
|
+
* This method is typically used when defining the manifest route
|
|
285
|
+
* ```js
|
|
286
|
+
* const podlet = new Podlet({ ... });
|
|
287
|
+
* app.get(podlet.manifest(), (req, res) => res.send(podlet));
|
|
288
|
+
* ```
|
|
289
|
+
*
|
|
290
|
+
* @param {{ prefix?: boolean }} [options]
|
|
291
|
+
* @returns {string}
|
|
292
|
+
*/
|
|
293
|
+
manifest({ prefix }?: {
|
|
294
|
+
prefix?: boolean;
|
|
295
|
+
}): string;
|
|
296
|
+
/**
|
|
297
|
+
* Method that returns the pathname for where a podlet's content route is to be mounted.
|
|
298
|
+
* By default the podlet's pathname value is prepended to the content value.
|
|
299
|
+
*
|
|
300
|
+
* @example
|
|
301
|
+
* Prefix is true by default which will prepend the pathname (/foo) in this example
|
|
302
|
+
* ```js
|
|
303
|
+
* const podlet = new Podlet({ pathname: '/foo', content: '/', ... });
|
|
304
|
+
* podlet.content() // /foo
|
|
305
|
+
* podlet.content({ prefix: false }) // /
|
|
306
|
+
* ```
|
|
307
|
+
*
|
|
308
|
+
* @example
|
|
309
|
+
* This method is typically used when defining the content route
|
|
310
|
+
* ```js
|
|
311
|
+
* const podlet = new Podlet({ ... });
|
|
312
|
+
* app.get(podlet.content(), (req, res) => res.podiumSend(...));
|
|
313
|
+
* ```
|
|
314
|
+
*
|
|
315
|
+
* @param {{ prefix?: boolean }} [options]
|
|
316
|
+
* @returns {string}
|
|
317
|
+
*/
|
|
318
|
+
content({ prefix }?: {
|
|
319
|
+
prefix?: boolean;
|
|
320
|
+
}): string;
|
|
321
|
+
/**
|
|
322
|
+
* Method that returns the pathname for where a podlet's fallback route is to be mounted.
|
|
323
|
+
* By default the podlet's pathname value is prepended to the fallback value.
|
|
324
|
+
*
|
|
325
|
+
* @example
|
|
326
|
+
* Prefix is true by default which will prepend the pathname (/foo) in this example
|
|
327
|
+
* ```js
|
|
328
|
+
* const podlet = new Podlet({ pathname: '/foo', fallback: '/fallback', ... });
|
|
329
|
+
* podlet.fallback() // /foo/fallback
|
|
330
|
+
* podlet.fallback({ prefix: false }) // /fallback
|
|
331
|
+
* ```
|
|
332
|
+
*
|
|
333
|
+
* @example
|
|
334
|
+
* This method is typically used when defining the fallback route
|
|
335
|
+
* ```js
|
|
336
|
+
* const podlet = new Podlet({ ... });
|
|
337
|
+
* app.get(podlet.fallback(), (req, res) => res.podiumSend(...));
|
|
338
|
+
* ```
|
|
339
|
+
*
|
|
340
|
+
* @param {{ prefix?: boolean }} [options]
|
|
341
|
+
* @returns {string}
|
|
342
|
+
*/
|
|
343
|
+
fallback({ prefix }?: {
|
|
344
|
+
prefix?: boolean;
|
|
345
|
+
}): string;
|
|
346
|
+
/**
|
|
347
|
+
* Method used to set CSS asset references for a podlet. Accepts an AssetCss object, a plain JS object with the same properties as an AssetCss object or
|
|
348
|
+
* an array containing AssetCss or plain JS objects. Asset references set in this way can be accessed via `podlet.cssRoute` and
|
|
349
|
+
* will be added to the podlet manifest file for sending to the layout
|
|
350
|
+
*
|
|
351
|
+
* @see https://podium-lib.io/docs/api/podlet/#cssoptionsoptions
|
|
352
|
+
* @see https://podium-lib.io/docs/api/assets#assetcss
|
|
353
|
+
* @see https://podium-lib.io/docs/api/assets
|
|
354
|
+
*
|
|
355
|
+
* @example ```js
|
|
356
|
+
* const podlet = new Podlet({ ... });
|
|
357
|
+
* podlet.css(new AssetCss{ value: 'https://my.assets.com/styles.css' });
|
|
358
|
+
* podlet.css({ value: 'https://my.assets.com/styles.css' });
|
|
359
|
+
* podlet.css([new AssetCss{ value: 'https://my.assets.com/styles.css' }, { value: 'https://my.assets.com/styles.css' }]);
|
|
360
|
+
* ```
|
|
361
|
+
*
|
|
362
|
+
* @param { AssetCss | AssetCss[] | AssetCssLike | AssetCssLike[] } options
|
|
363
|
+
* @returns {void}
|
|
364
|
+
*/
|
|
365
|
+
css(options: AssetCss | AssetCss[] | AssetCssLike | AssetCssLike[]): void;
|
|
366
|
+
/**
|
|
367
|
+
* Method used to set JS asset references for a podlet. Accepts an AssetJs object, a plain JS object with the same properties as an AssetJs object or
|
|
368
|
+
* an array containing AssetJs or plain JS objects. Asset references set in this way can be accessed via `podlet.jsRoute` and
|
|
369
|
+
* will be added to the podlet manifest file for sending to the layout
|
|
370
|
+
*
|
|
371
|
+
* @see https://podium-lib.io/docs/api/podlet/#jsoptionsoptions
|
|
372
|
+
* @see https://podium-lib.io/docs/api/assets#assetjs
|
|
373
|
+
* @see https://podium-lib.io/docs/api/assets
|
|
374
|
+
*
|
|
375
|
+
* @example ```js
|
|
376
|
+
* const podlet = new Podlet({ ... });
|
|
377
|
+
* podlet.js(new AssetJs{ value: 'https://my.assets.com/scripts.js' });
|
|
378
|
+
* podlet.js({ value: 'https://my.assets.com/scripts.js' });
|
|
379
|
+
* podlet.js([new AssetJs{ value: 'https://my.assets.com/scripts.js' }, { value: 'https://my.assets.com/scripts.js' }]);
|
|
380
|
+
* ```
|
|
381
|
+
*
|
|
382
|
+
* @param {AssetJs | AssetJs[] | AssetJsLike | AssetJsLike[] } [options]
|
|
383
|
+
* @returns {void}
|
|
384
|
+
*/
|
|
385
|
+
js(options?: AssetJs | AssetJs[] | AssetJsLike | AssetJsLike[]): void;
|
|
386
|
+
/**
|
|
387
|
+
* Method for defining proxy targets to be mounted in a layout server.
|
|
388
|
+
* Accepts an object with `target` and `name` keys where target is the relative or absolute path to proxy requests to and name is an identifier
|
|
389
|
+
* to distinguish it from other proxy endpoints. It's common to define a target of "/api" and a name of just "api". The method returns the target so that
|
|
390
|
+
* it's possible to both define the proxy and the route at the same time.
|
|
391
|
+
*
|
|
392
|
+
* For a detailed overview of how proxying works, please see the proxying guide for further details.
|
|
393
|
+
*
|
|
394
|
+
* @see https://podium-lib.io/docs/podlet/proxying
|
|
395
|
+
* @see https://podium-lib.io/docs/api/podlet#proxy-target-name-
|
|
396
|
+
*
|
|
397
|
+
* @example
|
|
398
|
+
* ```js
|
|
399
|
+
* podlet.proxy({ name: 'api', target: '/api' }); // returns /api
|
|
400
|
+
* ```
|
|
401
|
+
*
|
|
402
|
+
* @example
|
|
403
|
+
* Define the proxy and route at the same time
|
|
404
|
+
* ```js
|
|
405
|
+
* // proxy mounted at /api in the app
|
|
406
|
+
* app.get(podlet.proxy({ name: 'api', target: '/api' }), (req, res) => res.sendStatus(200));
|
|
407
|
+
* ```
|
|
408
|
+
*
|
|
409
|
+
* @param {{ target: string; name: string }} options
|
|
410
|
+
* @returns {string}
|
|
411
|
+
*/
|
|
412
|
+
proxy({ target, name }: {
|
|
413
|
+
target: string;
|
|
414
|
+
name: string;
|
|
415
|
+
}): string;
|
|
416
|
+
/**
|
|
417
|
+
* Method to alter the default context set when in development mode.
|
|
418
|
+
* In a production setup, this is not necessary since the context values are sent to the podlet from the layout.
|
|
419
|
+
* By default, the context will contain the following context values, all of which can be overridden.
|
|
420
|
+
*
|
|
421
|
+
* * `debug:` 'false',
|
|
422
|
+
* * `locale:` 'en-EN',
|
|
423
|
+
* * `deviceType:` 'desktop',
|
|
424
|
+
* * `requestedBy:` '<podlet name>',
|
|
425
|
+
* * `mountOrigin:` 'http://localhost:port',
|
|
426
|
+
* * `mountPathname:` '/<podlet pathname>',
|
|
427
|
+
* * `publicPathname:` '/:pathname/podium_resource/:manifestname',
|
|
428
|
+
*
|
|
429
|
+
* @see https://podium-lib.io/docs/api/podlet#defaultscontext
|
|
430
|
+
*
|
|
431
|
+
* @example
|
|
432
|
+
* Example of overriding deviceType
|
|
433
|
+
* ```js
|
|
434
|
+
* const podlet = new Podlet({ ... });
|
|
435
|
+
* podlet.defaults({ deviceType: 'mobile' });
|
|
436
|
+
* ```
|
|
437
|
+
*
|
|
438
|
+
* @param {any} context
|
|
439
|
+
* @returns {any}
|
|
440
|
+
*/
|
|
441
|
+
defaults(context?: any): any;
|
|
442
|
+
/**
|
|
443
|
+
* Method to set a Podium document template to be used when the podlet is in development mode.
|
|
444
|
+
* Must be used in conjunction with with the `.podiumSend()` method or the `podlet.render()` in the content/fallback route to have any effect.
|
|
445
|
+
* Has no effect when the podlet is not in development mode or if a request to the podlet comes from a Podium layout.
|
|
446
|
+
*
|
|
447
|
+
* @see https://podium-lib.io/docs/api/document
|
|
448
|
+
*
|
|
449
|
+
* @example
|
|
450
|
+
* A document template can be provided using the podlet.view method
|
|
451
|
+
* ```js
|
|
452
|
+
* const podlet = new Podlet({ ... });
|
|
453
|
+
* podlet.view(myDocumentTemplate);
|
|
454
|
+
* ```
|
|
455
|
+
*
|
|
456
|
+
* @example
|
|
457
|
+
* You need to call podiumSend or podlet.render to make use of the template you provided with podlet.view
|
|
458
|
+
* ```js
|
|
459
|
+
* app.get(podlet.content(), (req, res) => {
|
|
460
|
+
* res.podiumSend(`...podlet markup here...`);
|
|
461
|
+
* // or
|
|
462
|
+
* podlet.render(res.locals.podium, `...podlet markup here...`)
|
|
463
|
+
* });
|
|
464
|
+
* ```
|
|
465
|
+
*
|
|
466
|
+
* @template {{ [key: string]: unknown }} T
|
|
467
|
+
* @param {( incoming: HttpIncoming<T>, fragment: string, ...args: unknown[]) => string} fn
|
|
468
|
+
* @returns {void}
|
|
469
|
+
*/
|
|
470
|
+
view<T extends {
|
|
471
|
+
[key: string]: unknown;
|
|
472
|
+
}>(fn: (incoming: HttpIncoming<T>, fragment: string, ...args: unknown[]) => string): void;
|
|
473
|
+
/**
|
|
474
|
+
* Method for serialising the podlet instance into a plain JS object. Called automatically when stringifying the podlet with JSON.stringify(podlet).
|
|
475
|
+
* Doing so will result in a correct Podium manifest file string and so is suitable for usage in a manifest route hook.
|
|
476
|
+
*
|
|
477
|
+
* @see https://podium-lib.io/docs/podlet/getting_started#step-7-create-a-manifest-route
|
|
478
|
+
*
|
|
479
|
+
* @example
|
|
480
|
+
* ```js
|
|
481
|
+
* app.get(podlet.manifest(), (req, res) => res.send(JSON.stringify(podlet)));
|
|
482
|
+
* ```
|
|
483
|
+
*
|
|
484
|
+
* @example
|
|
485
|
+
* In frameworks that automatically serialise JS objects, such as Express, you can omit JSON.stringify
|
|
486
|
+
* ```js
|
|
487
|
+
* app.get(podlet.manifest(), (req, res) => res.send(podlet));
|
|
488
|
+
* ```
|
|
489
|
+
*/
|
|
490
|
+
toJSON(): {
|
|
491
|
+
name: string;
|
|
492
|
+
version: string;
|
|
493
|
+
content: string;
|
|
494
|
+
fallback: string;
|
|
495
|
+
css: AssetCss[];
|
|
496
|
+
js: AssetJs[];
|
|
497
|
+
proxy: Record<string, string>;
|
|
498
|
+
};
|
|
499
|
+
/**
|
|
500
|
+
* Method to render the document template. Will, by default, render the document template provided by Podium unless a custom document template is set using the .view method.
|
|
501
|
+
* In most HTTP frameworks this method can be ignored in favour of res.podiumSend().
|
|
502
|
+
* If present, res.podiumSend() has the advantage that it's not necessary to pass in an HttpIncoming object as the first argument.
|
|
503
|
+
*
|
|
504
|
+
* @see https://podium-lib.io/docs/api/podlet#renderhttpincoming-fragment-args
|
|
505
|
+
*
|
|
506
|
+
* @example
|
|
507
|
+
* ```js
|
|
508
|
+
* app.get(podlet.content(), (req, res) => {
|
|
509
|
+
* const incoming = res.locals.podium;
|
|
510
|
+
* const document = podlet.render(incoming, '<div>content to render</div>');
|
|
511
|
+
* res.send(document);
|
|
512
|
+
* });
|
|
513
|
+
* ```
|
|
514
|
+
*
|
|
515
|
+
* @template {{ [key: string]: unknown }} T
|
|
516
|
+
* @param {HttpIncoming<T>} incoming - Instance of Podium HttpIncoming object
|
|
517
|
+
* @param {string} data - the podlet content as an HTML markup string
|
|
518
|
+
* @param {...any} args - additional args depending on the template and what values it accepts
|
|
519
|
+
* @returns {string}
|
|
520
|
+
*/
|
|
521
|
+
render<T_1 extends {
|
|
522
|
+
[key: string]: unknown;
|
|
523
|
+
}>(incoming: HttpIncoming<T_1>, data: string, ...args: any[]): string;
|
|
524
|
+
/**
|
|
525
|
+
* Method for processing an incoming HTTP request. This method is intended to be used to implement support for multiple HTTP frameworks and in most cases will not need to be used directly by podlet developers when creating podlet servers.
|
|
526
|
+
*
|
|
527
|
+
* What it does:
|
|
528
|
+
* * Handles detection of development mode and sets the appropriate defaults
|
|
529
|
+
* * Runs context deserializing on the incoming request and sets a context object at HttpIncoming.context.
|
|
530
|
+
* * Returns an HttpIncoming object.
|
|
531
|
+
*
|
|
532
|
+
* @see https://podium-lib.io/docs/api/podlet#processhttpincoming
|
|
533
|
+
*
|
|
534
|
+
* @param {HttpIncoming} incoming
|
|
535
|
+
* @param {{ proxy?: boolean }} [options]
|
|
536
|
+
* @returns {Promise<HttpIncoming>}
|
|
537
|
+
*/
|
|
538
|
+
process(incoming: HttpIncoming, { proxy }?: {
|
|
539
|
+
proxy?: boolean;
|
|
540
|
+
}): Promise<HttpIncoming>;
|
|
541
|
+
/**
|
|
542
|
+
* A Connect/Express compatible middleware function which takes care of the multiple operations needed for a podlet to operate correctly. This function is more or less a wrapper for the .process() method.
|
|
543
|
+
* Returns an array of middleware that will create an HttpIncoming object and store it at res.locals.podium.
|
|
544
|
+
*
|
|
545
|
+
* **Important:** *This middleware must be mounted before defining any routes.*
|
|
546
|
+
*
|
|
547
|
+
* @see https://podium-lib.io/docs/api/podlet#middleware
|
|
548
|
+
*
|
|
549
|
+
* @example
|
|
550
|
+
* ```js
|
|
551
|
+
* const app = express();
|
|
552
|
+
* app.use(podlet.middleware());
|
|
553
|
+
* ```
|
|
554
|
+
*
|
|
555
|
+
* @returns {(req: any, res: any, next: function) => Promise<void>}
|
|
556
|
+
*/
|
|
557
|
+
middleware(): (req: any, res: any, next: Function) => Promise<void>;
|
|
558
|
+
get [Symbol.toStringTag](): string;
|
|
559
|
+
#private;
|
|
560
|
+
}
|
|
561
|
+
export type LogFunction = (...args: any) => void;
|
|
562
|
+
export type AbsLogger = {
|
|
563
|
+
trace: LogFunction;
|
|
564
|
+
debug: LogFunction;
|
|
565
|
+
info: LogFunction;
|
|
566
|
+
warn: LogFunction;
|
|
567
|
+
error: LogFunction;
|
|
568
|
+
fatal: LogFunction;
|
|
569
|
+
};
|
|
570
|
+
export type PodletOptions = {
|
|
571
|
+
/**
|
|
572
|
+
* - (required) podlet name
|
|
573
|
+
*/
|
|
574
|
+
name: string;
|
|
575
|
+
/**
|
|
576
|
+
* - (required) podlet version
|
|
577
|
+
*/
|
|
578
|
+
version: string;
|
|
579
|
+
/**
|
|
580
|
+
* - (required) podlet pathname
|
|
581
|
+
*/
|
|
582
|
+
pathname: string;
|
|
583
|
+
/**
|
|
584
|
+
* - path where the podlet manifest file is served from (default '/manifest.json')
|
|
585
|
+
*/
|
|
586
|
+
manifest?: string;
|
|
587
|
+
/**
|
|
588
|
+
* - path where the podlet content HTML markup is served from (default '/')
|
|
589
|
+
*/
|
|
590
|
+
content?: string;
|
|
591
|
+
/**
|
|
592
|
+
* - path where the podlet fallback HTML markup is served from (default '/fallback')
|
|
593
|
+
*/
|
|
594
|
+
fallback?: string;
|
|
595
|
+
/**
|
|
596
|
+
* - a boolean flag that, when true, enables additional development setup (default false)
|
|
597
|
+
*/
|
|
598
|
+
development?: boolean;
|
|
599
|
+
/**
|
|
600
|
+
* - a logger to use when provided. Can be the console object if console logging is desired but can also be any Log4j compatible logging object as well. Nothing is logged if no logger is provided. (default null)
|
|
601
|
+
*/
|
|
602
|
+
logger?: Console | AbsLogger;
|
|
603
|
+
/**
|
|
604
|
+
* - options that can be provided to configure the
|
|
605
|
+
*/
|
|
606
|
+
proxy: import("@podium/proxy").default.PodiumProxyOptions;
|
|
607
|
+
};
|
|
608
|
+
export type PodletContext = {
|
|
609
|
+
debug: 'true' | 'false';
|
|
610
|
+
locale: string;
|
|
611
|
+
deviceType: string;
|
|
612
|
+
requestedBy: string;
|
|
613
|
+
mountOrigin: string;
|
|
614
|
+
mountPathname: string;
|
|
615
|
+
publicPathname: string;
|
|
616
|
+
};
|
|
617
|
+
export type AssetCssLike = {
|
|
618
|
+
[key: string]: any;
|
|
619
|
+
as?: string | false | null;
|
|
620
|
+
crossorigin?: string | null | boolean;
|
|
621
|
+
disabled?: boolean | '' | null;
|
|
622
|
+
hreflang?: string | false | null;
|
|
623
|
+
title?: string | false | null;
|
|
624
|
+
media?: string | false | null;
|
|
625
|
+
rel?: string | false | null;
|
|
626
|
+
type?: string | false | null;
|
|
627
|
+
value: string | false | null;
|
|
628
|
+
data?: Array<{
|
|
629
|
+
key: string;
|
|
630
|
+
value: string;
|
|
631
|
+
}>;
|
|
632
|
+
strategy?: "beforeInteractive" | "afterInteractive" | "lazy";
|
|
633
|
+
scope?: "content" | "fallback" | "all";
|
|
634
|
+
};
|
|
635
|
+
export type AssetJsLike = {
|
|
636
|
+
[key: string]: any;
|
|
637
|
+
value: string | null;
|
|
638
|
+
crossorigin?: string | null | boolean;
|
|
639
|
+
type?: string | null | false;
|
|
640
|
+
integrity?: string | null | false;
|
|
641
|
+
referrerpolicy?: string | null | false;
|
|
642
|
+
nomodule?: boolean | null | '';
|
|
643
|
+
async?: boolean | null | '';
|
|
644
|
+
defer?: boolean | null | '';
|
|
645
|
+
data?: Array<{
|
|
646
|
+
key: string;
|
|
647
|
+
value: string;
|
|
648
|
+
}>;
|
|
649
|
+
strategy?: "beforeInteractive" | "afterInteractive" | "lazy";
|
|
650
|
+
scope?: "content" | "fallback" | "all";
|
|
651
|
+
};
|
|
652
|
+
import Proxy from '@podium/proxy';
|
|
653
|
+
import { AssetCss } from '@podium/utils';
|
|
654
|
+
import { AssetJs } from '@podium/utils';
|
|
655
|
+
import Metrics from '@metrics/client';
|
|
656
|
+
import { HttpIncoming } from '@podium/utils';
|
|
657
|
+
declare global {
|
|
658
|
+
namespace Express {
|
|
659
|
+
interface Response {
|
|
660
|
+
podiumSend(fragment: string, ...args: unknown[]): Response;
|
|
661
|
+
}
|
|
662
|
+
}
|
|
663
|
+
}
|