@sanity/functions 1.0.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/README.md ADDED
@@ -0,0 +1,107 @@
1
+ # @sanity/functions
2
+
3
+ Helper methods and type definitions for Sanity Functions.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @sanity/functions
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ### Basic
14
+
15
+ ```ts
16
+ import {documentEventHandler} from '@sanity/functions'
17
+ import {createClient} from '@sanity/client'
18
+
19
+ export const handler = documentEventHandler(async ({context, event}) => {
20
+ // Create a Sanity client using the context options
21
+ const client = createClient({
22
+ apiVersion: '2025-05-01',
23
+ ...context.clientOptions,
24
+ })
25
+
26
+ // Access the event data
27
+ const data = event.data
28
+
29
+ // Your function implementation
30
+ console.log('Document updated:', data)
31
+ })
32
+ ```
33
+
34
+ ### Typescript: Passing type for data
35
+
36
+ By default, the `event.data` property is untyped (`any`). If you know what the shape of the data that will be delivered is, you can specify it as a generic to the function:
37
+
38
+ ```ts
39
+ interface NotificationData {
40
+ documentId: string
41
+ text: string
42
+ }
43
+
44
+ export const handler = documentEventHandler<NotificationData>(async ({event}) => {
45
+ console.log(event.data.text) // Typed as `string`
46
+ console.log(event.data.notSet) // Will yield type error
47
+ })
48
+ ```
49
+
50
+ ### Type only (TypeScript)
51
+
52
+ ```ts
53
+ import {type DocumentEventHandler} from '@sanity/functions'
54
+ import {createClient} from '@sanity/client'
55
+
56
+ export const handler: DocumentEventHandler = async ({context, event}) => {
57
+ // …
58
+ }
59
+
60
+ // …you can also define the data type:
61
+ export const handler: DocumentEventHandler<{text: string}> = async ({event}) => {
62
+ console.log(event.data.text)
63
+ }
64
+ ```
65
+
66
+ ### Type only (JavaScript)
67
+
68
+ ```js
69
+ /** @type {import('@sanity/functions').DocumentEventHandler} */
70
+ export const handler = async ({context, event}) => {
71
+ console.log(event.data.text)
72
+ }
73
+
74
+ // …you can also define the data type:
75
+ /** @type {import('@sanity/functions').DocumentEventHandler<{text: string}>} */
76
+ export const handler = async ({event}) => {
77
+ console.log(event.data.text)
78
+ }
79
+ ```
80
+
81
+ ## Development
82
+
83
+ To build this project:
84
+
85
+ ```bash
86
+ npm run build
87
+ ```
88
+
89
+ To run tests:
90
+
91
+ ```bash
92
+ npm test
93
+ ```
94
+
95
+ To run type checking:
96
+
97
+ ```bash
98
+ npm run lint
99
+ ```
100
+
101
+ ## Release automation
102
+
103
+ This repository uses conventional commits and release-please. Any relevant changes to `main` will create a pull request for a new release. Approve it and merge it to trigger the actual release.
104
+
105
+ ## License
106
+
107
+ MIT © [Sanity.io](https://sanity.io)
@@ -0,0 +1,9 @@
1
+ import type { DocumentEventHandler } from './types.js';
2
+ /**
3
+ * Defines a "document event" function handler.
4
+ * Returns the handler function as-is, only providing the types and doing basic validation.
5
+ *
6
+ * @param handler - The event handler function to use.
7
+ * @returns The handler function, unmodified.
8
+ */
9
+ export declare function documentEventHandler<IData = any>(handler: DocumentEventHandler<IData>): DocumentEventHandler<IData>;
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Defines a "document event" function handler.
3
+ * Returns the handler function as-is, only providing the types and doing basic validation.
4
+ *
5
+ * @param handler - The event handler function to use.
6
+ * @returns The handler function, unmodified.
7
+ */
8
+ export function documentEventHandler(handler) {
9
+ if (typeof handler !== 'function')
10
+ throw new TypeError('`handler` must be a function');
11
+ return handler;
12
+ }
13
+ //# sourceMappingURL=definers.js.map
@@ -0,0 +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"}
@@ -0,0 +1,2 @@
1
+ export * from './types.js';
2
+ export * from './definers.js';
package/dist/index.js ADDED
@@ -0,0 +1,3 @@
1
+ export * from './types.js';
2
+ export * from './definers.js';
3
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAA;AAC1B,cAAc,eAAe,CAAA"}
@@ -0,0 +1,53 @@
1
+ /**
2
+ * The context object passed to the function handler.
3
+ *
4
+ * @beta
5
+ */
6
+ export interface FunctionContext {
7
+ /**
8
+ * Options that can be passed to a `@sanity/client` constructor to configure it
9
+ * against the project and dataset which triggered the event. Note that you should
10
+ * always specify an explicit `apiVersion` in YYYY-MM-DD format (e.g. `2025-05-01`).
11
+ *
12
+ * @example
13
+ * Constructing a client with the options from the context:
14
+ * ```ts
15
+ * const client = createClient({
16
+ * apiVersion: '2025-05-01',
17
+ * ...context.clientOptions,
18
+ * })
19
+ * ```
20
+ * @beta
21
+ */
22
+ clientOptions: {
23
+ apiHost?: string;
24
+ dataset: string;
25
+ projectId: string;
26
+ token: string;
27
+ };
28
+ }
29
+ /**
30
+ * The event object received by the function handler in the case of a document event,
31
+ * such as a publish, unpublish, delete or mutation event and similar.
32
+ *
33
+ * @beta
34
+ */
35
+ export interface DocumentEvent<IData = any> {
36
+ /**
37
+ * The data delivered to the function. This is the result of applying any configured
38
+ * GROQ-projection to the changed document. If no projection is configured, this is
39
+ * the document itself.
40
+ *
41
+ * @beta
42
+ */
43
+ data: IData;
44
+ }
45
+ /**
46
+ * A function handler for a document event.
47
+ *
48
+ * @beta
49
+ */
50
+ export type DocumentEventHandler<IData = any> = (envelope: {
51
+ context: FunctionContext;
52
+ event: DocumentEvent<IData>;
53
+ }) => void | Promise<void>;
package/dist/types.js ADDED
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
package/package.json ADDED
@@ -0,0 +1,60 @@
1
+ {
2
+ "name": "@sanity/functions",
3
+ "version": "1.0.2",
4
+ "description": "Helper methods and type definitions for Sanity Functions",
5
+ "keywords": [
6
+ "sanity",
7
+ "functions",
8
+ "serverless",
9
+ "types"
10
+ ],
11
+ "homepage": "https://github.com/sanity-io/functions-node#readme",
12
+ "bugs": {
13
+ "url": "https://github.com/sanity-io/functions-node/issues"
14
+ },
15
+ "repository": {
16
+ "type": "git",
17
+ "url": "git+ssh://git@github.com/sanity-io/functions-node.git"
18
+ },
19
+ "license": "MIT",
20
+ "author": "Sanity.io <hello@sanity.io>",
21
+ "sideEffects": false,
22
+ "type": "module",
23
+ "exports": {
24
+ ".": {
25
+ "source": "./src/index.ts",
26
+ "types": "./dist/index.d.ts",
27
+ "import": "./dist/index.js",
28
+ "default": "./dist/index.js"
29
+ },
30
+ "./package.json": "./package.json"
31
+ },
32
+ "main": "dist/index.js",
33
+ "files": [
34
+ "dist",
35
+ "src"
36
+ ],
37
+ "scripts": {
38
+ "build": "rimraf dist && tsc",
39
+ "coverage": "vitest --coverage",
40
+ "lint": "tsc --noEmit --project tsconfig.test.json",
41
+ "prepublishOnly": "npm run build",
42
+ "test": "vitest run --typecheck --reporter=verbose",
43
+ "test:watch": "vitest --typecheck --reporter=verbose"
44
+ },
45
+ "devDependencies": {
46
+ "@types/node": "^20.0.0",
47
+ "@vitest/coverage-v8": "^3.1.2",
48
+ "prettier": "^3.5.3",
49
+ "prettier-plugin-packagejson": "^2.5.10",
50
+ "rimraf": "^6.0.1",
51
+ "typescript": "^5.8.3",
52
+ "vitest": "3.1.2"
53
+ },
54
+ "engines": {
55
+ "node": ">=20"
56
+ },
57
+ "publishConfig": {
58
+ "access": "public"
59
+ }
60
+ }
@@ -0,0 +1,15 @@
1
+ import type {DocumentEventHandler} from './types.js'
2
+
3
+ /**
4
+ * Defines a "document event" function handler.
5
+ * Returns the handler function as-is, only providing the types and doing basic validation.
6
+ *
7
+ * @param handler - The event handler function to use.
8
+ * @returns The handler function, unmodified.
9
+ */
10
+ export function documentEventHandler<IData = any>(
11
+ handler: DocumentEventHandler<IData>,
12
+ ): DocumentEventHandler<IData> {
13
+ if (typeof handler !== 'function') throw new TypeError('`handler` must be a function')
14
+ return handler
15
+ }
package/src/index.ts ADDED
@@ -0,0 +1,2 @@
1
+ export * from './types.js'
2
+ export * from './definers.js'
package/src/types.ts ADDED
@@ -0,0 +1,55 @@
1
+ /**
2
+ * The context object passed to the function handler.
3
+ *
4
+ * @beta
5
+ */
6
+ export interface FunctionContext {
7
+ /**
8
+ * Options that can be passed to a `@sanity/client` constructor to configure it
9
+ * against the project and dataset which triggered the event. Note that you should
10
+ * always specify an explicit `apiVersion` in YYYY-MM-DD format (e.g. `2025-05-01`).
11
+ *
12
+ * @example
13
+ * Constructing a client with the options from the context:
14
+ * ```ts
15
+ * const client = createClient({
16
+ * apiVersion: '2025-05-01',
17
+ * ...context.clientOptions,
18
+ * })
19
+ * ```
20
+ * @beta
21
+ */
22
+ clientOptions: {
23
+ apiHost?: string
24
+ dataset: string
25
+ projectId: string
26
+ token: string
27
+ }
28
+ }
29
+
30
+ /**
31
+ * The event object received by the function handler in the case of a document event,
32
+ * such as a publish, unpublish, delete or mutation event and similar.
33
+ *
34
+ * @beta
35
+ */
36
+ export interface DocumentEvent<IData = any> {
37
+ /**
38
+ * The data delivered to the function. This is the result of applying any configured
39
+ * GROQ-projection to the changed document. If no projection is configured, this is
40
+ * the document itself.
41
+ *
42
+ * @beta
43
+ */
44
+ data: IData
45
+ }
46
+
47
+ /**
48
+ * A function handler for a document event.
49
+ *
50
+ * @beta
51
+ */
52
+ export type DocumentEventHandler<IData = any> = (envelope: {
53
+ context: FunctionContext
54
+ event: DocumentEvent<IData>
55
+ }) => void | Promise<void>