storyblok 4.14.0 → 4.14.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -13,9 +13,9 @@ import { MultiBar, Presets } from 'cli-progress';
13
13
  import { Spinner } from '@topcli/spinner';
14
14
  import fs, { mkdir, writeFile, readFile as readFile$1, appendFile, access, constants, readdir, unlink, stat } from 'node:fs/promises';
15
15
  import filenamify from 'filenamify';
16
- import { select, password, input, confirm } from '@inquirer/prompts';
17
16
  import { ManagementApiClient } from '@storyblok/management-api-client';
18
17
  import { RateLimit, Sema } from 'async-sema';
18
+ import { select, password, input, confirm } from '@inquirer/prompts';
19
19
  import { exec, spawn } from 'node:child_process';
20
20
  import { promisify } from 'node:util';
21
21
  import { minimatch } from 'minimatch';
@@ -1579,6 +1579,174 @@ class ConsoleTransport {
1579
1579
  }
1580
1580
  }
1581
1581
 
1582
+ const getCredentials = async (filePath = join(getStoryblokGlobalPath(), "credentials.json")) => {
1583
+ try {
1584
+ await access(filePath);
1585
+ const content = await readFile(filePath);
1586
+ const parsedContent = JSON.parse(content);
1587
+ if (Object.keys(parsedContent).length === 0) {
1588
+ return null;
1589
+ }
1590
+ return parsedContent;
1591
+ } catch (error) {
1592
+ if (error.code === "ENOENT") {
1593
+ await saveToFile(filePath, JSON.stringify({}, null, 2), { mode: 384 });
1594
+ return null;
1595
+ }
1596
+ handleFileSystemError("read", error);
1597
+ return null;
1598
+ }
1599
+ };
1600
+ const addCredentials = async ({
1601
+ filePath = join(getStoryblokGlobalPath(), "credentials.json"),
1602
+ machineName,
1603
+ login,
1604
+ password,
1605
+ region
1606
+ }) => {
1607
+ const credentials = {
1608
+ ...await getCredentials(filePath),
1609
+ [machineName]: {
1610
+ login,
1611
+ password,
1612
+ region
1613
+ }
1614
+ };
1615
+ try {
1616
+ await saveToFile(filePath, JSON.stringify(credentials, null, 2), { mode: 384 });
1617
+ } catch (error) {
1618
+ throw new FileSystemError("invalid_argument", "write", error, `Error adding/updating entry for machine ${machineName} in credentials.json file`);
1619
+ }
1620
+ };
1621
+ const removeAllCredentials = async (filepath = getStoryblokGlobalPath()) => {
1622
+ const filePath = join(filepath, "credentials.json");
1623
+ await saveToFile(filePath, JSON.stringify({}, null, 2), { mode: 384 });
1624
+ };
1625
+
1626
+ let sessionInstance = null;
1627
+ function createSession() {
1628
+ const state = {
1629
+ isLoggedIn: false
1630
+ };
1631
+ async function initializeSession() {
1632
+ const envCredentials = getEnvCredentials();
1633
+ if (envCredentials) {
1634
+ state.isLoggedIn = true;
1635
+ state.login = envCredentials.login;
1636
+ state.password = envCredentials.password;
1637
+ state.region = envCredentials.region;
1638
+ state.envLogin = true;
1639
+ return;
1640
+ }
1641
+ const credentials = await getCredentials();
1642
+ if (credentials) {
1643
+ const creds = Object.values(credentials)[0];
1644
+ state.isLoggedIn = true;
1645
+ state.login = creds.login;
1646
+ state.password = creds.password;
1647
+ state.region = creds.region;
1648
+ } else {
1649
+ state.isLoggedIn = false;
1650
+ state.login = void 0;
1651
+ state.password = void 0;
1652
+ state.region = void 0;
1653
+ }
1654
+ state.envLogin = false;
1655
+ }
1656
+ function getEnvCredentials() {
1657
+ const envLogin = process.env.STORYBLOK_LOGIN || process.env.TRAVIS_STORYBLOK_LOGIN;
1658
+ const envPassword = process.env.STORYBLOK_TOKEN || process.env.TRAVIS_STORYBLOK_TOKEN;
1659
+ const envRegion = process.env.STORYBLOK_REGION || process.env.TRAVIS_STORYBLOK_REGION;
1660
+ if (envLogin && envPassword && envRegion) {
1661
+ return {
1662
+ login: envLogin,
1663
+ password: envPassword,
1664
+ region: envRegion
1665
+ };
1666
+ }
1667
+ return null;
1668
+ }
1669
+ async function persistCredentials(region) {
1670
+ if (state.isLoggedIn && state.login && state.password && state.region) {
1671
+ await addCredentials({
1672
+ machineName: regionsDomain[region] || "mapi.storyblok.com",
1673
+ login: state.login,
1674
+ password: state.password,
1675
+ region: state.region
1676
+ });
1677
+ } else {
1678
+ throw new Error("No credentials to save.");
1679
+ }
1680
+ }
1681
+ function updateSession(login, password, region) {
1682
+ state.isLoggedIn = true;
1683
+ state.login = login;
1684
+ state.password = password;
1685
+ state.region = region;
1686
+ }
1687
+ function logout() {
1688
+ state.isLoggedIn = false;
1689
+ state.login = void 0;
1690
+ state.password = void 0;
1691
+ state.region = void 0;
1692
+ }
1693
+ return {
1694
+ state,
1695
+ initializeSession,
1696
+ updateSession,
1697
+ persistCredentials,
1698
+ logout
1699
+ };
1700
+ }
1701
+ function session() {
1702
+ if (!sessionInstance) {
1703
+ sessionInstance = createSession();
1704
+ }
1705
+ return sessionInstance;
1706
+ }
1707
+
1708
+ let instance = null;
1709
+ let storedConfig = null;
1710
+ let currentLimiterCapacity = Math.max(1, getActiveConfig().api.maxConcurrency);
1711
+ let limiter = RateLimit(currentLimiterCapacity, { uniformDistribution: true });
1712
+ function resolveLimiter() {
1713
+ const desiredCapacity = Math.max(1, getActiveConfig().api.maxConcurrency);
1714
+ if (desiredCapacity !== currentLimiterCapacity) {
1715
+ limiter = RateLimit(desiredCapacity, { uniformDistribution: true });
1716
+ currentLimiterCapacity = desiredCapacity;
1717
+ }
1718
+ return limiter;
1719
+ }
1720
+ function configsAreEqual(config1, config2) {
1721
+ return JSON.stringify(config1) === JSON.stringify(config2);
1722
+ }
1723
+ function applyRateLimit(client) {
1724
+ if (getActiveConfig().api.maxConcurrency > 0) {
1725
+ client.interceptors.request.use(async (request) => {
1726
+ const limit = resolveLimiter();
1727
+ await limit();
1728
+ return request;
1729
+ });
1730
+ }
1731
+ }
1732
+ function creategetMapiClient(options) {
1733
+ const client = new ManagementApiClient(options);
1734
+ applyRateLimit(client);
1735
+ return client;
1736
+ }
1737
+ function getMapiClient(options) {
1738
+ if (!instance && options) {
1739
+ instance = creategetMapiClient(options);
1740
+ storedConfig = options;
1741
+ } else if (!instance) {
1742
+ throw new Error("MAPI client not initialized. Call getMapiClient with configuration first.");
1743
+ } else if (options && storedConfig && !configsAreEqual(options, storedConfig)) {
1744
+ instance = creategetMapiClient(options);
1745
+ storedConfig = options;
1746
+ }
1747
+ return instance;
1748
+ }
1749
+
1582
1750
  const packageJson = getPackageJson();
