How to use the decoders.object function in decoders

To help you get started, we’ve selected a few decoders examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github nvie / decoders / src / types / object-tests.ts View on Github external
import { exact, object, pojo, string } from 'decoders';

// $ExpectType Decoder<{ foo: string; bar: { qux: string; }; }, unknown>
object({
    foo: string,
    bar: object({ qux: string }),
});

// $ExpectType Decoder<{ foo: string; bar: { qux: string; }; }, unknown>
exact({
    foo: string,
    bar: object({ qux: string }),
});

// $ExpectType Decoder<{ [key: string]: unknown; }, unknown>
pojo;
github purecloudlabs / iframe-coordinator / src / messages / Lifecycle.ts View on Github external
import { LabeledMsg } from './LabeledMsg';

/**
 * Client started indication.  The client will
 * now listen for messages and begin sending
 * messages from the client application.
 * @external
 */
export interface LabeledStarted extends LabeledMsg {
  /** Message identifier */
  msgType: 'client_started';
}

// We don't care what is in msg for Started messages.
/** @external */
const startedDecoder: Decoder = object({
  msgType: constant<'client_started'>('client_started'),
  msg: mixed
});

/**
 * Environmental data provided to all clients
 * in order to match behavior of the host application.
 */
export interface EnvData {
  /** Locale in use by the host app */
  locale: string;
  /** Location of the host app */
  hostRootUrl: string;
  /** Keys to notify changes on */
  registeredKeys?: KeyData[];
  /** Extra host-specific details */
github purecloudlabs / iframe-coordinator / src / messages / Lifecycle.ts View on Github external
}

/**
 * Initial setup message where environmental data
 * is sent to the client.
 * @external
 */
export interface LabeledEnvInit extends LabeledMsg {
  /** Message identifier */
  msgType: 'env_init';
  /** Environment data */
  msg: SetupData;
}

/* @external */
const envDecoder: Decoder = object({
  msgType: constant<'env_init'>('env_init'),
  msg: object({
    locale: string,
    hostRootUrl: string,
    assignedRoute: string,
    registeredKeys: optional(
      array(
        object({
          key: string,
          altKey: optional(boolean),
          ctrlKey: optional(boolean),
          metaKey: optional(boolean),
          shiftKey: optional(boolean)
        })
      )
    ),
github purecloudlabs / iframe-coordinator / src / messages / Lifecycle.ts View on Github external
/**
 * Initial setup message where environmental data
 * is sent to the client.
 * @external
 */
export interface LabeledEnvInit extends LabeledMsg {
  /** Message identifier */
  msgType: 'env_init';
  /** Environment data */
  msg: SetupData;
}

/* @external */
const envDecoder: Decoder = object({
  msgType: constant<'env_init'>('env_init'),
  msg: object({
    locale: string,
    hostRootUrl: string,
    assignedRoute: string,
    registeredKeys: optional(
      array(
        object({
          key: string,
          altKey: optional(boolean),
          ctrlKey: optional(boolean),
          metaKey: optional(boolean),
          shiftKey: optional(boolean)
        })
      )
    ),
    custom: mixed
  })
github nvie / decoders / src / types / typescript-tests.ts View on Github external
} from 'decoders';

const rect = object({ type: constant('rect') });
const circle = object({ type: constant('circle') });
const shape = dispatch(field('type', string), type => {
    switch (type) {
        case 'rect':
            return rect;
        case 'circle':
            return circle;
    }
    return fail('Must be a valid shape');
});

guard(
    object({
        a0: string,
        a1: number,
        a2: optional(nullable(either4(string, email, regex(/x/, 'Must be x'), url()))),
        a3: fail('foo'),
        a4: maybe(mixed),
        a5: either3(constant('foo'), constant('bar'), constant('qux')),
        a6: exact({ c: array(poja), d: pojo }),
        a7: tuple6(hardcoded('foo'), mixed, null_, undefined_, unknown, truthy),
        a8: tuple4(integer, number, positiveInteger, positiveNumber),
        a9: either(boolean, numericBoolean),
        b0: map(
            compose(
                string,
                predicate(s => s.startsWith('x'), 'Must start with x')
            ),
            s => s.toUpperCase()
github purecloudlabs / iframe-coordinator / src / messages / NavRequest.ts View on Github external
/**
 * A message used to request the host navigate to another
 * URI.
 * @external
 */
export interface LabeledNavRequest extends LabeledMsg {
  /** Message identifier */
  msgType: 'navRequest';
  /** Navigation request details */
  msg: NavRequest;
}

/** @external */
const decoder: Decoder = object({
  msgType: constant<'navRequest'>('navRequest'),
  msg: object({
    url: string
  })
});

export { decoder };
github purecloudlabs / iframe-coordinator / src / workers / worker-manager.ts View on Github external
*/
  terminateReadyWaitMillis?: number;
}

/**
 * WorkerClient registration config.
 */
export interface WorkerClientConfig
  extends ErrorWindowConfig,
    WorkerTerminateConfig {
  /** Hosted location of the worker to load */
  url: string;
}

const validateWorkerClientConfig: Guard = guard(
  object({
    url: string,
    errorWindowMillis: optional(integer),
    errorWindowCountThreshold: optional(integer),
    notifyBeforeTerminate: optional(boolean),
    terminateReadyWaitMillis: optional(integer)
  })
);

/**
 * A map from worker client identifiers to configuration describing
 * where the worker client app is hosted, options on how it should run, etc.
 */
export interface WorkerMap {
  [key: string]: WorkerClientConfig;
}
github purecloudlabs / iframe-coordinator / src / messages / KeyDown.ts View on Github external
import { LabeledMsg } from './LabeledMsg';

/**
 * A message used to send key information
 * to the host application.
 * @external
 */
export interface LabeledKeyDown extends LabeledMsg {
  /** Message identifier */
  msgType: 'registeredKeyFired';
  /** Key details */
  msg: NativeKey;
}

/** @external */
const decoder: Decoder = object({
  msgType: constant<'registeredKeyFired'>('registeredKeyFired'),
  msg: object({
    altKey: optional(boolean),
    charCode: optional(number),
    code: optional(string),
    ctrlKey: optional(boolean),
    key: string,
    keyCode: optional(number),
    metaKey: optional(boolean),
    shiftKey: optional(boolean)
  })
});

export { decoder };
github purecloudlabs / iframe-coordinator / src / messages / Toast.ts View on Github external
/**
 * A message used to request toasts to display
 * in the host application.
 * @external
 */
export interface LabeledToast extends LabeledMsg {
  /** Message identifier */
  msgType: 'toastRequest';
  /** Toast details */
  msg: Toast;
}

/** @external */
const decoder: Decoder = object({
  msgType: constant<'toastRequest'>('toastRequest'),
  msg: object({
    title: optional(string),
    message: string,
    custom: mixed
  })
});

export { decoder };
github purecloudlabs / iframe-coordinator / src / messages / Publication.ts View on Github external
}

/**
 * A message used to publish a generic messages
 * between the clients and the host application.
 * @external
 */
export interface LabeledPublication extends LabeledMsg {
  /** Message identifier */
  msgType: 'publish';
  /** Details of the data to publish */
  msg: Publication;
}

/** @external */
const decoder: Decoder = object({
  msgType: constant<'publish'>('publish'),
  msg: object({
    topic: string,
    payload: mixed,
    clientId: optional(string)
  })
});

export { decoder };