merchi_sdk_js 0.4.12 → 0.5.0

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "merchi_sdk_js",
3
- "version": "0.4.12",
3
+ "version": "0.5.0",
4
4
  "type": "module",
5
5
  "main": "src/merchi.js",
6
6
  "module": "src/merchi.js",
package/src/domain.js CHANGED
@@ -253,6 +253,159 @@ export function Domain() {
253
253
  this.isUnrestricted = function () {
254
254
  return this.domainType() === domainTypesInts.get('Unrestricted');
255
255
  }
256
+
257
+ function parsePayloadAndCallbacks(payload, success, error) {
258
+ if (typeof payload === 'function') {
259
+ return {payload: {}, success: payload, error: success};
260
+ }
261
+ return {payload: payload || {}, success: success, error: error};
262
+ }
263
+
264
+ function appendPayload(request, payload) {
265
+ Object.keys(payload).forEach(function(key) {
266
+ var value = payload[key];
267
+ if (value === undefined) {
268
+ return;
269
+ }
270
+ if (value !== null && typeof value === 'object') {
271
+ request.data().add(key, JSON.stringify(value));
272
+ } else {
273
+ request.data().add(key, value);
274
+ }
275
+ });
276
+ }
277
+
278
+ function sendStorefrontRequest(resource, method, payload, success, error) {
279
+ var request = new Request();
280
+ request.resource(resource).method(method);
281
+ request.query().add('skip_rights', 'y');
282
+ if (payload) {
283
+ appendPayload(request, payload);
284
+ }
285
+ function handleResponse(status, data) {
286
+ if (status >= 200 && status < 300) {
287
+ if (success) {
288
+ success(data);
289
+ }
290
+ } else if (error) {
291
+ error(status, data);
292
+ }
293
+ }
294
+ function handleError(status, data) {
295
+ if (error) {
296
+ error(status, data);
297
+ }
298
+ }
299
+ request.responseHandler(handleResponse).errorHandler(handleError);
300
+ request.send();
301
+ }
302
+
303
+ this.getStorefrontV2 = function (success, error) {
304
+ sendStorefrontRequest(
305
+ '/domains/' + this.id() + '/storefront_v2/',
306
+ 'GET',
307
+ null,
308
+ success,
309
+ error
310
+ );
311
+ };
312
+
313
+ this.provisionStorefrontV2 = function (payload, success, error) {
314
+ var args = parsePayloadAndCallbacks(payload, success, error);
315
+ sendStorefrontRequest(
316
+ '/domains/' + this.id() + '/storefront_v2/provision/',
317
+ 'POST',
318
+ args.payload,
319
+ args.success,
320
+ args.error
321
+ );
322
+ };
323
+
324
+ this.createStorefrontChangeRequest = function (payload, success, error) {
325
+ var args = parsePayloadAndCallbacks(payload, success, error);
326
+ sendStorefrontRequest(
327
+ '/domains/' + this.id() + '/storefront_v2/requests/',
328
+ 'POST',
329
+ args.payload,
330
+ args.success,
331
+ args.error
332
+ );
333
+ };
334
+
335
+ this.getStorefrontChangeRequest = function (requestId, success, error) {
336
+ sendStorefrontRequest(
337
+ '/storefront_change_requests/' + requestId + '/',
338
+ 'GET',
339
+ null,
340
+ success,
341
+ error
342
+ );
343
+ };
344
+
345
+ this.runStorefrontChangeRequest = function (requestId, payload, success, error) {
346
+ var args = parsePayloadAndCallbacks(payload, success, error);
347
+ sendStorefrontRequest(
348
+ '/storefront_change_requests/' + requestId + '/run/',
349
+ 'POST',
350
+ args.payload,
351
+ args.success,
352
+ args.error
353
+ );
354
+ };
355
+
356
+ this.approveStorefrontChangeRequest = function (requestId, payload, success, error) {
357
+ var args = parsePayloadAndCallbacks(payload, success, error);
358
+ sendStorefrontRequest(
359
+ '/storefront_change_requests/' + requestId + '/approve/',
360
+ 'POST',
361
+ args.payload,
362
+ args.success,
363
+ args.error
364
+ );
365
+ };
366
+
367
+ this.rejectStorefrontChangeRequest = function (requestId, payload, success, error) {
368
+ var args = parsePayloadAndCallbacks(payload, success, error);
369
+ sendStorefrontRequest(
370
+ '/storefront_change_requests/' + requestId + '/reject/',
371
+ 'POST',
372
+ args.payload,
373
+ args.success,
374
+ args.error
375
+ );
376
+ };
377
+
378
+ this.getStorefrontV2Deployments = function (success, error) {
379
+ sendStorefrontRequest(
380
+ '/domains/' + this.id() + '/storefront_v2/deployments/',
381
+ 'GET',
382
+ null,
383
+ success,
384
+ error
385
+ );
386
+ };
387
+
388
+ this.getStorefrontV2DeploymentLogs = function (deploymentId, success, error) {
389
+ sendStorefrontRequest(
390
+ '/domains/' + this.id() + '/storefront_v2/deployments/' +
391
+ deploymentId + '/logs/',
392
+ 'GET',
393
+ null,
394
+ success,
395
+ error
396
+ );
397
+ };
398
+
399
+ this.rollbackStorefrontV2 = function (payload, success, error) {
400
+ var args = parsePayloadAndCallbacks(payload, success, error);
401
+ sendStorefrontRequest(
402
+ '/domains/' + this.id() + '/storefront_v2/rollback/',
403
+ 'POST',
404
+ args.payload,
405
+ args.success,
406
+ args.error
407
+ );
408
+ };
256
409
  }
