How to use the monocle-ts.Optional function in monocle-ts

To help you get started, we’ve selected a few monocle-ts 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 unmock / unmock-js / packages / unmock-core / src / generator-experimental.ts View on Github external
o?: Operation,
): Option<[MethodNames, Operation]> =>
  o ? some([n, o]) : getFirstMethodInternal(m, p);

const getFirstMethodInternal = (m: MethodNames[], p: PathItem): Option<[MethodNames, Operation]> =>
  m.length === 0 ? none : getFirstMethodInternal2(p, m[0], m.slice(1), p[m[0]]);

export const getFirstMethod = (p: PathItem): Option<[MethodNames, Operation]> =>
  getFirstMethodInternal(allMethods, p);

/**
 * Gets `some` random operation (the first one) from a path item
 * or `none` if there are no operations
 * @param p A path item
 */
export const operationOptional = new Optional(
  a => getFirstMethod(a),
  a => s => ({ ...s, [a[0]]: a[1]}),
);

/**
 * Gets `some` header from a reference
 * or `none` if the reference doesn't exist
 * @param o an open api object
 * @param d the name of the header reference
 */
export const getHeaderFromRef = (o: OpenAPIObject, d: string): Option<header> =&gt;
  getComponentFromRef(
    o,
    d,
    a =&gt; (a.headers ? some(a.headers) : none),
    internalGetHeaderFromRef,</header>
github unmock / unmock-js / packages / unmock-core / src / generator.ts View on Github external
const getFirstMethodInternal = (
  m: MethodNames[],
  p: PathItem,
): Option&lt;[MethodNames, Operation]&gt; =&gt;
  m.length === 0 ? none : getFirstMethodInternal2(p, m[0], m.slice(1), p[m[0]]);

export const getFirstMethod = (p: PathItem): Option&lt;[MethodNames, Operation]&gt; =&gt;
  getFirstMethodInternal(allMethods, p);

/**
 * Gets `some` random operation (the first one) from a path item
 * or `none` if there are no operations
 * @param p A path item
 */
export const operationOptional = new Optional&lt;
  PathItem,
  [MethodNames, Operation]
&gt;(a =&gt; getFirstMethod(a), a =&gt; s =&gt; ({ ...s, [a[0]]: a[1] }));

/**
 * Gets `some` header from a reference
 * or `none` if the reference doesn't exist
 * @param o an open api object
 * @param d the name of the header reference
 */
export const getHeaderFromRef = (o: OpenAPIObject, d: string): Option<header> =&gt;
  getComponentFromRef(
    o,
    d,
    a =&gt; (a.headers ? some(a.headers) : none),
    internalGetHeaderFromRef,</header>