soma-core 1.0.2 → 1.1.0
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/app/SomaPageClient.tsx +11 -20
- package/app/[...path]/page.tsx +21 -0
- package/app/api/[...path]/route.ts +86 -77
- package/app/components/SomaHostPage.tsx +133 -0
- package/app/components/SomaHydrationShell.tsx +1 -1
- package/app/favicon.ico/route.ts +13 -0
- package/app/layout.tsx +10 -6
- package/app/page.tsx +7 -18
- package/app/soma/[...path]/route.ts +9 -0
- package/app/{api/soma-manifest → soma/manifest}/route.ts +3 -3
- package/app/soma/page/route.ts +29 -0
- package/app/soma/page-object/route.ts +158 -0
- package/app/soma/route.ts +9 -0
- package/bin/soma-mock.mjs +38 -0
- package/bin/soma.cjs +1 -1
- package/core/api/load-host-service.ts +5 -2
- package/core/base/SomaHttpClient.ts +5 -3
- package/core/compiler/full-manifest-builder.ts +84 -69
- package/core/compiler/host-config-sync.cjs +32 -6
- package/core/compiler/page-base-resolver.ts +3 -0
- package/core/compiler/soma-manifest-webpack-plugin.js +160 -10
- package/core/engine/dom-renderer.ts +160 -24
- package/core/engine/soma-headless-page.ts +86 -0
- package/core/host/app-config-composer.ts +90 -0
- package/core/host/app-config.stub.ts +2 -0
- package/core/host/host-config-paths.cjs +100 -0
- package/core/host/host-config-paths.ts +132 -0
- package/core/host/invoke-host-request-hook.ts +37 -0
- package/core/host/load-host-domain-class.ts +87 -0
- package/core/host/locale.stub.ts +2 -0
- package/core/host/page-response-mode.cjs +96 -0
- package/core/host/page-response-request.ts +35 -0
- package/core/host/request-origin.ts +18 -0
- package/core/host/resolve-host-request-hook.ts +29 -0
- package/core/host/server-page-assets.ts +193 -0
- package/core/host/soma-domain-loaders.stub.ts +6 -0
- package/core/host/soma-domain-loaders.ts +69 -0
- package/core/host/soma-request-hook-host.stub.ts +4 -0
- package/core/host/source-packages.ts +153 -0
- package/core/index.ts +22 -0
- package/core/infra/http/SomaHttpService.ts +23 -14
- package/core/infra/http/soma-server-request-context.ts +126 -0
- package/core/mock/node/catch-all.mjs +35 -0
- package/core/mock/node/cli.d.mts +58 -0
- package/core/mock/node/cli.mjs +243 -0
- package/core/mock/node/context.mjs +167 -0
- package/core/mock/node/environment-policy.mjs +52 -0
- package/core/mock/node/plugin-loader.mjs +212 -0
- package/core/mock/node/preload.mjs +91 -0
- package/core/mock/node/process-lifecycle.mjs +236 -0
- package/core/mock/node/public.d.mts +110 -0
- package/core/mock/node/public.mjs +4 -0
- package/core/mock/node/registry.mjs +336 -0
- package/core/mock/node/schema-generator.mjs +1310 -0
- package/core/mock/testing/host-harness.d.mts +66 -0
- package/core/mock/testing/host-harness.mjs +276 -0
- package/core/mock/testing/host-runtime.mjs +48 -0
- package/core/runtime/SomaRuntimeServices.ts +5 -1
- package/core/runtime/serialize-page-state.ts +152 -0
- package/dist/cli/soma.js +375 -4
- package/dist/core/index.cjs +2957 -2387
- package/dist/tsconfig.types.tsbuildinfo +1 -1
- package/dist/types/base/SomaHttpClient.d.ts +2 -0
- package/dist/types/compiler/full-manifest-builder.d.ts +2 -0
- package/dist/types/engine/dom-renderer.d.ts +6 -0
- package/dist/types/engine/soma-headless-page.d.ts +10 -0
- package/dist/types/host/app-config-composer.d.ts +6 -0
- package/dist/types/host/app-config.stub.d.ts +3 -0
- package/dist/types/host/host-config-paths.d.ts +23 -0
- package/dist/types/host/invoke-host-request-hook.d.ts +8 -0
- package/dist/types/host/load-host-domain-class.d.ts +12 -0
- package/dist/types/host/locale.stub.d.ts +3 -0
- package/dist/types/host/page-response-request.d.ts +9 -0
- package/dist/types/host/request-origin.d.ts +1 -0
- package/dist/types/host/resolve-host-request-hook.d.ts +16 -0
- package/dist/types/host/server-page-assets.d.ts +29 -0
- package/dist/types/host/soma-domain-loaders.d.ts +19 -0
- package/dist/types/host/soma-domain-loaders.stub.d.ts +4 -0
- package/dist/types/host/soma-request-hook-host.stub.d.ts +2 -0
- package/dist/types/host/source-packages.d.ts +12 -0
- package/dist/types/index.d.ts +3 -0
- package/dist/types/infra/http/SomaHttpService.d.ts +2 -0
- package/dist/types/infra/http/soma-server-request-context.d.ts +5 -0
- package/dist/types/runtime/serialize-page-state.d.ts +10 -0
- package/docs//351/241/265/351/235/242JSON/346/250/241/345/274/217/346/216/245/345/205/245/346/214/207/345/215/227.md +141 -0
- package/global.d.ts +74 -33
- package/middleware.ts +33 -0
- package/next.config.js +166 -16
- package/package.json +47 -13
- package/tailwind.config.ts +21 -17
- package/tsconfig.json +1 -5
- package/app/api/page/route.ts +0 -99
- package/app/loading.tsx +0 -5
|
@@ -0,0 +1,336 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { http, HttpResponse } from 'msw';
|
|
4
|
+
import { stableSerialize } from './schema-generator.mjs';
|
|
5
|
+
|
|
6
|
+
const HTTP_METHODS = new Set(['ALL', 'GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'HEAD', 'OPTIONS']);
|
|
7
|
+
const BODYLESS_STATUS_CODES = new Set([204, 205, 304]);
|
|
8
|
+
|
|
9
|
+
function hasOwn(value, key) {
|
|
10
|
+
return Object.prototype.hasOwnProperty.call(value, key);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function assertObject(value, label) {
|
|
14
|
+
if (!value || typeof value !== 'object' || Array.isArray(value)) {
|
|
15
|
+
throw new TypeError(`${label} must be an object`);
|
|
16
|
+
}
|
|
17
|
+
return value;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function assertJsonValue(value, label) {
|
|
21
|
+
let serialized;
|
|
22
|
+
try {
|
|
23
|
+
serialized = JSON.stringify(value);
|
|
24
|
+
} catch (error) {
|
|
25
|
+
throw new TypeError(`${label} must be JSON-compatible`, { cause: error });
|
|
26
|
+
}
|
|
27
|
+
if (serialized === undefined) throw new TypeError(`${label} must be JSON-compatible`);
|
|
28
|
+
return JSON.parse(serialized);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function isPathInside(root, target) {
|
|
32
|
+
const relative = path.relative(root, target);
|
|
33
|
+
return relative === '' || (!relative.startsWith(`..${path.sep}`) && relative !== '..' && !path.isAbsolute(relative));
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function hostMockDirectory(context) {
|
|
37
|
+
return path.resolve(context.hostDir, 'mock');
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function assertPathInsideHostMock(context, targetPath, label) {
|
|
41
|
+
const mockDirectory = hostMockDirectory(context);
|
|
42
|
+
if (!isPathInside(mockDirectory, targetPath)) {
|
|
43
|
+
throw new TypeError(`${label} must stay inside SOMA_MOCK_APP_ROOT/mock: ${targetPath}`);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// Lexical containment is not enough when mock/ or one of its children is a
|
|
47
|
+
// symlink. Existing files must also remain physically inside Host/mock.
|
|
48
|
+
if (fs.existsSync(targetPath)) {
|
|
49
|
+
const realHostDirectory = fs.realpathSync(context.hostDir);
|
|
50
|
+
const realMockDirectory = fs.realpathSync(mockDirectory);
|
|
51
|
+
const realTargetPath = fs.realpathSync(targetPath);
|
|
52
|
+
if (
|
|
53
|
+
!isPathInside(realHostDirectory, realMockDirectory)
|
|
54
|
+
|| !isPathInside(realMockDirectory, realTargetPath)
|
|
55
|
+
) {
|
|
56
|
+
throw new TypeError(`${label} must stay inside SOMA_MOCK_APP_ROOT/mock: ${targetPath}`);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return targetPath;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function resolveFixturePath(context, baseDir, fixture) {
|
|
63
|
+
if (typeof fixture !== 'string' || !fixture.trim()) throw new TypeError('Mock route fixture must be a non-empty path');
|
|
64
|
+
const fixturePath = path.resolve(baseDir, fixture);
|
|
65
|
+
assertPathInsideHostMock(context, fixturePath, 'Mock fixture');
|
|
66
|
+
if (path.extname(fixturePath).toLowerCase() !== '.json') {
|
|
67
|
+
throw new TypeError(`Mock fixture must be a .json file: ${fixturePath}`);
|
|
68
|
+
}
|
|
69
|
+
try {
|
|
70
|
+
return JSON.parse(fs.readFileSync(fixturePath, 'utf8'));
|
|
71
|
+
} catch (error) {
|
|
72
|
+
throw new Error(`Cannot read mock JSON fixture: ${fixturePath}`, { cause: error });
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export function deepMergeJson(base, override) {
|
|
77
|
+
if (
|
|
78
|
+
base && override
|
|
79
|
+
&& typeof base === 'object' && typeof override === 'object'
|
|
80
|
+
&& !Array.isArray(base) && !Array.isArray(override)
|
|
81
|
+
) {
|
|
82
|
+
const output = structuredClone(base);
|
|
83
|
+
for (const [key, value] of Object.entries(override)) {
|
|
84
|
+
output[key] = hasOwn(output, key) ? deepMergeJson(output[key], value) : structuredClone(value);
|
|
85
|
+
}
|
|
86
|
+
return output;
|
|
87
|
+
}
|
|
88
|
+
// Arrays and scalar values are intentionally replaced instead of merged.
|
|
89
|
+
return structuredClone(override);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export function defineMockRoute(route) {
|
|
93
|
+
return Object.freeze(structuredClone(assertObject(route, 'Mock route')));
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function normalizeHeaders(headers, routeId) {
|
|
97
|
+
if (headers === undefined) return {};
|
|
98
|
+
assertObject(headers, `Mock route ${routeId} headers`);
|
|
99
|
+
return Object.fromEntries(Object.entries(headers).map(([key, value]) => {
|
|
100
|
+
if (!/^[!#$%&'*+\-.^_`|~0-9A-Za-z]+$/u.test(key)) {
|
|
101
|
+
throw new TypeError(`Mock route ${routeId} has invalid response header name: ${JSON.stringify(key)}`);
|
|
102
|
+
}
|
|
103
|
+
if (!['string', 'number', 'boolean'].includes(typeof value)) {
|
|
104
|
+
throw new TypeError(`Mock route ${routeId} response header ${JSON.stringify(key)} must be a string, number or boolean`);
|
|
105
|
+
}
|
|
106
|
+
const normalizedValue = String(value);
|
|
107
|
+
if (/[\u0000-\u0008\u000A-\u001F\u007F]/u.test(normalizedValue)) {
|
|
108
|
+
throw new TypeError(`Mock route ${routeId} response header ${JSON.stringify(key)} contains invalid control characters`);
|
|
109
|
+
}
|
|
110
|
+
try {
|
|
111
|
+
const validationHeaders = new Headers();
|
|
112
|
+
validationHeaders.append(key, normalizedValue);
|
|
113
|
+
} catch (error) {
|
|
114
|
+
throw new TypeError(`Mock route ${routeId} has invalid response header ${JSON.stringify(key)}`, { cause: error });
|
|
115
|
+
}
|
|
116
|
+
return [key, normalizedValue];
|
|
117
|
+
}));
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function normalizeUrlPattern(value, routeId) {
|
|
121
|
+
if (typeof value !== 'string' || value !== value.trim() || /[\u0000-\u001F\u007F]/u.test(value)) {
|
|
122
|
+
throw new TypeError(`Mock route ${routeId} requires an absolute HTTP(S) URL pattern`);
|
|
123
|
+
}
|
|
124
|
+
let parsed;
|
|
125
|
+
try {
|
|
126
|
+
parsed = new URL(value);
|
|
127
|
+
} catch (error) {
|
|
128
|
+
throw new TypeError(`Mock route ${routeId} requires an absolute HTTP(S) URL pattern`, { cause: error });
|
|
129
|
+
}
|
|
130
|
+
if (
|
|
131
|
+
!['http:', 'https:'].includes(parsed.protocol)
|
|
132
|
+
|| !parsed.hostname
|
|
133
|
+
|| parsed.username
|
|
134
|
+
|| parsed.password
|
|
135
|
+
|| parsed.hash
|
|
136
|
+
) {
|
|
137
|
+
throw new TypeError(`Mock route ${routeId} requires an absolute HTTP(S) URL pattern without credentials or fragment`);
|
|
138
|
+
}
|
|
139
|
+
return value;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function normalizeRoute(context, route, index, baseDir) {
|
|
143
|
+
assertObject(route, `Mock route at index ${index}`);
|
|
144
|
+
if (route.enabled === false) return null;
|
|
145
|
+
const id = typeof route.id === 'string' && route.id.trim() ? route.id.trim() : `route-${index + 1}`;
|
|
146
|
+
const method = String(route.method || 'GET').toUpperCase();
|
|
147
|
+
if (!HTTP_METHODS.has(method)) throw new TypeError(`Mock route ${id} has unsupported HTTP method: ${method}`);
|
|
148
|
+
const url = normalizeUrlPattern(route.url, id);
|
|
149
|
+
const status = route.status === undefined ? 200 : Number(route.status);
|
|
150
|
+
if (!Number.isInteger(status) || status < 200 || status > 599) {
|
|
151
|
+
throw new TypeError(`Mock route ${id} has invalid HTTP status: ${route.status}`);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
const hasResponse = hasOwn(route, 'response');
|
|
155
|
+
const hasFixture = hasOwn(route, 'fixture');
|
|
156
|
+
const hasSchema = hasOwn(route, 'schema');
|
|
157
|
+
if (!hasResponse && !hasFixture && !hasSchema) {
|
|
158
|
+
throw new TypeError(`Mock route ${id} must define schema when response or fixture is absent`);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
let mode;
|
|
162
|
+
let payload;
|
|
163
|
+
if (hasResponse) {
|
|
164
|
+
mode = 'json';
|
|
165
|
+
payload = assertJsonValue(route.response, `Mock route ${id} response`);
|
|
166
|
+
} else if (hasFixture) {
|
|
167
|
+
mode = 'json';
|
|
168
|
+
payload = assertJsonValue(resolveFixturePath(context, baseDir, route.fixture), `Mock route ${id} fixture`);
|
|
169
|
+
} else {
|
|
170
|
+
mode = 'schema';
|
|
171
|
+
payload = assertObject(route.schema, `Mock route ${id} schema`);
|
|
172
|
+
try {
|
|
173
|
+
context.generateFromSchema(payload, {
|
|
174
|
+
seed: `${context.seed}|${id}|registry-compile`,
|
|
175
|
+
path: '$response',
|
|
176
|
+
});
|
|
177
|
+
} catch (error) {
|
|
178
|
+
throw new TypeError(`Mock route ${id} has an invalid or unsupported response schema`, { cause: error });
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
if (hasOwn(route, 'merge')) {
|
|
183
|
+
if (mode !== 'json') throw new TypeError(`Mock route ${id} merge is only valid with response or fixture`);
|
|
184
|
+
assertObject(route.merge, `Mock route ${id} merge`);
|
|
185
|
+
if (!payload || typeof payload !== 'object' || Array.isArray(payload)) {
|
|
186
|
+
throw new TypeError(`Mock route ${id} merge requires an object response`);
|
|
187
|
+
}
|
|
188
|
+
payload = assertJsonValue(deepMergeJson(payload, route.merge), `Mock route ${id} merged response`);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
return Object.freeze({
|
|
192
|
+
id,
|
|
193
|
+
method,
|
|
194
|
+
url,
|
|
195
|
+
status,
|
|
196
|
+
headers: normalizeHeaders(route.headers, id),
|
|
197
|
+
mode,
|
|
198
|
+
payload,
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
async function readRequestBody(request) {
|
|
203
|
+
if (request.method === 'GET' || request.method === 'HEAD') return undefined;
|
|
204
|
+
const text = await request.clone().text();
|
|
205
|
+
if (!text) return undefined;
|
|
206
|
+
const contentType = request.headers.get('content-type')?.toLowerCase() || '';
|
|
207
|
+
if (contentType.includes('application/json') || contentType.includes('+json')) {
|
|
208
|
+
try {
|
|
209
|
+
return JSON.parse(text);
|
|
210
|
+
} catch {
|
|
211
|
+
return text;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
if (contentType.includes('application/x-www-form-urlencoded')) {
|
|
215
|
+
const values = {};
|
|
216
|
+
for (const [key, value] of new URLSearchParams(text)) {
|
|
217
|
+
if (!hasOwn(values, key)) values[key] = value;
|
|
218
|
+
else values[key] = Array.isArray(values[key]) ? [...values[key], value] : [values[key], value];
|
|
219
|
+
}
|
|
220
|
+
return values;
|
|
221
|
+
}
|
|
222
|
+
return text;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
function createRouteHandler(context, route) {
|
|
226
|
+
const methodFactory = route.method === 'ALL' ? http.all : http[route.method.toLowerCase()];
|
|
227
|
+
return methodFactory(route.url, async ({ request }) => {
|
|
228
|
+
const requestBody = await readRequestBody(request);
|
|
229
|
+
const safeUrl = new URL(request.url);
|
|
230
|
+
safeUrl.username = '';
|
|
231
|
+
safeUrl.password = '';
|
|
232
|
+
const generationSeed = [
|
|
233
|
+
context.seed,
|
|
234
|
+
route.id,
|
|
235
|
+
request.method,
|
|
236
|
+
safeUrl.href,
|
|
237
|
+
stableSerialize(requestBody),
|
|
238
|
+
].join('|');
|
|
239
|
+
const responseBody = route.mode === 'schema'
|
|
240
|
+
? context.generateFromSchema(route.payload, { seed: generationSeed, path: '$response' })
|
|
241
|
+
: structuredClone(route.payload);
|
|
242
|
+
|
|
243
|
+
context.journal.record({
|
|
244
|
+
scope: 'registry',
|
|
245
|
+
routeId: route.id,
|
|
246
|
+
responseMode: route.mode,
|
|
247
|
+
method: request.method,
|
|
248
|
+
url: safeUrl.href,
|
|
249
|
+
requestBody,
|
|
250
|
+
status: route.status,
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
if (request.method === 'HEAD' || BODYLESS_STATUS_CODES.has(route.status)) {
|
|
254
|
+
return new HttpResponse(null, {
|
|
255
|
+
status: route.status,
|
|
256
|
+
headers: { ...route.headers, 'x-soma-mock-source': `registry-${route.mode}` },
|
|
257
|
+
});
|
|
258
|
+
}
|
|
259
|
+
return HttpResponse.json(responseBody, {
|
|
260
|
+
status: route.status,
|
|
261
|
+
headers: { ...route.headers, 'x-soma-mock-source': `registry-${route.mode}` },
|
|
262
|
+
});
|
|
263
|
+
});
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/** Compile validated registry definitions into MSW handlers. */
|
|
267
|
+
export function registerMockRoutes(context, routes, options = {}) {
|
|
268
|
+
if (!Array.isArray(routes)) throw new TypeError('Mock registry routes must be an array');
|
|
269
|
+
const baseDir = path.resolve(options.baseDir || context.hostDir);
|
|
270
|
+
const normalized = routes
|
|
271
|
+
.map((route, index) => normalizeRoute(context, route, index, baseDir))
|
|
272
|
+
.filter(Boolean);
|
|
273
|
+
const identifiers = new Set();
|
|
274
|
+
const matchers = new Set();
|
|
275
|
+
for (const route of normalized) {
|
|
276
|
+
if (identifiers.has(route.id)) throw new TypeError(`Duplicate mock route id: ${route.id}`);
|
|
277
|
+
identifiers.add(route.id);
|
|
278
|
+
const matcher = `${route.method} ${route.url}`;
|
|
279
|
+
if (matchers.has(matcher)) throw new TypeError(`Duplicate mock route matcher: ${matcher}`);
|
|
280
|
+
matchers.add(matcher);
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
const jsonRoutes = normalized.filter((route) => route.mode === 'json');
|
|
284
|
+
const schemaRoutes = normalized.filter((route) => route.mode === 'schema');
|
|
285
|
+
return {
|
|
286
|
+
routes: normalized,
|
|
287
|
+
jsonHandlers: jsonRoutes.map((route) => createRouteHandler(context, route)),
|
|
288
|
+
schemaHandlers: schemaRoutes.map((route) => createRouteHandler(context, route)),
|
|
289
|
+
};
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
function readRegistryFile(context, registryFile) {
|
|
293
|
+
const explicit = registryFile || process.env.SOMA_MOCK_REGISTRY_FILE;
|
|
294
|
+
const filePath = explicit
|
|
295
|
+
? path.resolve(context.hostDir, explicit)
|
|
296
|
+
: path.join(hostMockDirectory(context), 'routes.json');
|
|
297
|
+
assertPathInsideHostMock(context, filePath, 'MSW route registry');
|
|
298
|
+
if (path.extname(filePath).toLowerCase() !== '.json') {
|
|
299
|
+
throw new TypeError(`MSW route registry must be a .json file: ${filePath}`);
|
|
300
|
+
}
|
|
301
|
+
if (!fs.existsSync(filePath)) {
|
|
302
|
+
if (explicit) throw new Error(`Configured MSW route registry does not exist: ${filePath}`);
|
|
303
|
+
return { filePath, routes: [] };
|
|
304
|
+
}
|
|
305
|
+
let document;
|
|
306
|
+
try {
|
|
307
|
+
document = JSON.parse(fs.readFileSync(filePath, 'utf8'));
|
|
308
|
+
} catch (error) {
|
|
309
|
+
throw new Error(`Cannot read MSW route registry: ${filePath}`, { cause: error });
|
|
310
|
+
}
|
|
311
|
+
const routes = Array.isArray(document) ? document : assertObject(document, 'MSW route registry').routes;
|
|
312
|
+
if (!Array.isArray(routes)) throw new TypeError(`MSW route registry routes must be an array: ${filePath}`);
|
|
313
|
+
return { filePath, routes };
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
export function createRegistryHandlers(context, options = {}) {
|
|
317
|
+
const registry = options.routes
|
|
318
|
+
? {
|
|
319
|
+
filePath: options.registryFile
|
|
320
|
+
? assertPathInsideHostMock(
|
|
321
|
+
context,
|
|
322
|
+
path.resolve(context.hostDir, options.registryFile),
|
|
323
|
+
'MSW route registry',
|
|
324
|
+
)
|
|
325
|
+
: path.join(hostMockDirectory(context), 'routes.json'),
|
|
326
|
+
routes: options.routes,
|
|
327
|
+
}
|
|
328
|
+
: readRegistryFile(context, options.registryFile);
|
|
329
|
+
if (path.extname(registry.filePath).toLowerCase() !== '.json') {
|
|
330
|
+
throw new TypeError(`MSW route registry must be a .json file: ${registry.filePath}`);
|
|
331
|
+
}
|
|
332
|
+
return {
|
|
333
|
+
filePath: registry.filePath,
|
|
334
|
+
...registerMockRoutes(context, registry.routes, { baseDir: path.dirname(registry.filePath) }),
|
|
335
|
+
};
|
|
336
|
+
}
|