257
410
 
258
411
  export function Domains() {
package/src/merchi.js CHANGED
@@ -1,4 +1,3 @@
1
- import axios from 'axios';
2
1
  import { isBrowser } from 'browser-or-node';
3
2
  import { generateUUID } from './uuid.js';
4
3
  import { isNull, isUndefined, isUndefinedOrNull, id,
@@ -89,12 +88,6 @@ import { AgentSkillApproval, AgentSkillApprovals } from './agent_skill_approval.
89
88
  import { DomainChatSettings, DomainChatSettingsList } from './domain_chat_settings.js';
90
89
  import { SupportConversation, SupportConversations } from './support_conversation.js';
91
90
  import { SupportMessage, SupportMessages } from './support_message.js';
92
- import {
93
- ProductReview,
94
- ProductReviews,
95
- normalizeProductReviewApiJson,
96
- wireProductReviewCreateBody,
97
- } from './product_review.js';
98
91
 
99
92
  export function merchi(backendUri, websocketUri) {
100
93
  getGlobal().merchiJsonpHandlers = {};
@@ -873,98 +866,6 @@ export function merchi(backendUri, websocketUri) {
873
866
  request.send();
874
867
  }
875
868
 
876
- function productReviewsApiRoot() {
877
- var base = getGlobal().merchiBackendUri;
878
- if (!base.endsWith('/')) {
879
- base += '/';
880
- }
881
- return base + 'v6';
882
- }
883
-
884
- function productReviewsSessionSuffix() {
885
- if (getGlobal().currentSession &&
886
- getGlobal().currentSession.token()) {
887
- return '?session_token=' + encodeURIComponent(
888
- getGlobal().currentSession.token());
889
- }
890
- return '';
891
- }
892
-
893
- /**
894
- * List product reviews (domain manager/admin). ``success`` receives
895
- * ``{ productReviews: ... }`` — same keys as the API, but each row is a
896
- * ``ProductReview`` model instance with ``job`` / ``product`` / ``domain``.
897
- */
898
- function listProductReviews(productId, success, error) {
899
- var url = productReviewsApiRoot() + '/products/' + productId +
900
- '/reviews/' + productReviewsSessionSuffix();
901
- axios.get(url)
902
- .then(function (res) {
903
- var reviews = new ProductReviews().fromProductListResponse(
904
- res.data);
905
- success(Object.assign({}, res.data, {productReviews: reviews}));
906
- })
907
- .catch(function (err) {
908
- error(err.response ? err.response.data : err);
909
- });
910
- }
911
-
912
- /**
913
- * Create a product review (buyer, JSON body). Send ``jobId`` or ``job: { id }``
914
- * (completed job whose catalog product matches ``productId`` after clone
915
- * resolution), plus ``rating`` and optional ``title`` / ``content``.
916
- * ``success`` receives ``{ productReview }`` as a ``ProductReview`` model.
917
- */
918
- function createProductReview(productId, data, success, error) {
919
- var url = productReviewsApiRoot() + '/products/' + productId +
920
- '/reviews/' + productReviewsSessionSuffix();
921
- axios.post(url, wireProductReviewCreateBody(data), {
922
- headers: {'Content-Type': 'application/json'},
923
- })
924
- .then(function (res) {
925
- var pr = fromJson(
926
- new ProductReview(),
927
- normalizeProductReviewApiJson(res.data.productReview),
928
- {makesDirty: false});
929
- success(Object.assign({}, res.data, {productReview: pr}));
930
- })
931
- .catch(function (err) {
932
- error(err.response ? err.response.data : err);
933
- });
934
- }
935
-
936
- /**
937
- * Patch a review (``formFields`` plain object: status, rejectionReason,
938
- * rating, title, content — sent as multipart form data).
939
- * ``success`` receives ``{ productReview }`` as a model when present.
940
- */
941
- function patchProductReview(reviewId, formFields, success, error) {
942
- var url = productReviewsApiRoot() + '/product-reviews/' + reviewId +
943
- '/' + productReviewsSessionSuffix();
944
- var fd = new FormData();
945
- Object.keys(formFields || {}).forEach(function (k) {
946
- var v = formFields[k];
947
- if (v !== undefined && v !== null) {
948
- fd.append(k, String(v));
949
- }
950
- });
951
- axios.patch(url, fd)
952
- .then(function (res) {
953
- if (!res.data || !res.data.productReview) {
954
- success(res.data);
955
- return;
956
- }
957
- var pr = fromJson(
958
- new ProductReview(),
959
- normalizeProductReviewApiJson(res.data.productReview),
960
- {makesDirty: false});
961
- success(Object.assign({}, res.data, {productReview: pr}));
962
- })
963
- .catch(function (err) {
964
- error(err.response ? err.response.data : err);
965
- });
966
- }
967
-
968
869
  function getProductShipmentOptions(
969
870
  productId, quantity, address, success, error) {
970
871
  var request = new Request(),
@@ -1124,10 +1025,6 @@ export function merchi(backendUri, websocketUri) {
1124
1025
  'supportConversations': new SupportConversations(),
1125
1026
  'SupportMessage': SupportMessage,
1126
1027
  'supportMessages': new SupportMessages(),
1127
- 'ProductReview': ProductReview,
1128
- 'productReviews': new ProductReviews(),
1129
- 'normalizeProductReviewApiJson': normalizeProductReviewApiJson,
1130
- 'wireProductReviewCreateBody': wireProductReviewCreateBody,
1131
1028
  'SubscriptionPlan': SubscriptionPlan,
1132
1029
  'subscriptionPlans': new SubscriptionPlans(),
1133
1030
  'VariationField': VariationField,
@@ -1206,8 +1103,5 @@ export function merchi(backendUri, websocketUri) {
1206
1103
  'productTypes': productTypes,
1207
1104
  'productTypesInts': productTypesInts,
1208
1105
  'productTypesSeller': productTypesSeller,
1209
- 'getProductShipmentOptions': getProductShipmentOptions,
1210
- 'listProductReviews': listProductReviews,
1211
- 'createProductReview': createProductReview,
1212
- 'patchProductReview': patchProductReview};
1106
+ 'getProductShipmentOptions': getProductShipmentOptions};
1213
1107
  }
@@ -1,96 +0,0 @@
1
- import { generateUUID } from './uuid.js';
2
- import { addPropertyTo, fromJson } from './model.js';
3
- import { Domain } from './domain.js';
4
- import { Job } from './job.js';
5
- import { Product } from './product.js';
6
-
7
- /**
8
- * Map flat API fields (jobId, productId, domainId) onto job / product / domain
9
- * before ``fromJson``.
10
- */
11
- export function normalizeProductReviewApiJson(json) {
12
- if (json === null || json === undefined || typeof json !== 'object') {
13
- return json;
14
- }
15
- var row = Object.assign({}, json);
16
- if (row.jobId !== undefined && row.job === undefined) {
17
- if (row.jobId != null) {
18
- row.job = {id: row.jobId};
19
- }
20
- delete row.jobId;
21
- }
22
- if (row.productId !== undefined && row.product === undefined) {
23
- if (row.productId != null) {
24
- row.product = {id: row.productId};
25
- }
26
- delete row.productId;
27
- }
28
- if (row.domainId !== undefined && row.domain === undefined) {
29
- if (row.domainId != null) {
30
- row.domain = {id: row.domainId};
31
- }
32
- delete row.domainId;
33
- }
34
- return row;
35
- }
36
-
37
- /**
38
- * Turn create payload ``{ job: { id }, rating, ... }`` into API JSON with ``jobId``.
39
- * Pass-through if ``jobId`` is already set.
40
- */
41
- export function wireProductReviewCreateBody(data) {
42
- if (!data || typeof data !== 'object') {
43
- return data;
44
- }
45
- var body = Object.assign({}, data);
46
- if (body.job && body.job.id != null && body.jobId === undefined) {
47
- body.jobId = body.job.id;
48
- delete body.job;
49
- }
50
- return body;
51
- }
52
-
53
- export function ProductReview() {
54
- this.resource = '/product-reviews';
55
- this.json = 'productReview';
56
- this.temporaryId = generateUUID();
57
-
58
- addPropertyTo(this, 'id');
59
- addPropertyTo(this, 'product', Product);
60
- addPropertyTo(this, 'domain', Domain);
61
- addPropertyTo(this, 'authorUserId');
62
- addPropertyTo(this, 'authorName');
63
- addPropertyTo(this, 'job', Job);
64
- addPropertyTo(this, 'rating');
65
- addPropertyTo(this, 'title');
66
- addPropertyTo(this, 'content');
67
- addPropertyTo(this, 'status');
68
- addPropertyTo(this, 'purchaseInvoiceId');
69
- addPropertyTo(this, 'submittedAt');
70
- addPropertyTo(this, 'updatedAt');
71
- addPropertyTo(this, 'rejectionReason');
72
- }
73
-
74
- export function ProductReviews() {
75
- this.json = 'productReviews';
76
- this.single = ProductReview;
77
-
78
- /**
79
- * @param {object} data - API body with ``productReviews`` array
80
- * @param {object} [options] - passed to ``fromJson`` (e.g. ``{makesDirty: false}``)
81
- * @returns {ProductReview[]}
82
- */
83
- this.fromProductListResponse = function (data, options) {
84
- var raw = (data && data.productReviews) ? data.productReviews : [];
85
- var self = this;
86
- var defaults = {makesDirty: false};
87
- var opts = Object.assign({}, defaults, options);
88
- return raw.map(function (row) {
89
- return fromJson(
90
- new self.single(),
91
- normalizeProductReviewApiJson(row),
92
- opts
93
- );
94
- });
95
- };
96
- }