Skip to content

Commit 8fa74c9

Browse files
committed
contentstack init
1 parent 1a006e0 commit 8fa74c9

File tree

8 files changed

+783
-4
lines changed

8 files changed

+783
-4
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import contentstack from "../../contentstack.app.mjs";
2+
import { axios } from "@pipedream/platform";
3+
4+
export default {
5+
key: "contentstack-create-entry",
6+
name: "Create Entry",
7+
description: "Creates a new entry in Contentstack. [See the documentation]().",
8+
version: "0.0.{{ts}}",
9+
type: "action",
10+
props: {
11+
contentstack,
12+
stackId: {
13+
propDefinition: [
14+
contentstack,
15+
"stackId",
16+
],
17+
},
18+
contentTypeUid: {
19+
propDefinition: [
20+
contentstack,
21+
"contentTypeUid",
22+
],
23+
},
24+
entryTitle: {
25+
propDefinition: [
26+
contentstack,
27+
"entryTitle",
28+
],
29+
},
30+
content: {
31+
propDefinition: [
32+
contentstack,
33+
"content",
34+
],
35+
},
36+
metadata: {
37+
propDefinition: [
38+
contentstack,
39+
"metadata",
40+
],
41+
optional: true,
42+
},
43+
},
44+
async run({ $ }) {
45+
const response = await this.contentstack.createEntry();
46+
$.export("$summary", `Created entry "${response.title}" with UID ${response.uid}`);
47+
return response;
48+
},
49+
};
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import contentstack from "../../contentstack.app.mjs";
2+
import { axios } from "@pipedream/platform";
3+
4+
export default {
5+
key: "contentstack-publish-entry",
6+
name: "Publish Entry",
7+
description: "Publishes a specific entry using its UID. [See the documentation]()",
8+
version: "0.0.{{ts}}",
9+
type: "action",
10+
props: {
11+
contentstack,
12+
stackId: {
13+
propDefinition: [
14+
contentstack,
15+
"stackId",
16+
],
17+
},
18+
entryUid: {
19+
propDefinition: [
20+
contentstack,
21+
"entryUid",
22+
],
23+
},
24+
},
25+
async run({ $ }) {
26+
const response = await this.contentstack.publishEntry();
27+
$.export("$summary", `Successfully published entry with UID ${this.entryUid}`);
28+
return response;
29+
},
30+
};
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import contentstack from "../../contentstack.app.mjs";
2+
import { axios } from "@pipedream/platform";
3+
4+
export default {
5+
key: "contentstack-update-entry",
6+
name: "Update Entry",
7+
description: "Updates an existing Contentstack entry. [See the documentation](https://www.contentstack.com/docs/developers/apis/content-management-api#update-an-entry).",
8+
version: "0.0.{{ts}}",
9+
type: "action",
10+
props: {
11+
contentstack,
12+
stackId: {
13+
propDefinition: [
14+
"contentstack",
15+
"stackId",
16+
],
17+
},
18+
contentTypeUid: {
19+
propDefinition: [
20+
"contentstack",
21+
"contentTypeUid",
22+
],
23+
},
24+
entryUid: {
25+
propDefinition: [
26+
"contentstack",
27+
"entryUid",
28+
],
29+
},
30+
fieldsToUpdate: {
31+
propDefinition: [
32+
"contentstack",
33+
"fieldsToUpdate",
34+
],
35+
optional: true,
36+
},
37+
},
38+
async run({ $ }) {
39+
const response = await this.contentstack.updateEntry({
40+
data: this.fieldsToUpdate
41+
? this.fieldsToUpdate.reduce((acc, field) => {
42+
const parsedField = JSON.parse(field);
43+
return {
44+
...acc,
45+
...parsedField,
46+
};
47+
}, {})
48+
: {},
49+
});
50+
$.export("$summary", `Entry ${this.entryUid} updated successfully`);
51+
return response;
52+
},
53+
};
Lines changed: 164 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,172 @@
1+
import { axios } from "@pipedream/platform";
2+
13
export default {
24
type: "app",
35
app: "contentstack",
4-
propDefinitions: {},
6+
version: "0.0.{{ts}}",
7+
propDefinitions: {
8+
stackId: {
9+
type: "string",
10+
label: "Stack ID",
11+
description: "The ID of the Contentstack stack",
12+
},
13+
assetId: {
14+
type: "string",
15+
label: "Asset ID",
16+
description: "The ID of the asset",
17+
},
18+
contentTypeUid: {
19+
type: "string",
20+
label: "Content Type UID",
21+
description: "The UID of the content type for creating and listing entries",
22+
},
23+
entryId: {
24+
type: "string",
25+
label: "Entry ID",
26+
description: "The ID of the entry relevant to the published content",
27+
},
28+
entryUid: {
29+
type: "string",
30+
label: "Entry UID",
31+
description: "The UID of the entry to publish or update",
32+
},
33+
entryTitle: {
34+
type: "string",
35+
label: "Entry Title",
36+
description: "The title of the new entry",
37+
},
38+
content: {
39+
type: "string",
40+
label: "Content",
41+
description: "The content of the new entry",
42+
},
43+
metadata: {
44+
type: "string[]",
45+
label: "Metadata",
46+
description: "Array of metadata objects in JSON format",
47+
optional: true,
48+
},
49+
fieldsToUpdate: {
50+
type: "string[]",
51+
label: "Fields to Update",
52+
description: "Array of fields to update with new values in JSON format",
53+
optional: true,
54+
},
55+
},
556
methods: {
6-
// this.$auth contains connected account data
757
authKeys() {
858
console.log(Object.keys(this.$auth));
959
},
60+
_baseUrl() {
61+
return "https://api.contentstack.com/v3";
62+
},
63+
async _makeRequest(opts = {}) {
64+
const {
65+
$, method = "GET", path = "/", headers, ...otherOpts
66+
} = opts;
67+
return axios($, {
68+
...otherOpts,
69+
method,
70+
url: this._baseUrl() + path,
71+
headers: {
72+
...headers,
73+
"api_key": this.$auth.api_key,
74+
"access_token": this.$auth.access_token,
75+
"Content-Type": "application/json",
76+
},
77+
});
78+
},
79+
async listAssets(opts = {}) {
80+
return this._makeRequest({
81+
path: `/stacks/${this.stackId}/assets`,
82+
...opts,
83+
});
84+
},
85+
async getAsset(opts = {}) {
86+
return this._makeRequest({
87+
path: `/stacks/${this.stackId}/assets/${this.assetId}`,
88+
...opts,
89+
});
90+
},
91+
async listEntries(opts = {}) {
92+
return this._makeRequest({
93+
path: `/stacks/${this.stackId}/content_types/${this.contentTypeUid}/entries`,
94+
...opts,
95+
});
96+
},
97+
async getEntry(opts = {}) {
98+
return this._makeRequest({
99+
path: `/stacks/${this.stackId}/entries/${this.entryId}`,
100+
...opts,
101+
});
102+
},
103+
async createEntry(opts = {}) {
104+
const data = {
105+
title: this.entryTitle,
106+
content: this.content,
107+
...(this.metadata
108+
? {
109+
metadata: this.metadata.map(JSON.parse),
110+
}
111+
: {}),
112+
...opts.data,
113+
};
114+
return this._makeRequest({
115+
method: "POST",
116+
path: `/stacks/${this.stackId}/content_types/${this.contentTypeUid}/entries`,
117+
data,
118+
...opts,
119+
});
120+
},
121+
async updateEntry(opts = {}) {
122+
const data = {
123+
...(this.fieldsToUpdate
124+
? this.fieldsToUpdate.reduce((acc, field) => {
125+
const parsedField = JSON.parse(field);
126+
return {
127+
...acc,
128+
...parsedField,
129+
};
130+
}, {})
131+
: {}),
132+
...opts.data,
133+
};
134+
return this._makeRequest({
135+
method: "PUT",
136+
path: `/stacks/${this.stackId}/entries/${this.entryUid}`,
137+
data,
138+
...opts,
139+
});
140+
},
141+
async publishEntry(opts = {}) {
142+
return this._makeRequest({
143+
method: "POST",
144+
path: `/stacks/${this.stackId}/entries/${this.entryUid}/publish`,
145+
...opts,
146+
});
147+
},
148+
async paginate(fn, ...opts) {
149+
let allResults = [];
150+
let currentPage = 1;
151+
let hasMore = true;
152+
while (hasMore) {
153+
const response = await fn({
154+
page: currentPage,
155+
...opts,
156+
});
157+
if (response.items && response.items.length > 0) {
158+
allResults = [
159+
...allResults,
160+
...response.items,
161+
];
162+
}
163+
if (response.pagination && response.pagination.next_page) {
164+
currentPage += 1;
165+
} else {
166+
hasMore = false;
167+
}
168+
}
169+
return allResults;
170+
},
10171
},
11-
};
172+
};

components/contentstack/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@
1212
"publishConfig": {
1313
"access": "public"
1414
}
15-
}
15+
}

0 commit comments

Comments
 (0)