-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Sanity.io - new components #19137
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Sanity.io - new components #19137
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
components/sanity/actions/create-document/create-document.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| import sanity from "../../sanity.app.mjs"; | ||
| import { ConfigurationError } from "@pipedream/platform"; | ||
| 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", | ||
| ], | ||
| }, | ||
| publishId: { | ||
| type: "string", | ||
| label: "Publish ID", | ||
| description: "The publish ID of the document to create", | ||
| }, | ||
| document: { | ||
| type: "object", | ||
| label: "Document", | ||
| description: "The document to create. Must include `_type` and `_id` fields. Example: `{\"_type\":\"post\",\"_id\":\"drafts.post-123\",\"title\":\"My First Post\",\"slug\":{\"current\":\"my-first-post\"},\"body\":[{\"_type\":\"block\",\"children\":[{\"_type\":\"span\",\"text\":\"Hello world!\"}]}]}`", | ||
| }, | ||
| }, | ||
| async run({ $ }) { | ||
| const document = parseObject(this.document); | ||
| if (!document._type || !document._id) { | ||
| throw new ConfigurationError("Document must include `_type` and `_id` fields"); | ||
| } | ||
|
|
||
| const response = await this.sanity.createDocument({ | ||
| $, | ||
| dataset: this.dataset, | ||
| data: { | ||
| actions: [ | ||
| { | ||
| actionType: "sanity.action.document.create", | ||
| publishedId: this.publishId, | ||
| document, | ||
| }, | ||
| ], | ||
| }, | ||
| }); | ||
|
|
||
| $.export("$summary", "Successfully created document"); | ||
| return response; | ||
| }, | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| import sanity from "../../sanity.app.mjs"; | ||
|
|
||
| export default { | ||
| key: "sanity-get-document", | ||
| name: "Get Document", | ||
| description: "Get a document from a dataset in Sanity. [See the documentation](https://www.sanity.io/docs/http-reference/doc#getDocuments)", | ||
| version: "0.0.1", | ||
| type: "action", | ||
| annotations: { | ||
| destructiveHint: false, | ||
| openWorldHint: true, | ||
| readOnlyHint: true, | ||
| }, | ||
| props: { | ||
| sanity, | ||
| dataset: { | ||
| propDefinition: [ | ||
| sanity, | ||
| "dataset", | ||
| ], | ||
| }, | ||
| documentId: { | ||
| propDefinition: [ | ||
| sanity, | ||
| "documentId", | ||
| ({ dataset }) => ({ | ||
| dataset, | ||
| }), | ||
| ], | ||
| }, | ||
| }, | ||
| async run({ $ }) { | ||
| const response = await this.sanity.getDocument({ | ||
| $, | ||
| dataset: this.dataset, | ||
| documentId: this.documentId, | ||
| }); | ||
|
|
||
| $.export("$summary", "Successfully retrieved document"); | ||
| return response; | ||
| }, | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| import sanity from "../../sanity.app.mjs"; | ||
|
|
||
| export default { | ||
| key: "sanity-query-dataset", | ||
| name: "Query Dataset", | ||
| description: "Query a dataset in Sanity. [See the documentation](https://www.sanity.io/docs/http-reference/query)", | ||
| version: "0.0.1", | ||
| type: "action", | ||
| annotations: { | ||
| destructiveHint: false, | ||
| openWorldHint: true, | ||
| readOnlyHint: true, | ||
| }, | ||
| props: { | ||
| sanity, | ||
| dataset: { | ||
| propDefinition: [ | ||
| sanity, | ||
| "dataset", | ||
| ], | ||
| }, | ||
| query: { | ||
| type: "string", | ||
| label: "Query", | ||
| description: "The GROQ query to run. Example: `*[_type == 'movie']`. Note: The default query `*` returns all documents in the dataset.", | ||
| default: "*", | ||
| }, | ||
| }, | ||
| async run({ $ }) { | ||
| const response = await this.sanity.queryDataset({ | ||
| $, | ||
| dataset: this.dataset, | ||
| params: { | ||
| query: this.query, | ||
| }, | ||
| }); | ||
|
|
||
| $.export("$summary", "Successfully queried dataset"); | ||
| return response; | ||
| }, | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| const API_VERSION = "v2025-02-19"; | ||
| const DEFAULT_LIMIT = 50; | ||
|
|
||
| export { | ||
| API_VERSION, | ||
| DEFAULT_LIMIT, | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| const parseObject = (obj) => { | ||
| if (!obj) { | ||
| return undefined; | ||
| } | ||
| if (typeof obj === "string") { | ||
| try { | ||
| return JSON.parse(obj); | ||
| } catch (e) { | ||
| return obj; | ||
| } | ||
| } | ||
| if (Array.isArray(obj)) { | ||
| return obj.map(parseObject); | ||
| } | ||
| if (typeof obj === "object") { | ||
| return Object.fromEntries( | ||
| Object.entries(obj).map(([ | ||
| key, | ||
| value, | ||
| ]) => [ | ||
| key, | ||
| parseObject(value), | ||
| ]), | ||
| ); | ||
| } | ||
| return obj; | ||
| }; | ||
michelle0927 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| export { | ||
| parseObject, | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| { | ||
| "name": "@pipedream/sanity", | ||
| "version": "0.0.1", | ||
| "version": "0.1.0", | ||
| "description": "Pipedream Sanity Components", | ||
| "main": "sanity.app.mjs", | ||
| "keywords": [ | ||
|
|
@@ -11,5 +11,8 @@ | |
| "author": "Pipedream <[email protected]> (https://pipedream.com/)", | ||
| "publishConfig": { | ||
| "access": "public" | ||
| }, | ||
| "dependencies": { | ||
| "@pipedream/platform": "^3.1.1" | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,93 @@ | ||
| import { axios } from "@pipedream/platform"; | ||
| import { | ||
| API_VERSION, DEFAULT_LIMIT, | ||
| } from "./common/constants.mjs"; | ||
|
|
||
| export default { | ||
| type: "app", | ||
| app: "sanity", | ||
| propDefinitions: {}, | ||
| propDefinitions: { | ||
| dataset: { | ||
| type: "string", | ||
| label: "Dataset", | ||
| description: "The dataset to use", | ||
| }, | ||
| documentId: { | ||
| type: "string", | ||
| label: "Document ID", | ||
| description: "The document ID to use", | ||
| async options({ | ||
| dataset, page, | ||
| }) { | ||
| const response = await this.queryDataset({ | ||
| dataset, | ||
| params: { | ||
| query: `* | order(_id) [${DEFAULT_LIMIT * page}...${DEFAULT_LIMIT * (page + 1)}]`, | ||
| }, | ||
| }); | ||
| return response.result?.map(({ _id }) => _id) || []; | ||
| }, | ||
| }, | ||
| }, | ||
| methods: { | ||
| // this.$auth contains connected account data | ||
| authKeys() { | ||
| console.log(Object.keys(this.$auth)); | ||
| _projectId() { | ||
| return this.$auth.project_id; | ||
| }, | ||
| _baseUrl(version = API_VERSION) { | ||
| return `https://${this._projectId()}.api.sanity.io/${version}`; | ||
| }, | ||
| _makeRequest({ | ||
| $ = this, path, version, ...opts | ||
| }) { | ||
| return axios($, { | ||
| url: `${this._baseUrl(version)}${path}`, | ||
| headers: { | ||
| "Authorization": `Bearer ${this.$auth.api_token}`, | ||
| "Content-Type": "application/json", | ||
| }, | ||
| ...opts, | ||
| }); | ||
| }, | ||
| createWebhook(opts = {}) { | ||
| return this._makeRequest({ | ||
| path: `/hooks/projects/${this._projectId()}`, | ||
| method: "POST", | ||
| ...opts, | ||
| }); | ||
| }, | ||
| deleteWebhook({ | ||
| hookId, ...opts | ||
| }) { | ||
| return this._makeRequest({ | ||
| path: `/hooks/projects/${this._projectId()}/${hookId}`, | ||
| method: "DELETE", | ||
| ...opts, | ||
| }); | ||
| }, | ||
| getDocument({ | ||
| dataset, documentId, ...opts | ||
| }) { | ||
| return this._makeRequest({ | ||
| path: `/data/doc/${dataset}/${documentId}`, | ||
| ...opts, | ||
| }); | ||
| }, | ||
| queryDataset({ | ||
| dataset, ...opts | ||
| }) { | ||
| return this._makeRequest({ | ||
| path: `/data/query/${dataset}`, | ||
| ...opts, | ||
| }); | ||
| }, | ||
| createDocument({ | ||
| dataset, ...opts | ||
| }) { | ||
| return this._makeRequest({ | ||
| path: `/data/actions/${dataset}`, | ||
| method: "POST", | ||
| ...opts, | ||
| }); | ||
| }, | ||
| }, | ||
| }; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| import sanity from "../../sanity.app.mjs"; | ||
| import { ConfigurationError } from "@pipedream/platform"; | ||
| import { API_VERSION } from "../../common/constants.mjs"; | ||
|
|
||
| export default { | ||
| props: { | ||
| sanity, | ||
| db: "$.service.db", | ||
| http: { | ||
| type: "$.interface.http", | ||
| customResponse: true, | ||
| }, | ||
| name: { | ||
| type: "string", | ||
| label: "Webhook Name", | ||
| description: "The name of the webhook", | ||
| }, | ||
| dataset: { | ||
| propDefinition: [ | ||
| sanity, | ||
| "dataset", | ||
| ], | ||
| }, | ||
| }, | ||
| methods: { | ||
| _getHookId() { | ||
| return this.db.get("hookId"); | ||
| }, | ||
| _setHookId(hookId) { | ||
| this.db.set("hookId", hookId); | ||
| }, | ||
| getWebhookArgs() { | ||
| throw new ConfigurationError("getWebhookArgs is not implemented"); | ||
| }, | ||
| generateMeta() { | ||
| throw new ConfigurationError("generateMeta is not implemented"); | ||
| }, | ||
| }, | ||
| hooks: { | ||
| async activate() { | ||
| const webhookArgs = this.getWebhookArgs(); | ||
| const { id } = await this.sanity.createWebhook({ | ||
| data: { | ||
| name: this.name, | ||
| url: this.http.endpoint, | ||
| dataset: this.dataset, | ||
| apiVersion: API_VERSION, | ||
| ...webhookArgs, | ||
| }, | ||
| }); | ||
| this._setHookId(id); | ||
| }, | ||
| async deactivate() { | ||
| const hookId = this._getHookId(); | ||
| if (hookId) { | ||
| await this.sanity.deleteWebhook({ | ||
| hookId, | ||
| }); | ||
| } | ||
| }, | ||
| }, | ||
| async run({ body }) { | ||
| if (!body) { | ||
| return; | ||
| } | ||
|
|
||
| this.http.respond({ | ||
| status: 200, | ||
| }); | ||
|
|
||
| const meta = this.generateMeta(body); | ||
| this.$emit(body, meta); | ||
| }, | ||
michelle0927 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }; | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.