MCPcopy Create free account
hub / github.com/Effect-TS/effect / makeDef

Function makeDef

packages/effect/src/ui/Query.ts:2711–2814  ·  view source on GitHub ↗
(
  name: string,
  options: {
    readonly args?: Schema.Top | Schema.Struct.Fields | undefined
    readonly success: Schema.Top
    readonly error?: Schema.Top | undefined
    readonly staleTime: Duration.Input
    readonly idleTimeToLive?: Duration.Input | undefined
    readonly refetchInterval?: RefetchInterval<any, any, any> | undefined
    readonly tags?: ReadonlyArray<{ readonly name: string }> | undefined
    readonly persist?: boolean | undefined
    readonly reactivityKeys?: ((args: any) => Reactivity.KeysInput) | undefined
    readonly server?: boolean | undefined
  },
  list?: ListDef,
  live?: LiveOps
)

Source from the content-addressed store, hash-verified

2709 * Builds a query's data, and throws on what a definition may not say.
2710 */
2711const makeDef = (
2712 name: string,
2713 options: {
2714 readonly args?: Schema.Top | Schema.Struct.Fields | undefined
2715 readonly success: Schema.Top
2716 readonly error?: Schema.Top | undefined
2717 readonly staleTime: Duration.Input
2718 readonly idleTimeToLive?: Duration.Input | undefined
2719 readonly refetchInterval?: RefetchInterval<any, any, any> | undefined
2720 readonly tags?: ReadonlyArray<{ readonly name: string }> | undefined
2721 readonly persist?: boolean | undefined
2722 readonly reactivityKeys?: ((args: any) => Reactivity.KeysInput) | undefined
2723 readonly server?: boolean | undefined
2724 },
2725 list?: ListDef,
2726 live?: LiveOps
2727): QueryDef => {
2728 const staleMs = nonNegative(name, "staleTime", options.staleTime)
2729 const idleMs = options.idleTimeToLive === undefined
2730 ? undefined
2731 : nonNegative(name, "idleTimeToLive", options.idleTimeToLive)
2732 let refetchInterval: QueryDef["refetchInterval"]
2733 if (typeof options.refetchInterval === "function") {
2734 refetchInterval = options.refetchInterval
2735 } else if (options.refetchInterval !== undefined) {
2736 const ms = parseInterval(options.refetchInterval)
2737 if (ms === "invalid") {
2738 throw new Error(
2739 "[Query] " + quote(name) +
2740 ": refetchInterval must be positive, and not a bare number under 1000. " +
2741 subSecondHint
2742 )
2743 }
2744 refetchInterval = ms
2745 }
2746 const args: Schema.Top = Schema.isSchema(options.args)
2747 ? options.args
2748 : Schema.Struct(options.args ?? {})
2749 const error = options.error ?? Schema.Never
2750 // Built on first use: deriving a codec walks the Schema.
2751 const encodeArgs = lazy(() => argsEncoder(args.ast))
2752 const encodeLogArgs = lazy(() => {
2753 const redacted = redactAST(args.ast)
2754 // No `Schema.Redacted` in the args: the log key is the args key.
2755 return redacted === args.ast ? undefined : argsEncoder(redacted)
2756 })
2757 const drop = lazy(() => dropUndefinedOf(args.ast))
2758 const codec = lazy(() => makeCodec(args, options.success, error))
2759 const argsKey = (value: unknown): string => {
2760 const encode = encodeArgs()
2761 let encoded: unknown
2762 try {
2763 encoded = encode(value)
2764 } catch {
2765 // The Schema issue prints the value, so it isn't passed on.
2766 throw new Error(
2767 "[Query] " + quote(name) +
2768 ": the args don't encode with the query's args Schema"

Callers 3

ServiceFunction · 0.85
InfiniteServiceFunction · 0.85
StreamServiceFunction · 0.85

Calls 9

parseIntervalFunction · 0.85
argsEncoderFunction · 0.85
dropUndefinedOfFunction · 0.85
nonNegativeFunction · 0.70
quoteFunction · 0.70
lazyFunction · 0.70
makeCodecFunction · 0.70
dropFunction · 0.50
mapMethod · 0.45

Tested by

no test coverage detected