Skip to content

Instantly share code, notes, and snippets.

@roberto-butti
Created February 27, 2025 09:28
Show Gist options
  • Save roberto-butti/34f8f62bb91b489b203fe9c804c11115 to your computer and use it in GitHub Desktop.
Save roberto-butti/34f8f62bb91b489b203fe9c804c11115 to your computer and use it in GitHub Desktop.
updating configuration for Components
// 1. Import the Storyblok client
import StoryblokClient from "storyblok-js-client";
const spaceId = "yourspaceid";
// 2. Initialize the client with the oauth token
// from the my account area at https://app.storyblok.com
const Storyblok = new StoryblokClient({
oauthToken: process.env.STORYBLOK_OAUTH_TOKEN,
});
// Select all the components
let response = await Storyblok.get("spaces/" + spaceId + "/components/", {})
response.data.components.forEach(async (component) => {
let pleaseUpdateComponent = false
// for each field in the schema (parsing object properties, it is not an array)...
Object.keys(component.schema).forEach(async function(key,index) {
// key: the name of the object key (the component name)
// index: the ordinal position of the key within the object
// getting the current field
let field = component.schema[key]
// if the field type is a richtext...
if (field.type === "richtext"){
// check if the allow_target_blank is not set
if (field.allow_target_blank == true) {
console.log(component.id, component.name + "." + key , field.allow_target_blank)
} else {
console.log(component.id, component.name + "." + key , "Target blank is missing")
// if the allow_target_blank is not set or set to false,
// we can set it to true, and then
// set the flag to remember to update the component (that includes the changed fields)
component.schema[key].allow_target_blank = true
pleaseUpdateComponent = true
}
}
});
// if the component inclued at least 1 field that was updated
if (pleaseUpdateComponent) {
console.log("I have to update the component: ", component.id, component.name)
//console.log(component)
// we can update the component schema (all the component schema with also the unchanged fields)
await Storyblok.put("spaces/" + spaceId + "/components/" + component.id, {
"component": {
"schema": component.schema
}
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment