@sanity/functions 1.0.3 → 1.2.0

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.
@@ -1,4 +1,4 @@
1
- import type { DocumentEventHandler } from './types.js';
1
+ import type { DocumentEventHandler, ScheduleEventHandler } from './types.js';
2
2
  /**
3
3
  * Defines a "document event" function handler.
4
4
  * Returns the handler function as-is, only providing the types and doing basic validation.
@@ -7,3 +7,12 @@ import type { DocumentEventHandler } from './types.js';
7
7
  * @returns The handler function, unmodified.
8
8
  */
9
9
  export declare function documentEventHandler<IData = any>(handler: DocumentEventHandler<IData>): DocumentEventHandler<IData>;
10
+ /**
11
+ * Defines a "scheduled event" function handler.
12
+ * Returns the handler function as-is, only providing the types and doing basic validation.
13
+ *
14
+ * @beta
15
+ * @param handler - The event handler function to use.
16
+ * @returns The handler function, unmodified.
17
+ */
18
+ export declare function scheduleEventHandler(handler: ScheduleEventHandler): ScheduleEventHandler;
package/dist/definers.js CHANGED
@@ -10,4 +10,17 @@ export function documentEventHandler(handler) {
10
10
  throw new TypeError('`handler` must be a function');
11
11
  return handler;
12
12
  }
13
+ /**
14
+ * Defines a "scheduled event" function handler.
15
+ * Returns the handler function as-is, only providing the types and doing basic validation.
16
+ *
17
+ * @beta
18
+ * @param handler - The event handler function to use.
19
+ * @returns The handler function, unmodified.
20
+ */
21
+ export function scheduleEventHandler(handler) {
22
+ if (typeof handler !== 'function')
23
+ throw new TypeError('`handler` must be a function');
24
+ return handler;
25
+ }
13
26
  //# sourceMappingURL=definers.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"definers.js","sourceRoot":"","sources":["../src/definers.ts"],"names":[],"mappings":"AAEA;;;;;;GAMG;AACH,MAAM,UAAU,oBAAoB,CAClC,OAAoC;IAEpC,IAAI,OAAO,OAAO,KAAK,UAAU;QAAE,MAAM,IAAI,SAAS,CAAC,8BAA8B,CAAC,CAAA;IACtF,OAAO,OAAO,CAAA;AAChB,CAAC"}
1
+ {"version":3,"file":"definers.js","sourceRoot":"","sources":["../src/definers.ts"],"names":[],"mappings":"AAEA;;;;;;GAMG;AACH,MAAM,UAAU,oBAAoB,CAClC,OAAoC;IAEpC,IAAI,OAAO,OAAO,KAAK,UAAU;QAAE,MAAM,IAAI,SAAS,CAAC,8BAA8B,CAAC,CAAA;IACtF,OAAO,OAAO,CAAA;AAChB,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,oBAAoB,CAClC,OAA6B;IAE7B,IAAI,OAAO,OAAO,KAAK,UAAU;QAAE,MAAM,IAAI,SAAS,CAAC,8BAA8B,CAAC,CAAA;IACtF,OAAO,OAAO,CAAA;AAChB,CAAC"}
package/dist/types.d.ts CHANGED
@@ -4,6 +4,14 @@
4
4
  * @beta
5
5
  */
