sb-mig 4.1.0-beta.9 → 5.0.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 (156) hide show
  1. package/README.md +15 -0
  2. package/dist/api/assets/assets.d.ts +6 -0
  3. package/dist/api/assets/assets.js +166 -0
  4. package/dist/api/assets/assets.types.d.ts +65 -0
  5. package/dist/api/assets/assets.types.js +1 -0
  6. package/dist/api/assets/index.d.ts +1 -0
  7. package/dist/api/assets/index.js +1 -0
  8. package/dist/api/auth/auth.d.ts +3 -0
  9. package/dist/api/auth/auth.js +23 -0
  10. package/dist/api/auth/auth.types.d.ts +43 -0
  11. package/dist/api/auth/auth.types.js +1 -0
  12. package/dist/api/auth/index.d.ts +1 -0
  13. package/dist/api/auth/index.js +1 -0
  14. package/dist/api/components/components.d.ts +10 -0
  15. package/dist/api/components/components.js +170 -0
  16. package/dist/api/components/components.types.d.ts +13 -0
  17. package/dist/api/components/components.types.js +1 -0
  18. package/dist/api/components/index.d.ts +1 -0
  19. package/dist/api/components/index.js +1 -0
  20. package/dist/api/contentHubApi.d.ts +9 -0
  21. package/dist/api/contentHubApi.js +29 -0
  22. package/dist/api/data-migration/component-data-migration.d.ts +16 -0
  23. package/dist/api/data-migration/component-data-migration.js +244 -0
  24. package/dist/api/datasources/datasource-entries.d.ts +5 -0
  25. package/dist/api/datasources/datasource-entries.js +143 -0
  26. package/dist/api/datasources/datasources.d.ts +8 -0
  27. package/dist/api/datasources/datasources.js +179 -0
  28. package/dist/api/datasources/datasources.types.d.ts +46 -0
  29. package/dist/api/datasources/datasources.types.js +1 -0
  30. package/dist/api/datasources/index.d.ts +1 -0
  31. package/dist/api/datasources/index.js +1 -0
  32. package/dist/api/deliveryApi.d.ts +1 -0
  33. package/dist/api/deliveryApi.js +1 -0
  34. package/dist/api/managementApi.d.ts +73 -0
  35. package/dist/api/managementApi.js +20 -0
  36. package/dist/api/migrate.d.ts +10 -32
  37. package/dist/api/migrate.js +172 -107
  38. package/dist/api/migrate.types.d.ts +25 -0
  39. package/dist/api/migrate.types.js +1 -0
  40. package/dist/api/plugins/index.d.ts +1 -0
  41. package/dist/api/plugins/index.js +1 -0
  42. package/dist/api/plugins/plugins.d.ts +8 -0
  43. package/dist/api/plugins/plugins.js +107 -0
  44. package/dist/api/plugins/plugins.types.d.ts +19 -0
  45. package/dist/api/plugins/plugins.types.js +1 -0
  46. package/dist/api/presets/componentPresets.d.ts +2 -0
  47. package/dist/api/{componentPresets.js → presets/componentPresets.js} +8 -6
  48. package/dist/api/presets/index.d.ts +2 -0
  49. package/dist/api/presets/index.js +2 -0
  50. package/dist/api/presets/presets.d.ts +5 -0
  51. package/dist/api/presets/presets.helper.d.ts +1 -0
  52. package/dist/api/presets/presets.helper.js +7 -0
  53. package/dist/api/presets/presets.js +69 -0
  54. package/dist/api/presets/resolvePresets.d.ts +3 -0
  55. package/dist/api/{resolvePresets.js → presets/resolvePresets.js} +12 -10
  56. package/dist/api/roles/index.d.ts +1 -0
  57. package/dist/api/roles/index.js +1 -0
  58. package/dist/api/roles/roles.d.ts +8 -0
  59. package/dist/api/{roles.js → roles/roles.js} +48 -35
  60. package/dist/api/roles/roles.types.d.ts +13 -0
  61. package/dist/api/roles/roles.types.js +1 -0
  62. package/dist/api/spaces/index.d.ts +1 -0
  63. package/dist/api/spaces/index.js +1 -0
  64. package/dist/api/spaces/spaces.d.ts +4 -0
  65. package/dist/api/spaces/spaces.js +28 -0
  66. package/dist/api/spaces/spaces.types.d.ts +23 -0
  67. package/dist/api/spaces/spaces.types.js +1 -0
  68. package/dist/api/stories/backup.d.ts +2 -0
  69. package/dist/api/stories/backup.js +21 -0
  70. package/dist/api/stories/index.d.ts +2 -0
  71. package/dist/api/stories/index.js +2 -0
  72. package/dist/api/stories/stories.d.ts +10 -0
  73. package/dist/api/stories/stories.js +121 -0
  74. package/dist/api/stories/stories.types.d.ts +24 -0
  75. package/dist/api/stories/stories.types.js +1 -0
  76. package/dist/api/stories/tree.d.ts +3 -0
  77. package/dist/api/stories/tree.js +39 -0
  78. package/dist/api/stories/tree.types.d.ts +14 -0
  79. package/dist/api/stories/tree.types.js +1 -0
  80. package/dist/api/utils/helper-functions.d.ts +1 -0
  81. package/dist/api/utils/helper-functions.js +1 -0
  82. package/dist/api/utils/request.d.ts +15 -0
  83. package/dist/api/utils/request.js +22 -0
  84. package/dist/cli/api-config.d.ts +4 -0
  85. package/dist/cli/api-config.js +18 -0
  86. package/dist/cli/cli-descriptions.d.ts +10 -0
  87. package/dist/cli/cli-descriptions.js +193 -0
  88. package/dist/cli/commands/backup.d.ts +2 -0
  89. package/dist/{commands → cli/commands}/backup.js +148 -53
  90. package/dist/cli/commands/debug.js +34 -0
  91. package/dist/cli/commands/discover.d.ts +2 -0
  92. package/dist/cli/commands/discover.js +41 -0
  93. package/dist/{commands → cli/commands}/init.d.ts +1 -1
  94. package/dist/{commands → cli/commands}/init.js +12 -9
  95. package/dist/cli/commands/migrate.d.ts +2 -0
  96. package/dist/cli/commands/migrate.js +89 -0
  97. package/dist/cli/commands/migrations.d.ts +2 -0
  98. package/dist/cli/commands/migrations.js +78 -0
  99. package/dist/{commands → cli/commands}/remove.d.ts +1 -1
  100. package/dist/{commands → cli/commands}/remove.js +12 -7
  101. package/dist/cli/commands/revert.d.ts +2 -0
  102. package/dist/cli/commands/revert.js +61 -0
  103. package/dist/{commands → cli/commands}/sync.d.ts +1 -1
  104. package/dist/cli/commands/sync.js +220 -0
  105. package/dist/{index.js → cli/index.js} +58 -5
  106. package/dist/cli/sync.types.d.ts +1 -0
  107. package/dist/cli/sync.types.js +1 -0
  108. package/dist/config/config.d.ts +28 -3
  109. package/dist/config/config.js +5 -1
  110. package/dist/config/helper.js +39 -7
  111. package/dist/rollup/build-on-the-fly.d.ts +6 -0
  112. package/dist/rollup/build-on-the-fly.js +42 -0
  113. package/dist/rollup/setup-rollup.d.ts +7 -0
  114. package/dist/rollup/setup-rollup.js +35 -0
  115. package/dist/utils/discover.d.ts +25 -7
  116. package/dist/utils/discover.js +360 -121
  117. package/dist/utils/files.d.ts +19 -5
  118. package/dist/utils/files.js +79 -8
  119. package/dist/utils/interfaces.d.ts +2 -1
  120. package/dist/utils/logger.d.ts +2 -0
  121. package/dist/utils/logger.js +6 -0
  122. package/dist/utils/main.d.ts +5 -0
  123. package/dist/utils/main.js +79 -2
  124. package/dist/utils/migrations.d.ts +5 -0
  125. package/dist/utils/migrations.js +46 -0
  126. package/dist/utils/others.d.ts +3 -0
  127. package/dist/utils/others.js +47 -1
  128. package/package.json +51 -35
  129. package/dist/api/componentPresets.d.ts +0 -1
  130. package/dist/api/components.d.ts +0 -12
  131. package/dist/api/components.js +0 -88
  132. package/dist/api/config.d.ts +0 -2
  133. package/dist/api/config.js +0 -4
  134. package/dist/api/datasources.d.ts +0 -24
  135. package/dist/api/datasources.js +0 -181
  136. package/dist/api/debug.d.ts +0 -1
  137. package/dist/api/debug.js +0 -4
  138. package/dist/api/mutateComponents.d.ts +0 -2
  139. package/dist/api/mutateComponents.js +0 -45
  140. package/dist/api/plugins.d.ts +0 -13
  141. package/dist/api/plugins.js +0 -78
  142. package/dist/api/presets.d.ts +0 -4
  143. package/dist/api/presets.js +0 -52
  144. package/dist/api/resolvePresets.d.ts +0 -2
  145. package/dist/api/roles.d.ts +0 -14
  146. package/dist/api/spaces.d.ts +0 -9
  147. package/dist/api/spaces.js +0 -17
  148. package/dist/api/stories.d.ts +0 -16
  149. package/dist/api/stories.js +0 -37
  150. package/dist/cli-descriptions.d.ts +0 -6
  151. package/dist/cli-descriptions.js +0 -104
  152. package/dist/commands/backup.d.ts +0 -2
  153. package/dist/commands/debug.js +0 -11
  154. package/dist/commands/sync.js +0 -108
  155. /package/dist/{commands → cli/commands}/debug.d.ts +0 -0
  156. /package/dist/{index.d.ts → cli/index.d.ts} +0 -0
