forked from offlegacy/event-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresolveParams.ts
More file actions
24 lines (23 loc) · 1002 Bytes
/
Copy pathresolveParams.ts
File metadata and controls
24 lines (23 loc) · 1002 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import { isEventPropsWithSchema } from "../helpers/isEventPropsWithSchema";
import { isFunction } from "../helpers/isFunction";
import type { Context, EventParams, Schemas, SchemaParams, UnionPropsWithAndWithoutSchema } from "../types";
/**
* Resolves parameters from props to actual values.
* Handles both cases with and without schema,
* and executes functions by passing the current context.
*/
export function resolveParams<
TContext extends Context,
TEventParams extends EventParams,
TSchemas extends Schemas,
TKey extends keyof TSchemas,
>(
props: UnionPropsWithAndWithoutSchema<TContext, TEventParams, TSchemas, TKey>,
getContext: () => TContext,
): TEventParams | SchemaParams<TSchemas, TKey> {
if (isEventPropsWithSchema(props)) {
return isFunction<TContext, SchemaParams<TSchemas, TKey>>(props.params) ? props.params(getContext()) : props.params;
} else {
return isFunction<TContext, TEventParams>(props.params) ? props.params(getContext()) : props.params;
}
}