@dudko.dev/baton-format 0.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/LICENSE +538 -0
- package/README.md +39 -0
- package/dist/src/errors.d.ts +8 -0
- package/dist/src/errors.js +13 -0
- package/dist/src/index.d.ts +4 -0
- package/dist/src/index.js +5 -0
- package/dist/src/parse.d.ts +11 -0
- package/dist/src/parse.js +459 -0
- package/dist/src/serialize.d.ts +11 -0
- package/dist/src/serialize.js +334 -0
- package/dist/src/types.d.ts +164 -0
- package/dist/src/types.js +9 -0
- package/package.json +44 -0
|
@@ -0,0 +1,459 @@
|
|
|
1
|
+
import { parse as parseYaml } from "yaml";
|
|
2
|
+
import { FormatError } from "./errors.js";
|
|
3
|
+
import { DEFAULT_SETTINGS, FORMAT_VERSION, METHODS, } from "./types.js";
|
|
4
|
+
const ULID_RE = /^[0-9A-HJKMNP-TV-Z]{26}$/;
|
|
5
|
+
// MARK: - Primitive readers
|
|
6
|
+
function root(text) {
|
|
7
|
+
let value;
|
|
8
|
+
try {
|
|
9
|
+
value = parseYaml(text);
|
|
10
|
+
}
|
|
11
|
+
catch (cause) {
|
|
12
|
+
throw new FormatError("invalid-yaml", "", `not valid YAML: ${cause.message}`);
|
|
13
|
+
}
|
|
14
|
+
if (value === null || typeof value !== "object" || Array.isArray(value)) {
|
|
15
|
+
throw new FormatError("invalid-yaml", "", "document root must be a mapping");
|
|
16
|
+
}
|
|
17
|
+
return value;
|
|
18
|
+
}
|
|
19
|
+
function join(path, key) {
|
|
20
|
+
return path === "" ? key : `${path}.${key}`;
|
|
21
|
+
}
|
|
22
|
+
function checkKeys(obj, allowed, path) {
|
|
23
|
+
for (const key of Object.keys(obj)) {
|
|
24
|
+
if (!allowed.includes(key)) {
|
|
25
|
+
throw new FormatError("unknown-field", join(path, key), "unknown field");
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
function checkFormat(obj, path) {
|
|
30
|
+
const value = obj["format"];
|
|
31
|
+
if (value === undefined) {
|
|
32
|
+
throw new FormatError("missing-field", join(path, "format"), "required field is missing");
|
|
33
|
+
}
|
|
34
|
+
if (typeof value !== "number" || !Number.isInteger(value)) {
|
|
35
|
+
throw new FormatError("invalid-field", join(path, "format"), "must be an integer");
|
|
36
|
+
}
|
|
37
|
+
if (value > FORMAT_VERSION) {
|
|
38
|
+
throw new FormatError("unsupported-format", join(path, "format"), `format ${value} is newer than supported ${FORMAT_VERSION}`);
|
|
39
|
+
}
|
|
40
|
+
if (value < 1) {
|
|
41
|
+
throw new FormatError("invalid-field", join(path, "format"), "must be >= 1");
|
|
42
|
+
}
|
|
43
|
+
return value;
|
|
44
|
+
}
|
|
45
|
+
function reqString(obj, key, path) {
|
|
46
|
+
const value = obj[key];
|
|
47
|
+
if (value === undefined) {
|
|
48
|
+
throw new FormatError("missing-field", join(path, key), "required field is missing");
|
|
49
|
+
}
|
|
50
|
+
if (typeof value !== "string" || value === "") {
|
|
51
|
+
throw new FormatError("invalid-field", join(path, key), "must be a non-empty string");
|
|
52
|
+
}
|
|
53
|
+
return value;
|
|
54
|
+
}
|
|
55
|
+
function optString(obj, key, path) {
|
|
56
|
+
const value = obj[key];
|
|
57
|
+
if (value === undefined)
|
|
58
|
+
return undefined;
|
|
59
|
+
if (typeof value !== "string") {
|
|
60
|
+
throw new FormatError("invalid-field", join(path, key), "must be a string");
|
|
61
|
+
}
|
|
62
|
+
return value;
|
|
63
|
+
}
|
|
64
|
+
function optBool(obj, key, path, dflt) {
|
|
65
|
+
const value = obj[key];
|
|
66
|
+
if (value === undefined)
|
|
67
|
+
return dflt;
|
|
68
|
+
if (typeof value !== "boolean") {
|
|
69
|
+
throw new FormatError("invalid-field", join(path, key), "must be a boolean");
|
|
70
|
+
}
|
|
71
|
+
return value;
|
|
72
|
+
}
|
|
73
|
+
function optInt(obj, key, path, dflt) {
|
|
74
|
+
const value = obj[key];
|
|
75
|
+
if (value === undefined)
|
|
76
|
+
return dflt;
|
|
77
|
+
if (typeof value !== "number" || !Number.isInteger(value)) {
|
|
78
|
+
throw new FormatError("invalid-field", join(path, key), "must be an integer");
|
|
79
|
+
}
|
|
80
|
+
return value;
|
|
81
|
+
}
|
|
82
|
+
function optRecord(obj, key, path) {
|
|
83
|
+
const value = obj[key];
|
|
84
|
+
if (value === undefined || value === null)
|
|
85
|
+
return undefined;
|
|
86
|
+
if (typeof value !== "object" || Array.isArray(value)) {
|
|
87
|
+
throw new FormatError("invalid-field", join(path, key), "must be a mapping");
|
|
88
|
+
}
|
|
89
|
+
return value;
|
|
90
|
+
}
|
|
91
|
+
function optArray(obj, key, path) {
|
|
92
|
+
const value = obj[key];
|
|
93
|
+
if (value === undefined || value === null)
|
|
94
|
+
return [];
|
|
95
|
+
if (!Array.isArray(value)) {
|
|
96
|
+
throw new FormatError("invalid-field", join(path, key), "must be a list");
|
|
97
|
+
}
|
|
98
|
+
return value;
|
|
99
|
+
}
|
|
100
|
+
function itemRecord(value, path) {
|
|
101
|
+
if (value === null || typeof value !== "object" || Array.isArray(value)) {
|
|
102
|
+
throw new FormatError("invalid-field", path, "must be a mapping");
|
|
103
|
+
}
|
|
104
|
+
return value;
|
|
105
|
+
}
|
|
106
|
+
function ulid(obj, key, path) {
|
|
107
|
+
const value = reqString(obj, key, path);
|
|
108
|
+
if (!ULID_RE.test(value)) {
|
|
109
|
+
throw new FormatError("invalid-field", join(path, key), "must be a 26-character ULID");
|
|
110
|
+
}
|
|
111
|
+
return value;
|
|
112
|
+
}
|
|
113
|
+
// MARK: - Shared shapes
|
|
114
|
+
function parseKvRows(obj, key, path) {
|
|
115
|
+
return optArray(obj, key, path).map((item, i) => {
|
|
116
|
+
const itemPath = `${join(path, key)}[${i}]`;
|
|
117
|
+
const row = itemRecord(item, itemPath);
|
|
118
|
+
checkKeys(row, ["name", "value", "enabled"], itemPath);
|
|
119
|
+
return {
|
|
120
|
+
name: reqString(row, "name", itemPath),
|
|
121
|
+
value: reqString(row, "value", itemPath),
|
|
122
|
+
enabled: optBool(row, "enabled", itemPath, true),
|
|
123
|
+
};
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
function parseVariables(obj, key, path) {
|
|
127
|
+
return optArray(obj, key, path).map((item, i) => {
|
|
128
|
+
const itemPath = `${join(path, key)}[${i}]`;
|
|
129
|
+
const row = itemRecord(item, itemPath);
|
|
130
|
+
checkKeys(row, ["name", "value"], itemPath);
|
|
131
|
+
return {
|
|
132
|
+
name: reqString(row, "name", itemPath),
|
|
133
|
+
value: reqString(row, "value", itemPath),
|
|
134
|
+
};
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
function parseStringList(obj, key, path) {
|
|
138
|
+
return optArray(obj, key, path).map((item, i) => {
|
|
139
|
+
if (typeof item !== "string" || item === "") {
|
|
140
|
+
throw new FormatError("invalid-field", `${join(path, key)}[${i}]`, "must be a non-empty string");
|
|
141
|
+
}
|
|
142
|
+
return item;
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
function parseAuth(obj, path) {
|
|
146
|
+
const auth = optRecord(obj, "auth", path);
|
|
147
|
+
if (auth === undefined)
|
|
148
|
+
return { type: "inherit" };
|
|
149
|
+
const authPath = join(path, "auth");
|
|
150
|
+
const type = reqString(auth, "type", authPath);
|
|
151
|
+
switch (type) {
|
|
152
|
+
case "inherit":
|
|
153
|
+
checkKeys(auth, ["type"], authPath);
|
|
154
|
+
return { type: "inherit" };
|
|
155
|
+
case "none":
|
|
156
|
+
checkKeys(auth, ["type"], authPath);
|
|
157
|
+
return { type: "none" };
|
|
158
|
+
case "basic": {
|
|
159
|
+
checkKeys(auth, ["type", "basic"], authPath);
|
|
160
|
+
const cfg = optRecord(auth, "basic", authPath);
|
|
161
|
+
if (cfg === undefined) {
|
|
162
|
+
throw new FormatError("missing-field", join(authPath, "basic"), "required field is missing");
|
|
163
|
+
}
|
|
164
|
+
const cfgPath = join(authPath, "basic");
|
|
165
|
+
checkKeys(cfg, ["username", "password"], cfgPath);
|
|
166
|
+
return {
|
|
167
|
+
type: "basic",
|
|
168
|
+
basic: {
|
|
169
|
+
username: reqString(cfg, "username", cfgPath),
|
|
170
|
+
password: reqString(cfg, "password", cfgPath),
|
|
171
|
+
},
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
case "bearer": {
|
|
175
|
+
checkKeys(auth, ["type", "bearer"], authPath);
|
|
176
|
+
const cfg = optRecord(auth, "bearer", authPath);
|
|
177
|
+
if (cfg === undefined) {
|
|
178
|
+
throw new FormatError("missing-field", join(authPath, "bearer"), "required field is missing");
|
|
179
|
+
}
|
|
180
|
+
const cfgPath = join(authPath, "bearer");
|
|
181
|
+
checkKeys(cfg, ["token"], cfgPath);
|
|
182
|
+
return { type: "bearer", bearer: { token: reqString(cfg, "token", cfgPath) } };
|
|
183
|
+
}
|
|
184
|
+
case "apiKey": {
|
|
185
|
+
checkKeys(auth, ["type", "apiKey"], authPath);
|
|
186
|
+
const cfg = optRecord(auth, "apiKey", authPath);
|
|
187
|
+
if (cfg === undefined) {
|
|
188
|
+
throw new FormatError("missing-field", join(authPath, "apiKey"), "required field is missing");
|
|
189
|
+
}
|
|
190
|
+
const cfgPath = join(authPath, "apiKey");
|
|
191
|
+
checkKeys(cfg, ["name", "value", "in"], cfgPath);
|
|
192
|
+
const location = reqString(cfg, "in", cfgPath);
|
|
193
|
+
if (location !== "header" && location !== "query") {
|
|
194
|
+
throw new FormatError("invalid-field", join(cfgPath, "in"), 'must be "header" or "query"');
|
|
195
|
+
}
|
|
196
|
+
return {
|
|
197
|
+
type: "apiKey",
|
|
198
|
+
apiKey: {
|
|
199
|
+
name: reqString(cfg, "name", cfgPath),
|
|
200
|
+
value: reqString(cfg, "value", cfgPath),
|
|
201
|
+
in: location,
|
|
202
|
+
},
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
case "oauth2": {
|
|
206
|
+
checkKeys(auth, ["type", "oauth2"], authPath);
|
|
207
|
+
const cfg = optRecord(auth, "oauth2", authPath);
|
|
208
|
+
if (cfg === undefined) {
|
|
209
|
+
throw new FormatError("missing-field", join(authPath, "oauth2"), "required field is missing");
|
|
210
|
+
}
|
|
211
|
+
return { type: "oauth2", oauth2: parseOAuth2(cfg, join(authPath, "oauth2")) };
|
|
212
|
+
}
|
|
213
|
+
default:
|
|
214
|
+
throw new FormatError("invalid-field", join(authPath, "type"), `unknown auth type "${type}"`);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
function parseOAuth2(cfg, path) {
|
|
218
|
+
checkKeys(cfg, ["grant", "authUrl", "tokenUrl", "clientId", "clientSecret", "scopes", "usePkce"], path);
|
|
219
|
+
const grant = reqString(cfg, "grant", path);
|
|
220
|
+
if (grant !== "authorizationCode" && grant !== "clientCredentials") {
|
|
221
|
+
throw new FormatError("invalid-field", join(path, "grant"), 'must be "authorizationCode" or "clientCredentials"');
|
|
222
|
+
}
|
|
223
|
+
const authUrl = optString(cfg, "authUrl", path);
|
|
224
|
+
if (grant === "authorizationCode" && authUrl === undefined) {
|
|
225
|
+
throw new FormatError("missing-field", join(path, "authUrl"), "required for authorizationCode");
|
|
226
|
+
}
|
|
227
|
+
return {
|
|
228
|
+
grant,
|
|
229
|
+
...(authUrl !== undefined ? { authUrl } : {}),
|
|
230
|
+
tokenUrl: reqString(cfg, "tokenUrl", path),
|
|
231
|
+
clientId: reqString(cfg, "clientId", path),
|
|
232
|
+
...(optString(cfg, "clientSecret", path) !== undefined
|
|
233
|
+
? { clientSecret: optString(cfg, "clientSecret", path) }
|
|
234
|
+
: {}),
|
|
235
|
+
scopes: parseStringList(cfg, "scopes", path),
|
|
236
|
+
usePkce: optBool(cfg, "usePkce", path, grant === "authorizationCode"),
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
function parseBody(obj, path) {
|
|
240
|
+
const body = optRecord(obj, "body", path);
|
|
241
|
+
if (body === undefined)
|
|
242
|
+
return { type: "none" };
|
|
243
|
+
const bodyPath = join(path, "body");
|
|
244
|
+
const type = reqString(body, "type", bodyPath);
|
|
245
|
+
switch (type) {
|
|
246
|
+
case "none":
|
|
247
|
+
checkKeys(body, ["type"], bodyPath);
|
|
248
|
+
return { type: "none" };
|
|
249
|
+
case "json":
|
|
250
|
+
checkKeys(body, ["type", "json"], bodyPath);
|
|
251
|
+
return { type: "json", json: reqString(body, "json", bodyPath) };
|
|
252
|
+
case "text":
|
|
253
|
+
checkKeys(body, ["type", "text"], bodyPath);
|
|
254
|
+
return { type: "text", text: reqString(body, "text", bodyPath) };
|
|
255
|
+
case "form":
|
|
256
|
+
checkKeys(body, ["type", "form"], bodyPath);
|
|
257
|
+
return { type: "form", form: parseKvRows(body, "form", bodyPath) };
|
|
258
|
+
case "multipart": {
|
|
259
|
+
checkKeys(body, ["type", "multipart"], bodyPath);
|
|
260
|
+
const parts = optArray(body, "multipart", bodyPath).map((item, i) => {
|
|
261
|
+
const itemPath = `${join(bodyPath, "multipart")}[${i}]`;
|
|
262
|
+
const part = itemRecord(item, itemPath);
|
|
263
|
+
checkKeys(part, ["name", "value", "filePath", "enabled"], itemPath);
|
|
264
|
+
const value = optString(part, "value", itemPath);
|
|
265
|
+
const filePath = optString(part, "filePath", itemPath);
|
|
266
|
+
if ((value === undefined) === (filePath === undefined)) {
|
|
267
|
+
throw new FormatError("invalid-field", itemPath, "exactly one of value or filePath must be set");
|
|
268
|
+
}
|
|
269
|
+
return {
|
|
270
|
+
name: reqString(part, "name", itemPath),
|
|
271
|
+
...(value !== undefined ? { value } : {}),
|
|
272
|
+
...(filePath !== undefined ? { filePath } : {}),
|
|
273
|
+
enabled: optBool(part, "enabled", itemPath, true),
|
|
274
|
+
};
|
|
275
|
+
});
|
|
276
|
+
return { type: "multipart", multipart: parts };
|
|
277
|
+
}
|
|
278
|
+
case "binary": {
|
|
279
|
+
checkKeys(body, ["type", "binary"], bodyPath);
|
|
280
|
+
const cfg = optRecord(body, "binary", bodyPath);
|
|
281
|
+
if (cfg === undefined) {
|
|
282
|
+
throw new FormatError("missing-field", join(bodyPath, "binary"), "required field is missing");
|
|
283
|
+
}
|
|
284
|
+
const cfgPath = join(bodyPath, "binary");
|
|
285
|
+
checkKeys(cfg, ["path"], cfgPath);
|
|
286
|
+
return { type: "binary", binary: { path: reqString(cfg, "path", cfgPath) } };
|
|
287
|
+
}
|
|
288
|
+
case "graphql": {
|
|
289
|
+
checkKeys(body, ["type", "graphql"], bodyPath);
|
|
290
|
+
const cfg = optRecord(body, "graphql", bodyPath);
|
|
291
|
+
if (cfg === undefined) {
|
|
292
|
+
throw new FormatError("missing-field", join(bodyPath, "graphql"), "required field is missing");
|
|
293
|
+
}
|
|
294
|
+
const cfgPath = join(bodyPath, "graphql");
|
|
295
|
+
checkKeys(cfg, ["query", "variables"], cfgPath);
|
|
296
|
+
const variables = optString(cfg, "variables", cfgPath);
|
|
297
|
+
return {
|
|
298
|
+
type: "graphql",
|
|
299
|
+
graphql: {
|
|
300
|
+
query: reqString(cfg, "query", cfgPath),
|
|
301
|
+
...(variables !== undefined ? { variables } : {}),
|
|
302
|
+
},
|
|
303
|
+
};
|
|
304
|
+
}
|
|
305
|
+
default:
|
|
306
|
+
throw new FormatError("invalid-field", join(bodyPath, "type"), `unknown body type "${type}"`);
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
// MARK: - File parsers
|
|
310
|
+
/** Parse and validate a `*.req.yaml` file into its normalized form. */
|
|
311
|
+
export function parseRequestFile(text) {
|
|
312
|
+
const obj = root(text);
|
|
313
|
+
checkKeys(obj, ["format", "id", "name", "method", "url", "query", "headers", "auth", "body", "scripts", "presets", "meta"], "");
|
|
314
|
+
const format = checkFormat(obj, "");
|
|
315
|
+
const method = reqString(obj, "method", "");
|
|
316
|
+
if (!METHODS.includes(method)) {
|
|
317
|
+
throw new FormatError("invalid-field", "method", `unknown method "${method}"`);
|
|
318
|
+
}
|
|
319
|
+
const scriptsObj = optRecord(obj, "scripts", "");
|
|
320
|
+
let scripts = {};
|
|
321
|
+
if (scriptsObj !== undefined) {
|
|
322
|
+
checkKeys(scriptsObj, ["preRequest", "tests"], "scripts");
|
|
323
|
+
const preRequest = optString(scriptsObj, "preRequest", "scripts");
|
|
324
|
+
const tests = optString(scriptsObj, "tests", "scripts");
|
|
325
|
+
scripts = {
|
|
326
|
+
...(preRequest !== undefined ? { preRequest } : {}),
|
|
327
|
+
...(tests !== undefined ? { tests } : {}),
|
|
328
|
+
};
|
|
329
|
+
}
|
|
330
|
+
const presets = optArray(obj, "presets", "").map((item, i) => {
|
|
331
|
+
const itemPath = `presets[${i}]`;
|
|
332
|
+
const preset = itemRecord(item, itemPath);
|
|
333
|
+
checkKeys(preset, ["name", "payload"], itemPath);
|
|
334
|
+
return {
|
|
335
|
+
name: reqString(preset, "name", itemPath),
|
|
336
|
+
payload: reqString(preset, "payload", itemPath),
|
|
337
|
+
};
|
|
338
|
+
});
|
|
339
|
+
const metaObj = optRecord(obj, "meta", "");
|
|
340
|
+
let meta = { tags: [] };
|
|
341
|
+
if (metaObj !== undefined) {
|
|
342
|
+
checkKeys(metaObj, ["description", "tags"], "meta");
|
|
343
|
+
const description = optString(metaObj, "description", "meta");
|
|
344
|
+
meta = {
|
|
345
|
+
...(description !== undefined ? { description } : {}),
|
|
346
|
+
tags: parseStringList(metaObj, "tags", "meta"),
|
|
347
|
+
};
|
|
348
|
+
}
|
|
349
|
+
return {
|
|
350
|
+
format,
|
|
351
|
+
id: ulid(obj, "id", ""),
|
|
352
|
+
name: reqString(obj, "name", ""),
|
|
353
|
+
method: method,
|
|
354
|
+
url: reqString(obj, "url", ""),
|
|
355
|
+
query: parseKvRows(obj, "query", ""),
|
|
356
|
+
headers: parseKvRows(obj, "headers", ""),
|
|
357
|
+
auth: parseAuth(obj, ""),
|
|
358
|
+
body: parseBody(obj, ""),
|
|
359
|
+
scripts,
|
|
360
|
+
presets,
|
|
361
|
+
meta,
|
|
362
|
+
};
|
|
363
|
+
}
|
|
364
|
+
/** Parse and validate a `collection.yaml` / `folder.yaml` file. */
|
|
365
|
+
export function parseCollectionFile(text) {
|
|
366
|
+
const obj = root(text);
|
|
367
|
+
checkKeys(obj, ["format", "id", "name", "auth", "variables", "order"], "");
|
|
368
|
+
return {
|
|
369
|
+
format: checkFormat(obj, ""),
|
|
370
|
+
id: ulid(obj, "id", ""),
|
|
371
|
+
name: reqString(obj, "name", ""),
|
|
372
|
+
auth: parseAuth(obj, ""),
|
|
373
|
+
variables: parseVariables(obj, "variables", ""),
|
|
374
|
+
order: parseStringList(obj, "order", ""),
|
|
375
|
+
};
|
|
376
|
+
}
|
|
377
|
+
/** Parse and validate a `*.env.yaml` file. */
|
|
378
|
+
export function parseEnvironmentFile(text) {
|
|
379
|
+
const obj = root(text);
|
|
380
|
+
checkKeys(obj, ["format", "name", "variables", "secrets"], "");
|
|
381
|
+
return {
|
|
382
|
+
format: checkFormat(obj, ""),
|
|
383
|
+
name: reqString(obj, "name", ""),
|
|
384
|
+
variables: parseVariables(obj, "variables", ""),
|
|
385
|
+
secrets: parseStringList(obj, "secrets", ""),
|
|
386
|
+
};
|
|
387
|
+
}
|
|
388
|
+
/** Parse and validate the `baton.yaml` workspace manifest. */
|
|
389
|
+
export function parseWorkspaceManifest(text) {
|
|
390
|
+
const obj = root(text);
|
|
391
|
+
checkKeys(obj, ["format", "name", "description", "settings"], "");
|
|
392
|
+
const settingsObj = optRecord(obj, "settings", "");
|
|
393
|
+
let settings = { ...DEFAULT_SETTINGS };
|
|
394
|
+
if (settingsObj !== undefined) {
|
|
395
|
+
checkKeys(settingsObj, ["followRedirects", "timeoutMs", "validateTls"], "settings");
|
|
396
|
+
settings = {
|
|
397
|
+
followRedirects: optBool(settingsObj, "followRedirects", "settings", DEFAULT_SETTINGS.followRedirects),
|
|
398
|
+
timeoutMs: optInt(settingsObj, "timeoutMs", "settings", DEFAULT_SETTINGS.timeoutMs),
|
|
399
|
+
validateTls: optBool(settingsObj, "validateTls", "settings", DEFAULT_SETTINGS.validateTls),
|
|
400
|
+
};
|
|
401
|
+
}
|
|
402
|
+
const description = optString(obj, "description", "");
|
|
403
|
+
return {
|
|
404
|
+
format: checkFormat(obj, ""),
|
|
405
|
+
name: reqString(obj, "name", ""),
|
|
406
|
+
...(description !== undefined ? { description } : {}),
|
|
407
|
+
settings,
|
|
408
|
+
};
|
|
409
|
+
}
|
|
410
|
+
/** Parse and validate a `schedules.yaml` file. */
|
|
411
|
+
export function parseSchedulesFile(text) {
|
|
412
|
+
const obj = root(text);
|
|
413
|
+
checkKeys(obj, ["format", "schedules"], "");
|
|
414
|
+
const schedules = optArray(obj, "schedules", "").map((item, i) => {
|
|
415
|
+
const itemPath = `schedules[${i}]`;
|
|
416
|
+
const sched = itemRecord(item, itemPath);
|
|
417
|
+
checkKeys(sched, ["id", "cron", "timezone", "enabled", "run", "notify"], itemPath);
|
|
418
|
+
const runObj = optRecord(sched, "run", itemPath);
|
|
419
|
+
if (runObj === undefined) {
|
|
420
|
+
throw new FormatError("missing-field", join(itemPath, "run"), "required field is missing");
|
|
421
|
+
}
|
|
422
|
+
const runPath = join(itemPath, "run");
|
|
423
|
+
checkKeys(runObj, ["collection", "request", "environment"], runPath);
|
|
424
|
+
const collection = optString(runObj, "collection", runPath);
|
|
425
|
+
const request = optString(runObj, "request", runPath);
|
|
426
|
+
if ((collection === undefined) === (request === undefined)) {
|
|
427
|
+
throw new FormatError("invalid-field", runPath, "exactly one of collection or request must be set");
|
|
428
|
+
}
|
|
429
|
+
let notify;
|
|
430
|
+
const notifyObj = optRecord(sched, "notify", itemPath);
|
|
431
|
+
if (notifyObj !== undefined) {
|
|
432
|
+
const notifyPath = join(itemPath, "notify");
|
|
433
|
+
checkKeys(notifyObj, ["onFailure"], notifyPath);
|
|
434
|
+
const onFailure = optRecord(notifyObj, "onFailure", notifyPath);
|
|
435
|
+
if (onFailure !== undefined) {
|
|
436
|
+
const onFailurePath = join(notifyPath, "onFailure");
|
|
437
|
+
checkKeys(onFailure, ["webhookUrl"], onFailurePath);
|
|
438
|
+
notify = { onFailure: { webhookUrl: reqString(onFailure, "webhookUrl", onFailurePath) } };
|
|
439
|
+
}
|
|
440
|
+
else {
|
|
441
|
+
notify = {};
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
return {
|
|
445
|
+
id: reqString(sched, "id", itemPath),
|
|
446
|
+
cron: reqString(sched, "cron", itemPath),
|
|
447
|
+
timezone: optString(sched, "timezone", itemPath) ?? "UTC",
|
|
448
|
+
enabled: optBool(sched, "enabled", itemPath, true),
|
|
449
|
+
run: {
|
|
450
|
+
...(collection !== undefined ? { collection } : {}),
|
|
451
|
+
...(request !== undefined ? { request } : {}),
|
|
452
|
+
environment: reqString(runObj, "environment", runPath),
|
|
453
|
+
},
|
|
454
|
+
...(notify !== undefined ? { notify } : {}),
|
|
455
|
+
};
|
|
456
|
+
});
|
|
457
|
+
return { format: checkFormat(obj, ""), schedules };
|
|
458
|
+
}
|
|
459
|
+
//# sourceMappingURL=parse.js.map
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { CollectionFile, EnvironmentFile, RequestFile, SchedulesFile, WorkspaceManifest } from "./types.js";
|
|
2
|
+
/** Serialize a normalized request to canonical `*.req.yaml` bytes. */
|
|
3
|
+
export declare function serializeRequestFile(req: RequestFile): string;
|
|
4
|
+
/** Serialize a normalized collection/folder to canonical bytes. */
|
|
5
|
+
export declare function serializeCollectionFile(col: CollectionFile): string;
|
|
6
|
+
/** Serialize a normalized environment to canonical bytes. */
|
|
7
|
+
export declare function serializeEnvironmentFile(env: EnvironmentFile): string;
|
|
8
|
+
/** Serialize a normalized workspace manifest to canonical bytes. */
|
|
9
|
+
export declare function serializeWorkspaceManifest(ws: WorkspaceManifest): string;
|
|
10
|
+
/** Serialize normalized schedules to canonical bytes. */
|
|
11
|
+
export declare function serializeSchedulesFile(file: SchedulesFile): string;
|