@magiclabs.ai/magicbook-client 0.7.1-canary → 0.7.2-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/index.d.cts CHANGED
@@ -996,10 +996,9 @@ type DesignRequestEventDetail = {
996
996
  };
997
997
  type DesignRequestEvent = CustomEvent<DesignRequestEventDetail>;
998
998
  declare class DesignRequest {
999
+ private readonly parentId;
999
1000
  private readonly client;
1000
- private webSocket;
1001
1001
  state: State;
1002
- parentId: string;
1003
1002
  title: string;
1004
1003
  subtitle?: string;
1005
1004
  occasion: Occasion;
@@ -1012,6 +1011,7 @@ declare class DesignRequest {
1012
1011
  embellishmentLevel: EmbellishmentLevel;
1013
1012
  textStickerLevel: TextStickerLevel;
1014
1013
  images: Images;
1014
+ private webSocket?;
1015
1015
  userId?: string;
1016
1016
  guid?: string;
1017
1017
  timeout?: number;
package/index.d.ts CHANGED
@@ -996,10 +996,9 @@ type DesignRequestEventDetail = {
996
996
  };
997
997
  type DesignRequestEvent = CustomEvent<DesignRequestEventDetail>;
998
998
  declare class DesignRequest {
999
+ private readonly parentId;
999
1000
  private readonly client;
1000
- private webSocket;
1001
1001
  state: State;
1002
- parentId: string;
1003
1002
  title: string;
1004
1003
  subtitle?: string;
1005
1004
  occasion: Occasion;
@@ -1012,6 +1011,7 @@ declare class DesignRequest {
1012
1011
  embellishmentLevel: EmbellishmentLevel;
1013
1012
  textStickerLevel: TextStickerLevel;
1014
1013
  images: Images;
1014
+ private webSocket?;
1015
1015
  userId?: string;
1016
1016
  guid?: string;
1017
1017
  timeout?: number;
package/index.js CHANGED
@@ -4157,11 +4157,9 @@ var DesignRequestOptions = {
4157
4157
  textStickerLevel: textStickerLevels
4158
4158
  };
4159
4159
  var DesignRequest = class {
4160
- // eslint-disable-next-line no-unused-vars
4161
4160
  constructor(parentId, client, designRequestProps) {
4162
- this.client = client;
4163
4161
  this.parentId = parentId;
4164
- this.webSocket = new WebSocket(`${this.client.webSocketHost}/?book_id=${this.parentId}`);
4162
+ this.client = client;
4165
4163
  this.state = designRequestProps?.state || states[0];
4166
4164
  this.title = designRequestProps?.title || "";
4167
4165
  this.subtitle = designRequestProps?.subtitle;
@@ -4177,9 +4175,7 @@ var DesignRequest = class {
4177
4175
  this.images = new Images(this.client, this.parentId);
4178
4176
  this.userId = designRequestProps?.userId;
4179
4177
  }
4180
- webSocket;
4181
4178
  state;
4182
- parentId;
4183
4179
  title;
4184
4180
  subtitle;
4185
4181
  occasion;
@@ -4192,6 +4188,7 @@ var DesignRequest = class {
4192
4188
  embellishmentLevel;
4193
4189
  textStickerLevel;
4194
4190
  images;
4191
+ webSocket;
4195
4192
  userId;
4196
4193
  guid;
4197
4194
  timeout;
@@ -4213,6 +4210,7 @@ var DesignRequest = class {
4213
4210
  throw new Error("Design request already submitted");
4214
4211
  } else {
4215
4212
  submitDesignRequestProps && Object.assign(this, submitDesignRequestProps);
4213
+ this.webSocket = new WebSocket(`${this.client.webSocketHost}/?book_id=${this.parentId}`);
4216
4214
  this.updateDesignRequest(
4217
4215
  (await this.client.engineAPI.books.update(this.parentId, this.toBook())).toDesignRequestProps()
4218
4216
  );
@@ -4261,7 +4259,7 @@ var DesignRequest = class {
4261
4259
  async eventHandler(detail, type = "MagicBook.designRequestUpdated") {
4262
4260
  const customEvent = new CustomEvent(type, { detail });
4263
4261
  if (statesToCloseWS.includes(detail.slug)) {
4264
- this.webSocket.close();
4262
+ this.webSocket?.close();
4265
4263
  if (statesToReport.includes(detail.slug)) {
4266
4264
  await this.client.engineAPI.books.report(this.parentId, {
4267
4265
  error: detail.slug === "error" ? "design" : "timeout",
@@ -4282,16 +4280,18 @@ var DesignRequest = class {
4282
4280
  }
4283
4281
  }
4284
4282
  async getProgress() {
4285
- let timeout;
4286
- this.webSocket.onmessage = async (event) => {
4287
- const detail = JSON.parse(event.data);
4288
- if (this.state !== detail.slug) {
4289
- timeout && clearTimeout(timeout);
4290
- timeout = this.timeoutHandler();
4291
- await this.eventHandler(detail);
4292
- }
4293
- };
4294
- this.webSocket.onclose = () => clearTimeout(timeout);
4283
+ if (this.webSocket) {
4284
+ let timeout;
4285
+ this.webSocket.onmessage = async (event) => {
4286
+ const detail = JSON.parse(event.data);
4287
+ if (this.state !== detail.slug) {
4288
+ timeout && clearTimeout(timeout);
4289
+ timeout = this.timeoutHandler();
4290
+ await this.eventHandler(detail);
4291
+ }
4292
+ };
4293
+ this.webSocket.onclose = () => clearTimeout(timeout);
4294
+ }
4295
4295
  }
4296
4296
  toBook() {
4297
4297
  const designRequest = { ...this };