sb-mig 5.6.0-beta.1 → 5.6.0-beta.3

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.
Files changed (173) hide show
  1. package/README.md +20 -19
  2. package/dist/api/assets/assets.js +2 -15
  3. package/dist/api/auth/auth.types.d.ts +1 -1
  4. package/dist/api/components/components.js +2 -1
  5. package/dist/api/components/components.sync.d.ts +8 -0
  6. package/dist/api/components/components.sync.js +193 -0
  7. package/dist/api/data-migration/component-data-migration.js +2 -2
  8. package/dist/api/datasources/datasource-entries.js +4 -5
  9. package/dist/api/datasources/datasources.d.ts +5 -2
  10. package/dist/api/datasources/datasources.js +42 -35
  11. package/dist/api/datasources/datasources.sync.d.ts +2 -0
  12. package/dist/api/datasources/datasources.sync.js +11 -0
  13. package/dist/api/datasources/datasources.types.d.ts +1 -1
  14. package/dist/api/datasources/index.d.ts +2 -1
  15. package/dist/api/datasources/index.js +2 -1
  16. package/dist/api/managementApi.d.ts +2 -2
  17. package/dist/api/migrate.d.ts +1 -1
  18. package/dist/api/migrate.js +3 -48
  19. package/dist/api/plugins/index.d.ts +2 -1
  20. package/dist/api/plugins/index.js +2 -1
  21. package/dist/api/plugins/plugins.d.ts +7 -2
  22. package/dist/api/plugins/plugins.js +28 -15
  23. package/dist/api/plugins/plugins.sync.d.ts +2 -0
  24. package/dist/api/plugins/plugins.sync.js +11 -0
  25. package/dist/api/roles/index.d.ts +2 -1
  26. package/dist/api/roles/index.js +2 -1
  27. package/dist/api/roles/roles.d.ts +5 -2
  28. package/dist/api/roles/roles.js +34 -11
  29. package/dist/api/roles/roles.sync.d.ts +2 -0
  30. package/dist/api/roles/roles.sync.js +6 -0
  31. package/dist/api/roles/roles.types.d.ts +1 -1
  32. package/dist/api/stories/stories.js +3 -11
  33. package/dist/api/sync/sync.types.d.ts +30 -0
  34. package/dist/api/sync/sync.types.js +1 -0
  35. package/dist/api/testApi.d.ts +2 -2
  36. package/dist/api/utils/helper-functions.d.ts +5 -1
  37. package/dist/api/utils/helper-functions.js +6 -1
  38. package/dist/api/utils/request.d.ts +1 -1
  39. package/dist/api/utils/request.js +11 -2
  40. package/dist/api/utils/resolverTransformations.js +2 -57
  41. package/dist/api-v2/assets/index.d.ts +13 -0
  42. package/dist/api-v2/assets/index.js +25 -0
  43. package/dist/api-v2/auth/index.d.ts +3 -0
  44. package/dist/api-v2/auth/index.js +8 -0
  45. package/dist/api-v2/client.d.ts +13 -0
  46. package/dist/api-v2/client.js +17 -0
  47. package/dist/api-v2/components/index.d.ts +10 -0
  48. package/dist/api-v2/components/index.js +29 -0
  49. package/dist/api-v2/datasources/index.d.ts +8 -0
  50. package/dist/api-v2/datasources/index.js +58 -0
  51. package/dist/api-v2/discover/discover.d.ts +47 -0
  52. package/dist/api-v2/discover/discover.js +328 -0
  53. package/dist/api-v2/discover/index.d.ts +2 -0
  54. package/dist/api-v2/discover/index.js +1 -0
  55. package/dist/api-v2/index.d.ts +19 -0
  56. package/dist/api-v2/index.js +21 -0
  57. package/dist/api-v2/plugins/index.d.ts +9 -0
  58. package/dist/api-v2/plugins/index.js +42 -0
  59. package/dist/api-v2/precompile/index.d.ts +2 -0
  60. package/dist/api-v2/precompile/index.js +1 -0
  61. package/dist/api-v2/precompile/precompile.d.ts +65 -0
  62. package/dist/api-v2/precompile/precompile.js +127 -0
  63. package/dist/api-v2/presets/index.d.ts +13 -0
  64. package/dist/api-v2/presets/index.js +25 -0
  65. package/dist/api-v2/requestConfig.d.ts +5 -0
  66. package/dist/api-v2/requestConfig.js +34 -0
  67. package/dist/api-v2/roles/index.d.ts +5 -0
  68. package/dist/api-v2/roles/index.js +35 -0
  69. package/dist/api-v2/spaces/index.d.ts +7 -0
  70. package/dist/api-v2/spaces/index.js +11 -0
  71. package/dist/api-v2/stories/index.d.ts +34 -0
  72. package/dist/api-v2/stories/index.js +172 -0
  73. package/dist/api-v2/stories/types.d.ts +28 -0
  74. package/dist/api-v2/stories/types.js +1 -0
  75. package/dist/api-v2/sync/index.d.ts +24 -0
  76. package/dist/api-v2/sync/index.js +109 -0
  77. package/dist/api-v2/sync/types.d.ts +1 -0
  78. package/dist/api-v2/sync/types.js +1 -0
  79. package/dist/api-v2/test.d.ts +15 -0
  80. package/dist/api-v2/test.js +21 -0
  81. package/dist/cli/commands/backup.js +7 -3
  82. package/dist/cli/commands/copy.js +2 -2
  83. package/dist/cli/commands/migrate.js +1 -2
  84. package/dist/cli/commands/migrations.js +2 -2
  85. package/dist/cli/commands/remove.js +1 -1
  86. package/dist/cli/commands/revert.js +2 -2
  87. package/dist/cli/commands/sync.js +1 -2
  88. package/dist/cli/index.js +1 -1
  89. package/dist/cli/utils/cli-utils.d.ts +69 -0
  90. package/dist/cli/utils/cli-utils.js +100 -0
  91. package/dist/cli/utils/discover.d.ts +3 -22
  92. package/dist/cli/utils/discover.js +53 -100
  93. package/dist/config/config.d.ts +2 -39
  94. package/dist/config/config.types.d.ts +40 -0
  95. package/dist/config/config.types.js +1 -0
  96. package/dist/config/defaultConfig.d.ts +1 -1
  97. package/dist/config/defaultConfig.js +2 -2
  98. package/dist/utils/array-utils.d.ts +20 -0
  99. package/dist/utils/array-utils.js +20 -0
  100. package/dist/utils/async-utils.d.ts +13 -0
  101. package/dist/utils/async-utils.js +13 -0
  102. package/dist/utils/date-utils.d.ts +14 -0
  103. package/dist/utils/date-utils.js +21 -0
  104. package/dist/utils/files.d.ts +35 -0
  105. package/dist/utils/files.js +57 -2
  106. package/dist/utils/main.d.ts +8 -18
  107. package/dist/utils/main.js +12 -104
  108. package/dist/utils/migrations.d.ts +9 -3
  109. package/dist/utils/object-utils.d.ts +46 -0
  110. package/dist/utils/object-utils.js +71 -0
  111. package/dist/utils/others.d.ts +6 -9
  112. package/dist/utils/others.js +8 -15
  113. package/dist/utils/path-utils.d.ts +89 -0
  114. package/dist/utils/path-utils.js +106 -0
  115. package/dist/utils/pkg.d.ts +16 -2
  116. package/dist/utils/pkg.js +16 -3
  117. package/dist/utils/string-utils.d.ts +33 -0
  118. package/dist/utils/string-utils.js +45 -0
  119. package/dist/utils/transform-utils.d.ts +62 -0
  120. package/dist/utils/transform-utils.js +113 -0
  121. package/dist-cjs/api/auth/auth.js +28 -0
  122. package/dist-cjs/api/auth/auth.types.js +2 -0
  123. package/dist-cjs/api/components/components.js +202 -0
  124. package/dist-cjs/api/components/components.sync.js +199 -0
  125. package/dist-cjs/api/components/components.types.js +2 -0
  126. package/dist-cjs/api/datasources/datasource-entries.js +166 -0
  127. package/dist-cjs/api/datasources/datasources.js +166 -0
  128. package/dist-cjs/api/datasources/datasources.types.js +2 -0
  129. package/dist-cjs/api/plugins/plugins.js +132 -0
  130. package/dist-cjs/api/plugins/plugins.types.js +2 -0
  131. package/dist-cjs/api/presets/componentPresets.js +25 -0
  132. package/dist-cjs/api/presets/presets.js +92 -0
  133. package/dist-cjs/api/presets/presets.types.js +2 -0
  134. package/dist-cjs/api/presets/resolvePresets.js +49 -0
  135. package/dist-cjs/api/roles/roles.js +131 -0
  136. package/dist-cjs/api/roles/roles.types.js +2 -0
  137. package/dist-cjs/api/spaces/spaces.js +34 -0
  138. package/dist-cjs/api/spaces/spaces.types.js +2 -0
  139. package/dist-cjs/api/stories/stories.js +214 -0
  140. package/dist-cjs/api/stories/stories.types.js +2 -0
  141. package/dist-cjs/api/sync/sync.types.js +2 -0
  142. package/dist-cjs/api/utils/request.js +48 -0
  143. package/dist-cjs/api/utils/resolvers.types.js +2 -0
  144. package/dist-cjs/api-v2/assets/index.js +30 -0
  145. package/dist-cjs/api-v2/auth/index.js +12 -0
  146. package/dist-cjs/api-v2/client.js +23 -0
  147. package/dist-cjs/api-v2/components/index.js +40 -0
  148. package/dist-cjs/api-v2/datasources/index.js +64 -0
  149. package/dist-cjs/api-v2/discover/discover.js +368 -0
  150. package/dist-cjs/api-v2/discover/index.js +9 -0
  151. package/dist-cjs/api-v2/index.js +60 -0
  152. package/dist-cjs/api-v2/plugins/index.js +49 -0
  153. package/dist-cjs/api-v2/precompile/index.js +7 -0
  154. package/dist-cjs/api-v2/precompile/precompile.js +136 -0
  155. package/dist-cjs/api-v2/presets/index.js +33 -0
  156. package/dist-cjs/api-v2/requestConfig.js +37 -0
  157. package/dist-cjs/api-v2/roles/index.js +41 -0
  158. package/dist-cjs/api-v2/spaces/index.js +16 -0
  159. package/dist-cjs/api-v2/stories/index.js +180 -0
  160. package/dist-cjs/api-v2/stories/types.js +2 -0
  161. package/dist-cjs/api-v2/sync/index.js +115 -0
  162. package/dist-cjs/api-v2/sync/types.js +2 -0
  163. package/dist-cjs/api-v2/test.js +25 -0
  164. package/dist-cjs/config/config.types.js +2 -0
  165. package/dist-cjs/config/constants.js +29 -0
  166. package/dist-cjs/package.json +3 -0
  167. package/dist-cjs/utils/array-utils.js +24 -0
  168. package/dist-cjs/utils/logger.js +32 -0
  169. package/dist-cjs/utils/object-utils.js +77 -0
  170. package/dist-cjs/utils/path-utils.js +115 -0
  171. package/package.json +43 -27
  172. package/dist/utils/pkg-require.d.ts +0 -2
  173. package/dist/utils/pkg-require.js +0 -4
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.getComponentPresets = void 0;
7
+ const logger_js_1 = __importDefault(require("../../utils/logger.js"));
8
+ const components_js_1 = require("../components/components.js");
9
+ const presets_js_1 = require("./presets.js");
10
+ const getComponentPresets = (componentName, config) => {
11
+ logger_js_1.default.log(`Trying to get all '${componentName}' presets.`);
12
+ return (0, components_js_1.getAllComponents)(config).then(async (res) => {
13
+ const componentPresets = res.filter((component) => component.name === componentName);
14
+ if (componentPresets.length > 0) {
15
+ if (componentPresets[0].all_presets.length === 0) {
16
+ logger_js_1.default.warning(`There is no presets for: '${componentName}' component`);
17
+ return false;
18
+ }
19
+ return Promise.all(componentPresets[0].all_presets.map((preset) => (0, presets_js_1.getPreset)({ presetId: preset.id }, config).catch((err) => logger_js_1.default.error(err))));
20
+ }
21
+ logger_js_1.default.warning(`There is no '${componentName}' component`);
22
+ return false;
23
+ });
24
+ };
25
+ exports.getComponentPresets = getComponentPresets;
@@ -0,0 +1,92 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.updatePresets = exports.updatePreset = exports.createPreset = exports.getAllPresets = exports.getPreset = void 0;
7
+ const logger_js_1 = __importDefault(require("../../utils/logger.js"));
8
+ const request_js_1 = require("../utils/request.js");
9
+ // GET
10
+ const getPreset = (args, config) => {
11
+ const { presetId } = args;
12
+ const { spaceId, sbApi } = config;
13
+ logger_js_1.default.log(`Trying to get preset by id: ${presetId}`);
14
+ return sbApi
15
+ .get(`spaces/${spaceId}/presets/${presetId}`)
16
+ .then((response) => response.data)
17
+ .then((response) => {
18
+ if (Array.isArray(response.presets)) {
19
+ logger_js_1.default.warning(`There is no preset for '${presetId}' preset id`);
20
+ return false;
21
+ }
22
+ return response;
23
+ })
24
+ .catch((err) => logger_js_1.default.error(err));
25
+ };
26
+ exports.getPreset = getPreset;
27
+ const getAllPresets = (config) => {
28
+ const { spaceId, sbApi } = config;
29
+ logger_js_1.default.log("Trying to get all Presets.");
30
+ // TODO: All Presets doesnt support pagination...
31
+ // https://github.com/storyblok/storyblok-js-client/issues/535
32
+ return (0, request_js_1.getAllItemsWithPagination)({
33
+ apiFn: ({ per_page, page, spaceId }) => sbApi
34
+ .get(`spaces/${spaceId}/presets/`, { per_page, page })
35
+ .then((res) => {
36
+ logger_js_1.default.log(`Amount of presets: ${res.total}`);
37
+ return res;
38
+ })
39
+ .catch((err) => logger_js_1.default.error(err)),
40
+ params: {
41
+ spaceId,
42
+ },
43
+ itemsKey: "presets",
44
+ });
45
+ };
46
+ exports.getAllPresets = getAllPresets;
47
+ // CREATE
48
+ const createPreset = (p, config) => {
49
+ const { spaceId, sbApi } = config;
50
+ return sbApi
51
+ .post(`spaces/${spaceId}/presets/`, {
52
+ preset: p.preset,
53
+ })
54
+ .then((res) => {
55
+ logger_js_1.default.warning(`Preset: '${p.preset.name}' has been created.`);
56
+ return res;
57
+ })
58
+ .catch((e) => {
59
+ console.log("!!!!!!!!!!");
60
+ console.log(e);
61
+ logger_js_1.default.error(`Error happened. Preset: '${p.preset.name}' has been not created.`);
62
+ });
63
+ };
64
+ exports.createPreset = createPreset;
65
+ // UPDATE
66
+ const updatePreset = (args, config) => {
67
+ const { p } = args;
68
+ const { spaceId, sbApi } = config;
69
+ return sbApi
70
+ .put(`spaces/${spaceId}/presets/${p.preset.id}`, {
71
+ preset: p.preset,
72
+ })
73
+ .then((res) => {
74
+ logger_js_1.default.warning(`Preset: '${p.preset.name}' with '${p.preset.id}' id has been updated.`);
75
+ return res;
76
+ })
77
+ .catch(() => {
78
+ logger_js_1.default.error(`Error happened. Preset: '${p.preset.name}' with '${p.preset.id}' id has been not updated.`);
79
+ });
80
+ };
81
+ exports.updatePreset = updatePreset;
82
+ const updatePresets = (args, config) => {
83
+ const { presets, spaceId } = args;
84
+ return Promise.allSettled(presets.map(async (item) => {
85
+ return (0, exports.updatePreset)({
86
+ p: {
87
+ preset: item,
88
+ },
89
+ }, { ...config, spaceId });
90
+ }));
91
+ };
92
+ exports.updatePresets = updatePresets;
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,49 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const logger_js_1 = __importDefault(require("../../utils/logger.js"));
7
+ const componentPresets_js_1 = require("./componentPresets.js");
8
+ const presets_js_1 = require("./presets.js");
9
+ const _resolvePresets = async (res, all_presets, component, config) => {
10
+ const componentId = res.data.component.id;
11
+ if (all_presets && all_presets.length > 0) {
12
+ const all_presets_modified = all_presets.map((p) => {
13
+ return { preset: { ...p.preset, component_id: componentId } };
14
+ });
15
+ logger_js_1.default.log(`Checking preset for '${component.name}' component`);
16
+ const allRemoteComponentPresets = await (0, componentPresets_js_1.getComponentPresets)(component.name, config);
17
+ const presetsToUpdate = [];
18
+ const presetsToCreate = [];
19
+ for (const componentPreset of all_presets_modified) {
20
+ const shouldBeUpdated = allRemoteComponentPresets &&
21
+ allRemoteComponentPresets.find((remotePreset) => componentPreset.preset.name ===
22
+ remotePreset.preset.name);
23
+ if (shouldBeUpdated) {
24
+ presetsToUpdate.push({
25
+ ...componentPreset,
26
+ preset: {
27
+ id: shouldBeUpdated.preset.id,
28
+ ...componentPreset.preset,
29
+ },
30
+ });
31
+ }
32
+ else {
33
+ presetsToCreate.push(componentPreset);
34
+ }
35
+ }
36
+ const presetsToUpdateResult = await Promise.all(presetsToUpdate.map((preset) => {
37
+ return (0, presets_js_1.updatePreset)({ p: preset }, config);
38
+ }));
39
+ const presetsToCreateResult = await Promise.all(presetsToCreate.map((preset) => {
40
+ return (0, presets_js_1.createPreset)(preset, config);
41
+ }));
42
+ return [...presetsToCreateResult, presetsToUpdateResult];
43
+ }
44
+ else {
45
+ logger_js_1.default.warning("There are no presets for this component.");
46
+ return [];
47
+ }
48
+ };
49
+ exports.default = _resolvePresets;
@@ -0,0 +1,131 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.syncRolesData = exports.getRole = exports.getAllRoles = exports.updateRole = exports.createRole = void 0;
7
+ const logger_js_1 = __importDefault(require("../../utils/logger.js"));
8
+ const request_js_1 = require("../utils/request.js");
9
+ // POST
10
+ const createRole = (role, config) => {
11
+ const { sbApi, spaceId } = config;
12
+ return sbApi
13
+ .post(`spaces/${spaceId}/space_roles/`, {
14
+ space_role: role,
15
+ })
16
+ .then(() => {
17
+ logger_js_1.default.success(`Role '${role.role}' has been created.`);
18
+ })
19
+ .catch((err) => {
20
+ logger_js_1.default.error("error happened... :(");
21
+ console.log(`${err.message} in migration of ${role.role} in createRole function`);
22
+ throw err;
23
+ });
24
+ };
25
+ exports.createRole = createRole;
26
+ // PUT
27
+ const updateRole = (role, config) => {
28
+ const { sbApi, spaceId } = config;
29
+ return sbApi
30
+ .put(`spaces/${spaceId}/space_roles/${role.id}`, {
31
+ space_role: role,
32
+ })
33
+ .then(() => {
34
+ logger_js_1.default.success(`Role '${role.role}' has been updated.`);
35
+ })
36
+ .catch((err) => {
37
+ logger_js_1.default.error("error happened... :(");
38
+ console.log(`${err.message} in migration of ${role.role} in updateRole function`);
39
+ throw err;
40
+ });
41
+ };
42
+ exports.updateRole = updateRole;
43
+ // GET
44
+ const getAllRoles = async (config) => {
45
+ const { sbApi, spaceId } = config;
46
+ logger_js_1.default.log("Trying to get all roles.");
47
+ // TODO: All Roles doesnt support pagination...
48
+ // https://github.com/storyblok/storyblok-js-client/issues/535
49
+ return (0, request_js_1.getAllItemsWithPagination)({
50
+ apiFn: ({ per_page, page }) => sbApi
51
+ .get(`spaces/${spaceId}/space_roles/`, { per_page, page })
52
+ .then((res) => {
53
+ logger_js_1.default.log(`Amount of roles: ${res.total}`);
54
+ return res;
55
+ })
56
+ .catch((err) => {
57
+ if (err.response.status === 404) {
58
+ logger_js_1.default.error(`There is no roles in your Storyblok ${spaceId} space.`);
59
+ return true;
60
+ }
61
+ else {
62
+ logger_js_1.default.error(err);
63
+ return false;
64
+ }
65
+ }),
66
+ params: {
67
+ spaceId,
68
+ },
69
+ itemsKey: "space_roles",
70
+ });
71
+ };
72
+ exports.getAllRoles = getAllRoles;
73
+ // GET
74
+ const getRole = async (roleName, config) => {
75
+ logger_js_1.default.log(`Trying to get '${roleName}' role.`);
76
+ return (0, exports.getAllRoles)(config)
77
+ .then((res) => res.filter((role) => role.role === roleName))
78
+ .then((res) => {
79
+ if (Array.isArray(res) && res.length === 0) {
80
+ logger_js_1.default.warning(`There is no role named '${roleName}'`);
81
+ return false;
82
+ }
83
+ return res;
84
+ })
85
+ .catch((err) => logger_js_1.default.error(err));
86
+ };
87
+ exports.getRole = getRole;
88
+ const syncRolesData = async ({ roles }, config) => {
89
+ const result = {
90
+ created: [],
91
+ updated: [],
92
+ skipped: [],
93
+ errors: [],
94
+ };
95
+ const space_roles_raw = await (0, exports.getAllRoles)(config);
96
+ const space_roles = Array.isArray(space_roles_raw) ? space_roles_raw : [];
97
+ const rolesToUpdate = [];
98
+ const rolesToCreate = [];
99
+ for (const role of roles) {
100
+ if (!role || typeof role !== "object" || !("role" in role)) {
101
+ result.skipped.push(String(role?.role ?? "unknown"));
102
+ continue;
103
+ }
104
+ const shouldBeUpdated = space_roles.find((remoteRole) => role.role === remoteRole.role);
105
+ if (shouldBeUpdated) {
106
+ rolesToUpdate.push({ id: shouldBeUpdated.id, ...role });
107
+ }
108
+ else {
109
+ rolesToCreate.push(role);
110
+ }
111
+ }
112
+ const updateResults = await Promise.allSettled(rolesToUpdate.map((role) => (0, exports.updateRole)(role, config)));
113
+ updateResults.forEach((r, idx) => {
114
+ const name = String(rolesToUpdate[idx]?.role ?? "unknown");
115
+ if (r.status === "fulfilled")
116
+ result.updated.push(name);
117
+ else
118
+ result.errors.push({ name, message: String(r.reason) });
119
+ });
120
+ const createResults = await Promise.allSettled(rolesToCreate.map((role) => (0, exports.createRole)(role, config)));
121
+ createResults.forEach((r, idx) => {
122
+ const name = String(rolesToCreate[idx]?.role ?? "unknown");
123
+ if (r.status === "fulfilled")
124
+ result.created.push(name);
125
+ else
126
+ result.errors.push({ name, message: String(r.reason) });
127
+ });
128
+ return result;
129
+ };
130
+ exports.syncRolesData = syncRolesData;
131
+ // File-based sync wrapper lives in `roles.sync.ts` to keep this module CJS-safe.
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.updateSpace = exports.getAllSpaces = exports.getSpace = void 0;
4
+ const getSpace = (args, config) => {
5
+ const { sbApi } = config;
6
+ const { spaceId } = args;
7
+ return sbApi
8
+ .get(`spaces/${spaceId}`)
9
+ .then((res) => res.data)
10
+ .catch((err) => console.error(err));
11
+ };
12
+ exports.getSpace = getSpace;
13
+ const getAllSpaces = (config) => {
14
+ const { sbApi } = config;
15
+ return sbApi
16
+ .get(`spaces/`)
17
+ .then((res) => res.data.spaces)
18
+ .catch((err) => {
19
+ console.error(err);
20
+ return [];
21
+ });
22
+ };
23
+ exports.getAllSpaces = getAllSpaces;
24
+ const updateSpace = (args, config) => {
25
+ const { sbApi } = config;
26
+ const { spaceId, params } = args;
27
+ return sbApi
28
+ .put(`spaces/${spaceId}`, {
29
+ ...params,
30
+ })
31
+ .then((res) => res.data)
32
+ .catch((err) => console.error(err));
33
+ };
34
+ exports.updateSpace = updateSpace;
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,214 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.deepUpsertStory = exports.upsertStory = exports.updateStories = exports.updateStory = exports.createStory = exports.getStoryBySlug = exports.getStoryById = exports.getAllStories = exports.removeAllStories = exports.removeStory = void 0;
7
+ const chalk_1 = __importDefault(require("chalk"));
8
+ const logger_js_1 = __importDefault(require("../../utils/logger.js"));
9
+ const object_utils_js_1 = require("../../utils/object-utils.js");
10
+ const request_js_1 = require("../utils/request.js");
11
+ const removeStory = (args, config) => {
12
+ const { storyId } = args;
13
+ const { spaceId, sbApi } = config;
14
+ logger_js_1.default.log(`Removing ${storyId} from ${spaceId}`);
15
+ return sbApi
16
+ .delete(`spaces/${spaceId}/stories/${storyId}`, {})
17
+ .then((res) => {
18
+ logger_js_1.default.success(`Successfully removed: ${res.data.story.name} with ${res.data.story.id} id.`);
19
+ return res;
20
+ })
21
+ .catch(() => {
22
+ logger_js_1.default.error(`Unable to remove: ${storyId}`);
23
+ });
24
+ };
25
+ exports.removeStory = removeStory;
26
+ const removeAllStories = async (config) => {
27
+ const { spaceId } = config;
28
+ logger_js_1.default.warning(`Trying to remove all stories from space with spaceId: ${spaceId}`);
29
+ const stories = await (0, exports.getAllStories)({}, config);
30
+ const onlyRootStories = (story) => story.story.parent_id === 0 || story.story.parent_id === null;
31
+ const allResponses = Promise.all(stories
32
+ .filter(onlyRootStories)
33
+ .map(async (story) => await (0, exports.removeStory)({ storyId: story?.story?.id }, config)));
34
+ return allResponses;
35
+ };
36
+ exports.removeAllStories = removeAllStories;
37
+ // GET
38
+ const getAllStories = async (args, config) => {
39
+ const { options } = args;
40
+ const { spaceId, sbApi } = config;
41
+ logger_js_1.default.log(`Trying to get all Stories from: ${spaceId}`);
42
+ const params = (0, object_utils_js_1.notNullish)({
43
+ with_slug: options?.with_slug,
44
+ starts_with: options?.starts_with,
45
+ language: options?.language,
46
+ });
47
+ console.log("These are params i will use: ");
48
+ console.log(params);
49
+ const allStoriesWithoutContent = await (0, request_js_1.getAllItemsWithPagination)({
50
+ apiFn: ({ per_page, page }) => sbApi.get(`spaces/${spaceId}/stories/`, {
51
+ ...params,
52
+ per_page,
53
+ page,
54
+ }),
55
+ params: {
56
+ spaceId,
57
+ },
58
+ itemsKey: "stories",
59
+ });
60
+ logger_js_1.default.success(`Successfully pre-fetched ${allStoriesWithoutContent.length} stories.`);
61
+ let heartBeat = 0;
62
+ const allStories = await Promise.all(allStoriesWithoutContent.map(async (story) => {
63
+ const result = await (0, exports.getStoryById)(story.id, config);
64
+ heartBeat++;
65
+ if (heartBeat % 10 === 0 ||
66
+ heartBeat === allStoriesWithoutContent.length) {
67
+ logger_js_1.default.success(`Successfully fetched ${heartBeat} stories with full content.`);
68
+ }
69
+ return result;
70
+ }));
71
+ return allStories;
72
+ };
73
+ exports.getAllStories = getAllStories;
74
+ // GET
75
+ const getStoryById = (storyId, config) => {
76
+ const { spaceId, sbApi, debug } = config;
77
+ if (debug) {
78
+ console.log(`Trying to get Story with id: ${storyId} from space: ${spaceId}, to fill content field.`);
79
+ }
80
+ return sbApi
81
+ .get(`spaces/${spaceId}/stories/${storyId}`)
82
+ .then((res) => {
83
+ if (debug) {
84
+ logger_js_1.default.success(`Successfuly fetched story with content, with id: ${storyId} from space: ${spaceId}.`);
85
+ }
86
+ return res.data;
87
+ })
88
+ .catch((err) => logger_js_1.default.error(err));
89
+ };
90
+ exports.getStoryById = getStoryById;
91
+ const getStoryBySlug = async (slug, config) => {
92
+ const { spaceId, sbApi } = config;
93
+ const storiesWithoutContent = await sbApi
94
+ .get(`spaces/${spaceId}/stories/`, {
95
+ per_page: 100,
96
+ // @ts-ignore
97
+ with_slug: slug,
98
+ })
99
+ .then((res) => res.data.stories)
100
+ .catch((err) => console.error(err));
101
+ const storiesWithContent = await Promise.all(storiesWithoutContent.map(async (story) => await (0, exports.getStoryById)(story.id, config)));
102
+ return storiesWithContent[0];
103
+ };
104
+ exports.getStoryBySlug = getStoryBySlug;
105
+ // CREATE
106
+ const createStory = (content, config) => {
107
+ const { spaceId, sbApi } = config;
108
+ logger_js_1.default.log(`Creating story with name: ${content.name} in space: ${spaceId}`);
109
+ return sbApi
110
+ .post(`spaces/${spaceId}/stories/`, {
111
+ story: content,
112
+ publish: 1,
113
+ })
114
+ .then((res) => res.data)
115
+ .catch((err) => console.error(err));
116
+ };
117
+ exports.createStory = createStory;
118
+ // UPDATE
119
+ const updateStory = (content, storyId, options, config) => {
120
+ const { spaceId, sbApi } = config;
121
+ logger_js_1.default.warning("Trying to update Story...");
122
+ logger_js_1.default.log(`Updating story with name: ${content.name} in space: ${spaceId}`);
123
+ // console.log("THis is content to update: ");
124
+ // console.log(JSON.stringify(content, null, 2));
125
+ return sbApi
126
+ .put(`spaces/${spaceId}/stories/${storyId}`, {
127
+ story: content,
128
+ publish: options.publish ? 1 : 0,
129
+ force_update: options.force_update ? 1 : 0,
130
+ })
131
+ .then((res) => {
132
+ console.log(`${chalk_1.default.green(res.data.story.full_slug)} updated.`);
133
+ return res.data;
134
+ })
135
+ .catch((err) => console.error(err));
136
+ };
137
+ exports.updateStory = updateStory;
138
+ const updateStories = (args, config) => {
139
+ const { stories, options, spaceId } = args;
140
+ return Promise.allSettled(
141
+ // Run through stories, and update the space with migrated version of stories
142
+ stories.map(async (stories) => {
143
+ return (0, exports.updateStory)(stories.story, stories.story.id, { publish: options.publish }, { ...config, spaceId });
144
+ }));
145
+ };
146
+ exports.updateStories = updateStories;
147
+ const upsertStory = async (args, config) => {
148
+ console.log("Modifying story... in space with id:");
149
+ console.log(config.spaceId);
150
+ const { storyId, storySlug, content } = args;
151
+ console.log("This are args passed: ");
152
+ console.log(args);
153
+ if (storyId) {
154
+ // if this exist than we update story with this id
155
+ console.log("You've selected storyid!");
156
+ }
157
+ else if (storySlug) {
158
+ // if this exist than we update story with this slug (probably when we try to add story from one space to another,
159
+ console.log("You've selected slug!");
160
+ const foundStory = await (0, exports.getStoryBySlug)(storySlug, config);
161
+ console.log("This is story");
162
+ console.log(foundStory);
163
+ if (foundStory) {
164
+ // then update the story
165
+ }
166
+ else {
167
+ const { story: { parent_id, id, parent, ...rest }, } = content;
168
+ console.log("We are going to create story");
169
+ const response = await (0, exports.createStory)(rest, config);
170
+ console.log("This is response");
171
+ console.log(response);
172
+ }
173
+ }
174
+ else {
175
+ // if this exist than we create new story
176
+ console.log("Nothing passed, creating story...");
177
+ }
178
+ };
179
+ exports.upsertStory = upsertStory;
180
+ const deepUpsertStory = async (args, config) => {
181
+ console.log("Modifying story... in space with id:");
182
+ console.log(config.spaceId);
183
+ const { storyId, storySlug, content } = args;
184
+ console.log("This are args passed: ");
185
+ console.log(args);
186
+ if (storyId) {
187
+ // if this exist than we update story with this id
188
+ console.log("You've selected storyid!");
189
+ }
190
+ else if (storySlug) {
191
+ // if this exist than we update story with this slug (probably when we try to add story from one space to another,
192
+ console.log("You've selected slug!");
193
+ const slugs = storySlug.split("/");
194
+ console.log("Slugs for which we need to check for existence of stories");
195
+ console.log(slugs);
196
+ // const foundStory = await managementApi.stories.getStoryBySlug(storySlug, config)
197
+ // console.log("This is story")
198
+ // console.log(foundStory)
199
+ // if(foundStory) {
200
+ // // then update the story
201
+ // } else {
202
+ // const {story: {parent_id, id, parent,...rest}} = content
203
+ // console.log("We are going to create story")
204
+ // const response = await managementApi.stories.createStory(rest, config)
205
+ // console.log("This is response")
206
+ // console.log(response)
207
+ // }
208
+ }
209
+ else {
210
+ // if this exist than we create new story
211
+ console.log("Nothing passed, creating story...");
212
+ }
213
+ };
214
+ exports.deepUpsertStory = deepUpsertStory;
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,48 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.getAllItemsWithPagination = void 0;
7
+ const logger_js_1 = __importDefault(require("../../utils/logger.js"));
8
+ const getAllItemsWithPagination = async ({ apiFn, params, itemsKey, }) => {
9
+ const per_page = 100;
10
+ const allItems = [];
11
+ let page = 1;
12
+ let totalPages;
13
+ let amountOfFetchedItems = 0;
14
+ do {
15
+ const response = await apiFn({ per_page, page, ...params });
16
+ // Handle case where API call failed and returned undefined
17
+ if (!response || !response.data) {
18
+ logger_js_1.default.warning(`API returned no data for ${itemsKey}`);
19
+ return allItems;
20
+ }
21
+ if (!totalPages) {
22
+ totalPages =
23
+ Math.ceil((response.total ?? 0) / (response.perPage ?? per_page)) || 1;
24
+ }
25
+ /**
26
+ *
27
+ * Not every endpoint in storyblok give us pagination...
28
+ * so only for this who paginate we want to calculate values to show
29
+ *
30
+ * */
31
+ if (response.total) {
32
+ amountOfFetchedItems +=
33
+ response.total - amountOfFetchedItems > per_page
34
+ ? per_page
35
+ : response.total - amountOfFetchedItems;
36
+ if (amountOfFetchedItems && !Number.isNaN(amountOfFetchedItems)) {
37
+ logger_js_1.default.success(`${amountOfFetchedItems} of ${response.total} items fetched.`);
38
+ }
39
+ }
40
+ const items = response.data?.[itemsKey];
41
+ if (Array.isArray(items)) {
42
+ allItems.push(...items);
43
+ }
44
+ page++;
45
+ } while (page <= totalPages);
46
+ return allItems;
47
+ };
48
+ exports.getAllItemsWithPagination = getAllItemsWithPagination;
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getAllAssets = getAllAssets;
4
+ exports.getAssetById = getAssetById;
5
+ exports.getAssetByName = getAssetByName;
6
+ async function getAllAssets(client, args) {
7
+ const { spaceId, search } = args;
8
+ return client.sbApi
9
+ .get(`spaces/${spaceId}/assets/`, {
10
+ // @ts-ignore storyblok-js-client mismatch: documentation uses `search`
11
+ search: search ?? "",
12
+ per_page: 100,
13
+ })
14
+ .then(({ data }) => data);
15
+ }
16
+ async function getAssetById(client, args) {
17
+ const { spaceId, assetId } = args;
18
+ return client.sbApi
19
+ .get(`spaces/${spaceId}/assets/${assetId}`)
20
+ .then(({ data }) => data);
21
+ }
22
+ async function getAssetByName(client, args) {
23
+ const result = await getAllAssets(client, {
24
+ spaceId: args.spaceId,
25
+ search: args.fileName,
26
+ });
27
+ if (result?.assets?.length === 1)
28
+ return result.assets[0];
29
+ return undefined;
30
+ }
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getCurrentUser = getCurrentUser;
4
+ exports.hasAccessToSpace = hasAccessToSpace;
5
+ const auth_js_1 = require("../../api/auth/auth.js");
6
+ const requestConfig_js_1 = require("../requestConfig.js");
7
+ async function getCurrentUser(client) {
8
+ return await (0, auth_js_1.getCurrentUser)((0, requestConfig_js_1.toRequestConfig)(client));
9
+ }
10
+ async function hasAccessToSpace(client, spaceId) {
11
+ return await (0, auth_js_1.hasAccessToSpace)({ spaceId }, (0, requestConfig_js_1.toRequestConfig)(client));
12
+ }