-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Expand file tree
/
Copy pathcreate-document.mjs
More file actions
62 lines (60 loc) · 1.71 KB
/
create-document.mjs
File metadata and controls
62 lines (60 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import sanity from "../../sanity.app.mjs";
import { parseObject } from "../../common/utils.mjs";
export default {
key: "sanity-create-document",
name: "Create Document",
description: "Create a document in a dataset in Sanity. [See the documentation](https://www.sanity.io/docs/http-reference/actions)",
version: "0.0.1",
type: "action",
annotations: {
destructiveHint: false,
openWorldHint: true,
readOnlyHint: false,
},
props: {
sanity,
dataset: {
propDefinition: [
sanity,
"dataset",
],
},
documentId: {
type: "string",
label: "Document ID",
description: "A unique identifier for the document to create",
},
type: {
type: "string",
label: "Type",
description: "The type of document to create. Example: `post`",
},
additionalFields: {
type: "object",
label: "Additional Fields",
description: "Additional fields to add to the document. Example: `{\"title\":\"My First Post\",\"slug\":{\"current\":\"my-first-post\"},\"body\":[{\"_type\":\"block\",\"children\":[{\"_type\":\"span\",\"text\":\"Hello world!\"}]}]}`",
optional: true,
},
},
async run({ $ }) {
const response = await this.sanity.createDocument({
$,
dataset: this.dataset,
data: {
actions: [
{
actionType: "sanity.action.document.create",
publishedId: this.documentId,
document: {
_id: `drafts.${this.documentId}`,
_type: this.type,
...parseObject(this.additionalFields),
},
},
],
},
});
$.export("$summary", "Successfully created document");
return response;
},
};