@sanity/functions 1.1.0 → 1.2.1
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/definers.d.ts +10 -1
- package/dist/definers.js +13 -0
- package/dist/definers.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/types.d.ts +42 -0
- package/package.json +11 -10
- package/src/definers.ts +15 -4
- package/src/index.ts +1 -1
- package/src/types.ts +43 -4
package/dist/definers.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { DocumentEventHandler } from './types.js';
|
|
1
|
+
import type { DocumentEventHandler, ScheduledEventHandler } 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 scheduledEventHandler(handler: ScheduledEventHandler): ScheduledEventHandler;
|
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 scheduledEventHandler(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
|
package/dist/definers.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"definers.js","sourceRoot":"","sources":["../src/definers.ts"],"names":[],"mappings":"AAEA;;;;;;GAMG;AACH,MAAM,UAAU,oBAAoB,
|
|
1
|
+
{"version":3,"file":"definers.js","sourceRoot":"","sources":["../src/definers.ts"],"names":[],"mappings":"AAEA;;;;;;GAMG;AACH,MAAM,UAAU,oBAAoB,CAAc,OAAoC;IACpF,IAAI,OAAO,OAAO,KAAK,UAAU;QAAE,MAAM,IAAI,SAAS,CAAC,8BAA8B,CAAC,CAAA;IACtF,OAAO,OAAO,CAAA;AAChB,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,qBAAqB,CAAC,OAA8B;IAClE,IAAI,OAAO,OAAO,KAAK,UAAU;QAAE,MAAM,IAAI,SAAS,CAAC,8BAA8B,CAAC,CAAA;IACtF,OAAO,OAAO,CAAA;AAChB,CAAC"}
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,eAAe,CAAA;AAC7B,cAAc,YAAY,CAAA"}
|
package/dist/types.d.ts
CHANGED
|
@@ -40,6 +40,40 @@ export interface FunctionContext {
|
|
|
40
40
|
token: string;
|
|
41
41
|
};
|
|
42
42
|
}
|
|
43
|
+
/**
|
|
44
|
+
* The context object passed to the schedule function handler.
|
|
45
|
+
*
|
|
46
|
+
* @beta
|
|
47
|
+
*/
|
|
48
|
+
export interface ScheduledFunctionContext {
|
|
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
|
+
}
|
|
43
77
|
/**
|
|
44
78
|
* The event object received by the function handler in the case of a document event,
|
|
45
79
|
* such as a publish, unpublish, delete or mutation event and similar.
|
|
@@ -65,3 +99,11 @@ export type DocumentEventHandler<IData = any> = (envelope: {
|
|
|
65
99
|
context: FunctionContext;
|
|
66
100
|
event: DocumentEvent<IData>;
|
|
67
101
|
}) => void | Promise<void>;
|
|
102
|
+
/**
|
|
103
|
+
* A function handler for a schedule event.
|
|
104
|
+
*
|
|
105
|
+
* @beta
|
|
106
|
+
*/
|
|
107
|
+
export type ScheduledEventHandler = (envelope: {
|
|
108
|
+
context: ScheduledFunctionContext;
|
|
109
|
+
}) => void | Promise<void>;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sanity/functions",
|
|
3
|
-
"version": "1.1
|
|
3
|
+
"version": "1.2.1",
|
|
4
4
|
"description": "Helper methods and type definitions for Sanity Functions",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"sanity",
|
|
@@ -36,23 +36,24 @@
|
|
|
36
36
|
],
|
|
37
37
|
"scripts": {
|
|
38
38
|
"build": "rimraf dist && tsc",
|
|
39
|
+
"lint": "biome check .",
|
|
40
|
+
"lint:write": "biome check --write .",
|
|
39
41
|
"coverage": "vitest --coverage",
|
|
40
|
-
"
|
|
42
|
+
"typecheck": "tsc --noEmit --project tsconfig.test.json",
|
|
41
43
|
"prepare": "npm run build",
|
|
42
44
|
"test": "vitest run --typecheck --reporter=verbose",
|
|
43
45
|
"test:watch": "vitest --typecheck --reporter=verbose"
|
|
44
46
|
},
|
|
45
47
|
"devDependencies": {
|
|
46
|
-
"@
|
|
47
|
-
"@
|
|
48
|
-
"
|
|
49
|
-
"
|
|
50
|
-
"
|
|
51
|
-
"
|
|
52
|
-
"vitest": "3.1.2"
|
|
48
|
+
"@biomejs/biome": "2.3.12",
|
|
49
|
+
"@types/node": "^20",
|
|
50
|
+
"@vitest/coverage-v8": "^4.0.18",
|
|
51
|
+
"rimraf": "^6.1.2",
|
|
52
|
+
"typescript": "^5.9.3",
|
|
53
|
+
"vitest": "4.0.18"
|
|
53
54
|
},
|
|
54
55
|
"engines": {
|
|
55
|
-
"node": ">=20"
|
|
56
|
+
"node": ">=20.19"
|
|
56
57
|
},
|
|
57
58
|
"publishConfig": {
|
|
58
59
|
"access": "public"
|
package/src/definers.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {DocumentEventHandler} from './types.js'
|
|
1
|
+
import type {DocumentEventHandler, ScheduledEventHandler} from './types.js'
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Defines a "document event" function handler.
|
|
@@ -7,9 +7,20 @@ import type {DocumentEventHandler} from './types.js'
|
|
|
7
7
|
* @param handler - The event handler function to use.
|
|
8
8
|
* @returns The handler function, unmodified.
|
|
9
9
|
*/
|
|
10
|
-
export function documentEventHandler<IData = any>(
|
|
11
|
-
handler
|
|
12
|
-
|
|
10
|
+
export function documentEventHandler<IData = any>(handler: DocumentEventHandler<IData>): DocumentEventHandler<IData> {
|
|
11
|
+
if (typeof handler !== 'function') throw new TypeError('`handler` must be a function')
|
|
12
|
+
return handler
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Defines a "scheduled event" function handler.
|
|
17
|
+
* Returns the handler function as-is, only providing the types and doing basic validation.
|
|
18
|
+
*
|
|
19
|
+
* @beta
|
|
20
|
+
* @param handler - The event handler function to use.
|
|
21
|
+
* @returns The handler function, unmodified.
|
|
22
|
+
*/
|
|
23
|
+
export function scheduledEventHandler(handler: ScheduledEventHandler): ScheduledEventHandler {
|
|
13
24
|
if (typeof handler !== 'function') throw new TypeError('`handler` must be a function')
|
|
14
25
|
return handler
|
|
15
26
|
}
|
package/src/index.ts
CHANGED
package/src/types.ts
CHANGED
|
@@ -41,6 +41,41 @@ export interface FunctionContext {
|
|
|
41
41
|
}
|
|
42
42
|
}
|
|
43
43
|
|
|
44
|
+
/**
|
|
45
|
+
* The context object passed to the schedule function handler.
|
|
46
|
+
*
|
|
47
|
+
* @beta
|
|
48
|
+
*/
|
|
49
|
+
export interface ScheduledFunctionContext {
|
|
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
|
+
|
|
44
79
|
/**
|
|
45
80
|
* The event object received by the function handler in the case of a document event,
|
|
46
81
|
* such as a publish, unpublish, delete or mutation event and similar.
|
|
@@ -63,7 +98,11 @@ export interface DocumentEvent<IData = any> {
|
|
|
63
98
|
*
|
|
64
99
|
* @beta
|
|
65
100
|
*/
|
|
66
|
-
export type DocumentEventHandler<IData = any> = (envelope: {
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
101
|
+
export type DocumentEventHandler<IData = any> = (envelope: {context: FunctionContext; event: DocumentEvent<IData>}) => void | Promise<void>
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* A function handler for a schedule event.
|
|
105
|
+
*
|
|
106
|
+
* @beta
|
|
107
|
+
*/
|
|
108
|
+
export type ScheduledEventHandler = (envelope: {context: ScheduledFunctionContext}) => void | Promise<void>
|