package/README.md CHANGED
@@ -10,6 +10,21 @@ If you've found an issue or you have feature request - <a href="https://github.c
10
10
  [![GitHub issues](https://img.shields.io/github/issues/sb-mig/sb-mig.svg?style=flat-square&v=1)](https://github.com/sb-mig/sb-mig/issues?q=is%3Aopen+is%3Aissue)
11
11
  ![npm](https://img.shields.io/npms-io/maintenance-score/sb-mig)
12
12
 
13
+ https://docs.sb-mig.com/
14
+
15
+
16
+ # Requirements:
17
+
18
+ | | |
19
+ | ------------- | ------------- |
20
+ | Node | LTS (18.x.x) |
21
+
22
+
23
+ # Requirements:
24
+
25
+ | | |
26
+ | ------------- | ------------- |
27
+ | Node | LTS (18.x.x) |
13
28
 
14
29
  # 4.x.x version released!
15
30
 
@@ -0,0 +1,6 @@
1
+ import type { GetAllAssets, GetAssetById, GetAssetByName, MigrateAsset } from "./assets.types.js";
2
+ export declare const getAllAssets: GetAllAssets;
3
+ export declare const getAssetByName: GetAssetByName;
4
+ export declare const migrateAsset: MigrateAsset;
5
+ export declare const getAssetById: GetAssetById;
6
+ export declare const getAsset: (assetName: string | undefined) => Promise<void>;
@@ -0,0 +1,166 @@
1
+ import fs from "fs";
2
+ import https from "https";
3
+ import path from "path";
4
+ import FormData from "form-data";
5
+ import storyblokConfig from "../../config/config.js";
6
+ import { createDir, isDirectoryExists } from "../../utils/files.js";
7
+ import Logger from "../../utils/logger.js";
8
+ // GET
9
+ export const getAllAssets = async ({ spaceId, search }, config) => {
10
+ const { sbApi } = config;
11
+ return sbApi
12
+ .get(`spaces/${spaceId}/assets/`, {
13
+ // @ts-ignore TODO: have to submit ISSUE to storyblok-js-client (in documentation it is search, in typescript its search_term STORYBLOK_ISSUE
14
+ search: search ? search : "",
15
+ per_page: 100, // need to do pagination here probably
16
+ })
17
+ .then(({ data }) => data)
18
+ .catch((err) => {
19
+ if (err.response.status === 404) {
20
+ Logger.error(`There is no assets in your Storyblok ${spaceId} space.`);
21
+ return true;
22
+ }
23
+ else {
24
+ Logger.error(err);
25
+ return false;
26
+ }
27
+ });
28
+ };
29
+ export const getAssetByName = async ({ spaceId, fileName }, config) => {
30
+ const result = await getAllAssets({ spaceId, search: fileName }, config);
31
+ if (result.assets.length === 1) {
32
+ return result.assets[0];
33
+ }
34
+ else {
35
+ return undefined;
36
+ }
37
+ };
38
+ const requestSignedUploadUrl = ({ spaceId, payload }, config) => {
39
+ const { sbApi } = config;
40
+ const { filename: _1, asset_folder_id, ext_id, space_id, ...restPayload } = payload;
41
+ const filename = getFileName(payload.filename);
42
+ const size = getSizeFromURL(payload.filename);
43
+ return sbApi
44
+ .post(`spaces/${spaceId}/assets/`, {
45
+ filename,
46
+ size,
47
+ ...restPayload,
48
+ })
49
+ .then((signedResponseObject) => {
50
+ if (storyblokConfig.debug) {
51
+ Logger.log(`Signed upload URL has been requested for ${filename}.`);
52
+ }
53
+ return signedResponseObject.data; // this is very bad... but storyblok-js-client types are pretty broken
54
+ })
55
+ .catch((err) => {
56
+ console.log(err);
57
+ });
58
+ };
59
+ const uploadFile = ({ signedResponseObject, pathToFile }) => {
60
+ const file = pathToFile;
61
+ const form = new FormData();
62
+ // apply all fields from the signed response object to the second request
63
+ for (const key in signedResponseObject.fields) {
64
+ form.append(key, signedResponseObject.fields[key]);
65
+ }
66
+ // also append the file read stream
67
+ form.append("file", fs.createReadStream(file));
68
+ // submit your form
69
+ form.submit(signedResponseObject.post_url, async (err, res) => {
70
+ const statusCode = res.statusCode;
71
+ if (statusCode === 204) {
72
+ Logger.upload(`Asset uploaded ${getFileName(file)}`);
73
+ }
74
+ else {
75
+ if (err)
76
+ throw err;
77
+ }
78
+ });
79
+ };
80
+ const getFileName = (fileUrl) => {
81
+ const fileName = fileUrl.split("/").pop();
82
+ if (fileName) {
83
+ return fileName;
84
+ }
85
+ else {
86
+ throw Error("File name couldn't be extracted from URL.");
87
+ }
88
+ };
89
+ const getSizeFromURL = (fileUrl) => {
90
+ const data = fileUrl.split("/");
91
+ const sizePos = data.length - 3;
92
+ return data[sizePos];
93
+ };
94
+ const downloadAsset = async ({ payload }) => {
95
+ const fileName = getFileName(payload.filename);
96
+ const fileUrl = payload.filename;
97
+ const downloadedAssetsFolder = path.join(storyblokConfig.sbmigWorkingDirectory, "downloadedAssets");
98
+ Logger.log(`Downloading ${fileName} asset ${storyblokConfig.debug
99
+ ? `from ${fileUrl} to ${downloadedAssetsFolder}`
100
+ : ""}`);
101
+ if (!isDirectoryExists(downloadedAssetsFolder)) {
102
+ await createDir(downloadedAssetsFolder);
103
+ }
104
+ return new Promise((resolve, reject) => {
105
+ const file = fs.createWriteStream(path.join(downloadedAssetsFolder, fileName));
106
+ https
107
+ .get(fileUrl, (response) => {
108
+ response.pipe(file);
109
+ file.on("finish", () => {
110
+ file.close();
111
+ Logger.download(`Asset downloaded to ${path.join(downloadedAssetsFolder, fileName)}`);
112
+ resolve(path.join(downloadedAssetsFolder, fileName));
113
+ });
114
+ })
115
+ .on("error", (error) => {
116
+ Logger.error(`Error downloading image: ${error.message}`);
117
+ reject("error");
118
+ });
119
+ });
120
+ };
121
+ export const migrateAsset = async ({ migrateTo, payload, syncDirection }, config) => {
122
+ const pathToFile = await downloadAsset({ payload });
123
+ if (syncDirection === "fromSpaceToSpace") {
124
+ const signedResponseObject = await requestSignedUploadUrl({
125
+ spaceId: migrateTo,
126
+ payload,
127
+ }, config);
128
+ if (pathToFile) {
129
+ await uploadFile({ signedResponseObject, pathToFile });
130
+ }
131
+ }
132
+ return true;
133
+ };
134
+ // GET
135
+ export const getAssetById = async ({ spaceId, assetId }, config) => {
136
+ const { sbApi } = config;
137
+ Logger.log(`Trying to get '${assetId}' asset.`);
138
+ return sbApi
139
+ .get(`spaces/${spaceId}/assets/${assetId}`)
140
+ .then(({ data }) => data)
141
+ .catch((err) => {
142
+ if (err.response.status === 404) {
143
+ Logger.error(`There is no assets in your Storyblok ${spaceId} space.`);
144
+ return true;
145
+ }
146
+ else {
147
+ Logger.error(err);
148
+ return false;
149
+ }
150
+ });
151
+ };
152
+ export const getAsset = async (assetName) => {
153
+ Logger.log(`Trying to get '${assetName}' asset.`);
154
+ // return getAllAssets()
155
+ // .then((res) =>
156
+ // res.space_roles.filter((role: any) => role.role === assetName)
157
+ // )
158
+ // .then((res) => {
159
+ // if (Array.isArray(res) && res.length === 0) {
160
+ // Logger.warning(`There is no role named '${assetName}'`);
161
+ // return false;
162
+ // }
163
+ // return res;
164
+ // })
165
+ // .catch((err) => Logger.error(err));
166
+ };
@@ -0,0 +1,65 @@
1
+ import type { SyncDirection } from "../../cli/sync.types.js";
2
+ import type { RequestBaseConfig } from "../utils/request.js";
3
+ export type AssetTypes = "image/png" | "image/jpg" | `${string}/${string}`;
4
+ export interface SBAsset {
5
+ id: number;
6
+ filename: string;
7
+ space_id: number;
8
+ created_at: string;
9
+ updated_at: string;
10
+ asset_folder_id?: null;
11
+ deleted_at: null;
12
+ content_length: number;
13
+ content_type: AssetTypes;
14
+ alt: string;
15
+ copyright: string;
16
+ title: string;
17
+ focus: string;
18
+ ext_id: any | null;
19
+ expire_at: any | null;
20
+ source: string;
21
+ internal_tag_ids: any[];
22
+ locked: boolean;
23
+ is_private: boolean;
24
+ publish_at: any | null;
25
+ meta_data: any;
26
+ internal_tags_list: any[];
27
+ }
28
+ export interface SBAssetById {
29
+ file: any | null;
30
+ short_file_name: string;
31
+ permanently_deleted: boolean;
32
+ }
33
+ export interface SBAllAssetRequestResult {
34
+ assets: SBAsset[];
35
+ }
36
+ export type SignedResponseObject = any;
37
+ export type AssetPayload = Omit<SBAsset, "updated_at" | "created_at" | "id">;
38
+ export type GetAllAssets = ({ spaceId, }: {
39
+ spaceId: string;
40
+ search?: string;
41
+ }, config: RequestBaseConfig) => Promise<SBAllAssetRequestResult>;
42
+ export type GetAssetByName = ({ spaceId, fileName, }: {
43
+ spaceId: string;
44
+ fileName: string;
45
+ }, config: RequestBaseConfig) => Promise<SBAsset | undefined>;
46
+ export type GetAssetById = ({ spaceId, assetId, }: {
47
+ spaceId: string;
48
+ assetId: number;
49
+ }, config: RequestBaseConfig) => Promise<(SBAsset & SBAssetById) | undefined>;
50
+ export type MigrateAsset = ({ migrateTo, payload, syncDirection, }: {
51
+ migrateTo: string;
52
+ payload: AssetPayload;
53
+ syncDirection: SyncDirection;
54
+ }, config: RequestBaseConfig) => Promise<boolean>;
55
+ export type UploadFile = ({ signedResponseObject, pathToFile, }: {
56
+ signedResponseObject: SignedResponseObject;
57
+ pathToFile: string;
58
+ }) => void;
59
+ export type FinalizeUpload = ({ signedResponseObject, }: {
60
+ signedResponseObject: SignedResponseObject;
61
+ }) => void;
62
+ export type RequestSignedUploadUrl = ({ spaceId, payload, }: {
63
+ spaceId: string;
64
+ payload: AssetPayload;
65
+ }, config: RequestBaseConfig) => Promise<any>;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export { getAllAssets, migrateAsset, getAsset, getAssetById, getAssetByName, } from "./assets.js";
@@ -0,0 +1 @@
1
+ export { getAllAssets, migrateAsset, getAsset, getAssetById, getAssetByName, } from "./assets.js";
@@ -0,0 +1,3 @@
1
+ import type { GetCurrentUser, HasAccessToSpace } from "./auth.types.js";
2
+ export declare const getCurrentUser: GetCurrentUser;
3
+ export declare const hasAccessToSpace: HasAccessToSpace;
@@ -0,0 +1,23 @@
1
+ import { getAllSpaces } from "../spaces/spaces.js";
2
+ export const getCurrentUser = async (config) => {
3
+ const { sbApi } = config;
4
+ console.log("Trying to get current user current OAuthToken");
5
+ const currentUser = await sbApi
6
+ .get(`users/me`, {
7
+ per_page: 100,
8
+ })
9
+ .then((res) => {
10
+ return res.data.user;
11
+ })
12
+ .catch((err) => {
13
+ console.error(err);
14
+ return err;
15
+ });
16
+ return currentUser;
17
+ };
18
+ export const hasAccessToSpace = async (args, config) => {
19
+ const { spaceId } = args;
20
+ const allSpaces = await getAllSpaces(config);
21
+ const hasAccess = allSpaces.find((space) => Number(space.id) === Number(spaceId));
22
+ return !!hasAccess;
23
+ };
@@ -0,0 +1,43 @@
1
+ import type { RequestBaseConfig } from "../utils/request.js";
2
+ import type { ISbResult } from "storyblok-js-client/src/interfaces";
3
+ export interface Org {
4
+ }
5
+ export interface CurrentUserResult extends ISbResult {
6
+ data: {
7
+ user: {
8
+ userid: string;
9
+ email: string;
10
+ organization: string;
11
+ username?: string;
12
+ use_username: boolean;
13
+ alt_email: string;
14
+ firstname: string;
15
+ lastname: string;
16
+ phone?: number;
17
+ id: number;
18
+ login_strategy: "password" | string;
19
+ created_at: string;
20
+ org_role: "member" | string;
21
+ has_org: boolean;
22
+ has_partner: boolean;
23
+ org: Org[];
24
+ timezone: "UTC" | string;
25
+ avatar?: any;
26
+ friendly_name: string;
27
+ notified: any[];
28
+ lang: string;
29
+ partner_role: "partner_owner" | string;
30
+ favourite_spaces: any[];
31
+ role: "sso" | string;
32
+ beta_user: boolean;
33
+ track_statistics: boolean;
34
+ is_editor: boolean;
35
+ sso: boolean;
36
+ job_role?: any;
37
+ };
38
+ };
39
+ }
40
+ export type GetCurrentUser = (config: RequestBaseConfig) => Promise<any>;
41
+ export type HasAccessToSpace = (args: {
42
+ spaceId: string;
43
+ }, config: RequestBaseConfig) => Promise<boolean>;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export { getCurrentUser, hasAccessToSpace } from "./auth.js";
@@ -0,0 +1 @@
1
+ export { getCurrentUser, hasAccessToSpace } from "./auth.js";
@@ -0,0 +1,10 @@
1
+ import type { CreateComponent, CreateComponentsGroup, GetAllComponents, GetAllComponentsGroups, GetComponent, GetComponentsGroup, RemoveComponent, RemoveComponentGroup, UpdateComponent } from "./components.types.js";
2
+ export declare const getAllComponents: GetAllComponents;
3
+ export declare const getComponent: GetComponent;
4
+ export declare const createComponent: CreateComponent;
5
+ export declare const updateComponent: UpdateComponent;
6
+ export declare const removeComponent: RemoveComponent;
7
+ export declare const getAllComponentsGroups: GetAllComponentsGroups;
8
+ export declare const getComponentsGroup: GetComponentsGroup;
9
+ export declare const removeComponentGroup: RemoveComponentGroup;
10
+ export declare const createComponentsGroup: CreateComponentsGroup;
@@ -0,0 +1,170 @@
1
+ import Logger from "../../utils/logger.js";
2
+ import _resolvePresets from "../presets/resolvePresets.js";
3
+ import { getAllItemsWithPagination } from "../utils/request.js";
4
+ /*
5
+ *
6
+ * GET ALL components
7
+ *
8
+ * */
9
+ export const getAllComponents = (config) => {
10
+ const { spaceId, sbApi } = config;
11
+ Logger.log("Trying to get all components.");
12
+ return getAllItemsWithPagination({
13
+ apiFn: ({ per_page, page }) => sbApi
14
+ .get(`spaces/${spaceId}/components/`, { per_page, page })
15
+ .then((res) => {
16
+ Logger.log(`Amount of components: ${res.total}`);
17
+ return res;
18
+ })
19
+ .catch((err) => console.error(err)),
20
+ params: {
21
+ spaceId,
22
+ },
23
+ itemsKey: "components",
24
+ });
25
+ };
26
+ /*
27
+ *
28
+ * GET ONE component
29
+ *
30
+ * */
31
+ export const getComponent = (componentName, config) => {
32
+ Logger.log(`Trying to get '${componentName}' component.`);
33
+ return getAllComponents(config)
34
+ .then((res) => res.filter((component) => component.name === componentName))
35
+ .then((res) => {
36
+ if (Array.isArray(res) && res.length === 0) {
37
+ console.info(`There is no component named '${componentName}'`);
38
+ return false;
39
+ }
40
+ return res;
41
+ })
42
+ .catch((err) => console.error(err));
43
+ };
44
+ // CREATE
45
+ export const createComponent = (component, presets, config) => {
46
+ const { spaceId, sbApi } = config;
47
+ Logger.log(`Trying to create '${component.name}'`);
48
+ const componentWithPresets = component;
49
+ const { all_presets, ...componentWithoutPresets } = componentWithPresets;
50
+ sbApi
51
+ .post(`spaces/${spaceId}/components/`, {
52
+ component: componentWithoutPresets,
53
+ })
54
+ .then((res) => {
55
+ Logger.success(`Component '${component.name}' has been created.`);
56
+ if (presets) {
57
+ _resolvePresets(res, all_presets, component, config);
58
+ }
59
+ })
60
+ .catch((err) => {
61
+ Logger.error(`${err.message} in migration of ${component.name} in createComponent function`);
62
+ });
63
+ };
64
+ /*
65
+ *
66
+ * PUT ONE Component
67
+ *
68
+ * */
69
+ export const updateComponent = (component, presets, config) => {
70
+ const { spaceId, sbApi } = config;
71
+ Logger.log(`Trying to update '${component.name}' with id ${component.id}`);
72
+ const componentWithPresets = component;
73
+ const { all_presets, ...componentWithoutPresets } = componentWithPresets;
74
+ return sbApi
75
+ .put(`spaces/${spaceId}/components/${component.id}`, {
76
+ component: componentWithoutPresets,
77
+ })
78
+ .then(async (res) => {
79
+ Logger.success(`Component '${component.name}' has been updated.`);
80
+ if (presets) {
81
+ return await _resolvePresets(res, all_presets, component, config);
82
+ }
83
+ else {
84
+ return [];
85
+ }
86
+ })
87
+ .catch((err) => {
88
+ Logger.error(`${err.message} in migration of ${component.name} in updateComponent function`);
89
+ });
90
+ };
91
+ /*
92
+ *
93
+ * DEL one Component
94
+ *
95
+ * */
96
+ export const removeComponent = (component, config) => {
97
+ const { id, name } = component;
98
+ const { spaceId, sbApi } = config;
99
+ console.log(`Removing '${name}' component.`);
100
+ return sbApi
101
+ .delete(`spaces/${spaceId}/components/${id}`, {})
102
+ .then((res) => res.data)
103
+ .catch((err) => console.error(err));
104
+ };
105
+ /*
106
+ *
107
+ * GET All Component Groups
108
+ *
109
+ * */
110
+ export const getAllComponentsGroups = async (config) => {
111
+ const { spaceId, sbApi } = config;
112
+ Logger.log("Trying to get all groups.");
113
+ // TODO: All Components Groups doesnt support pagination...
114
+ // https://github.com/storyblok/storyblok-js-client/issues/535
115
+ return getAllItemsWithPagination({
116
+ apiFn: ({ per_page, page }) => sbApi
117
+ .get(`spaces/${spaceId}/component_groups/`, { per_page, page })
118
+ .then((res) => {
119
+ Logger.log(`Amount of component groups: ${res.total}`);
120
+ return res;
121
+ })
122
+ .catch((err) => console.error(err)),
123
+ params: {
124
+ spaceId,
125
+ },
126
+ itemsKey: "component_groups",
127
+ });
128
+ };
129
+ export const getComponentsGroup = (groupName, config) => {
130
+ console.log(`Trying to get '${groupName}' group.`);
131
+ return getAllComponentsGroups(config)
132
+ .then((res) => {
133
+ return res.filter((group) => group.name === groupName);
134
+ })
135
+ .then((res) => {
136
+ if (Array.isArray(res) && res.length === 0) {
137
+ console.info(`There is no group named '${groupName}'`);
138
+ return false;
139
+ }
140
+ return res;
141
+ })
142
+ .catch((err) => console.error(err));
143
+ };
144
+ export const removeComponentGroup = (componentGroup, config) => {
145
+ const { id, name } = componentGroup;
146
+ const { spaceId, sbApi } = config;
147
+ console.log(`Removing '${name}' component group.`);
148
+ return sbApi
149
+ .delete(`spaces/${spaceId}/component_groups/${id}`, {})
150
+ .then((res) => res.data)
151
+ .catch((err) => console.error(err));
152
+ };
153
+ export const createComponentsGroup = (groupName, config) => {
154
+ const { spaceId, sbApi } = config;
155
+ console.log(`Trying to create '${groupName}' group`);
156
+ return sbApi
157
+ .post(`spaces/${spaceId}/component_groups/`, {
158
+ component_group: {
159
+ name: groupName,
160
+ },
161
+ })
162
+ .then((res) => {
163
+ console.info(`'${groupName}' created with uuid: ${res.data.component_group.uuid}`);
164
+ return res.data;
165
+ })
166
+ .catch((err) => {
167
+ console.log(err.message);
168
+ console.error("Error happened :()");
169
+ });
170
+ };
@@ -0,0 +1,13 @@
1
+ import type { RequestBaseConfig } from "../utils/request.js";
2
+ export type GetAllComponents = (config: RequestBaseConfig) => Promise<any>;
3
+ export type GetComponent = (componentName: string | undefined, config: RequestBaseConfig) => Promise<any>;
4
+ export type UpdateComponent = (component: any, preset: boolean, config: RequestBaseConfig) => Promise<any>;
5
+ export type CreateComponent = (component: any, preset: boolean, config: RequestBaseConfig) => Promise<any> | void;
6
+ export type RemoveComponent = (component: any, config: RequestBaseConfig) => Promise<any>;
7
+ export type GetAllComponentsGroups = (config: RequestBaseConfig) => Promise<any>;
8
+ export type GetComponentsGroup = (groupName: string | undefined, config: RequestBaseConfig) => Promise<any>;
9
+ export type RemoveComponentGroup = (componentGroup: any, config: RequestBaseConfig) => Promise<any>;
10
+ export type CreateComponentsGroup = (groupName: string | undefined, config: RequestBaseConfig) => Promise<any>;
11
+ export type RemoveSpecificComponents = (components: any, config: RequestBaseConfig) => Promise<any>;
12
+ export type CheckAndPrepareGroups = (groupName: any, config: RequestBaseConfig) => Promise<any>;
13
+ export type ResolveGroups = (component: any, existedGroups: any, remoteComponentsGroups: any, config: RequestBaseConfig) => Promise<any>;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export { getAllComponents, getComponent, getAllComponentsGroups, createComponentsGroup, getComponentsGroup, removeComponentGroup, removeComponent, updateComponent, createComponent, } from "./components.js";
@@ -0,0 +1 @@
1
+ export { getAllComponents, getComponent, getAllComponentsGroups, createComponentsGroup, getComponentsGroup, removeComponentGroup, removeComponent, updateComponent, createComponent, } from "./components.js";
@@ -0,0 +1,9 @@
1
+ import type { RequestBaseConfig } from "./utils/request.js";
2
+ type GetAllStories = (args: {
3
+ spaceId: string;
4
+ storiesFilename?: string;
5
+ }, config: RequestBaseConfig) => Promise<any>;
6
+ export declare const contentHubApi: {
7
+ getAllStories: GetAllStories;
8
+ };
9
+ export {};
@@ -0,0 +1,29 @@
1
+ import Logger from "../utils/logger.js";
2
+ const getAllStories = async (args, config) => {
3
+ const { spaceId, storiesFilename } = args;
4
+ Logger.warning("Trying to get all stories from Content Hub...");
5
+ const queryParams = `spaceId=${spaceId}&storiesFilename=${storiesFilename}`;
6
+ const url = `${config.contentHubOriginUrl}/getStories?${queryParams}`;
7
+ const authorizationToken = config.contentHubAuthorizationToken;
8
+ if (config.debug) {
9
+ console.log("This is url: ", url);
10
+ }
11
+ try {
12
+ const response = await fetch(url, {
13
+ method: "GET",
14
+ headers: {
15
+ Authorization: authorizationToken,
16
+ },
17
+ });
18
+ if (!response.ok) {
19
+ throw new Error(`Error fetching stories: ${response.statusText}`);
20
+ }
21
+ const data = await response.json();
22
+ return data;
23
+ }
24
+ catch (error) {
25
+ console.error("Error fetching stories:", error);
26
+ throw error;
27
+ }
28
+ };
29
+ export const contentHubApi = { getAllStories };
@@ -0,0 +1,16 @@
1
+ import type { RequestBaseConfig } from "../utils/request.js";
2
+ export type MigrateFrom = "file" | "space";
3
+ interface MigrateStories {
4
+ from: string;
5
+ to: string;
6
+ migrateFrom: MigrateFrom;
7
+ migrationConfig: string;
8
+ componentsToMigrate: string[];
9
+ }
10
+ export type MapperDefinition = (data: any) => any;
11
+ export declare const prepareStoriesFromLocalFile: ({ from }: any) => any;
12
+ export declare const prepareMigrationConfig: ({ migrationConfig }: any) => any;
13
+ export declare const migrateAllComponentsDataInStories: ({ migrationConfig, migrateFrom, from, to, }: Omit<MigrateStories, "componentsToMigrate">, config: RequestBaseConfig) => Promise<void>;
14
+ export declare const doTheMigration: ({ from, storiesToMigrate, componentsToMigrate, migrationConfigFileContent, migrationConfig, to, }: any, config: RequestBaseConfig) => Promise<void>;
15
+ export declare const migrateProvidedComponentsDataInStories: ({ migrationConfig, migrateFrom, from, to, componentsToMigrate, }: MigrateStories, config: RequestBaseConfig) => Promise<void>;
16
+ export {};