GithubHelp home page GithubHelp logo

dawntraoz-storyblok / sb-management-api-helpers Goto Github PK

View Code? Open in Web Editor NEW
7.0 0.0 1.0 27 KB

Simple action scripts using the Management API

License: Apache License 2.0

JavaScript 100.00%
management-api storyblok storyblok-api

sb-management-api-helpers's Introduction

sb-management-api-helpers

Simple action scripts using the Management API

Set up the repo

Install the dependencies:

npm install

Then, copy .env.example and rename it to .env, provide your own Storyblok equivalent variables:

SB_MANAGEMENT_API_TOKEN=<your-personal-access-token>
SB_SPACE_ID=<space-ID>

# Migration between spaces scripts variables
SB_SPACE_A_ID=<SPACE-A-ID>
SB_SPACE_B_ID=<SPACE-B-ID>

# Update a task dialog script
SB_TASK_ID=

# App or Tool ID to get or update Space Settings script
SB_APP_ID=

Actions

  1. Make your space duplicable:

    npm run duplicable
  2. Get all the folders from your space:

    npm run folders
  3. Make your component fields translatable:

    Go to ./scripts/component-translatable.js and add the component details needed to make the fields translatable:

    // Component Details
    component: {
      id: '', // Provided in the URL when opening the blok in the Block library
      name: '', // Technical name in the Config Tab
      fields: [
        // Fields name & type in lowercase
        { name: 'headline', type: 'text' },
        { name: 'teaser', type: 'textarea' },
      ]
    }

    Then you will be able to run:

    npm run component-translatable
  4. Page History: Get the latest 25th story versions and the difference from each one

    Go to ./scripts/story-versions.js and add the story id:

    story_id: ""; // Provided in the URL when opening the story in the Visual Editor

    Then you will be able to run:

    npm run story-versions
  5. Migrate or update first 100 pages from space A to space B:

    npm run share-content
  6. Migrate first 25 assets from an old space to a new one:

    npm run migrate-assets
  7. Update a task dialog based on a specific Content Type entries:

    npm run update-task
  8. Get number of Nestable components in a space:

    npm run components-by-type
  9. Get or update the Space Settings from a Tool or App plugin:

    npm run app-space-settings

sb-management-api-helpers's People

Contributors

dawntraoz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Forkers

alexadark

sb-management-api-helpers's Issues

Feature Request: Cleaning script

💡 Idea from Alex Jover

Delete spaces created by yourself in a bulk action. Initial script by Alex that should be refactored to the repo:

import "isomorphic-fetch";
import readline from "readline";
import { setTimeout } from "timers/promises";
import StoryblokClient from "storyblok-js-client";

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});
const prompt = (query) => new Promise((resolve) => rl.question(query, resolve));

const client = new StoryblokClient({
  oauthToken: "<your-mgmnt-token>",
});

const SPACES_ID_EXCLUDED = [
  137624, // Folder-level translation
  149547, // Holland Casino POC
  147897, // SDK Playground
  132070, // T-Mobile
];
const MY_OWNER_ID = 97764; // Your Storyblok user ID

const run = async () => {
  const { spaces } = (await client.get("spaces/")).data;
  const mySpaces = spaces.filter((s) => s.owner_id === MY_OWNER_ID);
  const spacesToPurge = mySpaces.filter(
    (s) => !SPACES_ID_EXCLUDED.find((sId) => sId === s.id)
  );

  console.log(`
------------------------
Total spaces: ${spaces.length}
My own spaces: ${mySpaces.length}
Excluded spaces: ${SPACES_ID_EXCLUDED.length}
------------------------

`);

  const input = await prompt(
    `You're about to delete ${spacesToPurge.length} spaces. Do you want to confirm? (y/n) `
  );
  rl.close();

  if (input === "y") {
    console.log(`
☠️☠️☠️☠️☠️☠️☠️☠️☠️☠️☠️☠️☠️☠️☠️☠️☠️☠️☠️☠️☠️☠️☠️☠️☠️☠️☠️☠️☠️☠️☠️☠️☠️☠️☠️☠️
  PURGE STARTS IN 3 SECONDS 
☠️☠️☠️☠️☠️☠️☠️☠️☠️☠️☠️☠️☠️☠️☠️☠️☠️☠️☠️☠️☠️☠️☠️☠️☠️☠️☠️☠️☠️☠️☠️☠️☠️☠️☠️☠️

`);

    await setTimeout(3000);

    for (const space of spacesToPurge) {
      await client.delete(`spaces/${space.id}`);
      console.log(`    Deleted space:  ${space.id} - ${space.name}`);
    }

    console.log(`
    
✅ All ${spacesToPurge.length} purged successfully

`);
  }
};

run();

Feature Request: Add special workflow stage when uploading content programatically using MAPI

Example shared by @RobertoButti:

let files = [
  "<ID>_Category_1",
  "<ID>_Category_2"
];

const spaceId = <SPACE_ID>;

for (const filenameElement of files) {
    let path = "./scripts/data/" + filenameElement + ".json";
    let file = Bun.file(path);
    let contents = await file.json();

    let productName = contents.name ?? "default"
   
    const StoryblokClient = require('storyblok-js-client')

    const Storyblok = new StoryblokClient({
        oauthToken: process.env.STORYBLOK_MAPI_TOKEN
    })


    Storyblok.post('spaces/' + spaceId + '/stories/', {
        "story": {
            "name": contents.name,
            "slug": contents.slug,
            "parent_id": <PARENT_FOLDER_ID>,
            "content": {
                "component": "item-page",
                "description": contents.description,
                "name": contents.name,
                "image1": contents.image.url,
                "seo": {
                    plugin: "seo_metatags",
                    title: contents.name,
                    description: contents.description
                },
            }
        },
        "publish": 0
    }).then(response => {
        if (response.status === 422) {
            console.error("Content for " + productName + " not created", response.response)
        }
        if (response.status === 201) {
            Storyblok.post('spaces/' + spaceId + '/workflow_stage_changes/', {
                "workflow_stage_change": {
                    "workflow_stage_id": <WORKFLOW_STAGE_ID>,
                    "story_id": response.data.story.id
                }
            }).then(response => {
                console.log(response)
            }).catch(error => {
                console.log(error)
            })
        }


    }).catch(error => {
        console.log(error.status, error.response)
    })
};

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.