@magiclabs.ai/magicbook-client 0.7.3 → 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 +1 -0
- package/index.cjs +54 -24
- package/index.cjs.map +1 -1
- package/index.d.cts +10 -3
- package/index.d.ts +10 -3
- package/index.js +53 -24
- package/index.js.map +1 -1
- package/package.json +1 -1
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-
|
|
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.
|
|
4144
|
-
|
|
4145
|
-
|
|
4146
|
-
|
|
4147
|
-
|
|
4148
|
-
|
|
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
|
+
}
|
|
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,17 @@ var DesignRequest = class {
|
|
|
4277
4292
|
return options;
|
|
4278
4293
|
}
|
|
4279
4294
|
async submit(submitDesignRequestProps) {
|
|
4280
|
-
if (
|
|
4281
|
-
throw new Error("
|
|
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");
|
|
4282
4297
|
} else {
|
|
4283
|
-
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.
|
|
4302
|
+
(await this.client.engineAPI.books.design(this.parentId)).toDesignRequestProps()
|
|
4287
4303
|
);
|
|
4288
4304
|
this.getProgress();
|
|
4289
|
-
this.state
|
|
4305
|
+
this.updateDesignRequest({ state: states[1] });
|
|
4290
4306
|
return this;
|
|
4291
4307
|
}
|
|
4292
4308
|
}
|
|
@@ -4309,10 +4325,10 @@ var DesignRequest = class {
|
|
|
4309
4325
|
} else if (!isDesignRequestSubmitted(this.state)) {
|
|
4310
4326
|
throw new Error("Design request not submitted");
|
|
4311
4327
|
} else {
|
|
4312
|
-
this.updateDesignRequest(
|
|
4313
|
-
(await this.client.engineAPI.books.cancel(this.parentId)).toDesignRequestProps()
|
|
4314
|
-
|
|
4315
|
-
|
|
4328
|
+
this.updateDesignRequest({
|
|
4329
|
+
...(await this.client.engineAPI.books.cancel(this.parentId)).toDesignRequestProps(),
|
|
4330
|
+
state: "cancelled"
|
|
4331
|
+
});
|
|
4316
4332
|
await this.eventHandler(cancelledEventDetail);
|
|
4317
4333
|
return this;
|
|
4318
4334
|
}
|
|
@@ -4338,7 +4354,7 @@ var DesignRequest = class {
|
|
|
4338
4354
|
});
|
|
4339
4355
|
}
|
|
4340
4356
|
}
|
|
4341
|
-
this.state
|
|
4357
|
+
this.updateDesignRequest({ state: detail.slug });
|
|
4342
4358
|
window.dispatchEvent(customEvent);
|
|
4343
4359
|
}
|
|
4344
4360
|
timeoutHandler() {
|
|
@@ -4365,10 +4381,13 @@ var DesignRequest = class {
|
|
|
4365
4381
|
}
|
|
4366
4382
|
}
|
|
4367
4383
|
toBook() {
|
|
4368
|
-
const designRequest = {
|
|
4369
|
-
|
|
4370
|
-
|
|
4371
|
-
|
|
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;
|
|
4372
4391
|
const styleSlug = styles[this.style].slug;
|
|
4373
4392
|
const bookDesignRequest = camelCaseObjectKeysToSnakeCase(cleanJSON(designRequest));
|
|
4374
4393
|
bookDesignRequest.style = styleSlug;
|
|
@@ -4503,6 +4522,16 @@ var BooksEndpoints = class {
|
|
|
4503
4522
|
return new Book(res);
|
|
4504
4523
|
});
|
|
4505
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
|
+
}
|
|
4506
4535
|
cancel(bookId) {
|
|
4507
4536
|
return handleAsyncFunction(async () => {
|
|
4508
4537
|
const res = await this.engineAPI.fetcher.call({
|
|
@@ -4955,6 +4984,7 @@ var MagicBookClient = class {
|
|
|
4955
4984
|
bookDesignRequestSchema,
|
|
4956
4985
|
bookPropsSchema,
|
|
4957
4986
|
bookSizes,
|
|
4987
|
+
canResubmitDesignRequest,
|
|
4958
4988
|
cancelledEventDetail,
|
|
4959
4989
|
canvasSchema,
|
|
4960
4990
|
coverTypes,
|