Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 | 21x 17x 2x 2x 3x 3x 3x 4x 4x 1x 4x 1x 4x 3x 3x 1x 3x 1x 3x 5x 5x 5x 6x 6x 6x 6x 1x 6x 5x 5x 1x 1x 2x 1x 1x 1x | /**
* One-way converter from the `MethodSchema`/`JsonSchema` types used by
* `makeDiscoverableExo` (in `@metamask/kernel-utils`) into the richer
* `TypeSpec` / `ObjectSpec` / `RemotableSpec` / `MethodSpec` shape used in
* {@link ServiceDescription}.
*
* The conversion is lossy in the following ways:
*
* - `MethodSchema.args` is an unordered map of named parameters; the target
* `MethodSpec.parameters` is an ordered array of unnamed `ValueSpec`s. We
* use the iteration order of the args record, and we drop the names. The
* names are preserved as `ValueSpec.description` if no description was
* otherwise present, so they remain human-readable.
* - `JsonSchema` has no notion of `remotable`, `null`, `void`, `bigint`,
* `unknown`, or `union`. The converter never emits those kinds.
*/
import type { JsonSchema, MethodSchema } from '@metamask/kernel-utils';
import type {
MethodSpec,
ObjectSpec,
RemotableSpec,
TypeSpec,
ValueSpec,
} from './service-description.ts';
/**
* Convert a `JsonSchema` to a `TypeSpec`.
*
* @param schema - The source JsonSchema.
* @returns The equivalent TypeSpec.
*/
export function jsonSchemaToTypeSpec(schema: JsonSchema): TypeSpec {
switch (schema.type) {
case 'string':
case 'number':
case 'boolean':
return { kind: schema.type };
case 'array':
return {
kind: 'array',
elementType: jsonSchemaToTypeSpec(schema.items),
};
case 'object':
return {
kind: 'object',
spec: jsonSchemaToObjectSpec(schema),
};
default: {
// Exhaustive: JsonSchema is a closed union.
const unreachable: never = schema;
throw new Error(`Unsupported JsonSchema: ${JSON.stringify(unreachable)}`);
}
}
}
/**
* Convert an object-typed `JsonSchema` to an `ObjectSpec`.
*
* @param schema - The source object-typed JsonSchema.
* @returns The equivalent ObjectSpec.
*/
export function jsonSchemaToObjectSpec(
schema: Extract<JsonSchema, { type: 'object' }>,
): ObjectSpec {
const required = new Set(schema.required ?? []);
const properties: Record<string, ValueSpec> = {};
for (const [name, propSchema] of Object.entries(schema.properties)) {
const propSpec: ValueSpec = { type: jsonSchemaToTypeSpec(propSchema) };
if (propSchema.description !== undefined) {
propSpec.description = propSchema.description;
}
if (!required.has(name)) {
propSpec.optional = true;
}
properties[name] = propSpec;
}
const out: ObjectSpec = { properties };
if (schema.description !== undefined) {
out.description = schema.description;
}
if (schema.additionalProperties) {
out.extensible = true;
}
return out;
}
/**
* Convert a `MethodSchema` to a `MethodSpec`.
*
* Because `MethodSchema.args` is a named record while `MethodSpec.parameters`
* is a positional array, the parameters are emitted in the iteration order of
* the args record, and each parameter's name is preserved in its
* `description` field when the source did not supply one. An argument absent
* from `schema.required` becomes an optional parameter; an absent `required`
* treats every parameter as required.
*
* @param schema - The source MethodSchema.
* @returns The equivalent MethodSpec.
*/
export function methodSchemaToMethodSpec(schema: MethodSchema): MethodSpec {
const required = new Set(schema.required ?? Object.keys(schema.args));
const parameters: ValueSpec[] = [];
for (const [name, argSchema] of Object.entries(schema.args)) {
const type = jsonSchemaToTypeSpec(argSchema);
const description = argSchema.description ?? name;
const parameter: ValueSpec = { description, type };
if (!required.has(name)) {
parameter.optional = true;
}
parameters.push(parameter);
}
const returnType: TypeSpec = schema.returns
? jsonSchemaToTypeSpec(schema.returns)
: { kind: 'void' };
return {
description: schema.description,
parameters,
returnType,
};
}
/**
* Convert a `Record<string, MethodSchema>` (as accepted by
* `makeDiscoverableExo`) into a `RemotableSpec`.
*
* @param options - Conversion options.
* @param options.methods - The source methods record.
* @param options.description - Optional description for the resulting
* RemotableSpec.
* @returns The equivalent RemotableSpec.
*/
export function methodsToRemotableSpec(options: {
methods: Record<string, MethodSchema>;
description?: string;
}): RemotableSpec {
const { methods, description } = options;
const out: RemotableSpec = {
methods: Object.fromEntries(
Object.entries(methods).map(([name, schema]) => [
name,
methodSchemaToMethodSpec(schema),
]),
),
};
Eif (description !== undefined) {
out.description = description;
}
return out;
}
|