-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
40 lines (32 loc) · 1.24 KB
/
index.js
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
import { docSchemas } from '@comapeo/schema'
import { JSONSchemaFaker, createFakerSchema } from './lib/faker.js'
import { extractSchemaVersion, isValidSchemaName } from './lib/schema.js'
export function listSchemas() {
/** @type {{[name: string]: Array<string>}} */
const result = {}
for (const [schemaName, schema] of Object.entries(docSchemas)) {
result[schemaName] = [extractSchemaVersion(schema.$id)]
}
return result
}
/**
* @template {import('@comapeo/schema/dist/types.js').SchemaName} TSchemaName
* @param {TSchemaName} schemaName
* @param {{version?: string, count?: number}} [options]
* @returns {Array<Extract<import('@comapeo/schema').MapeoDoc, { schemaName: TSchemaName }>>}
*/
export function generate(schemaName, { count } = {}) {
isValidSchemaName(schemaName)
const targetSchema = docSchemas[schemaName]
const numberToGenerate = count || 1
/** @type {Array<Extract<import('@comapeo/schema').MapeoDoc, { schemaName: TSchemaName }>>} */
const result = []
for (let i = 0; i < numberToGenerate; i++) {
result.push(
/** @type {Extract<import('@comapeo/schema').MapeoDoc, { schemaName: TSchemaName }>} */ (
JSONSchemaFaker.generate(createFakerSchema(targetSchema))
),
)
}
return result
}