All files / kernel-platforms/src specification.ts

100% Statements 2/2
100% Branches 0/0
100% Functions 1/1
100% Lines 2/2

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                                                      8x                         19x          
import type { Struct, Infer, AnyStruct } from '@metamask/superstruct';
import type { Json } from '@metamask/utils';
 
export type CapabilitySpecification<
  ConfigStruct extends AnyStruct,
  Value,
  Options extends Record<string, unknown> = Record<string, unknown>,
> = Infer<
  Struct<
    {
      configStruct: ConfigStruct;
      capabilityFactory: (
        config: Infer<ConfigStruct>,
        options?: Options,
      ) => Value;
    },
    'CapabilitySpecification'
  >
>;
 
/**
 * Creates a capability specification from a configuration structure and a factory function.
 *
 * @param configStruct - The configuration structure for the capability
 * @param capabilityFactory - The factory function that creates the capability
 * @returns An object containing the configuration structure and the factory function
 */
export const makeCapabilitySpecification = <
  ConfigStruct extends AnyStruct,
  Value,
  Options extends Record<string, unknown> = Record<string, unknown>,
>(
  // configStruct must be a JSON-serializable struct
  configStruct: ConfigStruct extends AnyStruct
    ? Infer<ConfigStruct> extends Json
      ? ConfigStruct
      : never
    : never,
  capabilityFactory: (config: Infer<ConfigStruct>, options?: Options) => Value,
): CapabilitySpecification<ConfigStruct, Value, Options> => {
  return {
    configStruct,
    capabilityFactory,
  };
};