Skip to content

Commit 46ad025

Browse files
Make it easier to mix JSON and YAML files
1 parent f833b73 commit 46ad025

2 files changed

Lines changed: 53 additions & 53 deletions

File tree

src/server/utils/file-form-service.js

Lines changed: 42 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import fs from 'fs/promises'
2+
import path from 'node:path'
23

34
import YAML from 'yaml'
45

56
/**
67
* FileFormService abstract class
78
*/
8-
class FileFormService {
9+
export class FileFormService {
910
/**
1011
* The map of form metadatas by slug
1112
* @type {Map<string, FormMetadata>}
@@ -38,11 +39,47 @@ class FileFormService {
3839
* @param {string} filepath - the file path
3940
* @returns {Promise<FormDefinition>}
4041
*/
41-
// eslint-disable-next-line @typescript-eslint/require-await
4242
async readForm(filepath) {
43-
throw new Error(
44-
`Error reading path '${filepath}'. 'readForm' not implemented in abstract class`
45-
)
43+
const ext = path.extname(filepath).toLowerCase()
44+
45+
switch (ext) {
46+
case '.json':
47+
return this.readJsonForm(filepath)
48+
case '.yaml':
49+
return this.readYamlForm(filepath)
50+
default:
51+
throw new Error(`Invalid file extension '${ext}'`)
52+
}
53+
}
54+
55+
/**
56+
* Read the form definition from a json file
57+
* @param {string} filepath
58+
* @returns {Promise<FormDefinition>}
59+
*/
60+
async readJsonForm(filepath) {
61+
/**
62+
* @type {FormDefinition}
63+
*/
64+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
65+
const definition = JSON.parse(await fs.readFile(filepath, 'utf8'))
66+
67+
return definition
68+
}
69+
70+
/**
71+
* Read the form definition from a yaml file
72+
* @param {string} filepath
73+
* @returns {Promise<FormDefinition>}
74+
*/
75+
async readYamlForm(filepath) {
76+
/**
77+
* @type {FormDefinition}
78+
*/
79+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
80+
const definition = YAML.parse(await fs.readFile(filepath, 'utf8'))
81+
82+
return definition
4683
}
4784

4885
/**
@@ -102,48 +139,6 @@ class FileFormService {
102139
}
103140
}
104141

105-
/**
106-
* JsonFileFormService class
107-
* @augments FileFormService
108-
*/
109-
export class JsonFileFormService extends FileFormService {
110-
/**
111-
* Read the form definition from a json file
112-
* @param {string} filepath
113-
* @returns {Promise<FormDefinition>}
114-
*/
115-
async readForm(filepath) {
116-
/**
117-
* @type {FormDefinition}
118-
*/
119-
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
120-
const definition = JSON.parse(await fs.readFile(filepath, 'utf8'))
121-
122-
return definition
123-
}
124-
}
125-
126-
/**
127-
* YamlFileFormService class
128-
* @augments FileFormService
129-
*/
130-
export class YamlFileFormService extends FileFormService {
131-
/**
132-
* Read the form definition from a yaml file
133-
* @param {string} filepath
134-
* @returns {Promise<FormDefinition>}
135-
*/
136-
async readForm(filepath) {
137-
/**
138-
* @type {FormDefinition}
139-
*/
140-
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
141-
const definition = YAML.parse(await fs.readFile(filepath, 'utf8'))
142-
143-
return definition
144-
}
145-
}
146-
147142
/**
148143
* @import { FormMetadata, FormDefinition } from '@defra/forms-model'
149144
*/

src/server/utils/file-form-service.test.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
import { FormStatus } from '~/src/server/routes/types.js'
2-
import {
3-
JsonFileFormService,
4-
YamlFileFormService
5-
} from '~/src/server/utils/file-form-service.js'
2+
import { FileFormService } from '~/src/server/utils/file-form-service.js'
63

74
// Create the metadata which is shared for all forms
85
const now = new Date()
@@ -29,7 +26,7 @@ const metadata = {
2926

3027
describe('File Form Service', () => {
3128
it('should load JSON files from disk', async () => {
32-
const loader = new JsonFileFormService()
29+
const loader = new FileFormService()
3330

3431
const definition = await loader.addForm(
3532
'src/server/forms/test.json',
@@ -51,7 +48,7 @@ describe('File Form Service', () => {
5148
})
5249

5350
it('should load YAML files from disk', async () => {
54-
const loader = new YamlFileFormService()
51+
const loader = new FileFormService()
5552

5653
const definition = await loader.addForm(
5754
'src/server/forms/test.yaml',
@@ -71,4 +68,12 @@ describe('File Form Service', () => {
7168
"Form definition 'invalid-id' not found"
7269
)
7370
})
71+
72+
it("should throw if the file isn't JSON or YAML", async () => {
73+
const loader = new FileFormService()
74+
75+
await expect(
76+
loader.addForm('src/server/forms/test.txt', metadata)
77+
).rejects.toThrow("Invalid file extension '.txt'")
78+
})
7479
})

0 commit comments

Comments
 (0)