Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions components/sanity/actions/create-document/create-document.mjs
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;
},
};
42 changes: 42 additions & 0 deletions components/sanity/actions/get-document/get-document.mjs
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;
},
};
41 changes: 41 additions & 0 deletions components/sanity/actions/query-dataset/query-dataset.mjs
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;
},
};
7 changes: 7 additions & 0 deletions components/sanity/common/constants.mjs
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,
};
31 changes: 31 additions & 0 deletions components/sanity/common/utils.mjs
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;
};

export {
parseObject,
};
7 changes: 5 additions & 2 deletions components/sanity/package.json
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": [
Expand All @@ -11,5 +11,8 @@
"author": "Pipedream <[email protected]> (https://pipedream.com/)",
"publishConfig": {
"access": "public"
},
"dependencies": {
"@pipedream/platform": "^3.1.1"
}
}
}
92 changes: 87 additions & 5 deletions components/sanity/sanity.app.mjs
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,
});
},
},
};
};
74 changes: 74 additions & 0 deletions components/sanity/sources/common/base.mjs
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);
},
};
Loading
Loading