Skip to content

Commit 67192f5

Browse files
committed
new components
1 parent 8fa74c9 commit 67192f5

File tree

14 files changed

+1525
-646
lines changed

14 files changed

+1525
-646
lines changed
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
import contentstack from "../../contentstack.app.mjs";
2+
import {
3+
parseArray, parseEntry,
4+
} from "../../common/utils.mjs";
5+
6+
const createDocLink = "https://www.contentstack.com/docs/developers/apis/content-management-api#create-an-entry";
7+
const updateDocLink = "https://www.contentstack.com/docs/developers/apis/content-management-api#update-an-entry";
8+
9+
export default {
10+
props: {
11+
contentstack,
12+
contentType: {
13+
propDefinition: [
14+
contentstack,
15+
"contentType",
16+
],
17+
reloadProps: true,
18+
},
19+
},
20+
async additionalProps() {
21+
if (!this.contentType) {
22+
return {};
23+
}
24+
try {
25+
return await this.buildFieldProps(this.contentType);
26+
} catch {
27+
return {
28+
entryObj: {
29+
type: "object",
30+
label: "Entry",
31+
description: `Enter the entry object as JSON. [See the documentation](${this.isUpdate()
32+
? updateDocLink
33+
: createDocLink}) for more information.`,
34+
},
35+
};
36+
}
37+
},
38+
methods: {
39+
getType(field) {
40+
if (field.data_type === "boolean") {
41+
return "boolean";
42+
}
43+
if (field.data_type === "json") {
44+
return "object";
45+
}
46+
return field.multiple
47+
? "string[]"
48+
: "string";
49+
},
50+
isUpdate() {
51+
return false;
52+
},
53+
async getOptions(field) {
54+
if (field.data_type === "reference") {
55+
const referenceContentType = field.reference_to[0];
56+
const { entries } = await this.contentstack.listEntries({
57+
contentType: referenceContentType,
58+
});
59+
return entries?.map(({
60+
uid: value, title: label,
61+
}) => ({
62+
value,
63+
label: label ?? value,
64+
})) || [];
65+
}
66+
if (field.data_type === "file") {
67+
const { assets } = await this.contentstack.listAssets();
68+
return assets?.map(({
69+
uid: value, title: label,
70+
}) => ({
71+
value,
72+
label: label ?? value,
73+
})) || [];
74+
}
75+
return undefined;
76+
},
77+
async buildFieldProps(contentType) {
78+
const props = {};
79+
const { content_type: { schema } } = await this.contentstack.getContentType({
80+
contentType,
81+
});
82+
for (const field of schema) {
83+
props[field.uid] = {
84+
type: this.getType(field),
85+
label: field.display_name ?? field.uid,
86+
description: `Value of field ${field.display_name}. Field type: \`${field.data_type}\``,
87+
optional: this.isUpdate()
88+
? true
89+
: !field.mandatory,
90+
options: await this.getOptions(field),
91+
};
92+
}
93+
return props;
94+
},
95+
async buildEntry() {
96+
if (this.entryObj) {
97+
return parseEntry(this.entryObj);
98+
}
99+
const { content_type: { schema } } = await this.contentstack.getContentType({
100+
contentType: this.contentType,
101+
});
102+
const entry = {};
103+
for (const field of schema) {
104+
if (!this[field.uid]) {
105+
continue;
106+
}
107+
if (field.data_type === "reference") {
108+
if (field.multiple) {
109+
const referenceField = parseArray(this[field.uid]);
110+
entry[field.uid] = referenceField?.map((value) => ({
111+
uid: value,
112+
_content_type_uid: field.reference_to[0],
113+
}));
114+
} else {
115+
entry[field.uid] = {
116+
uid: this[field.uid],
117+
_content_type_uid: field.reference_to[0],
118+
};
119+
}
120+
continue;
121+
}
122+
entry[field.uid] = this[field.uid];
123+
}
124+
return parseEntry(entry);
125+
},
126+
},
127+
};
Lines changed: 19 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,33 @@
1-
import contentstack from "../../contentstack.app.mjs";
2-
import { axios } from "@pipedream/platform";
1+
import common from "../common/entries.mjs";
32

