-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcontext.js
More file actions
100 lines (83 loc) · 3.06 KB
/
context.js
File metadata and controls
100 lines (83 loc) · 3.06 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import { readFileSync } from 'node:fs'
import { basename, join } from 'node:path'
import Boom from '@hapi/boom'
import { StatusCodes } from 'http-status-codes'
import pkg from '~/package.json' with { type: 'json' }
import { config } from '~/src/config/index.js'
import { createLogger } from '~/src/server/common/helpers/logging/logger.js'
import {
checkFormStatus,
encodeUrl,
safeGenerateCrumb
} from '~/src/server/plugins/engine/helpers.js'
const logger = createLogger()
/** @type {Record<string, string> | undefined} */
let webpackManifest
/**
* @param {FormRequest | FormRequestPayload | null} request
*/
export async function context(request) {
const { params, response } = request ?? {}
const { isPreview: isPreviewMode, state: formState } = checkFormStatus(params)
// Only add the slug in to the context if the response is OK.
// Footer meta links are not rendered when the slug is missing.
const isResponseOK =
!Boom.isBoom(response) && response?.statusCode === StatusCodes.OK
const pluginStorage = request?.server.plugins['forms-engine-plugin']
let consumerViewContext = {}
if (!pluginStorage) {
throw Error('context called before plugin registered')
}
if (!pluginStorage.baseLayoutPath) {
throw Error('Missing baseLayoutPath in plugin.options.nunjucks')
}
if (typeof pluginStorage.viewContext === 'function') {
consumerViewContext = await pluginStorage.viewContext(request)
}
/** @type {ViewContext} */
const ctx = {
// take consumers props first so we can override it
...consumerViewContext,
baseLayoutPath: pluginStorage.baseLayoutPath,
appVersion: pkg.version,
config: {
cdpEnvironment: config.get('cdpEnvironment'),
designerUrl: config.get('designerUrl'),
feedbackLink: encodeUrl(config.get('feedbackLink')),
phaseTag: config.get('phaseTag'),
serviceName: config.get('serviceName'),
serviceVersion: config.get('serviceVersion')
},
crumb: safeGenerateCrumb(request),
currentPath: `${request.path}${request.url.search}`,
previewMode: isPreviewMode ? formState : undefined,
slug: isResponseOK ? params?.slug : undefined
}
return ctx
}
/**
* Returns the context for the devtool. Consumers won't have access to this.
* @param {FormRequest | FormRequestPayload | null} _request
* @returns {Record<string, unknown> & { assetPath: string, getDxtAssetPath: (asset: string) => string }}
*/
export function devtoolContext(_request) {
const manifestPath = join(config.get('publicDir'), 'assets-manifest.json')
if (!webpackManifest) {
try {
// eslint-disable-next-line -- Allow JSON type 'any'
webpackManifest = JSON.parse(readFileSync(manifestPath, 'utf-8'))
} catch {
logger.error(`Webpack ${basename(manifestPath)} not found`)
}
}
return {
assetPath: '/assets',
getDxtAssetPath: (asset = '') => {
return `/${webpackManifest?.[asset] ?? asset}`
}
}
}
/**
* @import { ViewContext } from '~/src/server/plugins/nunjucks/types.js'
* @import { FormRequest, FormRequestPayload } from '~/src/server/routes/types.js'
*/