@servable/tools 2.0.1 → 2.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/global.d.ts +13 -0
- package/package.json +17 -15
- package/src/domain/protocolInstance/factory/index.js +8 -4
- package/src/domain/protocolInstance/index.js +15 -2
- package/src/domain/protocolLoader/base.js +19 -0
- package/src/domain/protocolLoader/v1.0.0.js +6 -3
- package/src/domain/protocolLoader/v1.1.0.js +7 -4
- package/src/domain/servable/parse/utils/index.js +21 -6
- package/src/index.js +8 -0
- package/src/lib/defineConfig.js +94 -0
- package/src/lib/directoryFilesRecursive.js +9 -1
- package/src/lib/directoryGlob.js +15 -1
- package/src/lib/ensureDirectoryExists.js +18 -5
- package/src/lib/getModuleDir.js +8 -3
- package/src/manifest/access/index.js +21 -0
- package/src/manifest/extract/extractItem.js +1 -1
- package/src/manifest/extract/route/extractFiles.js +8 -4
- package/src/manifest/extract/route/imageToBase64.js +12 -0
- package/src/schema/build/buildProtocol/classStruct/preserveMostRestricted.js +16 -2
- package/src/schema/build/index.js +4 -1
- package/src/schema/protocolResources/generateTypes.js +106 -0
- package/src/schema/protocolResources/index.js +103 -0
- package/src/schema/types/index.js +97 -0
- package/src/schema/validate/index.js +0 -1
- package/types/config/candidates/index.d.ts +2 -0
- package/types/config/candidates/protocolCandidates/index.d.ts +9 -0
- package/types/config/index.d.ts +2 -0
- package/types/config/validate/index.d.ts +4 -0
- package/types/config/validate/protocol/index.d.ts +5 -0
- package/types/document/gateway/all/index.d.ts +2 -0
- package/types/document/gateway/generateGithubReadme/index.d.ts +2 -0
- package/types/domain/lib/checkFileExists.d.ts +2 -0
- package/types/domain/protocol/factory/absoluteModuleSrcPath.d.ts +2 -0
- package/types/domain/protocol/factory/index.d.ts +7 -0
- package/types/domain/protocol/factory/protocolPath.d.ts +2 -0
- package/types/domain/protocol/index.d.ts +34 -0
- package/types/domain/protocol/lib/instancesPathIdUniqueId.d.ts +5 -0
- package/types/domain/protocolInstance/factory/getLoaderWithPayload.d.ts +4 -0
- package/types/domain/protocolInstance/factory/index.d.ts +6 -0
- package/types/domain/protocolInstance/factory/instancesWithPath.d.ts +6 -0
- package/types/domain/protocolInstance/index.d.ts +71 -0
- package/types/domain/protocolLoader/base.d.ts +52 -0
- package/types/domain/protocolLoader/factory/getModule.d.ts +6 -0
- package/types/domain/protocolLoader/factory/index.d.ts +4 -0
- package/types/domain/protocolLoader/lib/importJSDefault.d.ts +6 -0
- package/types/domain/protocolLoader/lib/importJSONDefault.d.ts +6 -0
- package/types/domain/protocolLoader/lib/mergeClassProtocols.d.ts +6 -0
- package/types/domain/protocolLoader/lib/triggerItems.d.ts +2 -0
- package/types/domain/protocolLoader/v1.0.0.d.ts +68 -0
- package/types/domain/protocolLoader/v1.1.0.d.ts +70 -0
- package/types/domain/servable/index.d.ts +8 -0
- package/types/domain/servable/parse/index.d.ts +5 -0
- package/types/domain/servable/parse/utils/index.d.ts +41 -0
- package/types/index.d.ts +22 -0
- package/types/lib/adaptConfig/basic.d.ts +4 -0
- package/types/lib/adaptProtocolPayload.d.ts +2 -0
- package/types/lib/checkFileExists.d.ts +2 -0
- package/types/lib/cleanProtocols.d.ts +2 -0
- package/types/lib/defineConfig.d.ts +175 -0
- package/types/lib/directories.d.ts +9 -0
- package/types/lib/directoryFilesRecursive.d.ts +7 -0
- package/types/lib/directoryGlob.d.ts +5 -0
- package/types/lib/ensureDirectoryExists.d.ts +15 -0
- package/types/lib/foldersInFolder.d.ts +9 -0
- package/types/lib/getModuleDir.d.ts +2 -0
- package/types/lib/importFileAsText.d.ts +2 -0
- package/types/lib/importJSONAsync.d.ts +2 -0
- package/types/lib/protocolApiVersion.d.ts +4 -0
- package/types/lib/sanitize.d.ts +2 -0
- package/types/lib/stableStringify.d.ts +2 -0
- package/types/manifest/access/accessChildInTreeWithRoute.d.ts +5 -0
- package/types/manifest/access/index.d.ts +10 -0
- package/types/manifest/data/1.0.0/enums.d.ts +382 -0
- package/types/manifest/data/1.1.0/enums.d.ts +401 -0
- package/types/manifest/extract/extractAst.d.ts +2 -0
- package/types/manifest/extract/extractItem.d.ts +2 -0
- package/types/manifest/extract/formatFile.d.ts +4 -0
- package/types/manifest/extract/index.d.ts +8 -0
- package/types/manifest/extract/route/extractFileWithVariants.d.ts +2 -0
- package/types/manifest/extract/route/extractFiles.d.ts +2 -0
- package/types/manifest/extract/route/imageToBase64.d.ts +13 -0
- package/types/manifest/extract/route/index.d.ts +2 -0
- package/types/manifest/extract/templateDataForId.d.ts +5 -0
- package/types/schema/artifact/index.d.ts +21 -0
- package/types/schema/build/buildProtocol/classStruct/extractProtocolsProtocols.d.ts +6 -0
- package/types/schema/build/buildProtocol/classStruct/index.d.ts +15 -0
- package/types/schema/build/buildProtocol/classStruct/preserveMostRestricted.d.ts +4 -0
- package/types/schema/build/buildProtocol/classStruct/protocolInClassClassSchema.d.ts +2 -0
- package/types/schema/build/buildProtocol/extractClassesProtocol.d.ts +8 -0
- package/types/schema/build/buildProtocol/index.d.ts +7 -0
- package/types/schema/build/buildProtocol/index.for.d.ts +7 -0
- package/types/schema/build/buildProtocol/index.promiseall.d.ts +7 -0
- package/types/schema/build/index.d.ts +9 -0
- package/types/schema/build/lib/getProtocolsLiveClasses.d.ts +4 -0
- package/types/schema/build/lib/updateProtocolsExcerpt.d.ts +5 -0
- package/types/schema/plan/classifyField.d.ts +23 -0
- package/types/schema/plan/index.d.ts +11 -0
- package/types/schema/protocolResources/generateTypes.d.ts +11 -0
- package/types/schema/protocolResources/index.d.ts +42 -0
- package/types/schema/types/index.d.ts +18 -0
- package/types/schema/validate/handleProtocol/computeVersions.d.ts +16 -0
- package/types/schema/validate/handleProtocol/index.d.ts +18 -0
- package/types/schema/validate/index.d.ts +26 -0
- package/src/schema/index.js +0 -56
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
// lucide 0.6 (protocol-resources), PEAKUB DX initiative - the codegen half of
|
|
2
|
+
// buildProtocolResources(). Turns that scan's output into a `.d.ts` that merges into
|
|
3
|
+
// @servable/server's `ServableServiceCallMap` (see that package's own global.d.ts for why that
|
|
4
|
+
// interface is declared open there rather than typed fully in one place). Each entry references
|
|
5
|
+
// the real service file with `typeof import(path).default` - not a serialized copy of its
|
|
6
|
+
// signature - so the generated type can never drift from the live handler: whatever JSDoc that
|
|
7
|
+
// file has on its `handler` IS the type, always, by construction.
|
|
8
|
+
//
|
|
9
|
+
// Jobs are emitted as a plain per-protocol listing (`ServableProtocolJobs`), not merged into any
|
|
10
|
+
// call-map, since nothing invokes a job by id - see buildProtocolResources()'s own file for why.
|
|
11
|
+
|
|
12
|
+
const HEADER = `// AUTO-GENERATED by \`servable schema protocol-types\` - do not hand-edit, it will be
|
|
13
|
+
// overwritten on the next run. Regenerate whenever a protocol adds/removes/renames a service or
|
|
14
|
+
// job. Commit it alongside servable.schema.json so editors don't need to run the generator to
|
|
15
|
+
// get these types.
|
|
16
|
+
//
|
|
17
|
+
// Every \`typeof import(...)\` below points at the real, live service/job file - the type is
|
|
18
|
+
// never a hand-maintained copy of that file's signature, so it can't drift from it.
|
|
19
|
+
`
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* @param {string} path - absolute path to a service/job file.
|
|
23
|
+
* @returns {string} a module specifier safe to embed in a `typeof import('...')` type query.
|
|
24
|
+
*/
|
|
25
|
+
const importSpecifier = path => JSON.stringify(path)
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* @param {import('./index.js').ProtocolResourceEntry} service
|
|
29
|
+
* @returns {string}
|
|
30
|
+
*/
|
|
31
|
+
const serviceEntryLine = service =>
|
|
32
|
+
` ${JSON.stringify(service.id)}: typeof import(${importSpecifier(service.path)})['default']`
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* @param {import('./index.js').ProtocolResourceEntry} job
|
|
36
|
+
* @returns {string}
|
|
37
|
+
*/
|
|
38
|
+
const jobEntryLine = job =>
|
|
39
|
+
` ${JSON.stringify(job.id)}: typeof import(${importSpecifier(job.path)})['default']`
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Generates a `.d.ts` that:
|
|
43
|
+
* - merges every discovered service into `ServableServiceCallMap` (sharpening
|
|
44
|
+
* `Servable.Services.call({ id, params })`'s `params`/return type for that `id`).
|
|
45
|
+
* - emits one `ServableProtocolJobs` interface per protocol, keyed by protocol id, listing its
|
|
46
|
+
* jobs for discoverability (no call-site typing - nothing invokes a job by id).
|
|
47
|
+
*
|
|
48
|
+
* @param {Record<string, import('./index.js').ProtocolResources>} resources - `buildProtocolResources()`'s output.
|
|
49
|
+
* @returns {string} the full `.d.ts` file contents.
|
|
50
|
+
*/
|
|
51
|
+
export default function generateProtocolResourceTypes(resources) {
|
|
52
|
+
const protocolIds = Object.keys(resources || {}).sort((a, b) => a.localeCompare(b))
|
|
53
|
+
|
|
54
|
+
// Deduped by `id`, first occurrence wins - mirrors the real registry's own semantics
|
|
55
|
+
// (domain/servable/services/index.js's `register()` skips a service whose `id`+`version` is
|
|
56
|
+
// already registered), and keeps this from ever emitting a TS interface with a duplicate
|
|
57
|
+
// property name, which would be invalid output.
|
|
58
|
+
const seenServiceIds = new Set()
|
|
59
|
+
const allServices = protocolIds
|
|
60
|
+
.flatMap(protocolId => resources[protocolId].services)
|
|
61
|
+
.sort((a, b) => a.id.localeCompare(b.id))
|
|
62
|
+
.filter(service => {
|
|
63
|
+
if (seenServiceIds.has(service.id)) {
|
|
64
|
+
return false
|
|
65
|
+
}
|
|
66
|
+
seenServiceIds.add(service.id)
|
|
67
|
+
return true
|
|
68
|
+
})
|
|
69
|
+
|
|
70
|
+
// Sanitizing a protocol id into an identifier can collide (e.g. `@a/foo-bar` and `@a/foo_bar`
|
|
71
|
+
// both sanitize to `_a_foo_bar`) even though real protocol ids never collide with each other -
|
|
72
|
+
// disambiguated with a numeric suffix rather than risk emitting two interfaces with the same
|
|
73
|
+
// name, which would be invalid output.
|
|
74
|
+
const seenJobInterfaceNames = new Set()
|
|
75
|
+
const jobInterfaces = protocolIds
|
|
76
|
+
.filter(protocolId => resources[protocolId].jobs.length)
|
|
77
|
+
.map(protocolId => {
|
|
78
|
+
const jobs = resources[protocolId].jobs.slice().sort((a, b) => a.id.localeCompare(b.id))
|
|
79
|
+
const jobLines = jobs.map(jobEntryLine).join('\n')
|
|
80
|
+
let safeProtocolName = protocolId.replace(/[^A-Za-z0-9_$]/g, '_')
|
|
81
|
+
let interfaceName = `ServableProtocolJobs_${safeProtocolName}`
|
|
82
|
+
let suffix = 2
|
|
83
|
+
while (seenJobInterfaceNames.has(interfaceName)) {
|
|
84
|
+
interfaceName = `ServableProtocolJobs_${safeProtocolName}_${suffix}`
|
|
85
|
+
suffix += 1
|
|
86
|
+
}
|
|
87
|
+
seenJobInterfaceNames.add(interfaceName)
|
|
88
|
+
return `/** Jobs defined by protocol \`${protocolId}\`. */\n` +
|
|
89
|
+
`export interface ${interfaceName} {\n${jobLines}\n}`
|
|
90
|
+
})
|
|
91
|
+
.join('\n\n')
|
|
92
|
+
|
|
93
|
+
// Omitted entirely (rather than merged as an empty/index-signature interface) when there are
|
|
94
|
+
// no services anywhere in the app - @servable/server's own `ServableServiceCallMap` already
|
|
95
|
+
// declares the open, loose-fallback index signature; restating it here would be redundant.
|
|
96
|
+
const serviceMapMerge = allServices.length
|
|
97
|
+
? `declare global {\n interface ServableServiceCallMap {\n${allServices.map(serviceEntryLine).join('\n')}\n }\n}\n`
|
|
98
|
+
: ''
|
|
99
|
+
|
|
100
|
+
return [
|
|
101
|
+
HEADER,
|
|
102
|
+
serviceMapMerge,
|
|
103
|
+
jobInterfaces,
|
|
104
|
+
'export {}\n',
|
|
105
|
+
].filter(Boolean).join('\n')
|
|
106
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import checkFileExists from '../../lib/checkFileExists.js'
|
|
2
|
+
import directoryFilesRecursive from '../../lib/directoryFilesRecursive.js'
|
|
3
|
+
|
|
4
|
+
// lucide 0.6 (protocol-resources), PEAKUB DX initiative.
|
|
5
|
+
//
|
|
6
|
+
// A separate, additive pass over an app's already-built protocol list - NOT part of
|
|
7
|
+
// buildSchema()/protocolLoader's own boot-critical path, and never invoked at server boot.
|
|
8
|
+
// buildSchema() already has a known perf finding (redundant reads, unparallelized loops); this
|
|
9
|
+
// module intentionally does not touch it or run as part of it, so it can't make that worse or
|
|
10
|
+
// put schema-compatibility-floor correctness at risk. It's meant to be called once, by a CLI
|
|
11
|
+
// command (see @servable/cli's `servable schema protocol-types`), consuming the `protocols`
|
|
12
|
+
// array `buildSchema()` already produces as plain input - not re-discovering protocols itself.
|
|
13
|
+
//
|
|
14
|
+
// Only `services/` and `jobs/` are scanned. Routes and Parse Cloud functions were deliberately
|
|
15
|
+
// left out of this first pass (not because they're unsupported, but because they're not a good
|
|
16
|
+
// fit yet): no live protocol under a real app currently has a `routes/` directory (the
|
|
17
|
+
// convention is only exercised by the app-level `app/routes/` tree), and `functions/`/`cloudCode`
|
|
18
|
+
// files use a structurally different export shape (multiple named exports, each becoming its own
|
|
19
|
+
// `Parse.Cloud.define()`-registered function, rather than one `{ id, handler }` default export)
|
|
20
|
+
// that this scanner doesn't handle yet. Both are real gaps, not silently ignored - see this
|
|
21
|
+
// package's own CLAUDE.md or file a follow-up rather than assuming they're covered.
|
|
22
|
+
//
|
|
23
|
+
// Services are the one resource here actually invoked through a string-keyed indirection
|
|
24
|
+
// (`Servable.Services.call({ id, params })`) that static analysis can't see through on its own -
|
|
25
|
+
// that's the whole reason this exists. Jobs are listed for discoverability (so an editor can show
|
|
26
|
+
// "what jobs does this protocol define"), not because anything calls a job by name - jobs run on
|
|
27
|
+
// their own cron schedule.
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* @typedef {object} ProtocolResourceEntry
|
|
31
|
+
* @property {string} id - the resource's own `id` field (services) or `id`/name (jobs).
|
|
32
|
+
* @property {string} path - absolute path to the file that exports it, for `typeof import(...)`.
|
|
33
|
+
* @property {string} [cron] - jobs only.
|
|
34
|
+
*/
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* @typedef {object} ProtocolResources
|
|
38
|
+
* @property {ProtocolResourceEntry[]} services
|
|
39
|
+
* @property {ProtocolResourceEntry[]} jobs
|
|
40
|
+
*/
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Scans `services/` and `jobs/` for every protocol in `protocols`, keyed by protocol id.
|
|
44
|
+
* Protocols with neither directory (or with directories that only contain malformed exports -
|
|
45
|
+
* missing `id`, logged and skipped rather than thrown) are omitted from the result entirely.
|
|
46
|
+
*
|
|
47
|
+
* @param {object} props
|
|
48
|
+
* @param {any[]} props.protocols - the `protocols` array `buildSchema()` already produced (each
|
|
49
|
+
* a `ProtocolInstance` with `.id` and `.loader.path`).
|
|
50
|
+
* @returns {Promise<Record<string, ProtocolResources>>}
|
|
51
|
+
*/
|
|
52
|
+
export default async function buildProtocolResources({ protocols }) {
|
|
53
|
+
/** @type {Record<string, ProtocolResources>} */
|
|
54
|
+
const result = {}
|
|
55
|
+
|
|
56
|
+
for (const protocol of protocols || []) {
|
|
57
|
+
const protocolId = protocol && protocol.id
|
|
58
|
+
const rootPath = protocol && protocol.loader && protocol.loader.path
|
|
59
|
+
if (!protocolId || !rootPath) {
|
|
60
|
+
continue
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const services = await scanResourceDir({ dirPath: `${rootPath}/services` })
|
|
64
|
+
const jobs = await scanResourceDir({ dirPath: `${rootPath}/jobs`, isJob: true })
|
|
65
|
+
|
|
66
|
+
if (!services.length && !jobs.length) {
|
|
67
|
+
continue
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
result[protocolId] = { services, jobs }
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return result
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* @param {object} props
|
|
78
|
+
* @param {string} props.dirPath
|
|
79
|
+
* @param {boolean} [props.isJob]
|
|
80
|
+
* @returns {Promise<ProtocolResourceEntry[]>}
|
|
81
|
+
*/
|
|
82
|
+
const scanResourceDir = async ({ dirPath, isJob = false }) => {
|
|
83
|
+
if (!(await checkFileExists(dirPath))) {
|
|
84
|
+
return []
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const files = await directoryFilesRecursive({ path: dirPath, includeMeta: true })
|
|
88
|
+
if (!files) {
|
|
89
|
+
return []
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
return files
|
|
93
|
+
.map(({ module, path }) => {
|
|
94
|
+
const exported = module && module.default
|
|
95
|
+
if (!exported || !exported.id) {
|
|
96
|
+
return null
|
|
97
|
+
}
|
|
98
|
+
return isJob
|
|
99
|
+
? { id: exported.id, path, cron: exported.cron }
|
|
100
|
+
: { id: exported.id, path }
|
|
101
|
+
})
|
|
102
|
+
.filter(entry => entry !== null)
|
|
103
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
// lucide (PEAKUB DX initiative) - PEAKUB-321's sibling: `servable.schema.json` already fully
|
|
2
|
+
// describes every class and field (that's the whole point of unischema's committed artifact -
|
|
3
|
+
// see .docs/technical/unischema-plan.md). This turns that same artifact into a `.d.ts` so
|
|
4
|
+
// `object.get('caption')`/`object.set('caption', value)` gets real autocomplete and a typo in a
|
|
5
|
+
// field name gets flagged, for every app-specific class - pure codegen, no new modeling effort,
|
|
6
|
+
// regenerated fresh on every `servable schema build` (see `servable schema types`, the CLI
|
|
7
|
+
// command this backs).
|
|
8
|
+
|
|
9
|
+
/** Parse field type -> the TS type a getter/setter for it should carry. */
|
|
10
|
+
const FIELD_TYPE_MAP = {
|
|
11
|
+
String: 'string',
|
|
12
|
+
Number: 'number',
|
|
13
|
+
Boolean: 'boolean',
|
|
14
|
+
Date: 'Date',
|
|
15
|
+
Array: 'any[]',
|
|
16
|
+
Object: 'Record<string, any>',
|
|
17
|
+
GeoPoint: '{ latitude: number, longitude: number }',
|
|
18
|
+
Polygon: 'Array<[number, number]>',
|
|
19
|
+
File: '{ url: () => string, name: () => string }',
|
|
20
|
+
ACL: 'Record<string, { read?: boolean, write?: boolean }>',
|
|
21
|
+
Relation: 'ServableRelation<any>',
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* @param {{ type: string, targetClass?: string }} field
|
|
26
|
+
* @param {Set<string>} knownClassNames - classes the artifact actually defines; a `Pointer`
|
|
27
|
+
* to anything else (e.g. a core Parse class like `_User` when only app classes were passed
|
|
28
|
+
* in) falls back to a generic shape rather than referencing a type that won't exist.
|
|
29
|
+
*/
|
|
30
|
+
const tsTypeForField = (field, knownClassNames) => {
|
|
31
|
+
if (field.type === 'Pointer') {
|
|
32
|
+
return field.targetClass && knownClassNames.has(field.targetClass)
|
|
33
|
+
? `ServablePointer<${field.targetClass}>`
|
|
34
|
+
: 'ServablePointer<any>'
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return FIELD_TYPE_MAP[field.type] || 'any'
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const HEADER = `// AUTO-GENERATED by \`servable schema types\` from servable.schema.json - do not hand-edit,
|
|
41
|
+
// it will be overwritten on the next \`servable schema build\`. Commit it alongside
|
|
42
|
+
// servable.schema.json itself so editors don't need to run the generator to get these types.
|
|
43
|
+
//
|
|
44
|
+
// Every field is optional: Parse never enforces a field's presence purely from schema (that's
|
|
45
|
+
// what CLPs/beforeSave validation are for), so a freshly-fetched or not-yet-saved object may
|
|
46
|
+
// legitimately be missing any of them.
|
|
47
|
+
|
|
48
|
+
/** A Parse Pointer field, narrowed to the class it points at. */
|
|
49
|
+
export interface ServablePointer<T> {
|
|
50
|
+
id: string
|
|
51
|
+
className: string
|
|
52
|
+
get<K extends keyof T>(key: K): T[K]
|
|
53
|
+
set<K extends keyof T>(key: K, value: T[K]): this
|
|
54
|
+
fetch(options?: Record<string, any>): Promise<this>
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** A Parse Relation field, narrowed to the class it relates to. */
|
|
58
|
+
export interface ServableRelation<T> {
|
|
59
|
+
query(): { find(options?: Record<string, any>): Promise<T[]> }
|
|
60
|
+
add(object: T): void
|
|
61
|
+
remove(object: T): void
|
|
62
|
+
}
|
|
63
|
+
`
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Generates one TypeScript interface per class in a `servable.schema.json` artifact, each
|
|
67
|
+
* field typed from its Parse field type (`Pointer`/`Relation` narrowed to the target class
|
|
68
|
+
* when that class is also present in the artifact).
|
|
69
|
+
*
|
|
70
|
+
* @param {{ classes: Array<{ className: string, fields: Record<string, { type: string, targetClass?: string }> }> }} artifact
|
|
71
|
+
* - a normalized `servable.schema.json` (or anything with the same `classes` shape).
|
|
72
|
+
* @returns {string} the full `.d.ts` file contents.
|
|
73
|
+
*/
|
|
74
|
+
export default function generateSchemaTypes(artifact) {
|
|
75
|
+
const classes = artifact?.classes || []
|
|
76
|
+
const knownClassNames = new Set(classes.map(c => c.className))
|
|
77
|
+
|
|
78
|
+
const interfaces = classes
|
|
79
|
+
.slice()
|
|
80
|
+
.sort((a, b) => a.className.localeCompare(b.className))
|
|
81
|
+
.map(({ className, fields = {} }) => {
|
|
82
|
+
const fieldLines = Object.entries(fields)
|
|
83
|
+
.sort(([a], [b]) => a.localeCompare(b))
|
|
84
|
+
.map(([fieldName, field]) => {
|
|
85
|
+
const safeName = /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(fieldName)
|
|
86
|
+
? fieldName
|
|
87
|
+
: JSON.stringify(fieldName)
|
|
88
|
+
return ` ${safeName}?: ${tsTypeForField(field, knownClassNames)}`
|
|
89
|
+
})
|
|
90
|
+
.join('\n')
|
|
91
|
+
|
|
92
|
+
return `export interface ${className} {\n${fieldLines || ' [key: string]: any'}\n}`
|
|
93
|
+
})
|
|
94
|
+
.join('\n\n')
|
|
95
|
+
|
|
96
|
+
return `${HEADER}\n${interfaces}\n`
|
|
97
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
export default class Protocol {
|
|
2
|
+
constructor(props: any);
|
|
3
|
+
_id: any;
|
|
4
|
+
_type: any;
|
|
5
|
+
_instances: any[];
|
|
6
|
+
_extractionStatus: number;
|
|
7
|
+
addInstanceIfNeeded({ protocolPayload, servableConfig, instancesPathId }: {
|
|
8
|
+
protocolPayload: any;
|
|
9
|
+
servableConfig: any;
|
|
10
|
+
instancesPathId: any;
|
|
11
|
+
}): Promise<any>;
|
|
12
|
+
set instances(value: any[]);
|
|
13
|
+
get instances(): any[];
|
|
14
|
+
instancesClassesPayloads(): {
|
|
15
|
+
protocolPayload: any;
|
|
16
|
+
instancesPathId: any;
|
|
17
|
+
instancesPathIdString: any;
|
|
18
|
+
instance: any;
|
|
19
|
+
}[];
|
|
20
|
+
set mainInstance(value: any);
|
|
21
|
+
get mainInstance(): any;
|
|
22
|
+
set version(value: any);
|
|
23
|
+
get version(): any;
|
|
24
|
+
set params(value: any);
|
|
25
|
+
get params(): any;
|
|
26
|
+
set loader(value: any);
|
|
27
|
+
get loader(): any;
|
|
28
|
+
set extractionStatus(value: number);
|
|
29
|
+
get extractionStatus(): number;
|
|
30
|
+
set schema(value: any);
|
|
31
|
+
get schema(): any;
|
|
32
|
+
set id(value: any);
|
|
33
|
+
get id(): any;
|
|
34
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
export default class ProtocolInstance {
|
|
2
|
+
constructor(props: any);
|
|
3
|
+
_className: any;
|
|
4
|
+
_params: {};
|
|
5
|
+
_instancesPathId: any[];
|
|
6
|
+
_instancesPathIdString: any;
|
|
7
|
+
_loader: any;
|
|
8
|
+
_version: string;
|
|
9
|
+
_minimumCompatibleVersion: string;
|
|
10
|
+
_loadState: number;
|
|
11
|
+
_module: {};
|
|
12
|
+
_path: any;
|
|
13
|
+
_id: any;
|
|
14
|
+
_protocolPayload: any;
|
|
15
|
+
_schema: {
|
|
16
|
+
classes: {
|
|
17
|
+
managed: any[];
|
|
18
|
+
all: any[];
|
|
19
|
+
};
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* @param {object} [props]
|
|
23
|
+
* @param {object} [props.servableConfig] - used to resolve a pinned version for this
|
|
24
|
+
* protocol instance from `servableConfig.versions`, if present.
|
|
25
|
+
*/
|
|
26
|
+
load({ servableConfig }?: {
|
|
27
|
+
servableConfig?: object;
|
|
28
|
+
}): Promise<void>;
|
|
29
|
+
set loadState(value: number);
|
|
30
|
+
get loadState(): number;
|
|
31
|
+
set module(value: {});
|
|
32
|
+
get module(): {};
|
|
33
|
+
set version(value: string);
|
|
34
|
+
get version(): string;
|
|
35
|
+
set className(value: any);
|
|
36
|
+
get className(): any;
|
|
37
|
+
set params(value: {});
|
|
38
|
+
get params(): {};
|
|
39
|
+
set protocolPayload(value: any);
|
|
40
|
+
get protocolPayload(): any;
|
|
41
|
+
toString(): string;
|
|
42
|
+
set instancesPathIdString(value: any);
|
|
43
|
+
get instancesPathIdString(): any;
|
|
44
|
+
set instancesPathId(value: any[]);
|
|
45
|
+
get instancesPathId(): any[];
|
|
46
|
+
set id(value: any);
|
|
47
|
+
get id(): any;
|
|
48
|
+
set path(value: any);
|
|
49
|
+
get path(): any;
|
|
50
|
+
set minimumCompatibleVersion(value: string);
|
|
51
|
+
get minimumCompatibleVersion(): string;
|
|
52
|
+
set loader(value: any);
|
|
53
|
+
get loader(): any;
|
|
54
|
+
set schema(value: {
|
|
55
|
+
classes: {
|
|
56
|
+
managed: any[];
|
|
57
|
+
all: any[];
|
|
58
|
+
};
|
|
59
|
+
});
|
|
60
|
+
get schema(): {
|
|
61
|
+
classes: {
|
|
62
|
+
managed: any[];
|
|
63
|
+
all: any[];
|
|
64
|
+
};
|
|
65
|
+
};
|
|
66
|
+
/**
|
|
67
|
+
* @returns {Promise<object | null>} this protocol instance's raw `schema.json`
|
|
68
|
+
* contents, memoized by the loader.
|
|
69
|
+
*/
|
|
70
|
+
schemaRaw(): Promise<object | null>;
|
|
71
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
export default class ProtocolLoader {
|
|
2
|
+
constructor(props: any);
|
|
3
|
+
_cache: {};
|
|
4
|
+
_extraClasses: {};
|
|
5
|
+
_extraction: any;
|
|
6
|
+
_path: any;
|
|
7
|
+
_protocolInstance: any;
|
|
8
|
+
set path(value: any);
|
|
9
|
+
get path(): any;
|
|
10
|
+
set cache(value: {});
|
|
11
|
+
get cache(): {};
|
|
12
|
+
set extraction(value: any);
|
|
13
|
+
get extraction(): any;
|
|
14
|
+
set protocolInstance(value: any);
|
|
15
|
+
get protocolInstance(): any;
|
|
16
|
+
/**
|
|
17
|
+
* @param {object} [props]
|
|
18
|
+
* @param {object} [props.servableConfig] - currently unused by this base
|
|
19
|
+
* implementation; accepted for interface parity with the loader subclasses that do
|
|
20
|
+
* read it (e.g. version resolution).
|
|
21
|
+
*/
|
|
22
|
+
loadExtraction({ servableConfig }?: {
|
|
23
|
+
servableConfig?: object;
|
|
24
|
+
}): Promise<void>;
|
|
25
|
+
_accessManifestItem({ item }: {
|
|
26
|
+
item: any;
|
|
27
|
+
}): Promise<any>;
|
|
28
|
+
isValid(): Promise<boolean>;
|
|
29
|
+
getModule(): Promise<any>;
|
|
30
|
+
/**
|
|
31
|
+
* @param {object} props
|
|
32
|
+
* @param {string} props.path
|
|
33
|
+
* @param {string} [props.cacheKey] - most callers omit this. Note:
|
|
34
|
+
* `importJSDefault`/`importJSONDefault` currently accept `cache`/`cacheKey` but never
|
|
35
|
+
* read them - every call is a fresh dynamic `import()`, not actually cached. Caching
|
|
36
|
+
* in this class goes through the separate `_valueInCache`/`this.cache` mechanism
|
|
37
|
+
* instead (see e.g. `classFunctions()`).
|
|
38
|
+
*/
|
|
39
|
+
_importJSDefault({ path, cacheKey }: {
|
|
40
|
+
path: string;
|
|
41
|
+
cacheKey?: string;
|
|
42
|
+
}): Promise<any>;
|
|
43
|
+
/** @param {object} props
|
|
44
|
+
* @param {string} props.path
|
|
45
|
+
* @param {string} [props.cacheKey] - see `_importJSDefault`'s note; unused today.
|
|
46
|
+
*/
|
|
47
|
+
_importJSONDefault({ path, cacheKey }: {
|
|
48
|
+
path: string;
|
|
49
|
+
cacheKey?: string;
|
|
50
|
+
}): Promise<any>;
|
|
51
|
+
_valueInCache(value: any): any;
|
|
52
|
+
}
|