-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathconfigureEnginePlugin.ts
More file actions
88 lines (77 loc) · 2.64 KB
/
configureEnginePlugin.ts
File metadata and controls
88 lines (77 loc) · 2.64 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
import { join, parse } from 'node:path'
import { type FormDefinition } from '@defra/forms-model'
import { FORM_PREFIX } from '~/src/server/constants.js'
import { FormModel } from '~/src/server/plugins/engine/models/FormModel.js'
import { plugin } from '~/src/server/plugins/engine/plugin.js'
import * as defaultServices from '~/src/server/plugins/engine/services/index.js'
import { formsService } from '~/src/server/plugins/engine/services/localFormsService.js'
import { type PluginOptions } from '~/src/server/plugins/engine/types.js'
import { findPackageRoot } from '~/src/server/plugins/engine/vision.js'
import { devtoolContext } from '~/src/server/plugins/nunjucks/context.js'
import { type CacheService } from '~/src/server/services/cacheService.js'
import { type RouteConfig } from '~/src/server/types.js'
export const configureEnginePlugin = async (
{
formFileName,
formFilePath,
services,
controllers,
preparePageEventRequestOptions,
onRequest,
saveAndExit,
ordnanceSurveyApiKey,
ordnanceSurveyApiSecret
}: RouteConfig = {},
cache?: CacheService
): Promise<{
plugin: typeof plugin
options: PluginOptions
}> => {
let model: FormModel | undefined
if (formFileName && formFilePath) {
const definition = await getForm(join(formFilePath, formFileName))
const { name } = parse(formFileName)
const initialBasePath = `${FORM_PREFIX}${name}`
model = new FormModel(
definition,
{ basePath: initialBasePath, ordnanceSurveyApiKey },
services,
controllers
)
}
return {
plugin,
options: {
model,
services: services ?? {
// services for testing, else use the disk loader option for running this service locally
...defaultServices,
formsService: await formsService()
},
controllers,
cache: cache ?? 'session',
nunjucks: {
baseLayoutPath: 'dxt-devtool-baselayout.html',
paths: [join(findPackageRoot(), 'src/server/devserver')] // custom layout to make it really clear this is not the same as the runner
},
viewContext: devtoolContext,
preparePageEventRequestOptions,
onRequest,
baseUrl: 'http://localhost:3009', // always runs locally
saveAndExit,
ordnanceSurveyApiKey,
ordnanceSurveyApiSecret
}
}
}
export async function getForm(importPath: string) {
const { ext } = parse(importPath)
const attributes: ImportAttributes = {
type: ext === '.json' ? 'json' : 'module'
}
const formImport = import(importPath, { with: attributes }) as Promise<{
default: FormDefinition
}>
const { default: definition } = await formImport
return definition
}