Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

next
ts 30 lines 1.26 KB
Raw
1 import { Array as Arr, Option, Record as EffectRecord } from 'effect';
2
3 /**
4 * The request parameters a record of optional flags turns into: each present
5 * flag keeps its value and each absent one becomes `undefined`, which the client
6 * leaves out of the query string.
7 */
8 export type OptionalParams<R extends Readonly<Record<string, Option.Option<unknown>>>> = {
9 readonly [K in keyof R]: R[K] extends Option.Option<infer A> ? A | undefined : never;
10 };
11
12 /**
13 * Turns optional flags into request parameters in one place, instead of an
14 * `Option.getOrUndefined` per field at the call site.
15 *
16 * @example
17 * client._internal.logs.triggers.list({ ...optionalParams({ cursor, from, to }), limit })
18 */
19 export const optionalParams = <const R extends Readonly<Record<string, Option.Option<unknown>>>>(
20 flags: R
21 ): OptionalParams<R> => EffectRecord.map(flags, Option.getOrUndefined) as OptionalParams<R>;
22
23 /**
24 * The request parameter a list of filters turns into: the list when it has
25 * elements and `undefined` when it is empty, so an unused filter is left out of
26 * the request instead of being sent as `[]`.
27 */
28 export const nonEmptyOrUndefined = <const T extends ReadonlyArray<unknown>>(
29 values: T
30 ): T | undefined => (Arr.isReadonlyArrayNonEmpty(values) ? values : undefined);