43
export default {
4+
...common,
55
key: "contentstack-create-entry",
66
name: "Create Entry",
7-
description: "Creates a new entry in Contentstack. [See the documentation]().",
8-
version: "0.0.{{ts}}",
7+
description: "Creates a new entry in Contentstack. [See the documentation](https://www.contentstack.com/docs/developers/apis/content-management-api#create-an-entry).",
8+
version: "0.0.1",
99
type: "action",
1010
props: {
11-
contentstack,
12-
stackId: {
11+
...common.props,
12+
locale: {
1313
propDefinition: [
14-
contentstack,
15-
"stackId",
14+
common.props.contentstack,
15+
"locale",
1616
],
1717
},
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-
},
4318
},
4419
async run({ $ }) {
45-
const response = await this.contentstack.createEntry();
46-
$.export("$summary", `Created entry "${response.title}" with UID ${response.uid}`);
20+
const response = await this.contentstack.createEntry({
21+
$,
22+
contentType: this.contentType,
23+
params: {
24+
locale: this.locale,
25+
},
26+
data: {
27+
entry: await this.buildEntry(),
28+
},
29+
});
30+
$.export("$summary", `Created entry "${response.entry.title}" with UID ${response.entry.uid}`);
4731
return response;
4832
},
4933
};
Lines changed: 52 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,73 @@
11
import contentstack from "../../contentstack.app.mjs";
2-
import { axios } from "@pipedream/platform";
2+
import { parseArray } from "../../common/utils.mjs";
33

44
export default {
55
key: "contentstack-publish-entry",
66
name: "Publish Entry",
7-
description: "Publishes a specific entry using its UID. [See the documentation]()",
8-
version: "0.0.{{ts}}",
7+
description: "Publishes a specific entry using its UID. [See the documentation](https://www.contentstack.com/docs/developers/apis/content-management-api#publish-entry)",
8+
version: "0.0.1",
99
type: "action",
1010
props: {
1111
contentstack,
12-
stackId: {
12+
contentType: {
1313
propDefinition: [
1414
contentstack,
15-
"stackId",
15+
"contentType",
1616
],
1717
},
18-
entryUid: {
18+
entryId: {
1919
propDefinition: [
2020
contentstack,
21-
"entryUid",
21+
"entryId",
22+
(c) => ({
23+
contentType: c.contentType,
24+
}),
2225
],
2326
},
27+
environments: {
28+
propDefinition: [
29+
contentstack,
30+
"environments",
31+
],
32+
},
33+
locales: {
34+
propDefinition: [
35+
contentstack,
36+
"locale",
37+
],
38+
type: "string[]",
39+
label: "Locale",
40+
description: "The code of the language in which you want your entry to be localized in",
41+
},
42+
scheduledAt: {
43+
type: "string",
44+
label: "Scheduled At",
45+
description: "The date/time in the ISO format to publish the entry. Example: `2016-10-07T12:34:36.000Z`",
46+
optional: true,
47+
},
2448
},
2549
async run({ $ }) {
26-
const response = await this.contentstack.publishEntry();
27-
$.export("$summary", `Successfully published entry with UID ${this.entryUid}`);
50+
const { entry } = await this.contentstack.getEntry({
51+
$,
52+
contentType: this.contentType,
53+
entryId: this.entryId,
54+
});
55+
56+
const response = await this.contentstack.publishEntry({
57+
$,
58+
contentType: this.contentType,
59+
entryId: this.entryId,
60+
data: {
61+
entry: {
62+
environments: parseArray(this.environments),
63+
locales: parseArray(this.locales),
64+
},
65+
locale: entry.locale,
66+
version: entry._version,
67+
scheduled_at: this.scheduledAt,
68+
},
69+
});
70+
$.export("$summary", `Successfully published entry with UID ${this.entryId}`);
2871
return response;
2972
},
3073
};
Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,50 @@
1-
import contentstack from "../../contentstack.app.mjs";
2-
import { axios } from "@pipedream/platform";
1+
import common from "../common/entries.mjs";
32

43
export default {
4+
...common,
55
key: "contentstack-update-entry",
66
name: "Update Entry",
77
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}}",
8+
version: "0.0.1",
99
type: "action",
1010
props: {
11-
contentstack,
12-
stackId: {
11+
...common.props,
12+
entryId: {
1313
propDefinition: [
14-
"contentstack",
15-
"stackId",
14+
common.props.contentstack,
15+
"entryId",
16+
(c) => ({
17+
contentType: c.contentType,
18+
}),
1619
],
1720
},
18-
contentTypeUid: {
21+
locale: {
1922
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",
23+
common.props.contentstack,
24+
"locale",
3425
],
3526
optional: true,
3627
},
3728
},
29+
methods: {
30+
...common.methods,
31+
isUpdate() {
32+
return true;
33+
},
34+
},
3835
async run({ $ }) {
3936
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-
: {},
37+
$,
38+
contentType: this.contentType,
39+
entryId: this.entryId,
40+
params: {
41+
locale: this.locale,
42+
},
43+
data: {
44+
entry: await this.buildEntry(),
45+
},
4946
});
50-
$.export("$summary", `Entry ${this.entryUid} updated successfully`);
47+
$.export("$summary", `Entry ${this.entryId} updated successfully`);
5148
return response;
5249
},
5350
};

0 commit comments

Comments
 (0)