-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathapply-extension-spec.ts
More file actions
48 lines (38 loc) · 1.67 KB
/
apply-extension-spec.ts
File metadata and controls
48 lines (38 loc) · 1.67 KB
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
41
42
43
44
45
46
47
48
import { OpenrpcDocument as OpenRPC } from "./types";
/**
* Applies extension specifications to an OpenRPC document by modifying the meta schema
* @param document - The OpenRPC document containing x-extensions
* @param metaSchema - The meta schema to extend
* @returns The modified meta schema with extensions applied
* @throws {Error} If the schema definition doesn't exist or if extension name conflicts
*/
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-explicit-any
function applyExtensionSpec(document: OpenRPC, metaSchema: any): any {
const extendedMetaSchema = metaSchema;
if (!document["x-extensions"]) return extendedMetaSchema;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
document["x-extensions"].forEach((extension: any) => {
const { name, schema, summary, description, restricted } = extension;
if (extendedMetaSchema.definitions[name]) {
throw new Error(`${name} already exists in definitions, cannot apply extension ${name}`);
}
restricted.forEach((schemaDefinition: string) => {
const def = extendedMetaSchema.definitions[schemaDefinition];
if (!def)
throw new Error(`${schemaDefinition} does not exist, cannot apply extension ${name}`);
if (def.properties[name])
throw new Error(
`${name} already exists in ${schemaDefinition}, cannot apply extension ${name}`
);
def.properties[name] = {
title: name,
description,
summary,
...schema,
};
extendedMetaSchema.definitions[name] = def.properties[name];
});
});
return extendedMetaSchema;
}
export default applyExtensionSpec;