@magiclabs.ai/magicbook-client 0.7.3 → 0.7.4

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
+ canSubmitDesignRequest: () => canSubmitDesignRequest,
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 canSubmitDesignRequest = (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" },
@@ -4090,11 +4092,13 @@ var bookPropsSchema = z.object({
4090
4092
  guid: z.string().optional(),
4091
4093
  cancelled_at: z.string().optional(),
4092
4094
  mb_client_timeout: z.number().optional(),
4093
- user_id: z.string().optional()
4095
+ user_id: z.string().optional(),
4096
+ revision: z.number().optional()
4094
4097
  });
4095
4098
  var Book = class {
4096
4099
  id;
4097
4100
  title;
4101
+ revision;
4098
4102
  subtitle;
4099
4103
  design_request;
4100
4104
  state;
@@ -4112,11 +4116,13 @@ var Book = class {
4112
4116
  this.cancelled_at = props.cancelled_at;
4113
4117
  this.timeout = props.mb_client_timeout ? props.mb_client_timeout * 1e3 : void 0;
4114
4118
  this.user_id = props.user_id;
4119
+ this.revision = props.revision;
4115
4120
  }
4116
4121
  toDesignRequestProps() {
4117
4122
  const props = { ...this, ...this.design_request };
4118
4123
  props.style = getStyleIdBySlug(props.style);
4119
4124
  delete props.design_request;
4125
+ delete props.revision;
4120
4126
  return snakeCaseObjectKeysToCamelCase(props);
4121
4127
  }
4122
4128
  toBookProps() {
@@ -4130,22 +4136,28 @@ var Book = class {
4130
4136
  // ../../core/models/design-request/image.ts
4131
4137
  var Images = class {
4132
4138
  // eslint-disable-next-line no-unused-vars
4133
- constructor(client, parentId) {
4139
+ constructor(client, parentId, designRequestState) {
4134
4140
  this.client = client;
4135
4141
  this.parentId = parentId;
4136
4142
  this.images = [];
4137
4143
  this.length = this.images.length;
4144
+ this.designRequestState = designRequestState;
4138
4145
  }
4139
4146
  parentId;
4140
4147
  images;
4141
4148
  length;
4149
+ designRequestState;
4142
4150
  async add(image) {
4143
- this.images.push(image);
4144
- this.length = this.images.length;
4145
- await this.client.engineAPI.images.addToBook(this.parentId, new ImageServer(image));
4146
- return new Promise((resolve) => {
4147
- resolve(this.length);
4148
- });
4151
+ if (!canSubmitDesignRequest(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
+ }
4149
4161
  }
4150
4162
  };
4151
4163
  var imageServerSchema = z.object({
@@ -4243,7 +4255,7 @@ var DesignRequest = class {
4243
4255
  this.imageFilteringLevel = designRequestProps?.imageFilteringLevel || imageFilteringLevels[0];
4244
4256
  this.embellishmentLevel = designRequestProps?.embellishmentLevel || embellishmentLevels[0];
4245
4257
  this.textStickerLevel = designRequestProps?.textStickerLevel || textStickerLevels[0];
4246
- this.images = new Images(this.client, this.parentId);
4258
+ this.images = new Images(this.client, this.parentId, this.state);
4247
4259
  this.userId = designRequestProps?.userId;
4248
4260
  }
4249
4261
  state;
@@ -4265,6 +4277,9 @@ var DesignRequest = class {
4265
4277
  timeout;
4266
4278
  updateDesignRequest(designRequestProps) {
4267
4279
  Object.assign(this, designRequestProps);
4280
+ if (designRequestProps.state) {
4281
+ this.images.designRequestState = designRequestProps.state;
4282
+ }
4268
4283
  }
4269
4284
  async getOptions(imageCount) {
4270
4285
  const options = designOptionsSchema.parse(snakeCaseObjectKeysToCamelCase(
@@ -4277,16 +4292,16 @@ var DesignRequest = class {
4277
4292
  return options;
4278
4293
  }
4279
4294
  async submit(submitDesignRequestProps) {
4280
- if (isDesignRequestSubmitted(this.state)) {
4281
- throw new Error("Design request already submitted");
4295
+ if (!canSubmitDesignRequest(this.state)) {
4296
+ throw new Error("You need to wait for the current design request to be ready before submitting a new one");
4282
4297
  } else {
4283
- submitDesignRequestProps && Object.assign(this, submitDesignRequestProps);
4298
+ submitDesignRequestProps && this.updateDesignRequest(submitDesignRequestProps);
4284
4299
  this.webSocket = new WebSocket(`${this.client.webSocketHost}/?book_id=${this.parentId}`);
4300
+ await this.client.engineAPI.books.update(this.parentId, this.toBook());
4285
4301
  this.updateDesignRequest(
4286
- (await this.client.engineAPI.books.update(this.parentId, this.toBook())).toDesignRequestProps()
4302
+ (await this.client.engineAPI.books.design(this.parentId)).toDesignRequestProps()
4287
4303
  );
4288
4304
  this.getProgress();
4289
- this.state = states[1];
4290
4305
  return this;
4291
4306
  }
4292
4307
  }
@@ -4309,10 +4324,10 @@ var DesignRequest = class {
4309
4324
  } else if (!isDesignRequestSubmitted(this.state)) {
4310
4325
  throw new Error("Design request not submitted");
4311
4326
  } else {
4312
- this.updateDesignRequest(
4313
- (await this.client.engineAPI.books.cancel(this.parentId)).toDesignRequestProps()
4314
- );
4315
- this.state = "cancelled";
4327
+ this.updateDesignRequest({
4328
+ ...(await this.client.engineAPI.books.cancel(this.parentId)).toDesignRequestProps(),
4329
+ state: "cancelled"
4330
+ });
4316
4331
  await this.eventHandler(cancelledEventDetail);
4317
4332
  return this;
4318
4333
  }
@@ -4338,7 +4353,7 @@ var DesignRequest = class {
4338
4353
  });
4339
4354
  }
4340
4355
  }
4341
- this.state = detail.slug;
4356
+ this.updateDesignRequest({ state: detail.slug });
4342
4357
  window.dispatchEvent(customEvent);
4343
4358
  }
4344
4359
  timeoutHandler() {
@@ -4355,7 +4370,7 @@ var DesignRequest = class {
4355
4370
  let timeout;
4356
4371
  this.webSocket.onmessage = async (event) => {
4357
4372
  const detail = JSON.parse(event.data);
4358
- if (this.state !== detail.slug) {
4373
+ if (this.state !== detail.slug || detail.slug === "submitted") {
4359
4374
  timeout && clearTimeout(timeout);
4360
4375
  timeout = this.timeoutHandler();
4361
4376
  await this.eventHandler(detail);
@@ -4365,10 +4380,13 @@ var DesignRequest = class {
4365
4380
  }
4366
4381
  }
4367
4382
  toBook() {
4368
- const designRequest = { ...this };
4369
- delete designRequest["client"];
4370
- delete designRequest["images"]["client"];
4371
- delete designRequest["webSocket"];
4383
+ const designRequest = {
4384
+ ...this,
4385
+ images: this.images["images"]
4386
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
4387
+ };
4388
+ delete designRequest.client;
4389
+ delete designRequest.webSocket;
4372
4390
  const styleSlug = styles[this.style].slug;
4373
4391
  const bookDesignRequest = camelCaseObjectKeysToSnakeCase(cleanJSON(designRequest));
4374
4392
  bookDesignRequest.style = styleSlug;
@@ -4503,6 +4521,16 @@ var BooksEndpoints = class {
4503
4521
  return new Book(res);
4504
4522
  });
4505
4523
  }
4524
+ design(bookId) {
4525
+ return handleAsyncFunction(async () => {
4526
+ const res = await this.engineAPI.fetcher.call({
4527
+ path: `/v1/books/${bookId}/design`,
4528
+ options: { method: "POST" }
4529
+ });
4530
+ bookPropsSchema.safeParse(res);
4531
+ return new Book(res);
4532
+ });
4533
+ }
4506
4534
  cancel(bookId) {
4507
4535
  return handleAsyncFunction(async () => {
4508
4536
  const res = await this.engineAPI.fetcher.call({
@@ -4955,6 +4983,7 @@ var MagicBookClient = class {
4955
4983
  bookDesignRequestSchema,
4956
4984
  bookPropsSchema,
4957
4985
  bookSizes,
4986
+ canSubmitDesignRequest,
4958
4987
  cancelledEventDetail,
4959
4988
  canvasSchema,
4960
4989
  coverTypes,