sb-mig 5.0.8-beta.2 → 5.0.8-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.
@@ -239,7 +239,7 @@ export const migrateProvidedComponentsDataInStories = async ({ itemType, migrati
239
239
  // Get all stories to be migrated from storyblok space
240
240
  let itemsToMigrate = [];
241
241
  if (itemType === "story") {
242
- itemsToMigrate = await managementApi.stories.getAllStories({
242
+ itemsToMigrate = await managementApi.stories.getAllStories({}, {
243
243
  ...config,
244
244
  spaceId: from,
245
245
  });
@@ -60,6 +60,7 @@ export declare const managementApi: {
60
60
  updateStories: import("./stories/stories.types.js").UpdateStories;
61
61
  getAllStories: import("./stories/stories.types.js").GetAllStories;
62
62
  removeAllStories: import("./stories/stories.types.js").RemoveAllStories;
63
+ upsertStory: import("./stories/stories.types.js").UpsertStory;
63
64
  backupStories: import("./stories/stories.types.js").BackupStories;
64
65
  };
65
66
  spaces: {
@@ -207,7 +207,7 @@ export const syncContent = async ({ type, transmission, syncDirection, filename
207
207
  }, config);
208
208
  }
209
209
  if (syncDirection === "fromSpaceToSpace") {
210
- const stories = await getAllStories({
210
+ const stories = await getAllStories({}, {
211
211
  ...config,
212
212
  spaceId: transmission.from,
213
213
  sbApi: config.sbApi,
@@ -14,10 +14,10 @@ export interface Space {
14
14
  created_at: string;
15
15
  }
16
16
  export type GetSpace = (args: {
17
- spaceId: number;
17
+ spaceId: string;
18
18
  }, config: RequestBaseConfig) => Promise<any>;
19
19
  export type GetAllSpaces = (config: RequestBaseConfig) => Promise<Space[]>;
20
20
  export type UpdateSpace = (args: {
21
- spaceId: number;
21
+ spaceId: string;
22
22
  params: any;
23
23
  }, config: RequestBaseConfig) => Promise<any>;
@@ -3,7 +3,7 @@ import Logger from "../../utils/logger.js";
3
3
  import { getAllStories } from "./stories.js";
4
4
  export const backupStories = async ({ suffix, spaceId, filename }, config) => {
5
5
  Logger.log(`Making backup of your stories.`);
6
- await getAllStories({ ...config, spaceId })
6
+ await getAllStories({}, { ...config, spaceId })
7
7
  .then(async (res) => {
8
8
  await createAndSaveToFile({
9
9
  ext: "json",
@@ -1,2 +1,2 @@
1
- export { createStory, updateStory, getStoryById, removeStory, getStoryBySlug, updateStories, getAllStories, removeAllStories, } from "./stories.js";
1
+ export { createStory, updateStory, getStoryById, removeStory, getStoryBySlug, updateStories, getAllStories, removeAllStories, upsertStory, } from "./stories.js";
2
2
  export { backupStories } from "./backup.js";
@@ -1,2 +1,2 @@
1
- export { createStory, updateStory, getStoryById, removeStory, getStoryBySlug, updateStories, getAllStories, removeAllStories, } from "./stories.js";
1
+ export { createStory, updateStory, getStoryById, removeStory, getStoryBySlug, updateStories, getAllStories, removeAllStories, upsertStory, } from "./stories.js";
2
2
  export { backupStories } from "./backup.js";
@@ -1,4 +1,4 @@
1
- import type { GetAllStories, GetStoryById, RemoveStory, CreateStory, UpdateStory, UpdateStories, RemoveAllStories } from "./stories.types.js";
1
+ import type { GetAllStories, GetStoryById, RemoveStory, CreateStory, UpdateStory, UpdateStories, RemoveAllStories, UpsertStory, DeepUpsertStory } from "./stories.types.js";
2
2
  import type { GetStoryBySlug } from "./stories.types.js";
3
3
  export declare const removeStory: RemoveStory;
4
4
  export declare const removeAllStories: RemoveAllStories;
@@ -8,3 +8,5 @@ export declare const getStoryBySlug: GetStoryBySlug;
8
8
  export declare const createStory: CreateStory;
9
9
  export declare const updateStory: UpdateStory;
10
10
  export declare const updateStories: UpdateStories;
11
+ export declare const upsertStory: UpsertStory;
12
+ export declare const deepUpsertStory: DeepUpsertStory;
@@ -1,5 +1,6 @@
1
1
  import chalk from "chalk";
2
2
  import Logger from "../../utils/logger.js";
3
+ import { managementApi } from "../managementApi.js";
3
4
  import { getAllItemsWithPagination } from "../utils/request.js";
4
5
  export const removeStory = (args, config) => {
5
6
  const { storyId } = args;
@@ -18,7 +19,7 @@ export const removeStory = (args, config) => {
18
19
  export const removeAllStories = async (config) => {
19
20
  const { spaceId } = config;
20
21
  Logger.warning(`Trying to remove all stories from space with spaceId: ${spaceId}`);
21
- const stories = await getAllStories(config);
22
+ const stories = await getAllStories({}, config);
22
23
  const onlyRootStories = (story) => story.story.parent_id === 0 || story.story.parent_id === null;
23
24
  const allResponses = Promise.all(stories
24
25
  .filter(onlyRootStories)
@@ -26,13 +27,15 @@ export const removeAllStories = async (config) => {
26
27
  return allResponses;
27
28
  };
28
29
  // GET
29
- export const getAllStories = async (config) => {
30
+ export const getAllStories = async (args, config) => {
31
+ const { options } = args;
30
32
  const { spaceId, sbApi } = config;
31
33
  Logger.log(`Trying to get all Stories from: ${spaceId}`);
32
34
  const allStoriesWithoutContent = await getAllItemsWithPagination({
33
35
  apiFn: ({ per_page, page }) => sbApi.get(`spaces/${spaceId}/stories/`, {
34
36
  per_page,
35
37
  page,
38
+ starts_with: options?.starts_with,
36
39
  }),
37
40
  params: {
38
41
  spaceId,
@@ -117,3 +120,69 @@ export const updateStories = (args, config) => {
117
120
  return updateStory(stories.story, stories.story.id, { publish: options.publish }, { ...config, spaceId });
118
121
  }));
119
122
  };
123
+ export const upsertStory = async (args, config) => {
124
+ console.log("Modifying story... in space with id:");
125
+ console.log(config.spaceId);
126
+ const { storyId, storySlug, content } = args;
127
+ console.log("This are args passed: ");
128
+ console.log(args);
129
+ if (storyId) {
130
+ // if this exist than we update story with this id
131
+ console.log("You've selected storyid!");
132
+ }
133
+ else if (storySlug) {
134
+ // if this exist than we update story with this slug (probably when we try to add story from one space to another,
135
+ console.log("You've selected slug!");
136
+ const foundStory = await managementApi.stories.getStoryBySlug(storySlug, config);
137
+ console.log("This is story");
138
+ console.log(foundStory);
139
+ if (foundStory) {
140
+ // then update the story
141
+ }
142
+ else {
143
+ const { story: { parent_id, id, parent, ...rest }, } = content;
144
+ console.log("We are going to create story");
145
+ const response = await managementApi.stories.createStory(rest, config);
146
+ console.log("This is response");
147
+ console.log(response);
148
+ }
149
+ }
150
+ else {
151
+ // if this exist than we create new story
152
+ console.log("Nothing passed, creating story...");
153
+ }
154
+ };
155
+ export const deepUpsertStory = async (args, config) => {
156
+ console.log("Modifying story... in space with id:");
157
+ console.log(config.spaceId);
158
+ const { storyId, storySlug, content } = args;
159
+ console.log("This are args passed: ");
160
+ console.log(args);
161
+ if (storyId) {
162
+ // if this exist than we update story with this id
163
+ console.log("You've selected storyid!");
164
+ }
165
+ else if (storySlug) {
166
+ // if this exist than we update story with this slug (probably when we try to add story from one space to another,
167
+ console.log("You've selected slug!");
168
+ const slugs = storySlug.split("/");
169
+ console.log("Slugs for which we need to check for existence of stories");
170
+ console.log(slugs);
171
+ // const foundStory = await managementApi.stories.getStoryBySlug(storySlug, config)
172
+ // console.log("This is story")
173
+ // console.log(foundStory)
174
+ // if(foundStory) {
175
+ // // then update the story
176
+ // } else {
177
+ // const {story: {parent_id, id, parent,...rest}} = content
178
+ // console.log("We are going to create story")
179
+ // const response = await managementApi.stories.createStory(rest, config)
180
+ // console.log("This is response")
181
+ // console.log(response)
182
+ // }
183
+ }
184
+ else {
185
+ // if this exist than we create new story
186
+ console.log("Nothing passed, creating story...");
187
+ }
188
+ };
@@ -1,24 +1,38 @@
1
1
  import type { RequestBaseConfig } from "../utils/request.js";
2
+ interface ModifyStoryOptions {
3
+ publish?: boolean;
4
+ }
2
5
  export type RemoveStory = (args: {
3
6
  storyId: string;
4
7
  }, config: RequestBaseConfig) => Promise<any>;
5
8
  export type RemoveAllStories = (config: RequestBaseConfig) => Promise<any>;
6
9
  export type GetStoryById = (storyId: string, config: RequestBaseConfig) => Promise<any>;
7
10
  export type GetStoryBySlug = (slug: string, config: RequestBaseConfig) => Promise<any>;
8
- export type GetAllStories = (config: RequestBaseConfig) => Promise<any>;
9
- export type CreateStory = (content: any, config: RequestBaseConfig) => Promise<any>;
10
- export type UpdateStory = (content: any, storyId: string, options: {
11
- publish?: boolean;
11
+ export type GetAllStories = (args: {
12
+ options?: {
13
+ starts_with: string;
14
+ };
12
15
  }, config: RequestBaseConfig) => Promise<any>;
16
+ export type CreateStory = (content: any, config: RequestBaseConfig) => Promise<any>;
17
+ export type UpdateStory = (content: any, storyId: string, options: ModifyStoryOptions, config: RequestBaseConfig) => Promise<any>;
13
18
  export type UpdateStories = (args: {
14
19
  stories: any;
15
- options: {
16
- publish?: boolean;
17
- };
20
+ options: ModifyStoryOptions;
18
21
  spaceId: string;
19
22
  }, config: RequestBaseConfig) => Promise<any>;
23
+ export type UpsertStory = (args: {
24
+ content: any;
25
+ storyId?: string;
26
+ storySlug?: string;
27
+ }, config: RequestBaseConfig) => void;
28
+ export type DeepUpsertStory = (args: {
29
+ content: any;
30
+ storyId?: string;
31
+ storySlug?: string;
32
+ }, config: RequestBaseConfig) => void;
20
33
  export type BackupStories = (args: {
21
34
  filename: string;
22
35
  spaceId: string;
23
36
  suffix?: string;
24
37
  }, config: RequestBaseConfig) => void;
38
+ export {};
@@ -60,6 +60,7 @@ export declare const testApi: {
60
60
  updateStories: import("./stories/stories.types.js").UpdateStories;
61
61
  getAllStories: import("./stories/stories.types.js").GetAllStories;
62
62
  removeAllStories: import("./stories/stories.types.js").RemoveAllStories;
63
+ upsertStory: import("./stories/stories.types.js").UpsertStory;
63
64
  backupStories: import("./stories/stories.types.js").BackupStories;
64
65
  };
65
66
  spaces: {
@@ -23,7 +23,9 @@ export const init = async (props) => {
23
23
  gtmToken,
24
24
  });
25
25
  const localSbApi = new StoryblokClient({ oauthToken }, storyblokApiUrl);
26
- const spaceData = await managementApi.spaces.getSpace(spaceId, apiConfig);
26
+ console.log("This is api config: ");
27
+ console.log({ ...apiConfig, oauthToken });
28
+ const spaceData = await managementApi.spaces.getSpace({ spaceId }, { ...apiConfig, sbApi: localSbApi });
27
29
  const STORYBLOK_SPACE_ID = spaceId;
28
30
  const STORYBLOK_OAUTH_TOKEN = oauthToken;
29
31
  const NEXT_PUBLIC_GTM_ID = gtmToken ?? "put-your-gtm-token-here";
@@ -51,7 +53,7 @@ export const init = async (props) => {
51
53
  params: {
52
54
  domain: `https://localhost:3000/api/preview/preview?secret=${STORYBLOK_PREVIEW_SECRET}&slug=`,
53
55
  },
54
- }, apiConfig);
56
+ }, { ...apiConfig, sbApi: localSbApi });
55
57
  Logger.success("Successfully updated space domain");
56
58
  }
57
59
  catch (e) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sb-mig",
3
- "version": "5.0.8-beta.2",
3
+ "version": "5.0.8-beta.3",
4
4
  "description": "CLI to rule the world. (and handle stuff related to Storyblok CMS)",
5
5
  "author": "Marcin Krawczyk <marckraw@icloud.com>",
6
6
  "license": "MIT",