@magiclabs.ai/magicbook-client 0.7.2 → 0.7.4-canary

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 CHANGED
@@ -79,6 +79,7 @@ window.addEventListener('Magicbook.designRequestUpdated', async ((designRequestE
79
79
  ```
80
80
 
81
81
  Submit the design request. Again, the argument object can receive additional or updated design parameters.
82
+ You can submit multiple design requests by calling this function after receiving the **"ready"** event from the previous one.
82
83
 
83
84
  ```ts
84
85
  await designRequest.submit({
package/index.cjs CHANGED
@@ -32,6 +32,7 @@ __export(src_exports, {
32
32
  bookDesignRequestSchema: () => bookDesignRequestSchema,
33
33
  bookPropsSchema: () => bookPropsSchema,
34
34
  bookSizes: () => bookSizes,
35
+ canResubmitDesignRequest: () => canResubmitDesignRequest,
35
36
  cancelledEventDetail: () => cancelledEventDetail,
36
37
  canvasSchema: () => canvasSchema,
37
38
  coverTypes: () => coverTypes,
@@ -85,6 +86,7 @@ var states = [
85
86
  var statesToCloseWS = ["error", "timeout", "ready", "cancelled"];
86
87
  var statesToReport = ["error", "timeout"];
87
88
  var isDesignRequestSubmitted = (state) => !["new", "ingesting"].includes(state);
89
+ var canResubmitDesignRequest = (state) => ["new", "ingesting", "ready"].includes(state);
88
90
  var occasions = [
89
91
  "baby",
90
92
  "birthday",
@@ -248,7 +250,7 @@ var styles = {
248
250
  6116: { slug: "boho-baby-sfly" },
249
251
  6117: { slug: "simply-gallery-sfly" },
250
252
  6118: { slug: "this-is-love-sfly" },
251
- 6120: { slug: "wedding-gallery-sfly" },
253
+ 6120: { slug: "wedding-photo-album-sfly" },
252
254
  6121: { slug: "watercolorwashes-sfly" },
253
255
  6124: { slug: "graduation-photo-album-sfly" },
254
256
  6125: { slug: "modern-year-in-review-photo-album-sfly" },
@@ -260,6 +262,10 @@ var styles = {
260
262
  6144: { slug: "seasonal-year-in-review-sfly" },
261
263
  6143: { slug: "wanderlust-gallery-sfly" },
262
264
  6146: { slug: "wedding-elopement-gallery-sfly" },
265
+ 6148: { slug: "love-wins-sfly" },
266
+ 6151: { slug: "adventures-in-mexico-sfly" },
267
+ 6152: { slug: "generations-of-love-sfly" },
268
+ 6153: { slug: "cheerful-patterns-sfly" },
263
269
  6062: { slug: "create-your-own-sfly" },
264
270
  6075: { slug: "together-again-sfly" },
265
271
  6078: { slug: "everyday-boho-by-umaiana-studio-sfly" },
@@ -4086,11 +4092,13 @@ var bookPropsSchema = z.object({
4086
4092
  guid: z.string().optional(),
4087
4093
  cancelled_at: z.string().optional(),
4088
4094
  mb_client_timeout: z.number().optional(),
4089
- user_id: z.string().optional()
4095
+ user_id: z.string().optional(),
4096
+ revision: z.number().optional()
4090
4097
  });
4091
4098
  var Book = class {
4092
4099
  id;
4093
4100
  title;
4101
+ revision;
4094
4102
  subtitle;
4095
4103
  design_request;
4096
4104
  state;
@@ -4108,11 +4116,13 @@ var Book = class {
4108
4116
  this.cancelled_at = props.cancelled_at;
4109
4117
  this.timeout = props.mb_client_timeout ? props.mb_client_timeout * 1e3 : void 0;
4110
4118
  this.user_id = props.user_id;
4119
+ this.revision = props.revision;
4111
4120
  }
4112
4121
  toDesignRequestProps() {
4113
4122
  const props = { ...this, ...this.design_request };
4114
4123
  props.style = getStyleIdBySlug(props.style);
4115
4124
  delete props.design_request;
4125
+ delete props.revision;
4116
4126
  return snakeCaseObjectKeysToCamelCase(props);
4117
4127
  }
4118
4128
  toBookProps() {
@@ -4126,22 +4136,28 @@ var Book = class {
4126
4136
  // ../../core/models/design-request/image.ts
4127
4137
  var Images = class {
4128
4138
  // eslint-disable-next-line no-unused-vars
4129
- constructor(client, parentId) {
4139
+ constructor(client, parentId, designRequestState) {
4130
4140
  this.client = client;
4131
4141
  this.parentId = parentId;
4132
4142
  this.images = [];
4133
4143
  this.length = this.images.length;
4144
+ this.designRequestState = designRequestState;
4134
4145
  }
4135
4146
  parentId;
4136
4147
  images;
4137
4148
  length;
4149
+ designRequestState;
4138
4150
  async add(image) {
4139
- this.images.push(image);
4140
- this.length = this.images.length;
4141
- await this.client.engineAPI.images.addToBook(this.parentId, new ImageServer(image));
4142
- return new Promise((resolve) => {
4143
- resolve(this.length);
4144
- });
4151
+ if (!canResubmitDesignRequest(this.designRequestState)) {
4152
+ throw new Error("You need to wait for the current design request to be ready before adding new images.");
4153
+ } else {
4154
+ this.images.push(image);
4155
+ this.length = this.images.length;
4156
+ await this.client.engineAPI.images.addToBook(this.parentId, new ImageServer(image));
4157
+ return new Promise((resolve) => {
4158
+ resolve(this.length);
4159
+ });
4160
+ }
4145
4161
  }
4146
4162
  };
4147
4163
  var imageServerSchema = z.object({
@@ -4239,7 +4255,7 @@ var DesignRequest = class {
4239
4255
  this.imageFilteringLevel = designRequestProps?.imageFilteringLevel || imageFilteringLevels[0];
4240
4256
  this.embellishmentLevel = designRequestProps?.embellishmentLevel || embellishmentLevels[0];
4241
4257
  this.textStickerLevel = designRequestProps?.textStickerLevel || textStickerLevels[0];
4242
- this.images = new Images(this.client, this.parentId);
4258
+ this.images = new Images(this.client, this.parentId, this.state);
4243
4259
  this.userId = designRequestProps?.userId;
4244
4260
  }
4245
4261
  state;
@@ -4261,6 +4277,9 @@ var DesignRequest = class {
4261
4277
  timeout;
4262
4278
  updateDesignRequest(designRequestProps) {
4263
4279
  Object.assign(this, designRequestProps);
4280
+ if (designRequestProps.state) {
4281
+ this.images.designRequestState = designRequestProps.state;
4282
+ }
4264
4283
  }
4265
4284
  async getOptions(imageCount) {
4266
4285
  const options = designOptionsSchema.parse(snakeCaseObjectKeysToCamelCase(
@@ -4273,16 +4292,17 @@ var DesignRequest = class {
4273
4292
  return options;
4274
4293
  }
4275
4294
  async submit(submitDesignRequestProps) {
4276
- if (isDesignRequestSubmitted(this.state)) {
4277
- throw new Error("Design request already submitted");
4295
+ if (!canResubmitDesignRequest(this.state)) {
4296
+ throw new Error("You need to wait for the current design request to be ready before submitting a new one");
4278
4297
  } else {
4279
- submitDesignRequestProps && Object.assign(this, submitDesignRequestProps);
4298
+ submitDesignRequestProps && this.updateDesignRequest(submitDesignRequestProps);
4280
4299
  this.webSocket = new WebSocket(`${this.client.webSocketHost}/?book_id=${this.parentId}`);
4300
+ await this.client.engineAPI.books.update(this.parentId, this.toBook());
4281
4301
  this.updateDesignRequest(
4282
- (await this.client.engineAPI.books.update(this.parentId, this.toBook())).toDesignRequestProps()
4302
+ (await this.client.engineAPI.books.design(this.parentId)).toDesignRequestProps()
4283
4303
  );
4284
4304
  this.getProgress();
4285
- this.state = states[1];
4305
+ this.updateDesignRequest({ state: states[1] });
4286
4306
  return this;
4287
4307
  }
4288
4308
  }
@@ -4305,10 +4325,10 @@ var DesignRequest = class {
4305
4325
  } else if (!isDesignRequestSubmitted(this.state)) {
4306
4326
  throw new Error("Design request not submitted");
4307
4327
  } else {
4308
- this.updateDesignRequest(
4309
- (await this.client.engineAPI.books.cancel(this.parentId)).toDesignRequestProps()
4310
- );
4311
- this.state = "cancelled";
4328
+ this.updateDesignRequest({
4329
+ ...(await this.client.engineAPI.books.cancel(this.parentId)).toDesignRequestProps(),
4330
+ state: "cancelled"
4331
+ });
4312
4332
  await this.eventHandler(cancelledEventDetail);
4313
4333
  return this;
4314
4334
  }
@@ -4334,7 +4354,7 @@ var DesignRequest = class {
4334
4354
  });
4335
4355
  }
4336
4356
  }
4337
- this.state = detail.slug;
4357
+ this.updateDesignRequest({ state: detail.slug });
4338
4358
  window.dispatchEvent(customEvent);
4339
4359
  }
4340
4360
  timeoutHandler() {
@@ -4361,10 +4381,13 @@ var DesignRequest = class {
4361
4381
  }
4362
4382
  }
4363
4383
  toBook() {
4364
- const designRequest = { ...this };
4365
- delete designRequest["client"];
4366
- delete designRequest["images"]["client"];
4367
- delete designRequest["webSocket"];
4384
+ const designRequest = {
4385
+ ...this,
4386
+ images: this.images["images"]
4387
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
4388
+ };
4389
+ delete designRequest.client;
4390
+ delete designRequest.webSocket;
4368
4391
  const styleSlug = styles[this.style].slug;
4369
4392
  const bookDesignRequest = camelCaseObjectKeysToSnakeCase(cleanJSON(designRequest));
4370
4393
  bookDesignRequest.style = styleSlug;
@@ -4499,6 +4522,16 @@ var BooksEndpoints = class {
4499
4522
  return new Book(res);
4500
4523
  });
4501
4524
  }
4525
+ design(bookId) {
4526
+ return handleAsyncFunction(async () => {
4527
+ const res = await this.engineAPI.fetcher.call({
4528
+ path: `/v1/books/${bookId}/design`,
4529
+ options: { method: "POST" }
4530
+ });
4531
+ bookPropsSchema.safeParse(res);
4532
+ return new Book(res);
4533
+ });
4534
+ }
4502
4535
  cancel(bookId) {
4503
4536
  return handleAsyncFunction(async () => {
4504
4537
  const res = await this.engineAPI.fetcher.call({
@@ -4951,6 +4984,7 @@ var MagicBookClient = class {
4951
4984
  bookDesignRequestSchema,
4952
4985
  bookPropsSchema,
4953
4986
  bookSizes,
4987
+ canResubmitDesignRequest,
4954
4988
  cancelledEventDetail,
4955
4989
  canvasSchema,
4956
4990
  coverTypes,