1583
1751
  let programInstance = null;
1584
1752
  function getProgram() {
@@ -1607,6 +1775,16 @@ function getProgram() {
1607
1775
  const resolvedConfig = await resolveConfig(targetCommand, ancestry);
1608
1776
  applyConfigToCommander(ancestry, resolvedConfig);
1609
1777
  setActiveConfig(resolvedConfig);
1778
+ const { state, initializeSession } = session();
1779
+ await initializeSession();
1780
+ if (state.password) {
1781
+ getMapiClient({
1782
+ token: {
1783
+ accessToken: state.password
1784
+ },
1785
+ region: state.region ?? resolvedConfig.region
1786
+ });
1787
+ }
1610
1788
  const options = targetCommand.optsWithGlobals();
1611
1789
  const commandPieces = [];
1612
1790
  for (let c = targetCommand; c; c = c.parent) {
@@ -1678,51 +1856,9 @@ const getStoryblokUrl = (region = "eu") => {
1678
1856
  return `https://${managementApiRegions[region]}/${API_VERSION}`;
1679
1857
  };
1680
1858
 
1681
- let instance = null;
1682
- let storedConfig = null;
1683
- let currentLimiterCapacity = Math.max(1, getActiveConfig().api.maxConcurrency);
1684
- let limiter = RateLimit(currentLimiterCapacity, { uniformDistribution: true });
1685
- function resolveLimiter() {
1686
- const desiredCapacity = Math.max(1, getActiveConfig().api.maxConcurrency);
1687
- if (desiredCapacity !== currentLimiterCapacity) {
1688
- limiter = RateLimit(desiredCapacity, { uniformDistribution: true });
1689
- currentLimiterCapacity = desiredCapacity;
1690
- }
1691
- return limiter;
1692
- }
1693
- function configsAreEqual(config1, config2) {
1694
- return JSON.stringify(config1) === JSON.stringify(config2);
1695
- }
1696
- function mapiClient(options) {
1697
- if (!instance && options) {
1698
- instance = new ManagementApiClient(options);
1699
- if (getActiveConfig().api.maxConcurrency > 0) {
1700
- instance.interceptors.request.use(async (request) => {
1701
- const limit = resolveLimiter();
1702
- await limit();
1703
- return request;
1704
- });
1705
- }
1706
- storedConfig = options;
1707
- } else if (!instance) {
1708
- throw new Error("MAPI client not initialized. Call mapiClient with configuration first.");
1709
- } else if (options && storedConfig && !configsAreEqual(options, storedConfig)) {
1710
- instance = new ManagementApiClient(options);
1711
- if (getActiveConfig().api.maxConcurrency > 0) {
1712
- instance.interceptors.request.use(async (request) => {
1713
- const limit = resolveLimiter();
1714
- await limit();
1715
- return request;
1716
- });
1717
- }
1718
- storedConfig = options;
1719
- }
1720
- return instance;
1721
- }
1722
-
1723
1859
  const getUser = async (token, region) => {
1724
1860
  try {
1725
- const client = mapiClient({
1861
+ const client = creategetMapiClient({
1726
1862
  token: {
1727
1863
  accessToken: token
1728
1864
  },
@@ -1807,132 +1943,6 @@ const loginWithOtp = async (email, password, otp, region) => {
1807
1943
  }
1808
1944
  };
1809
1945
 
1810
- const getCredentials = async (filePath = join(getStoryblokGlobalPath(), "credentials.json")) => {
1811
- try {
1812
- await access(filePath);
1813
- const content = await readFile(filePath);
1814
- const parsedContent = JSON.parse(content);
1815
- if (Object.keys(parsedContent).length === 0) {
1816
- return null;
1817
- }
1818
- return parsedContent;
1819
- } catch (error) {
1820
- if (error.code === "ENOENT") {
1821
- await saveToFile(filePath, JSON.stringify({}, null, 2), { mode: 384 });
1822
- return null;
1823
- }
1824
- handleFileSystemError("read", error);
1825
- return null;
1826
- }
1827
- };
1828
- const addCredentials = async ({
1829
- filePath = join(getStoryblokGlobalPath(), "credentials.json"),
1830
- machineName,
1831
- login,
1832
- password,
1833
- region
1834
- }) => {
1835
- const credentials = {
1836
- ...await getCredentials(filePath),
1837
- [machineName]: {
1838
- login,
1839
- password,
1840
- region
1841
- }
1842
- };
1843
- try {
1844
- await saveToFile(filePath, JSON.stringify(credentials, null, 2), { mode: 384 });
1845
- } catch (error) {
1846
- throw new FileSystemError("invalid_argument", "write", error, `Error adding/updating entry for machine ${machineName} in credentials.json file`);
1847
- }
1848
- };
1849
- const removeAllCredentials = async (filepath = getStoryblokGlobalPath()) => {
1850
- const filePath = join(filepath, "credentials.json");
1851
- await saveToFile(filePath, JSON.stringify({}, null, 2), { mode: 384 });
1852
- };
1853
-
1854
- let sessionInstance = null;
1855
- function createSession() {
1856
- const state = {
1857
- isLoggedIn: false
1858
- };
1859
- async function initializeSession() {
1860
- const envCredentials = getEnvCredentials();
1861
- if (envCredentials) {
1862
- state.isLoggedIn = true;
1863
- state.login = envCredentials.login;
1864
- state.password = envCredentials.password;
1865
- state.region = envCredentials.region;
1866
- state.envLogin = true;
1867
- return;
1868
- }
1869
- const credentials = await getCredentials();
1870
- if (credentials) {
1871
- const creds = Object.values(credentials)[0];
1872
- state.isLoggedIn = true;
1873
- state.login = creds.login;
1874
- state.password = creds.password;
1875
- state.region = creds.region;
1876
- } else {
1877
- state.isLoggedIn = false;
1878
- state.login = void 0;
1879
- state.password = void 0;
1880
- state.region = void 0;
1881
- }
1882
- state.envLogin = false;
1883
- }
1884
- function getEnvCredentials() {
1885
- const envLogin = process.env.STORYBLOK_LOGIN || process.env.TRAVIS_STORYBLOK_LOGIN;
1886
- const envPassword = process.env.STORYBLOK_TOKEN || process.env.TRAVIS_STORYBLOK_TOKEN;
1887
- const envRegion = process.env.STORYBLOK_REGION || process.env.TRAVIS_STORYBLOK_REGION;
1888
- if (envLogin && envPassword && envRegion) {
1889
- return {
1890
- login: envLogin,
1891
- password: envPassword,
1892
- region: envRegion
1893
- };
1894
- }
1895
- return null;
1896
- }
1897
- async function persistCredentials(region) {
1898
- if (state.isLoggedIn && state.login && state.password && state.region) {
1899
- await addCredentials({
1900
- machineName: regionsDomain[region] || "mapi.storyblok.com",
1901
- login: state.login,
1902
- password: state.password,
1903
- region: state.region
1904
- });
1905
- } else {
1906
- throw new Error("No credentials to save.");
1907
- }
1908
- }
1909
- function updateSession(login, password, region) {
1910
- state.isLoggedIn = true;
1911
- state.login = login;
1912
- state.password = password;
1913
- state.region = region;
1914
- }
1915
- function logout() {
1916
- state.isLoggedIn = false;
1917
- state.login = void 0;
1918
- state.password = void 0;
1919
- state.region = void 0;
1920
- }
1921
- return {
1922
- state,
1923
- initializeSession,
1924
- updateSession,
1925
- persistCredentials,
1926
- logout
1927
- };
1928
- }
1929
- function session() {
1930
- if (!sessionInstance) {
1931
- sessionInstance = createSession();
1932
- }
1933
- return sessionInstance;
1934
- }
1935
-
1936
1946
  async function performInteractiveLogin(options) {
1937
1947
  const { verbose = false, preSelectedRegion, showWelcomeMessage = true } = options || {};
1938
1948
  const spinner = new Spinner({
@@ -2051,8 +2061,7 @@ program$j.command(commands.LOGIN).description("Login to the Storyblok CLI").opti
2051
2061
  konsola.title(`${commands.LOGIN}`, colorPalette.LOGIN);
2052
2062
  const verbose = program$j.opts().verbose;
2053
2063
  const { token, region } = options;
2054
- const { state, updateSession, persistCredentials, initializeSession } = session();
2055
- await initializeSession();
2064
+ const { state, updateSession, persistCredentials } = session();
2056
2065
  if (state.isLoggedIn && !state.envLogin) {
2057
2066
  konsola.ok(`You are already logged in. If you want to login with a different account, please logout first.`);
2058
2067
  return;
@@ -2113,8 +2122,7 @@ program$i.command(commands.LOGOUT).description("Logout from the Storyblok CLI").
2113
2122
  konsola.title(`${commands.LOGOUT}`, colorPalette.LOGOUT);
2114
2123
  const verbose = program$i.opts().verbose;
2115
2124
  try {
2116
- const { state, initializeSession } = session();
2117
- await initializeSession();
2125
+ const { state } = session();
2118
2126
  if (!state.isLoggedIn || !state.password || !state.region) {
2119
2127
  konsola.warn(`You are already logged out. If you want to login, please use the login command.`);
2120
2128
  konsola.br();
@@ -2163,8 +2171,7 @@ const program$h = getProgram();
2163
2171
  program$h.command(commands.SIGNUP).description("Sign up for Storyblok").action(async () => {
2164
2172
  konsola.title(`${commands.SIGNUP}`, colorPalette.SIGNUP);
2165
2173
  const verbose = program$h.opts().verbose;
2166
- const { state, initializeSession } = session();
2167
- await initializeSession();
2174
+ const { state } = session();
2168
2175
  if (state.isLoggedIn && !state.envLogin) {
2169
2176
  konsola.ok(`You are already logged in. If you want to signup with a different account, please logout first.`);
2170
2177
  return;
@@ -2188,8 +2195,7 @@ const program$g = getProgram();
2188
2195
  program$g.command(commands.USER).description("Get the current user").action(async () => {
2189
2196
  konsola.title(`${commands.USER}`, colorPalette.USER);
2190
2197
  const verbose = program$g.opts().verbose;
2191
- const { state, initializeSession } = session();
2192
- await initializeSession();
2198
+ const { state } = session();
2193
2199
  if (!requireAuthentication(state)) {
2194
2200
  return;
2195
2201
  }
@@ -2226,7 +2232,7 @@ const DEFAULT_TAGS_FILENAME = "tags";
2226
2232
 
2227
2233
  const fetchComponents = async (spaceId) => {
2228
2234
  try {
2229
- const client = mapiClient();
2235
+ const client = getMapiClient();
2230
2236
  const { data } = await client.components.list({
2231
2237
  path: {
2232
2238
  space_id: spaceId
@@ -2240,7 +2246,7 @@ const fetchComponents = async (spaceId) => {
2240
2246
  };
2241
2247
  const fetchComponent = async (spaceId, componentName) => {
2242
2248
  try {
2243
- const client = mapiClient();
2249
+ const client = getMapiClient();
2244
2250
  const { data } = await client.components.list({
2245
2251
  path: {
2246
2252
  space_id: spaceId
@@ -2257,7 +2263,7 @@ const fetchComponent = async (spaceId, componentName) => {
2257
2263
  };
2258
2264
  const fetchComponentGroups = async (spaceId) => {
2259
2265
  try {
2260
- const client = mapiClient();
2266
+ const client = getMapiClient();
2261
2267
  const { data } = await client.componentFolders.list({
2262
2268
  path: {
2263
2269
  space_id: spaceId
@@ -2270,7 +2276,7 @@ const fetchComponentGroups = async (spaceId) => {
2270
2276
  };
2271
2277
  const fetchComponentPresets = async (spaceId) => {
2272
2278
  try {
2273
- const client = mapiClient();
2279
+ const client = getMapiClient();
2274
2280
  const { data } = await client.presets.list({
2275
2281
  path: {
2276
2282
  space_id: spaceId
@@ -2283,7 +2289,7 @@ const fetchComponentPresets = async (spaceId) => {
2283
2289
  };
2284
2290
  const fetchComponentInternalTags = async (spaceId) => {
2285
2291
  try {
2286
- const client = mapiClient();
2292
+ const client = getMapiClient();
2287
2293
  const { data } = await client.internalTags.list({
2288
2294
  path: {
2289
2295
  space_id: spaceId
@@ -2337,7 +2343,7 @@ const saveComponentsToFiles = async (space, spaceData, options) => {
2337
2343
 
2338
2344
  const pushComponent = async (space, component) => {
2339
2345
  try {
2340
- const client = mapiClient();
2346
+ const client = getMapiClient();
2341
2347
  const { data } = await client.components.create({
2342
2348
  path: {
2343
2349
  space_id: space
@@ -2353,7 +2359,7 @@ const pushComponent = async (space, component) => {
2353
2359
  };
2354
2360
  const updateComponent = async (space, componentId, component) => {
2355
2361
  try {
2356
- const client = mapiClient();
2362
+ const client = getMapiClient();
2357
2363
  const { data } = await client.components.update({
2358
2364
  path: {
2359
2365
  space_id: Number(space),
@@ -2378,7 +2384,7 @@ const upsertComponent = async (space, component, existingId) => {
2378
2384
  };
2379
2385
  const pushComponentGroup = async (space, componentGroup) => {
2380
2386
  try {
2381
- const client = mapiClient();
2387
+ const client = getMapiClient();
2382
2388
  const { data } = await client.componentFolders.create({
2383
2389
  path: {
2384
2390
  space_id: Number(space)
@@ -2395,7 +2401,7 @@ const pushComponentGroup = async (space, componentGroup) => {
2395
2401
  };
2396
2402
  const updateComponentGroup = async (space, groupId, componentGroup) => {
2397
2403
  try {
2398
- const client = mapiClient();
2404
+ const client = getMapiClient();
2399
2405
  const { data } = await client.componentFolders.update({
2400
2406
  path: {
2401
2407
  space_id: Number(space),
@@ -2420,7 +2426,7 @@ const upsertComponentGroup = async (space, group, existingId) => {
2420
2426
  };
2421
2427
  const pushComponentPreset = async (space, preset) => {
2422
2428
  try {
2423
- const client = mapiClient();
2429
+ const client = getMapiClient();
2424
2430
  const { data } = await client.presets.create({
2425
2431
  path: {
2426
2432
  space_id: Number(space)
@@ -2437,7 +2443,7 @@ const pushComponentPreset = async (space, preset) => {
2437
2443
  };
2438
2444
  const updateComponentPreset = async (space, presetId, preset) => {
2439
2445
  try {
2440
- const client = mapiClient();
2446
+ const client = getMapiClient();
2441
2447
  const { data } = await client.presets.update({
2442
2448
  path: {
2443
2449
  space_id: Number(space),
@@ -2462,7 +2468,7 @@ const upsertComponentPreset = async (space, preset, existingId) => {
2462
2468
  };
2463
2469
  const pushComponentInternalTag = async (space, componentInternalTag) => {
2464
2470
  try {
2465
- const client = mapiClient();
2471
+ const client = getMapiClient();
2466
2472
  const { data } = await client.internalTags.create({
2467
2473
  path: {
2468
2474
  space_id: Number(space)
@@ -2477,7 +2483,7 @@ const pushComponentInternalTag = async (space, componentInternalTag) => {
2477
2483
  };
2478
2484
  const updateComponentInternalTag = async (space, tagId, componentInternalTag) => {
2479
2485
  try {
2480
- const client = mapiClient();
2486
+ const client = getMapiClient();
2481
2487
  const { data } = await client.internalTags.update({
2482
2488
  path: {
2483
2489
  space_id: Number(space),
@@ -2614,8 +2620,7 @@ componentsCommand.command("pull [componentName]").option("-f, --filename <filena
2614
2620
  } = options;
2615
2621
  const actualFilename = filename ?? DEFAULT_COMPONENTS_FILENAME;
2616
2622
  const componentsOutputDir = resolveCommandPath(directories.components, space, path);
2617
- const { state, initializeSession } = session();
2618
- await initializeSession();
2623
+ const { state } = session();
2619
2624
  if (!requireAuthentication(state, verbose)) {
2620
2625
  return;
2621
2626
  }
@@ -2623,13 +2628,6 @@ componentsCommand.command("pull [componentName]").option("-f, --filename <filena
2623
2628
  handleError(new CommandError(`Please provide the space as argument --space YOUR_SPACE_ID.`), verbose);
2624
2629
  return;
2625
2630
  }
2626
- const { password, region } = state;
2627
- mapiClient({
2628
- token: {
2629
- accessToken: password
2630
- },
2631
- region
2632
- });
2633
2631
  const spinnerGroups = new Spinner({
2634
2632
  verbose: !isVitest
2635
2633
  });
@@ -3654,8 +3652,7 @@ componentsCommand.command("push [componentName]").description(`Push your space's
3654
3652
  const { space, path } = componentsCommand.opts();
3655
3653
  const { filter } = options;
3656
3654
  const fromSpace = options.from || space;
3657
- const { state, initializeSession } = session();
3658
- await initializeSession();
3655
+ const { state } = session();
3659
3656
  if (!requireAuthentication(state, verbose)) {
3660
3657
  return;
3661
3658
  }
@@ -3665,14 +3662,8 @@ componentsCommand.command("push [componentName]").description(`Push your space's
3665
3662
  }
3666
3663
  konsola.info(`Attempting to push components ${chalk.bold("from")} space ${chalk.hex(colorPalette.COMPONENTS)(fromSpace)} ${chalk.bold("to")} ${chalk.hex(colorPalette.COMPONENTS)(space)}`);
3667
3664
  konsola.br();
3668
- const { password, region } = state;
3669
3665
  let requestCount = 0;
3670
- const client = mapiClient({
3671
- token: {
3672
- accessToken: password
3673
- },
3674
- region
3675
- });
3666
+ const client = getMapiClient();
3676
3667
  client.interceptors.request.use((config) => {
3677
3668
  requestCount++;
3678
3669
  return config;
@@ -3794,7 +3785,7 @@ const DEFAULT_LANGUAGES_FILENAME = "languages";
3794
3785
 
3795
3786
  const fetchSpace = async (spaceId) => {
3796
3787
  try {
3797
- const client = mapiClient();
3788
+ const client = getMapiClient();
3798
3789
  const { data } = await client.spaces.get({
3799
3790
  path: {
3800
3791
  space_id: spaceId
@@ -3808,7 +3799,7 @@ const fetchSpace = async (spaceId) => {
3808
3799
  };
3809
3800
  const createSpace = async (space) => {
3810
3801
  try {
3811
- const client = mapiClient();
3802
+ const client = getMapiClient();
3812
3803
  const { data } = await client.spaces.create({
3813
3804
  body: {
3814
3805
  space
@@ -3853,8 +3844,7 @@ languagesCommand.command("pull").description(`Download your space's languages sc
3853
3844
  const verbose = program$c.opts().verbose;
3854
3845
  const { space, path } = languagesCommand.opts();
3855
3846
  const { filename = "languages", suffix = options.space } = options;
3856
- const { state, initializeSession } = session();
3857
- await initializeSession();
3847
+ const { state } = session();
3858
3848
  if (!requireAuthentication(state, verbose)) {
3859
3849
  return;
3860
3850
  }
@@ -3862,13 +3852,6 @@ languagesCommand.command("pull").description(`Download your space's languages sc
3862
3852
  handleError(new CommandError(`Please provide the space as argument --space YOUR_SPACE_ID.`), verbose);
3863
3853
  return;
3864
3854
  }
3865
- const { password, region } = state;
3866
- mapiClient({
3867
- token: {
3868
- accessToken: password
3869
- },
3870
- region
3871
- });
3872
3855
  const spinner = new Spinner({
3873
3856
  verbose: !isVitest
3874
3857
  });
@@ -3943,8 +3926,7 @@ migrationsCommand.command("generate [componentName]").description("Generate a mi
3943
3926
  handleError(new CommandError(`Please provide the component name as argument ${chalk.hex(colorPalette.MIGRATIONS)("storyblok migrations generate YOUR_COMPONENT_NAME.")}`), verbose);
3944
3927
  return;
3945
3928
  }
3946
- const { state, initializeSession } = session();
3947
- await initializeSession();
3929
+ const { state } = session();
3948
3930
  if (!requireAuthentication(state, verbose)) {
3949
3931
  return;
3950
3932
  }
@@ -3952,13 +3934,6 @@ migrationsCommand.command("generate [componentName]").description("Generate a mi
3952
3934
  handleError(new CommandError(`Please provide the space as argument --space YOUR_SPACE_ID.`), verbose);
3953
3935
  return;
3954
3936
  }
3955
- const { password, region } = state;
3956
- mapiClient({
3957
- token: {
3958
- accessToken: password
3959
- },
3960
- region
3961
- });
3962
3937
  const spinner = ui.createSpinner(`Generating migration for component ${componentName}...`);
3963
3938
  try {
3964
3939
  const component = await fetchComponent(space, componentName);
@@ -3986,7 +3961,7 @@ migrationsCommand.command("generate [componentName]").description("Generate a mi
3986
3961
 
3987
3962
  const fetchStories = async (spaceId, params) => {
3988
3963
  try {
3989
- const client = mapiClient();
3964
+ const client = getMapiClient();
3990
3965
  const { data, response } = await client.stories.list({
3991
3966
  path: {
3992
3967
  space_id: spaceId
@@ -4008,7 +3983,7 @@ const fetchStories = async (spaceId, params) => {
4008
3983
  };
4009
3984
  const fetchStory = async (spaceId, storyId) => {
4010
3985
  try {
4011
- const client = mapiClient();
3986
+ const client = getMapiClient();
4012
3987
  const { data } = await client.stories.get({
4013
3988
  path: {
4014
3989
  space_id: spaceId,
@@ -4023,7 +3998,7 @@ const fetchStory = async (spaceId, storyId) => {
4023
3998
  };
4024
3999
  const createStory = async (spaceId, payload) => {
4025
4000
  try {
4026
- const client = mapiClient();
4001
+ const client = getMapiClient();
4027
4002
  const { data } = await client.stories.create({
4028
4003
  path: {
4029
4004
  space_id: spaceId
@@ -4041,7 +4016,7 @@ const createStory = async (spaceId, payload) => {
4041
4016
  };
4042
4017
  const updateStory = async (spaceId, storyId, payload) => {
4043
4018
  try {
4044
- const client = mapiClient();
4019
+ const client = getMapiClient();
4045
4020
  const { data } = await client.stories.updateStory({
4046
4021
  path: {
4047
4022
  space_id: spaceId,
@@ -4693,8 +4668,7 @@ migrationsCommand.command("run [componentName]").description("Run migrations").o
4693
4668
  }
4694
4669
  const verbose = program.opts().verbose;
4695
4670
  const { space, path } = migrationsCommand.opts();
4696
- const { state, initializeSession } = session();
4697
- await initializeSession();
4671
+ const { state } = session();
4698
4672
  if (!requireAuthentication(state, verbose)) {
4699
4673
  return;
4700
4674
  }
@@ -4703,13 +4677,6 @@ migrationsCommand.command("run [componentName]").description("Run migrations").o
4703
4677
  return;
4704
4678
  }
4705
4679
  const { filter, dryRun = false, query, startsWith, publish } = options;
4706
- const { password, region } = state;
4707
- mapiClient({
4708
- token: {
4709
- accessToken: password
4710
- },
4711
- region
4712
- });
4713
4680
  try {
4714
4681
  const spinner = ui.createSpinner(`Fetching migration files and stories...`);
4715
4682
  const migrationFiles = await readMigrationFiles({
@@ -4827,8 +4794,7 @@ migrationsCommand.command("rollback [migrationFile]").description("Rollback a mi
4827
4794
  space,
4828
4795
  path
4829
4796
  });
4830
- const { state, initializeSession } = session();
4831
- await initializeSession();
4797
+ const { state } = session();
4832
4798
  if (!requireAuthentication(state, verbose)) {
4833
4799
  return;
4834
4800
  }
@@ -4836,13 +4802,6 @@ migrationsCommand.command("rollback [migrationFile]").description("Rollback a mi
4836
4802
  handleError(new CommandError(`Please provide the space as argument --space YOUR_SPACE_ID.`), verbose);
4837
4803
  return;
4838
4804
  }
4839
- const { password, region } = state;
4840
- mapiClient({
4841
- token: {
4842
- accessToken: password
4843
- },
4844
- region
4845
- });
4846
4805
  try {
4847
4806
  const rollbackData = await readRollbackFile({
4848
4807
  space,
@@ -5659,7 +5618,7 @@ const DEFAULT_DATASOURCES_FILENAME = "datasources";
5659
5618
 
5660
5619
  const pushDatasource = async (spaceId, datasource) => {
5661
5620
  try {
5662
- const client = mapiClient();
5621
+ const client = getMapiClient();
5663
5622
  const { data } = await client.datasources.create({
5664
5623
  path: {
5665
5624
  space_id: spaceId
@@ -5674,7 +5633,7 @@ const pushDatasource = async (spaceId, datasource) => {
5674
5633
  };
5675
5634
  const updateDatasource = async (spaceId, datasourceId, datasource) => {
5676
5635
  try {
5677
- const client = mapiClient();
5636
+ const client = getMapiClient();
5678
5637
  const { data } = await client.datasources.update({
5679
5638
  path: {
5680
5639
  space_id: spaceId,
@@ -5699,7 +5658,7 @@ const upsertDatasource = async (space, datasource, existingId) => {
5699
5658
  };
5700
5659
  const pushDatasourceEntry = async (spaceId, datasourceId, entry) => {
5701
5660
  try {
5702
- const client = mapiClient();
5661
+ const client = getMapiClient();
5703
5662
  const { data } = await client.datasourceEntries.create({
5704
5663
  path: {
5705
5664
  space_id: spaceId
@@ -5719,7 +5678,7 @@ const pushDatasourceEntry = async (spaceId, datasourceId, entry) => {
5719
5678
  };
5720
5679
  const updateDatasourceEntry = async (spaceId, entryId, entry) => {
5721
5680
  try {
5722
- const client = mapiClient();
5681
+ const client = getMapiClient();
5723
5682
  await client.datasourceEntries.updateDatasourceEntry({
5724
5683
  path: {
5725
5684
  space_id: spaceId,
@@ -5889,7 +5848,7 @@ async function fetchAllPages(fetchFunction, extractDataFunction, page = 1, colle
5889
5848
  }
5890
5849
  const fetchDatasourceEntries = async (spaceId, datasourceId) => {
5891
5850
  try {
5892
- const client = mapiClient();
5851
+ const client = getMapiClient();
5893
5852
  return await fetchAllPages(
5894
5853
  (page) => client.datasourceEntries.list({
5895
5854
  path: {
@@ -5909,7 +5868,7 @@ const fetchDatasourceEntries = async (spaceId, datasourceId) => {
5909
5868
  };
5910
5869
  const fetchDatasources = async (spaceId) => {
5911
5870
  try {
5912
- const client = mapiClient();
5871
+ const client = getMapiClient();
5913
5872
  const datasources = await fetchAllPages(
5914
5873
  (page) => client.datasources.list({
5915
5874
  path: {
@@ -5938,7 +5897,7 @@ const fetchDatasources = async (spaceId) => {
5938
5897
  };
5939
5898
  const fetchDatasource = async (spaceId, datasourceName) => {
5940
5899
  try {
5941
- const client = mapiClient();
5900
+ const client = getMapiClient();
5942
5901
  const { data } = await client.datasources.list({
5943
5902
  path: {
5944
5903
  space_id: spaceId
@@ -5989,8 +5948,7 @@ datasourcesCommand.command("pull [datasourceName]").option("-f, --filename <file
5989
5948
  } = options;
5990
5949
  const actualFilename = filename ?? DEFAULT_DATASOURCES_FILENAME;
5991
5950
  const datasourcesOutputDir = resolveCommandPath(directories.datasources, space, path);
5992
- const { state, initializeSession } = session();
5993
- await initializeSession();
5951
+ const { state } = session();
5994
5952
  if (!requireAuthentication(state, verbose)) {
5995
5953
  return;
5996
5954
  }
@@ -5998,13 +5956,6 @@ datasourcesCommand.command("pull [datasourceName]").option("-f, --filename <file
5998
5956
  handleError(new CommandError(`Please provide the space as argument --space YOUR_SPACE_ID.`), verbose);
5999
5957
  return;
6000
5958
  }
6001
- const { password, region } = state;
6002
- mapiClient({
6003
- token: {
6004
- accessToken: password
6005
- },
6006
- region
6007
- });
6008
5959
  const spinnerDatasources = new Spinner({
6009
5960
  verbose: !isVitest
6010
5961
  });
@@ -6065,8 +6016,7 @@ datasourcesCommand.command("push [datasourceName]").description(`Push your space
6065
6016
  const { space, path } = datasourcesCommand.opts();
6066
6017
  const { filter } = options;
6067
6018
  const fromSpace = options.from || space;
6068
- const { state, initializeSession } = session();
6069
- await initializeSession();
6019
+ const { state } = session();
6070
6020
  if (!requireAuthentication(state, verbose)) {
6071
6021
  return;
6072
6022
  }
@@ -6076,13 +6026,6 @@ datasourcesCommand.command("push [datasourceName]").description(`Push your space
6076
6026
  }
6077
6027
  konsola.info(`Attempting to push datasources ${chalk.bold("from")} space ${chalk.hex(colorPalette.DATASOURCES)(fromSpace)} ${chalk.bold("to")} ${chalk.hex(colorPalette.DATASOURCES)(space)}`);
6078
6028
  konsola.br();
6079
- const { password, region } = state;
6080
- mapiClient({
6081
- token: {
6082
- accessToken: password
6083
- },
6084
- region
6085
- });
6086
6029
  try {
6087
6030
  const spaceState = {
6088
6031
  local: await readDatasourcesFiles({
@@ -6169,7 +6112,7 @@ datasourcesCommand.command("push [datasourceName]").description(`Push your space
6169
6112
 
6170
6113
  async function deleteDatasource(spaceId, id) {
6171
6114
  try {
6172
- const client = mapiClient();
6115
+ const client = getMapiClient();
6173
6116
  await client.datasources.delete({
6174
6117
  path: {
6175
6118
  space_id: spaceId,
@@ -6195,8 +6138,7 @@ datasourcesCommand.command("delete [name]").description("Delete a datasource fro
6195
6138
  }
6196
6139
  const { space } = datasourcesCommand.opts();
6197
6140
  const verbose = datasourcesCommand.parent?.opts().verbose;
6198
- const { state, initializeSession } = session();
6199
- await initializeSession();
6141
+ const { state } = session();
6200
6142
  if (!requireAuthentication(state, verbose)) {
6201
6143
  return;
6202
6144
  }
@@ -6204,13 +6146,6 @@ datasourcesCommand.command("delete [name]").description("Delete a datasource fro
6204
6146
  handleError(new CommandError("Please provide the space as argument --space YOUR_SPACE_ID."), verbose);
6205
6147
  return;
6206
6148
  }
6207
- const { password, region } = state;
6208
- mapiClient({
6209
- token: {
6210
- accessToken: password
6211
- },
6212
- region
6213
- });
6214
6149
  const spinner = new Spinner({
6215
6150
  verbose: !isVitest
6216
6151
  });
@@ -6457,7 +6392,6 @@ program$5.command(`${commands.CREATE} [project-path]`).alias("c").description(`S
6457
6392
  konsola.warn(`Both --blueprint and --template provided. Using --template and ignoring --blueprint.`);
6458
6393
  }
6459
6394
  const { state, initializeSession } = session();
6460
- await initializeSession();
6461
6395
  let password;
6462
6396
  let region;
6463
6397
  if (state.region) {
@@ -6478,12 +6412,6 @@ program$5.command(`${commands.CREATE} [project-path]`).alias("c").description(`S
6478
6412
  handleError(new CommandError(`Cannot create space in region "${options.region}". Your account is configured for region "${region}". Space creation must use your account's region.`));
6479
6413
  return;
6480
6414
  }
6481
- mapiClient({
6482
- token: {
6483
- accessToken: password
6484
- },
6485
- region
6486
- });
6487
6415
  } else if (state.isLoggedIn && state.password) {
6488
6416
  password = state.password;
6489
6417
  if (state.region) {
@@ -6722,7 +6650,7 @@ const assetsCommand = program$2.command(commands.ASSETS).description(`Manage you
6722
6650
 
6723
6651
  const fetchAssets = async ({ spaceId, params }) => {
6724
6652
  try {
6725
- const client = mapiClient();
6653
+ const client = getMapiClient();
6726
6654
  const { data, response } = await client.assets.list({
6727
6655
  path: {
6728
6656
  space_id: spaceId
@@ -6768,7 +6696,7 @@ const getSignedAssetUrl = async (filename, assetToken, region) => {
6768
6696
  };
6769
6697
  const fetchAssetFolders = async ({ spaceId }) => {
6770
6698
  try {
6771
- const client = mapiClient();
6699
+ const client = getMapiClient();
6772
6700
  const { data, response } = await client.assetFolders.list({
6773
6701
  path: {
6774
6702
  space_id: spaceId
@@ -6788,7 +6716,7 @@ const createAssetFolder = async (folder, {
6788
6716
  spaceId
6789
6717
  }) => {
6790
6718
  try {
6791
- const client = mapiClient();
6719
+ const client = getMapiClient();
6792
6720
  const { data } = await client.assetFolders.create({
6793
6721
  path: {
6794
6722
  space_id: spaceId
@@ -6810,7 +6738,7 @@ const updateAssetFolder = async (folder, {
6810
6738
  spaceId
6811
6739
  }) => {
6812
6740
  try {
6813
- const client = mapiClient();
6741
+ const client = getMapiClient();
6814
6742
  await client.assetFolders.update({
6815
6743
  path: {
6816
6744
  asset_folder_id: folder.id,
@@ -6827,7 +6755,7 @@ const updateAssetFolder = async (folder, {
6827
6755
  };
6828
6756
  const requestAssetUpload = async (asset, { spaceId }) => {
6829
6757
  try {
6830
- const client = mapiClient();
6758
+ const client = getMapiClient();
6831
6759
  const { data } = await client.assets.upload({
6832
6760
  path: {
6833
6761
  space_id: spaceId
@@ -6877,7 +6805,7 @@ const finishAssetUpload = async (assetId, {
6877
6805
  spaceId
6878
6806
  }) => {
6879
6807
  try {
6880
- const client = mapiClient();
6808
+ const client = getMapiClient();
6881
6809
  await client.assets.finalize({
6882
6810
  path: {
6883
6811
  space_id: spaceId,
@@ -6940,7 +6868,7 @@ const updateAsset = async (asset, fileBuffer, {
6940
6868
  assetWithNewFilename.filename = uploadedAsset.filename;
6941
6869
  assetWithNewFilename.short_filename = uploadedAsset.short_filename;
6942
6870
  }
6943
- const client = mapiClient();
6871
+ const client = getMapiClient();
6944
6872
  await client.assets.update({
6945
6873
  path: {
6946
6874
  space_id: spaceId,
@@ -7301,7 +7229,7 @@ const makeCreateAssetFolderAPITransport = ({ spaceId }) => (folder) => createAss
7301
7229
  });
7302
7230
  const makeUpdateAssetFolderAPITransport = ({ spaceId }) => (folder) => updateAssetFolder(folder, { spaceId });
7303
7231
  const makeGetAssetFolderAPITransport = ({ spaceId }) => async (folderId) => {
7304
- const { data, response } = await mapiClient().assetFolders.get({
7232
+ const { data, response } = await getMapiClient().assetFolders.get({
7305
7233
  path: {
7306
7234
  asset_folder_id: folderId,
7307
7235
  space_id: spaceId
@@ -7441,7 +7369,7 @@ const makeAppendAssetFolderManifestFSTransport = ({ manifestFile }) => async (lo
7441
7369
  }));
7442
7370
  };
7443
7371
  const makeGetAssetAPITransport = ({ spaceId }) => async (assetId) => {
7444
- const { data, response } = await mapiClient().assets.get({
7372
+ const { data, response } = await getMapiClient().assets.get({
7445
7373
  path: {
7446
7374
  space_id: spaceId,
7447
7375
  asset_id: assetId
@@ -7607,8 +7535,7 @@ assetsCommand.command("pull").option("-d, --dry-run", "Preview changes without a
7607
7535
  }
7608
7536
  const { space, path: basePath, verbose } = command.optsWithGlobals();
7609
7537
  const assetToken = options.assetToken;
7610
- const { state, initializeSession } = session();
7611
- await initializeSession();
7538
+ const { state } = session();
7612
7539
  if (!requireAuthentication(state, verbose)) {
7613
7540
  process.exitCode = 2;
7614
7541
  return;
@@ -7618,13 +7545,7 @@ assetsCommand.command("pull").option("-d, --dry-run", "Preview changes without a
7618
7545
  process.exitCode = 2;
7619
7546
  return;
7620
7547
  }
7621
- const { password, region } = state;
7622
- mapiClient({
7623
- token: {
7624
- accessToken: password
7625
- },
7626
- region
7627
- });
7548
+ const { region } = state;
7628
7549
  const summary = {
7629
7550
  folderResults: { total: 0, succeeded: 0, failed: 0 },
7630
7551
  fetchAssetPages: { total: 0, succeeded: 0, failed: 0 },
@@ -8073,7 +7994,7 @@ const mapReferencesStream = ({
8073
7994
  });
8074
7995
  };
8075
7996
  const getRemoteStory = async ({ spaceId, storyId }) => {
8076
- const { data, response } = await mapiClient().stories.get({
7997
+ const { data, response } = await getMapiClient().stories.get({
8077
7998
  path: {
8078
7999
  space_id: spaceId,
8079
8000
  story_id: storyId
@@ -8093,8 +8014,8 @@ const makeCreateStoryAPITransport = ({ spaceId }) => async (localStory) => {
8093
8014
  story: {
8094
8015
  ...newStoryData,
8095
8016
  content: {
8096
- // @ts-expect-error Our types are wrong.
8097
- component: content?.component
8017
+ _uid: "",
8018
+ component: "__tmp__"
8098
8019
  }
8099
8020
  },
8100
8021
  publish: 0
@@ -8471,8 +8392,7 @@ assetsCommand.command("push").argument("[asset]", "path or URL of a single asset
8471
8392
  const { space: targetSpace, path: basePath, verbose } = command.optsWithGlobals();
8472
8393
  const fromSpace = options.from || targetSpace;
8473
8394
  const assetToken = options.assetToken;
8474
- const { state, initializeSession } = session();
8475
- await initializeSession();
8395
+ const { state } = session();
8476
8396
  if (!requireAuthentication(state, verbose)) {
8477
8397
  process.exitCode = 2;
8478
8398
  return;
@@ -8482,13 +8402,7 @@ assetsCommand.command("push").argument("[asset]", "path or URL of a single asset
8482
8402
  process.exitCode = 2;
8483
8403
  return;
8484
8404
  }
8485
- const { password, region } = state;
8486
- mapiClient({
8487
- token: {
8488
- accessToken: password
8489
- },
8490
- region
8491
- });
8405
+ const { region } = state;
8492
8406
  const summaries = [];
8493
8407
  let fatalError = false;
8494
8408
  const manifestFile = join(resolveCommandPath(directories.assets, fromSpace, basePath), "manifest.jsonl");
@@ -8621,8 +8535,7 @@ storiesCommand.command("pull").option("-d, --dry-run", "Preview changes without
8621
8535
  logger.warn("Dry run mode enabled");
8622
8536
  }
8623
8537
  const { space, path: basePath, verbose } = command.optsWithGlobals();
8624
- const { state, initializeSession } = session();
8625
- await initializeSession();
8538
+ const { state } = session();
8626
8539
  if (!requireAuthentication(state, verbose)) {
8627
8540
  return;
8628
8541
  }
@@ -8630,13 +8543,6 @@ storiesCommand.command("pull").option("-d, --dry-run", "Preview changes without
8630
8543
  handleError(new CommandError(`Please provide the space as argument --space YOUR_SPACE_ID.`), verbose);
8631
8544
  return;
8632
8545
  }
8633
- const { password, region } = state;
8634
- mapiClient({
8635
- token: {
8636
- accessToken: password
8637
- },
8638
- region
8639
- });
8640
8546
  const summary = {
8641
8547
  fetchStoryPages: { total: 0, succeeded: 0, failed: 0 },
8642
8548
  fetchStories: { total: 0, succeeded: 0, failed: 0 },
@@ -8739,8 +8645,7 @@ storiesCommand.command("push").option("-f, --from <from>", "source space id").op
8739
8645
  }
8740
8646
  const { space, path: basePath, verbose } = command.optsWithGlobals();
8741
8647
  const fromSpace = options.from || space;
8742
- const { state, initializeSession } = session();
8743
- await initializeSession();
8648
+ const { state } = session();
8744
8649
  if (!requireAuthentication(state, verbose)) {
8745
8650
  return;
8746
8651
  }
@@ -8748,13 +8653,6 @@ storiesCommand.command("push").option("-f, --from <from>", "source space id").op
8748
8653
  handleError(new CommandError(`Please provide the space as argument --space YOUR_SPACE_ID.`), verbose);
8749
8654
  return;
8750
8655
  }
8751
- const { password, region } = state;
8752
- mapiClient({
8753
- token: {
8754
- accessToken: password
8755
- },
8756
- region
8757
- });
8758
8656
  const warnAboutCustomPlugins = (fields, story) => {
8759
8657
  const warnedPlugins = /* @__PURE__ */ new Set();
8760
8658
  for (const field of fields) {