6
6
  export interface FunctionContext {
7
+ /** The resource type of the event source; resource type that invoked the function. */
8
+ eventResourceType: string;
9
+ /** The resource ID of the event source; resource ID that invoked the function. */
10
+ eventResourceId: string;
11
+ /** The resource type of the function container; resource type that houses the function. */
12
+ functionResourceType: string;
13
+ /** The resource ID of the function container; resource ID that houses the function. */
14
+ functionResourceId: string;
7
15
  /**
8
16
  * `local` is set to `true` when testing your function locally.
9
17
  * i.e. `sanity function test func-name`
@@ -32,6 +40,40 @@ export interface FunctionContext {
32
40
  token: string;
33
41
  };
34
42
  }
43
+ /**
44
+ * The context object passed to the schedule function handler.
45
+ *
46
+ * @beta
47
+ */
48
+ export interface ScheduleFunctionContext {
49
+ /**
50
+ * `local` is set to `true` when testing your function locally.
51
+ * i.e. `sanity function test func-name`
52
+ * Otherwise, the property is not set.
53
+ */
54
+ local?: boolean;
55
+ /**
56
+ * Options that can be passed to a `@sanity/client` constructor to configure it
57
+ * against the project and dataset which triggered the event. Note that you should
58
+ * always specify an explicit `apiVersion` in YYYY-MM-DD format (e.g. `2025-05-01`).
59
+ *
60
+ * @example
61
+ * Constructing a client with the options from the context:
62
+ * ```ts
63
+ * const client = createClient({
64
+ * apiVersion: '2025-05-01',
65
+ * ...context.clientOptions,
66
+ * })
67
+ * ```
68
+ * @beta
69
+ */
70
+ clientOptions?: {
71
+ apiHost?: string;
72
+ dataset?: string;
73
+ projectId?: string;
74
+ token?: string;
75
+ };
76
+ }
35
77
  /**
36
78
  * The event object received by the function handler in the case of a document event,
37
79
  * such as a publish, unpublish, delete or mutation event and similar.
@@ -57,3 +99,11 @@ export type DocumentEventHandler<IData = any> = (envelope: {
57
99
  context: FunctionContext;
58
100
  event: DocumentEvent<IData>;
59
101
  }) => void | Promise<void>;
102
+ /**
103
+ * A function handler for a schedule event.
104
+ *
105
+ * @beta
106
+ */
107
+ export type ScheduleEventHandler = (envelope: {
108
+ context: ScheduleFunctionContext;
109
+ }) => void | Promise<void>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sanity/functions",
3
- "version": "1.0.3",
3
+ "version": "1.2.0",
4
4
  "description": "Helper methods and type definitions for Sanity Functions",
5
5
  "keywords": [
6
6
  "sanity",
@@ -38,7 +38,7 @@
38
38
  "build": "rimraf dist && tsc",
39
39
  "coverage": "vitest --coverage",
40
40
  "lint": "tsc --noEmit --project tsconfig.test.json",
41
- "prepublishOnly": "npm run build",
41
+ "prepare": "npm run build",
42
42
  "test": "vitest run --typecheck --reporter=verbose",
43
43
  "test:watch": "vitest --typecheck --reporter=verbose"
44
44
  },
package/src/definers.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type {DocumentEventHandler} from './types.js'
1
+ import type { DocumentEventHandler, ScheduleEventHandler } from './types.js'
2
2
 
3
3
  /**
4
4
  * Defines a "document event" function handler.
@@ -13,3 +13,18 @@ export function documentEventHandler<IData = any>(
13
13
  if (typeof handler !== 'function') throw new TypeError('`handler` must be a function')
14
14
  return handler
15
15
  }
16
+
17
+ /**
18
+ * Defines a "scheduled event" function handler.
19
+ * Returns the handler function as-is, only providing the types and doing basic validation.
20
+ *
21
+ * @beta
22
+ * @param handler - The event handler function to use.
23
+ * @returns The handler function, unmodified.
24
+ */
25
+ export function scheduleEventHandler(
26
+ handler: ScheduleEventHandler,
27
+ ): ScheduleEventHandler {
28
+ if (typeof handler !== 'function') throw new TypeError('`handler` must be a function')
29
+ return handler
30
+ }
package/src/types.ts CHANGED
@@ -4,6 +4,14 @@
4
4
  * @beta
5
5
  */
6
6
  export interface FunctionContext {
7
+ /** The resource type of the event source; resource type that invoked the function. */
8
+ eventResourceType: string
9
+ /** The resource ID of the event source; resource ID that invoked the function. */
10
+ eventResourceId: string
11
+ /** The resource type of the function container; resource type that houses the function. */
12
+ functionResourceType: string
13
+ /** The resource ID of the function container; resource ID that houses the function. */
14
+ functionResourceId: string
7
15
  /**
8
16
  * `local` is set to `true` when testing your function locally.
9
17
  * i.e. `sanity function test func-name`
@@ -33,6 +41,42 @@ export interface FunctionContext {
33
41
  }
34
42
  }
35
43
 
44
+ /**
45
+ * The context object passed to the schedule function handler.
46
+ *
47
+ * @beta
48
+ */
49
+ export interface ScheduleFunctionContext {
50
+ /**
51
+ * `local` is set to `true` when testing your function locally.
52
+ * i.e. `sanity function test func-name`
53
+ * Otherwise, the property is not set.
54
+ */
55
+ local?: boolean
56
+ /**
57
+ * Options that can be passed to a `@sanity/client` constructor to configure it
58
+ * against the project and dataset which triggered the event. Note that you should
59
+ * always specify an explicit `apiVersion` in YYYY-MM-DD format (e.g. `2025-05-01`).
60
+ *
61
+ * @example
62
+ * Constructing a client with the options from the context:
63
+ * ```ts
64
+ * const client = createClient({
65
+ * apiVersion: '2025-05-01',
66
+ * ...context.clientOptions,
67
+ * })
68
+ * ```
69
+ * @beta
70
+ */
71
+ clientOptions?: {
72
+ apiHost?: string
73
+ dataset?: string
74
+ projectId?: string
75
+ token?: string
76
+ }
77
+ }
78
+
79
+
36
80
  /**
37
81
  * The event object received by the function handler in the case of a document event,
38
82
  * such as a publish, unpublish, delete or mutation event and similar.
@@ -59,3 +103,12 @@ export type DocumentEventHandler<IData = any> = (envelope: {
59
103
  context: FunctionContext
60
104
  event: DocumentEvent<IData>
61
105
  }) => void | Promise<void>
106
+
107
+ /**
108
+ * A function handler for a schedule event.
109
+ *
110
+ * @beta
111
+ */
112
+ export type ScheduleEventHandler = (envelope: {
113
+ context: ScheduleFunctionContext
114
+ }) => void | Promise<void>