sb-mig 5.6.0-beta.1 → 5.6.0-beta.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/README.md +20 -19
- package/dist/api/assets/assets.js +1 -14
- package/dist/api/components/components.js +2 -1
- package/dist/api/components/components.sync.d.ts +8 -0
- package/dist/api/components/components.sync.js +193 -0
- package/dist/api/data-migration/component-data-migration.js +2 -2
- package/dist/api/datasources/datasource-entries.js +4 -5
- package/dist/api/datasources/datasources.d.ts +5 -2
- package/dist/api/datasources/datasources.js +42 -35
- package/dist/api/datasources/datasources.sync.d.ts +2 -0
- package/dist/api/datasources/datasources.sync.js +11 -0
- package/dist/api/datasources/datasources.types.d.ts +1 -1
- package/dist/api/datasources/index.d.ts +2 -1
- package/dist/api/datasources/index.js +2 -1
- package/dist/api/managementApi.d.ts +2 -2
- package/dist/api/migrate.d.ts +1 -1
- package/dist/api/migrate.js +3 -48
- package/dist/api/plugins/index.d.ts +2 -1
- package/dist/api/plugins/index.js +2 -1
- package/dist/api/plugins/plugins.d.ts +7 -2
- package/dist/api/plugins/plugins.js +28 -15
- package/dist/api/plugins/plugins.sync.d.ts +2 -0
- package/dist/api/plugins/plugins.sync.js +11 -0
- package/dist/api/roles/index.d.ts +2 -1
- package/dist/api/roles/index.js +2 -1
- package/dist/api/roles/roles.d.ts +5 -2
- package/dist/api/roles/roles.js +34 -11
- package/dist/api/roles/roles.sync.d.ts +2 -0
- package/dist/api/roles/roles.sync.js +6 -0
- package/dist/api/roles/roles.types.d.ts +1 -1
- package/dist/api/stories/stories.js +3 -11
- package/dist/api/sync/sync.types.d.ts +30 -0
- package/dist/api/sync/sync.types.js +1 -0
- package/dist/api/testApi.d.ts +2 -2
- package/dist/api/utils/helper-functions.d.ts +5 -1
- package/dist/api/utils/helper-functions.js +6 -1
- package/dist/api/utils/request.d.ts +1 -1
- package/dist/api/utils/request.js +11 -2
- package/dist/api/utils/resolverTransformations.js +2 -57
- package/dist/api-v2/assets/index.d.ts +13 -0
- package/dist/api-v2/assets/index.js +25 -0
- package/dist/api-v2/auth/index.d.ts +3 -0
- package/dist/api-v2/auth/index.js +8 -0
- package/dist/api-v2/client.d.ts +13 -0
- package/dist/api-v2/client.js +17 -0
- package/dist/api-v2/components/index.d.ts +10 -0
- package/dist/api-v2/components/index.js +29 -0
- package/dist/api-v2/datasources/index.d.ts +8 -0
- package/dist/api-v2/datasources/index.js +58 -0
- package/dist/api-v2/discover/discover.d.ts +36 -0
- package/dist/api-v2/discover/discover.js +281 -0
- package/dist/api-v2/discover/index.d.ts +2 -0
- package/dist/api-v2/discover/index.js +1 -0
- package/dist/api-v2/index.d.ts +19 -0
- package/dist/api-v2/index.js +21 -0
- package/dist/api-v2/plugins/index.d.ts +9 -0
- package/dist/api-v2/plugins/index.js +42 -0
- package/dist/api-v2/precompile/index.d.ts +2 -0
- package/dist/api-v2/precompile/index.js +1 -0
- package/dist/api-v2/precompile/precompile.d.ts +65 -0
- package/dist/api-v2/precompile/precompile.js +127 -0
- package/dist/api-v2/presets/index.d.ts +13 -0
- package/dist/api-v2/presets/index.js +25 -0
- package/dist/api-v2/requestConfig.d.ts +5 -0
- package/dist/api-v2/requestConfig.js +34 -0
- package/dist/api-v2/roles/index.d.ts +5 -0
- package/dist/api-v2/roles/index.js +35 -0
- package/dist/api-v2/spaces/index.d.ts +7 -0
- package/dist/api-v2/spaces/index.js +11 -0
- package/dist/api-v2/stories/index.d.ts +34 -0
- package/dist/api-v2/stories/index.js +172 -0
- package/dist/api-v2/stories/types.d.ts +28 -0
- package/dist/api-v2/stories/types.js +1 -0
- package/dist/api-v2/sync/index.d.ts +24 -0
- package/dist/api-v2/sync/index.js +109 -0
- package/dist/api-v2/sync/types.d.ts +1 -0
- package/dist/api-v2/sync/types.js +1 -0
- package/dist/api-v2/test.d.ts +15 -0
- package/dist/api-v2/test.js +21 -0
- package/dist/cli/commands/backup.js +7 -3
- package/dist/cli/commands/copy.js +2 -2
- package/dist/cli/commands/migrate.js +1 -2
- package/dist/cli/commands/migrations.js +2 -2
- package/dist/cli/commands/remove.js +1 -1
- package/dist/cli/commands/revert.js +2 -2
- package/dist/cli/commands/sync.js +1 -2
- package/dist/cli/index.js +1 -1
- package/dist/cli/utils/cli-utils.d.ts +69 -0
- package/dist/cli/utils/cli-utils.js +100 -0
- package/dist/cli/utils/discover.d.ts +3 -22
- package/dist/cli/utils/discover.js +4 -51
- package/dist/config/config.d.ts +2 -39
- package/dist/config/config.types.d.ts +40 -0
- package/dist/config/config.types.js +1 -0
- package/dist/config/defaultConfig.d.ts +1 -1
- package/dist/config/defaultConfig.js +2 -2
- package/dist/utils/array-utils.d.ts +20 -0
- package/dist/utils/array-utils.js +20 -0
- package/dist/utils/async-utils.d.ts +13 -0
- package/dist/utils/async-utils.js +13 -0
- package/dist/utils/date-utils.d.ts +14 -0
- package/dist/utils/date-utils.js +21 -0
- package/dist/utils/files.d.ts +35 -0
- package/dist/utils/files.js +57 -2
- package/dist/utils/main.d.ts +8 -18
- package/dist/utils/main.js +12 -104
- package/dist/utils/migrations.d.ts +9 -3
- package/dist/utils/object-utils.d.ts +46 -0
- package/dist/utils/object-utils.js +71 -0
- package/dist/utils/others.d.ts +6 -9
- package/dist/utils/others.js +8 -15
- package/dist/utils/path-utils.d.ts +89 -0
- package/dist/utils/path-utils.js +106 -0
- package/dist/utils/pkg.d.ts +16 -2
- package/dist/utils/pkg.js +16 -3
- package/dist/utils/string-utils.d.ts +33 -0
- package/dist/utils/string-utils.js +45 -0
- package/dist/utils/transform-utils.d.ts +62 -0
- package/dist/utils/transform-utils.js +113 -0
- package/dist-cjs/api/auth/auth.js +28 -0
- package/dist-cjs/api/auth/auth.types.js +2 -0
- package/dist-cjs/api/components/components.js +202 -0
- package/dist-cjs/api/components/components.sync.js +199 -0
- package/dist-cjs/api/components/components.types.js +2 -0
- package/dist-cjs/api/datasources/datasource-entries.js +166 -0
- package/dist-cjs/api/datasources/datasources.js +166 -0
- package/dist-cjs/api/datasources/datasources.types.js +2 -0
- package/dist-cjs/api/plugins/plugins.js +132 -0
- package/dist-cjs/api/plugins/plugins.types.js +2 -0
- package/dist-cjs/api/presets/componentPresets.js +25 -0
- package/dist-cjs/api/presets/presets.js +92 -0
- package/dist-cjs/api/presets/presets.types.js +2 -0
- package/dist-cjs/api/presets/resolvePresets.js +49 -0
- package/dist-cjs/api/roles/roles.js +131 -0
- package/dist-cjs/api/roles/roles.types.js +2 -0
- package/dist-cjs/api/spaces/spaces.js +34 -0
- package/dist-cjs/api/spaces/spaces.types.js +2 -0
- package/dist-cjs/api/stories/stories.js +214 -0
- package/dist-cjs/api/stories/stories.types.js +2 -0
- package/dist-cjs/api/sync/sync.types.js +2 -0
- package/dist-cjs/api/utils/request.js +48 -0
- package/dist-cjs/api/utils/resolvers.types.js +2 -0
- package/dist-cjs/api-v2/assets/index.js +30 -0
- package/dist-cjs/api-v2/auth/index.js +12 -0
- package/dist-cjs/api-v2/client.js +23 -0
- package/dist-cjs/api-v2/components/index.js +40 -0
- package/dist-cjs/api-v2/datasources/index.js +64 -0
- package/dist-cjs/api-v2/discover/discover.js +321 -0
- package/dist-cjs/api-v2/discover/index.js +9 -0
- package/dist-cjs/api-v2/index.js +60 -0
- package/dist-cjs/api-v2/plugins/index.js +49 -0
- package/dist-cjs/api-v2/precompile/index.js +7 -0
- package/dist-cjs/api-v2/precompile/precompile.js +136 -0
- package/dist-cjs/api-v2/presets/index.js +33 -0
- package/dist-cjs/api-v2/requestConfig.js +37 -0
- package/dist-cjs/api-v2/roles/index.js +41 -0
- package/dist-cjs/api-v2/spaces/index.js +16 -0
- package/dist-cjs/api-v2/stories/index.js +180 -0
- package/dist-cjs/api-v2/stories/types.js +2 -0
- package/dist-cjs/api-v2/sync/index.js +115 -0
- package/dist-cjs/api-v2/sync/types.js +2 -0
- package/dist-cjs/api-v2/test.js +25 -0
- package/dist-cjs/config/config.types.js +2 -0
- package/dist-cjs/config/constants.js +29 -0
- package/dist-cjs/package.json +3 -0
- package/dist-cjs/utils/array-utils.js +24 -0
- package/dist-cjs/utils/logger.js +32 -0
- package/dist-cjs/utils/object-utils.js +77 -0
- package/dist-cjs/utils/path-utils.js +115 -0
- package/package.json +37 -20
- package/dist/utils/pkg-require.d.ts +0 -2
- package/dist/utils/pkg-require.js +0 -4
|
@@ -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,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,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,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,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
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
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.createClient = createClient;
|
|
7
|
+
const storyblok_js_client_1 = __importDefault(require("storyblok-js-client"));
|
|
8
|
+
function createClient(options) {
|
|
9
|
+
const sbApi = new storyblok_js_client_1.default({
|
|
10
|
+
oauthToken: options.oauthToken,
|
|
11
|
+
accessToken: options.accessToken,
|
|
12
|
+
rateLimit: options.rateLimit ?? 3,
|
|
13
|
+
cache: {
|
|
14
|
+
clear: "auto",
|
|
15
|
+
type: "none",
|
|
16
|
+
},
|
|
17
|
+
}, "https://mapi.storyblok.com/v1");
|
|
18
|
+
return {
|
|
19
|
+
config: options,
|
|
20
|
+
sbApi,
|
|
21
|
+
spaceId: options.spaceId,
|
|
22
|
+
};
|
|
23
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getAllComponents = getAllComponents;
|
|
4
|
+
exports.getComponent = getComponent;
|
|
5
|
+
exports.getAllComponentsGroups = getAllComponentsGroups;
|
|
6
|
+
exports.getComponentsGroup = getComponentsGroup;
|
|
7
|
+
exports.createComponentsGroup = createComponentsGroup;
|
|
8
|
+
exports.removeComponentGroup = removeComponentGroup;
|
|
9
|
+
exports.removeComponent = removeComponent;
|
|
10
|
+
exports.createComponent = createComponent;
|
|
11
|
+
exports.updateComponent = updateComponent;
|
|
12
|
+
const components_js_1 = require("../../api/components/components.js");
|
|
13
|
+
const requestConfig_js_1 = require("../requestConfig.js");
|
|
14
|
+
async function getAllComponents(client) {
|
|
15
|
+
return await (0, components_js_1.getAllComponents)((0, requestConfig_js_1.toRequestConfig)(client));
|
|
16
|
+
}
|
|
17
|
+
async function getComponent(client, componentName) {
|
|
18
|
+
return await (0, components_js_1.getComponent)(componentName, (0, requestConfig_js_1.toRequestConfig)(client));
|
|
19
|
+
}
|
|
20
|
+
async function getAllComponentsGroups(client) {
|
|
21
|
+
return await (0, components_js_1.getAllComponentsGroups)((0, requestConfig_js_1.toRequestConfig)(client));
|
|
22
|
+
}
|
|
23
|
+
async function getComponentsGroup(client, groupName) {
|
|
24
|
+
return await (0, components_js_1.getComponentsGroup)(groupName, (0, requestConfig_js_1.toRequestConfig)(client));
|
|
25
|
+
}
|
|
26
|
+
async function createComponentsGroup(client, groupName) {
|
|
27
|
+
return await (0, components_js_1.createComponentsGroup)(groupName, (0, requestConfig_js_1.toRequestConfig)(client));
|
|
28
|
+
}
|
|
29
|
+
async function removeComponentGroup(client, componentGroup) {
|
|
30
|
+
return await (0, components_js_1.removeComponentGroup)(componentGroup, (0, requestConfig_js_1.toRequestConfig)(client));
|
|
31
|
+
}
|
|
32
|
+
async function removeComponent(client, component) {
|
|
33
|
+
return await (0, components_js_1.removeComponent)(component, (0, requestConfig_js_1.toRequestConfig)(client));
|
|
34
|
+
}
|
|
35
|
+
async function createComponent(client, component, presets = false) {
|
|
36
|
+
return await (0, components_js_1.createComponent)(component, presets, (0, requestConfig_js_1.toRequestConfig)(client));
|
|
37
|
+
}
|
|
38
|
+
async function updateComponent(client, component, presets = false) {
|
|
39
|
+
return await (0, components_js_1.updateComponent)(component, presets, (0, requestConfig_js_1.toRequestConfig)(client));
|
|
40
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getAllDatasources = getAllDatasources;
|
|
4
|
+
exports.getDatasource = getDatasource;
|
|
5
|
+
exports.createDatasource = createDatasource;
|
|
6
|
+
exports.updateDatasource = updateDatasource;
|
|
7
|
+
const request_js_1 = require("../../api/utils/request.js");
|
|
8
|
+
async function getAllDatasources(client) {
|
|
9
|
+
const spaceId = client.spaceId;
|
|
10
|
+
return (0, request_js_1.getAllItemsWithPagination)({
|
|
11
|
+
apiFn: ({ per_page, page }) => client.sbApi.get(`spaces/${spaceId}/datasources/`, {
|
|
12
|
+
per_page,
|
|
13
|
+
page,
|
|
14
|
+
}),
|
|
15
|
+
params: { spaceId },
|
|
16
|
+
itemsKey: "datasources",
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
async function getDatasource(client, datasourceName) {
|
|
20
|
+
const datasources = await getAllDatasources(client);
|
|
21
|
+
const match = datasources.filter((d) => d.name === datasourceName);
|
|
22
|
+
if (Array.isArray(match) && match.length === 0)
|
|
23
|
+
return false;
|
|
24
|
+
return match;
|
|
25
|
+
}
|
|
26
|
+
async function createDatasource(client, datasource) {
|
|
27
|
+
const spaceId = client.spaceId;
|
|
28
|
+
const finalDatasource = {
|
|
29
|
+
name: datasource.name,
|
|
30
|
+
slug: datasource.slug,
|
|
31
|
+
dimensions: [...(datasource.dimensions ?? [])],
|
|
32
|
+
dimensions_attributes: [...(datasource.dimensions ?? [])],
|
|
33
|
+
};
|
|
34
|
+
return client.sbApi
|
|
35
|
+
.post(`spaces/${spaceId}/datasources/`, {
|
|
36
|
+
datasource: finalDatasource,
|
|
37
|
+
})
|
|
38
|
+
.then((res) => res.data);
|
|
39
|
+
}
|
|
40
|
+
async function updateDatasource(client, args) {
|
|
41
|
+
const spaceId = client.spaceId;
|
|
42
|
+
const { datasource, datasourceToBeUpdated } = args;
|
|
43
|
+
const dimensionsToCreate = (datasource.dimensions ?? []).filter((dimension) => {
|
|
44
|
+
const isDimensionInRemoteDatasource = datasourceToBeUpdated.dimensions?.find((d) => dimension.name === d.name);
|
|
45
|
+
return !isDimensionInRemoteDatasource;
|
|
46
|
+
});
|
|
47
|
+
return client.sbApi
|
|
48
|
+
.put(`spaces/${spaceId}/datasources/${datasourceToBeUpdated.id}`, {
|
|
49
|
+
datasource: {
|
|
50
|
+
id: datasourceToBeUpdated.id,
|
|
51
|
+
name: datasource.name,
|
|
52
|
+
slug: datasource.slug,
|
|
53
|
+
dimensions: [
|
|
54
|
+
...(datasourceToBeUpdated.dimensions ?? []),
|
|
55
|
+
...dimensionsToCreate,
|
|
56
|
+
],
|
|
57
|
+
dimensions_attributes: [
|
|
58
|
+
...(datasourceToBeUpdated.dimensions ?? []),
|
|
59
|
+
...dimensionsToCreate,
|
|
60
|
+
],
|
|
61
|
+
},
|
|
62
|
+
})
|
|
63
|
+
.then((res) => res.data);
|
|
64
|
+
}
|