Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
127 changes: 127 additions & 0 deletions components/contentstack/actions/common/entries.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import contentstack from "../../contentstack.app.mjs";
import {
parseArray, parseEntry,
} from "../../common/utils.mjs";

const createDocLink = "https://www.contentstack.com/docs/developers/apis/content-management-api#create-an-entry";
const updateDocLink = "https://www.contentstack.com/docs/developers/apis/content-management-api#update-an-entry";

export default {
props: {
contentstack,
contentType: {
propDefinition: [
contentstack,
"contentType",
],
reloadProps: true,
},
},
async additionalProps() {
if (!this.contentType) {
return {};
}
try {
return await this.buildFieldProps(this.contentType);
} catch {
return {
entryObj: {
type: "object",
label: "Entry",
description: `Enter the entry object as JSON. [See the documentation](${this.isUpdate()
? updateDocLink
: createDocLink}) for more information.`,
},
};
}
},
methods: {
getType(field) {
if (field.data_type === "boolean") {
return "boolean";
}
if (field.data_type === "json") {
return "object";
}
return field.multiple
? "string[]"
: "string";
},
isUpdate() {
return false;
},
async getOptions(field) {
if (field.data_type === "reference") {
const referenceContentType = field.reference_to[0];
const { entries } = await this.contentstack.listEntries({
contentType: referenceContentType,
});
return entries?.map(({
uid: value, title: label,
}) => ({
value,
label: label ?? value,
})) || [];
}
if (field.data_type === "file") {
const { assets } = await this.contentstack.listAssets();
return assets?.map(({
uid: value, title: label,
}) => ({
value,
label: label ?? value,
})) || [];
}
return undefined;
},
async buildFieldProps(contentType) {
const props = {};
const { content_type: { schema } } = await this.contentstack.getContentType({
contentType,
});
for (const field of schema) {
props[field.uid] = {
type: this.getType(field),
label: field.display_name ?? field.uid,
description: `Value of field ${field.display_name}. Field type: \`${field.data_type}\``,
optional: this.isUpdate()
? true
: !field.mandatory,
options: await this.getOptions(field),
};
}
return props;
},
async buildEntry() {
if (this.entryObj) {
return parseEntry(this.entryObj);
}
const { content_type: { schema } } = await this.contentstack.getContentType({
contentType: this.contentType,
});
const entry = {};
for (const field of schema) {
if (!this[field.uid]) {
continue;
}
if (field.data_type === "reference") {
if (field.multiple) {
const referenceField = parseArray(this[field.uid]);
entry[field.uid] = referenceField?.map((value) => ({
uid: value,
_content_type_uid: field.reference_to[0],
}));
} else {
entry[field.uid] = {
uid: this[field.uid],
_content_type_uid: field.reference_to[0],
};
}
continue;
}
entry[field.uid] = this[field.uid];
}
return parseEntry(entry);
},
},
};
33 changes: 33 additions & 0 deletions components/contentstack/actions/create-entry/create-entry.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import common from "../common/entries.mjs";

export default {
...common,
key: "contentstack-create-entry",
name: "Create Entry",
description: "Creates a new entry in Contentstack. [See the documentation](https://www.contentstack.com/docs/developers/apis/content-management-api#create-an-entry).",
version: "0.0.1",
type: "action",
props: {
...common.props,
locale: {
propDefinition: [
common.props.contentstack,
"locale",
],
},
},
async run({ $ }) {
const response = await this.contentstack.createEntry({
$,
contentType: this.contentType,
params: {
locale: this.locale,
},
data: {
entry: await this.buildEntry(),
},
});
$.export("$summary", `Created entry "${response.entry.title}" with UID ${response.entry.uid}`);
return response;
},
};
73 changes: 73 additions & 0 deletions components/contentstack/actions/publish-entry/publish-entry.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import contentstack from "../../contentstack.app.mjs";
import { parseArray } from "../../common/utils.mjs";

export default {
key: "contentstack-publish-entry",
name: "Publish Entry",
description: "Publishes a specific entry using its UID. [See the documentation](https://www.contentstack.com/docs/developers/apis/content-management-api#publish-entry)",
version: "0.0.1",
type: "action",
props: {
contentstack,
contentType: {
propDefinition: [
contentstack,
"contentType",
],
},
entryId: {
propDefinition: [
contentstack,
"entryId",
(c) => ({
contentType: c.contentType,
}),
],
},
environments: {
propDefinition: [
contentstack,
"environments",
],
},
locales: {
propDefinition: [
contentstack,
"locale",
],
type: "string[]",
label: "Locale",
description: "The code of the language in which you want your entry to be localized in",
},
scheduledAt: {
type: "string",
label: "Scheduled At",
description: "The date/time in the ISO format to publish the entry. Example: `2016-10-07T12:34:36.000Z`",
optional: true,
},
},
async run({ $ }) {
const { entry } = await this.contentstack.getEntry({
$,
contentType: this.contentType,
entryId: this.entryId,
});

const response = await this.contentstack.publishEntry({
$,
contentType: this.contentType,
entryId: this.entryId,
data: {
entry: {
environments: parseArray(this.environments),
locales: parseArray(this.locales),
},
locale: entry.locale,
version: entry._version,
scheduled_at: this.scheduledAt,
},
});
$.export("$summary", `Successfully published entry with UID ${this.entryId}`);
return response;
},
};
50 changes: 50 additions & 0 deletions components/contentstack/actions/update-entry/update-entry.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import common from "../common/entries.mjs";

export default {
...common,
key: "contentstack-update-entry",
name: "Update Entry",
description: "Updates an existing Contentstack entry. [See the documentation](https://www.contentstack.com/docs/developers/apis/content-management-api#update-an-entry).",
version: "0.0.1",
type: "action",
props: {
...common.props,
entryId: {
propDefinition: [
common.props.contentstack,
"entryId",
(c) => ({
contentType: c.contentType,
}),
],
},
locale: {
propDefinition: [
common.props.contentstack,
"locale",
],
optional: true,
},
},
methods: {
...common.methods,
isUpdate() {
return true;
},
},
async run({ $ }) {
const response = await this.contentstack.updateEntry({
$,
contentType: this.contentType,
entryId: this.entryId,
params: {
locale: this.locale,
},
data: {
entry: await this.buildEntry(),
},
});
$.export("$summary", `Entry ${this.entryId} updated successfully`);
return response;
},
};
42 changes: 42 additions & 0 deletions components/contentstack/common/utils.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
export function parseArray(value) {
if (!value) {
return undefined;
}
if (typeof value === "string") {
try {
return JSON.parse(value);
} catch {
throw new Error(`Could not parse as array: ${value}`);
}
}
return value;
}

export function parseEntry(entry) {
if (!entry) {
return undefined;
}
if (typeof entry === "string") {
try {
return JSON.parse(entry);
} catch {
throw new Error("Could not parse entry as JSON");
}
}
const parsedEntry = {};
for (const [
key,
value,
] of Object.entries(entry)) {
if (typeof value === "string") {
try {
parsedEntry[key] = JSON.parse(value);
} catch {
parsedEntry[key] = value;
}
} else {
parsedEntry[key] = value;
}
}
return parsedEntry;
}
Loading
Loading