@sanity/functions 1.6.1 → 1.6.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/types.d.ts CHANGED
@@ -1,13 +1,9 @@
1
1
  /**
2
2
  * Callback provided by Sanity CLI when doing local development
3
- * @alpha
4
- * @hidden
5
3
  */
6
4
  export type InvokeCallback = <T = unknown>(name: string, payload: FunctionPayload, options?: InvokeOptions) => Promise<T>;
7
5
  /**
8
6
  * Options provided to invoke
9
- * @alpha
10
- * @hidden
11
7
  */
12
8
  export type InvokeOptions = {
13
9
  sync?: boolean;
@@ -138,18 +134,28 @@ export type GenericContext = FunctionContext | ScheduledFunctionContext | SyncTa
138
134
  */
139
135
  export type GenericEvent = DocumentEvent | SyncTagInvalidateEvent;
140
136
  /**
141
- * A generic function handler that can receive the payload of any function type.
137
+ * A function handler for a pubsub event.
142
138
  *
143
- * The envelope is intentionally permissive so a single handler can be registered
144
- * regardless of the event source without narrowing: `context` is any supported
145
- * context, `event` is any supported event, and `done` is optional (present only
146
- * for sync-tag-invalidate events).
139
+ * A pubsub function can be triggered by any event source, so the envelope is intentionally
140
+ * permissive and needs no narrowing: `context` is any supported context, `event` is any
141
+ * supported event, and `done` is optional (present only for sync-tag-invalidate events).
142
+ *
143
+ * `IResult` is the value the handler resolves to and defaults to `void`. Pubsub is the one
144
+ * function type that may be invoked with {@link invoke | `invoke(name, payload, {sync: true})`},
145
+ * which returns the handler's resolved value to the caller.
147
146
  */
148
- export type EventHandler<IData = any> = (envelope: {
147
+ export type PubSubEventHandler<IData = any, IResult = void> = (envelope: {
149
148
  context: GenericContext;
150
149
  event: DocumentEvent<IData> | SyncTagInvalidateEvent;
151
150
  done?: SyncTagInvalidateCallback;
152
- }) => void | Promise<void>;
151
+ }) => IResult | Promise<IResult>;
152
+ /**
153
+ * A generic function handler that can receive the payload of any function type.
154
+ *
155
+ * @deprecated Renamed to {@link PubSubEventHandler}. This alias will be removed in the next
156
+ * major version.
157
+ */
158
+ export type EventHandler<IData = any, IResult = void> = PubSubEventHandler<IData, IResult>;
153
159
  /**
154
160
  * A callback function to invoke once a sync-tag-invalidate event has been processed. Signals to Sanity that sync tag invalidation has completed.
155
161
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sanity/functions",
3
- "version": "1.6.1",
3
+ "version": "1.6.2",
4
4
  "description": "Helper methods and type definitions for Sanity Functions",
5
5
  "keywords": [
6
6
  "sanity",
package/src/definers.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type {DocumentEventHandler, EventHandler, ScheduledEventHandler, SyncTagInvalidateEventHandler} from './types.js'
1
+ import type {DocumentEventHandler, PubSubEventHandler, ScheduledEventHandler, SyncTagInvalidateEventHandler} from './types.js'
2
2
 
3
3
  /**
4
4
  * Defines a "document event" function handler.
@@ -37,13 +37,31 @@ export function syncTagInvalidateEventHandler(handler: SyncTagInvalidateEventHan
37
37
  }
38
38
 
39
39
  /**
40
- * Defines a "generic" function handler.
40
+ * Defines a "pubsub event" function handler.
41
41
  * Returns the handler function as-is, only providing the types and doing basic validation.
42
42
  *
43
+ * The handler's return type is inferred, so a pubsub function can resolve to a value that
44
+ * callers receive from `invoke(name, payload, {sync: true})`.
45
+ *
43
46
  * @param handler - The event handler function to use.
44
47
  * @returns The handler function, unmodified.
45
48
  */
46
- export function eventHandler<IData = any>(handler: EventHandler<IData>): EventHandler<IData> {
49
+ export function pubSubEventHandler<IData = any, IResult = void>(
50
+ handler: PubSubEventHandler<IData, IResult>,
51
+ ): PubSubEventHandler<IData, IResult> {
47
52
  if (typeof handler !== 'function') throw new TypeError('`handler` must be a function')
48
53
  return handler
49
54
  }
55
+
56
+ /**
57
+ * Defines a "generic" function handler.
58
+ * Returns the handler function as-is, only providing the types and doing basic validation.
59
+ *
60
+ * @deprecated Renamed to {@link pubSubEventHandler}. This alias will be removed in the next
61
+ * major version.
62
+ * @param handler - The event handler function to use.
63
+ * @returns The handler function, unmodified.
64
+ */
65
+ export function eventHandler<IData = any, IResult = void>(handler: PubSubEventHandler<IData, IResult>): PubSubEventHandler<IData, IResult> {
66
+ return pubSubEventHandler(handler)
67
+ }
package/src/invoke.ts CHANGED
@@ -112,8 +112,6 @@ export function buildLineageToken(name: string, lineage: string | undefined) {
112
112
  * event source and nothing is returned. Pass `{sync: true}` to invoke the function and
113
113
  * wait for its return value.
114
114
  *
115
- * @alpha Experimental / preview function not ready for use
116
- * @hidden
117
115
  * @param name - Name of the function to invoke
118
116
  * @param payload - The `{event, context}` envelope to deliver
119
117
  * @param options - Set `sync: true` to wait for and return the function's result
package/src/types.ts CHANGED
@@ -1,14 +1,10 @@
1
1
  /**
2
2
  * Callback provided by Sanity CLI when doing local development
3
- * @alpha
4
- * @hidden
5
3
  */
6
4
  export type InvokeCallback = <T = unknown>(name: string, payload: FunctionPayload, options?: InvokeOptions) => Promise<T>
7
5
 
8
6
  /**
9
7
  * Options provided to invoke
10
- * @alpha
11
- * @hidden
12
8
  */
13
9
  export type InvokeOptions = {
14
10
  // Whether the function should be invoked synchronously
@@ -144,18 +140,29 @@ export type GenericContext = FunctionContext | ScheduledFunctionContext | SyncTa
144
140
  export type GenericEvent = DocumentEvent | SyncTagInvalidateEvent
145
141
 
146
142
  /**
147
- * A generic function handler that can receive the payload of any function type.
143
+ * A function handler for a pubsub event.
148
144
  *
149
- * The envelope is intentionally permissive so a single handler can be registered
150
- * regardless of the event source without narrowing: `context` is any supported
151
- * context, `event` is any supported event, and `done` is optional (present only
152
- * for sync-tag-invalidate events).
145
+ * A pubsub function can be triggered by any event source, so the envelope is intentionally
146
+ * permissive and needs no narrowing: `context` is any supported context, `event` is any
147
+ * supported event, and `done` is optional (present only for sync-tag-invalidate events).
148
+ *
149
+ * `IResult` is the value the handler resolves to and defaults to `void`. Pubsub is the one
150
+ * function type that may be invoked with {@link invoke | `invoke(name, payload, {sync: true})`},
151
+ * which returns the handler's resolved value to the caller.
153
152
  */
154
- export type EventHandler<IData = any> = (envelope: {
153
+ export type PubSubEventHandler<IData = any, IResult = void> = (envelope: {
155
154
  context: GenericContext
156
155
  event: DocumentEvent<IData> | SyncTagInvalidateEvent
157
156
  done?: SyncTagInvalidateCallback
158
- }) => void | Promise<void>
157
+ }) => IResult | Promise<IResult>
158
+
159
+ /**
160
+ * A generic function handler that can receive the payload of any function type.
161
+ *
162
+ * @deprecated Renamed to {@link PubSubEventHandler}. This alias will be removed in the next
163
+ * major version.
164
+ */
165
+ export type EventHandler<IData = any, IResult = void> = PubSubEventHandler<IData, IResult>
159
166
 
160
167
  /**
161
168
  * A callback function to invoke once a sync-tag-invalidate event has been processed. Signals to Sanity that sync tag invalidation has completed.