merchi_sdk_ts 0.0.1
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/.babelrc +13 -0
- package/.eslintrc.cjs +43 -0
- package/LICENSE +21 -0
- package/README.md +2 -0
- package/jest.config.js +17 -0
- package/package.json +44 -0
- package/src/constants/auto_assign_production_on_actions.ts +6 -0
- package/src/constants/call_to_actions.test.ts +5 -0
- package/src/constants/call_to_actions.ts +14 -0
- package/src/constants/discount_types.test.ts +5 -0
- package/src/constants/discount_types.ts +3 -0
- package/src/constants/domain_types.test.ts +5 -0
- package/src/constants/domain_types.ts +12 -0
- package/src/constants/errors.ts +32 -0
- package/src/constants/event_types.test.ts +5 -0
- package/src/constants/event_types.ts +5 -0
- package/src/constants/field_types.test.ts +5 -0
- package/src/constants/field_types.ts +13 -0
- package/src/constants/input_types.test.ts +5 -0
- package/src/constants/input_types.ts +13 -0
- package/src/constants/inventory_status.test.ts +5 -0
- package/src/constants/inventory_statuses.ts +6 -0
- package/src/constants/invoice_types.test.ts +5 -0
- package/src/constants/invoice_types.ts +5 -0
- package/src/constants/item_types.test.ts +5 -0
- package/src/constants/item_types.ts +5 -0
- package/src/constants/job/drafting_status.test.ts +5 -0
- package/src/constants/job/drafting_status.ts +7 -0
- package/src/constants/job/payment_status.test.ts +5 -0
- package/src/constants/job/payment_status.ts +7 -0
- package/src/constants/job/production_status.test.ts +5 -0
- package/src/constants/job/production_status.ts +12 -0
- package/src/constants/job/shipment_status.test.ts +5 -0
- package/src/constants/job/shipment_status.ts +11 -0
- package/src/constants/job/supply_chain_request_status.test.ts +5 -0
- package/src/constants/job/supply_chain_request_status.ts +6 -0
- package/src/constants/job_priorities.test.ts +5 -0
- package/src/constants/job_priorities.ts +6 -0
- package/src/constants/job_status.test.ts +5 -0
- package/src/constants/job_status.ts +49 -0
- package/src/constants/job_types.test.ts +5 -0
- package/src/constants/job_types.ts +15 -0
- package/src/constants/notification_sections.test.ts +5 -0
- package/src/constants/notification_sections.ts +13 -0
- package/src/constants/notification_types.test.ts +5 -0
- package/src/constants/notification_types.ts +97 -0
- package/src/constants/notification_urgencies.test.ts +5 -0
- package/src/constants/notification_urgencies.ts +7 -0
- package/src/constants/payment_types.test.ts +5 -0
- package/src/constants/payment_types.ts +10 -0
- package/src/constants/platform.test.ts +15 -0
- package/src/constants/platform.ts +6 -0
- package/src/constants/product_types.test.ts +6 -0
- package/src/constants/product_types.ts +25 -0
- package/src/constants/rights.test.ts +5 -0
- package/src/constants/rights.ts +5 -0
- package/src/constants/robots_meta_directives.test.ts +5 -0
- package/src/constants/robots_meta_directives.ts +11 -0
- package/src/constants/roles.test.ts +5 -0
- package/src/constants/roles.ts +38 -0
- package/src/constants/shipment_companies.test.ts +5 -0
- package/src/constants/shipment_companies.ts +18 -0
- package/src/constants/shipment_services.test.ts +5 -0
- package/src/constants/shipment_services.ts +3 -0
- package/src/constants/system_roles.test.ts +5 -0
- package/src/constants/system_roles.ts +3 -0
- package/src/constants/theme_foundations.test.ts +5 -0
- package/src/constants/theme_foundations.ts +6 -0
- package/src/constants/theme_status.test.ts +5 -0
- package/src/constants/theme_status.ts +5 -0
- package/src/constants/user_types.test.ts +5 -0
- package/src/constants/user_types.ts +17 -0
- package/src/constants/white_label_accessibilities.test.ts +5 -0
- package/src/constants/white_label_accessibilities.ts +6 -0
- package/src/cookie.test.ts +11 -0
- package/src/cookie.ts +16 -0
- package/src/entities/address.test.ts +7 -0
- package/src/entities/address.ts +65 -0
- package/src/entities/assignment.test.ts +39 -0
- package/src/entities/assignment.ts +92 -0
- package/src/entities/automatic_payment_relationship.test.ts +8 -0
- package/src/entities/automatic_payment_relationship.ts +26 -0
- package/src/entities/backup.test.ts +7 -0
- package/src/entities/backup.ts +14 -0
- package/src/entities/bank.test.ts +7 -0
- package/src/entities/bank.ts +45 -0
- package/src/entities/cart.test.ts +50 -0
- package/src/entities/cart.ts +89 -0
- package/src/entities/cart_item.test.ts +33 -0
- package/src/entities/cart_item.ts +77 -0
- package/src/entities/cart_shipment_group.ts +21 -0
- package/src/entities/cart_shipment_quote.ts +33 -0
- package/src/entities/category.test.ts +57 -0
- package/src/entities/category.ts +37 -0
- package/src/entities/company.test.ts +7 -0
- package/src/entities/company.ts +275 -0
- package/src/entities/company_invitation.test.ts +7 -0
- package/src/entities/company_invitation.ts +33 -0
- package/src/entities/component.test.ts +14 -0
- package/src/entities/component.ts +92 -0
- package/src/entities/component_tag.test.ts +7 -0
- package/src/entities/component_tag.ts +17 -0
- package/src/entities/component_version.test.ts +7 -0
- package/src/entities/component_version.ts +30 -0
- package/src/entities/country_tax.test.ts +13 -0
- package/src/entities/country_tax.ts +46 -0
- package/src/entities/discount.test.ts +18 -0
- package/src/entities/discount.ts +34 -0
- package/src/entities/discount_group.test.ts +7 -0
- package/src/entities/discount_group.ts +28 -0
- package/src/entities/domain.test.ts +79 -0
- package/src/entities/domain.ts +246 -0
- package/src/entities/domain_invitation.test.ts +7 -0
- package/src/entities/domain_invitation.ts +36 -0
- package/src/entities/domain_tag.test.ts +7 -0
- package/src/entities/domain_tag.ts +42 -0
- package/src/entities/draft.test.ts +43 -0
- package/src/entities/draft.ts +88 -0
- package/src/entities/draft_comment.test.ts +7 -0
- package/src/entities/draft_comment.ts +54 -0
- package/src/entities/draft_template.test.ts +7 -0
- package/src/entities/draft_template.ts +40 -0
- package/src/entities/email_address.test.ts +7 -0
- package/src/entities/email_address.ts +24 -0
- package/src/entities/email_counter.test.ts +7 -0
- package/src/entities/email_counter.ts +20 -0
- package/src/entities/enrolled_domain.test.ts +7 -0
- package/src/entities/enrolled_domain.ts +40 -0
- package/src/entities/exchange_rate.test.ts +7 -0
- package/src/entities/exchange_rate.ts +22 -0
- package/src/entities/file.test.ts +43 -0
- package/src/entities/file.ts +172 -0
- package/src/entities/internal_tag.test.ts +7 -0
- package/src/entities/internal_tag.ts +51 -0
- package/src/entities/inventory.test.ts +28 -0
- package/src/entities/inventory.ts +56 -0
- package/src/entities/inventory_unit_variation.test.ts +7 -0
- package/src/entities/inventory_unit_variation.ts +29 -0
- package/src/entities/invoice.test.ts +43 -0
- package/src/entities/invoice.ts +179 -0
- package/src/entities/item.test.ts +19 -0
- package/src/entities/item.ts +44 -0
- package/src/entities/job.test.ts +95 -0
- package/src/entities/job.ts +349 -0
- package/src/entities/job_comment.test.ts +7 -0
- package/src/entities/job_comment.ts +50 -0
- package/src/entities/matching_inventory.ts +29 -0
- package/src/entities/menu.test.ts +7 -0
- package/src/entities/menu.ts +30 -0
- package/src/entities/menu_item.test.ts +7 -0
- package/src/entities/menu_item.ts +29 -0
- package/src/entities/notification.test.ts +7 -0
- package/src/entities/notification.ts +99 -0
- package/src/entities/page.test.ts +7 -0
- package/src/entities/page.ts +32 -0
- package/src/entities/payment.test.ts +19 -0
- package/src/entities/payment.ts +65 -0
- package/src/entities/payment_device.test.ts +7 -0
- package/src/entities/payment_device.ts +30 -0
- package/src/entities/phone_number.test.ts +7 -0
- package/src/entities/phone_number.ts +36 -0
- package/src/entities/product.test.ts +750 -0
- package/src/entities/product.ts +427 -0
- package/src/entities/production_comment.test.ts +7 -0
- package/src/entities/production_comment.ts +50 -0
- package/src/entities/quote.test.ts +98 -0
- package/src/entities/quote.ts +140 -0
- package/src/entities/quote_item.test.ts +50 -0
- package/src/entities/quote_item.ts +71 -0
- package/src/entities/seo_domain_page.test.ts +7 -0
- package/src/entities/seo_domain_page.ts +33 -0
- package/src/entities/session.test.ts +7 -0
- package/src/entities/session.ts +28 -0
- package/src/entities/shipment.test.ts +24 -0
- package/src/entities/shipment.ts +185 -0
- package/src/entities/shipment_item.test.ts +7 -0
- package/src/entities/shipment_item.ts +15 -0
- package/src/entities/shipment_item_fulfillment.test.ts +8 -0
- package/src/entities/shipment_item_fulfillment.ts +15 -0
- package/src/entities/shipment_method.test.ts +7 -0
- package/src/entities/shipment_method.ts +61 -0
- package/src/entities/shipment_method_variation.test.ts +7 -0
- package/src/entities/shipment_method_variation.ts +39 -0
- package/src/entities/short_url.test.ts +7 -0
- package/src/entities/short_url.ts +36 -0
- package/src/entities/subscription_plan.test.ts +7 -0
- package/src/entities/subscription_plan.ts +73 -0
- package/src/entities/supply_domain.test.ts +7 -0
- package/src/entities/supply_domain.ts +27 -0
- package/src/entities/system_role.test.ts +7 -0
- package/src/entities/system_role.ts +17 -0
- package/src/entities/theme.test.ts +38 -0
- package/src/entities/theme.ts +267 -0
- package/src/entities/theme_css_setting.test.ts +7 -0
- package/src/entities/theme_css_setting.ts +23 -0
- package/src/entities/user.test.ts +154 -0
- package/src/entities/user.ts +394 -0
- package/src/entities/user_company.test.ts +7 -0
- package/src/entities/user_company.ts +27 -0
- package/src/entities/variation.test.ts +7 -0
- package/src/entities/variation.ts +61 -0
- package/src/entities/variation_field.test.ts +76 -0
- package/src/entities/variation_field.ts +169 -0
- package/src/entities/variation_fields_option.test.ts +46 -0
- package/src/entities/variation_fields_option.ts +95 -0
- package/src/entities/variation_option.test.ts +7 -0
- package/src/entities/variation_option.ts +52 -0
- package/src/entities/variations_group.test.ts +7 -0
- package/src/entities/variations_group.ts +41 -0
- package/src/entity.ts +1002 -0
- package/src/merchi.test.ts +25 -0
- package/src/merchi.ts +379 -0
- package/src/request.test.ts +43 -0
- package/src/request.ts +124 -0
- package/src/test_util.ts +18 -0
- package/src/toasts.test.ts +38 -0
- package/src/toasts.ts +34 -0
- package/src/util/float.ts +12 -0
- package/src/util/query_string.test.ts +7 -0
- package/src/util/query_string.ts +11 -0
- package/src/util/validation.test.ts +16 -0
- package/src/util/validation.ts +2 -0
- package/src/uuid.test.ts +15 -0
- package/src/uuid.ts +8 -0
- package/tsconfig.json +23 -0
- package/webpack.config.cjs +27 -0
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { setup, mockFetch } from './test_util';
|
|
2
|
+
import { Merchi } from './merchi';
|
|
3
|
+
|
|
4
|
+
setup();
|
|
5
|
+
|
|
6
|
+
test('can get current user merchi has session token', () => {
|
|
7
|
+
const testToken = 'YrDwzmh8&QGtAfg9quh(4QfSlE^RPXWl';
|
|
8
|
+
const merchi = new Merchi(testToken);
|
|
9
|
+
mockFetch(true, { session: { user: { name: 'currentUser' } } }, 200);
|
|
10
|
+
return merchi
|
|
11
|
+
.getCurrentUser()
|
|
12
|
+
.then(user => expect(user.name).toBe('currentUser'));
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
test('get current user return empty if there is no cookie', () => {
|
|
16
|
+
Object.defineProperty(document, 'cookie', {
|
|
17
|
+
get: jest.fn().mockImplementation(() => {
|
|
18
|
+
return '';
|
|
19
|
+
}),
|
|
20
|
+
});
|
|
21
|
+
const merchi = new Merchi();
|
|
22
|
+
mockFetch(true, { user: { name: 'currentUser' } }, 200);
|
|
23
|
+
return merchi.getCurrentUser({embed: {}}).then(
|
|
24
|
+
user => expect(user).toBe(null));
|
|
25
|
+
});
|
package/src/merchi.ts
ADDED
|
@@ -0,0 +1,379 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Entity,
|
|
3
|
+
// eslint-disable-next-line no-unused-vars
|
|
4
|
+
EmbedDescriptor,
|
|
5
|
+
} from './entity';
|
|
6
|
+
import { AutomaticPaymentRelationship } from './entities/automatic_payment_relationship';
|
|
7
|
+
import { Session } from './entities/session';
|
|
8
|
+
import { JobComment } from './entities/job_comment';
|
|
9
|
+
import { Domain } from './entities/domain';
|
|
10
|
+
import { ExchangeRate } from './entities/exchange_rate';
|
|
11
|
+
import { Job } from './entities/job';
|
|
12
|
+
import { Menu } from './entities/menu';
|
|
13
|
+
import { Backup } from './entities/backup';
|
|
14
|
+
import { VariationField } from './entities/variation_field';
|
|
15
|
+
import { VariationOption } from './entities/variation_option';
|
|
16
|
+
import { ProductionComment } from './entities/production_comment';
|
|
17
|
+
import { Product } from './entities/product';
|
|
18
|
+
import { InternalTag } from './entities/internal_tag';
|
|
19
|
+
import { Inventory } from './entities/inventory';
|
|
20
|
+
import { QuoteItem } from './entities/quote_item';
|
|
21
|
+
import { Category } from './entities/category';
|
|
22
|
+
import { Invoice } from './entities/invoice';
|
|
23
|
+
import { UserCompany } from './entities/user_company';
|
|
24
|
+
import { InventoryUnitVariation } from './entities/inventory_unit_variation';
|
|
25
|
+
import { VariationFieldsOption } from './entities/variation_fields_option';
|
|
26
|
+
import { Bank } from './entities/bank';
|
|
27
|
+
import { Shipment } from './entities/shipment';
|
|
28
|
+
import { ShipmentItem } from './entities/shipment_item';
|
|
29
|
+
import { ShipmentItemFulfillment } from './entities/shipment_item_fulfillment';
|
|
30
|
+
import { ShipmentMethod } from './entities/shipment_method';
|
|
31
|
+
import { ShipmentMethodVariation } from './entities/shipment_method_variation';
|
|
32
|
+
import { DomainInvitation } from './entities/domain_invitation';
|
|
33
|
+
import { EmailCounter } from './entities/email_counter';
|
|
34
|
+
import { MenuItem } from './entities/menu_item';
|
|
35
|
+
import { SupplyDomain } from './entities/supply_domain';
|
|
36
|
+
import { Cart } from './entities/cart';
|
|
37
|
+
import { CartShipmentGroup } from './entities/cart_shipment_group';
|
|
38
|
+
import { CartShipmentQuote } from './entities/cart_shipment_quote';
|
|
39
|
+
import { Theme } from './entities/theme';
|
|
40
|
+
import { ThemeCssSetting } from './entities/theme_css_setting';
|
|
41
|
+
import { Component } from './entities/component';
|
|
42
|
+
import { ComponentVersion } from './entities/component_version';
|
|
43
|
+
import { MerchiFile } from './entities/file';
|
|
44
|
+
import { EmailAddress } from './entities/email_address';
|
|
45
|
+
import { SeoDomainPage } from './entities/seo_domain_page';
|
|
46
|
+
import { ShortUrl } from './entities/short_url';
|
|
47
|
+
import { VariationsGroup } from './entities/variations_group';
|
|
48
|
+
import { Quote } from './entities/quote';
|
|
49
|
+
import { Draft } from './entities/draft';
|
|
50
|
+
import { DraftTemplate } from './entities/draft_template';
|
|
51
|
+
import { Discount } from './entities/discount';
|
|
52
|
+
import { DiscountGroup } from './entities/discount_group';
|
|
53
|
+
import { User } from './entities/user';
|
|
54
|
+
import { Company } from './entities/company';
|
|
55
|
+
import { ComponentTag } from './entities/component_tag';
|
|
56
|
+
import { EnrolledDomain } from './entities/enrolled_domain';
|
|
57
|
+
import { CountryTax } from './entities/country_tax';
|
|
58
|
+
import { Item } from './entities/item';
|
|
59
|
+
import { DomainTag } from './entities/domain_tag';
|
|
60
|
+
import { DraftComment } from './entities/draft_comment';
|
|
61
|
+
import { Notification } from './entities/notification';
|
|
62
|
+
import { Payment } from './entities/payment';
|
|
63
|
+
import { Page } from './entities/page';
|
|
64
|
+
import { CompanyInvitation } from './entities/company_invitation';
|
|
65
|
+
import { SystemRole } from './entities/system_role';
|
|
66
|
+
import { PaymentDevice } from './entities/payment_device';
|
|
67
|
+
import { PhoneNumber } from './entities/phone_number';
|
|
68
|
+
import { Variation } from './entities/variation';
|
|
69
|
+
import { CartItem } from './entities/cart_item';
|
|
70
|
+
import { Address } from './entities/address';
|
|
71
|
+
import { Assignment } from './entities/assignment';
|
|
72
|
+
import { MatchingInventory } from './entities/matching_inventory';
|
|
73
|
+
import { SubscriptionPlan } from './entities/subscription_plan';
|
|
74
|
+
import { generateUUID } from './uuid';
|
|
75
|
+
// eslint-disable-next-line no-unused-vars
|
|
76
|
+
import { RequestOptions, apiFetch, apiFetchWithProgress } from './request';
|
|
77
|
+
import { getCookie } from './cookie';
|
|
78
|
+
|
|
79
|
+
// the type of classes
|
|
80
|
+
export interface Type<T, A extends any[]> extends Function {
|
|
81
|
+
new (...args: A): T;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function cloneClass<T, A extends []>(
|
|
85
|
+
original: Type<T, A>,
|
|
86
|
+
arg: any
|
|
87
|
+
): Type<T, A> {
|
|
88
|
+
// copy the constructor, but use the empty object as `this`
|
|
89
|
+
const copy = original.bind({}, arg);
|
|
90
|
+
// pick up any static members (this is shallow, the members are not copied)
|
|
91
|
+
Object.assign(copy, original);
|
|
92
|
+
return copy;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
interface UserRequestOptions {
|
|
96
|
+
embed?: EmbedDescriptor;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export class Merchi {
|
|
100
|
+
public id: string = generateUUID();
|
|
101
|
+
|
|
102
|
+
public sessionToken?: string;
|
|
103
|
+
public invoiceToken?: string;
|
|
104
|
+
public clientToken?: string;
|
|
105
|
+
public cartToken?: string;
|
|
106
|
+
|
|
107
|
+
public AutomaticPaymentRelationship: typeof AutomaticPaymentRelationship;
|
|
108
|
+
public Notification: typeof Notification;
|
|
109
|
+
public EnrolledDomain: typeof EnrolledDomain;
|
|
110
|
+
public Backup: typeof Backup;
|
|
111
|
+
public SystemRole: typeof SystemRole;
|
|
112
|
+
public UserCompany: typeof UserCompany;
|
|
113
|
+
public Variation: typeof Variation;
|
|
114
|
+
public CountryTax: typeof CountryTax;
|
|
115
|
+
public MenuItem: typeof MenuItem;
|
|
116
|
+
public VariationField: typeof VariationField;
|
|
117
|
+
public Assignment: typeof Assignment;
|
|
118
|
+
public InternalTag: typeof InternalTag;
|
|
119
|
+
public Inventory: typeof Inventory;
|
|
120
|
+
public JobComment: typeof JobComment;
|
|
121
|
+
public VariationOption: typeof VariationOption;
|
|
122
|
+
public SupplyDomain: typeof SupplyDomain;
|
|
123
|
+
public ProductionComment: typeof ProductionComment;
|
|
124
|
+
public DraftComment: typeof DraftComment;
|
|
125
|
+
public Shipment: typeof Shipment;
|
|
126
|
+
public ShipmentItem: typeof ShipmentItem;
|
|
127
|
+
public ShipmentItemFulfillment: typeof ShipmentItemFulfillment;
|
|
128
|
+
public ShipmentMethod: typeof ShipmentMethod;
|
|
129
|
+
public ShipmentMethodVariation: typeof ShipmentMethodVariation;
|
|
130
|
+
public Draft: typeof Draft;
|
|
131
|
+
public DraftTemplate: typeof DraftTemplate;
|
|
132
|
+
public VariationFieldsOption: typeof VariationFieldsOption;
|
|
133
|
+
public Category: typeof Category;
|
|
134
|
+
public MerchiFile: typeof MerchiFile;
|
|
135
|
+
public Invoice: typeof Invoice;
|
|
136
|
+
public CompanyInvitation: typeof CompanyInvitation;
|
|
137
|
+
public Bank: typeof Bank;
|
|
138
|
+
public Job: typeof Job;
|
|
139
|
+
public DomainInvitation: typeof DomainInvitation;
|
|
140
|
+
public Product: typeof Product;
|
|
141
|
+
public DomainTag: typeof DomainTag;
|
|
142
|
+
public EmailAddress: typeof EmailAddress;
|
|
143
|
+
public PhoneNumber: typeof PhoneNumber;
|
|
144
|
+
public Company: typeof Company;
|
|
145
|
+
public Address: typeof Address;
|
|
146
|
+
public ComponentTag: typeof ComponentTag;
|
|
147
|
+
public Discount: typeof Discount;
|
|
148
|
+
public DiscountGroup: typeof DiscountGroup;
|
|
149
|
+
public User: typeof User;
|
|
150
|
+
public Session: typeof Session;
|
|
151
|
+
public Theme: typeof Theme;
|
|
152
|
+
public ThemeCssSetting: typeof ThemeCssSetting;
|
|
153
|
+
public Item: typeof Item;
|
|
154
|
+
public EmailCounter: typeof EmailCounter;
|
|
155
|
+
public SeoDomainPage: typeof SeoDomainPage;
|
|
156
|
+
public Domain: typeof Domain;
|
|
157
|
+
public ExchangeRate: typeof ExchangeRate;
|
|
158
|
+
public Payment: typeof Payment;
|
|
159
|
+
public PaymentDevice: typeof PaymentDevice;
|
|
160
|
+
public Page: typeof Page;
|
|
161
|
+
public ShortUrl: typeof ShortUrl;
|
|
162
|
+
public CartItem: typeof CartItem;
|
|
163
|
+
public InventoryUnitVariation: typeof InventoryUnitVariation;
|
|
164
|
+
public VariationsGroup: typeof VariationsGroup;
|
|
165
|
+
public Menu: typeof Menu;
|
|
166
|
+
public Cart: typeof Cart;
|
|
167
|
+
public CartShipmentGroup: typeof CartShipmentGroup;
|
|
168
|
+
public CartShipmentQuote: typeof CartShipmentQuote;
|
|
169
|
+
public Quote: typeof Quote;
|
|
170
|
+
public Component: typeof Component;
|
|
171
|
+
public ComponentVersion : typeof ComponentVersion;
|
|
172
|
+
public QuoteItem: typeof QuoteItem;
|
|
173
|
+
public MatchingInventory: typeof MatchingInventory;
|
|
174
|
+
public SubscriptionPlan: typeof SubscriptionPlan;
|
|
175
|
+
|
|
176
|
+
public setupClass(cls: typeof Entity) {
|
|
177
|
+
const result = cloneClass(cls, this) as typeof Entity;
|
|
178
|
+
result.merchi = this;
|
|
179
|
+
return result;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
public constructor(
|
|
183
|
+
sessionToken?: string,
|
|
184
|
+
clientToken?: string,
|
|
185
|
+
invoiceToken?: string,
|
|
186
|
+
cartToken?: string
|
|
187
|
+
) {
|
|
188
|
+
if (sessionToken) {
|
|
189
|
+
this.sessionToken = sessionToken;
|
|
190
|
+
} else {
|
|
191
|
+
this.sessionToken = getCookie('session_token');
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
if (clientToken) {
|
|
195
|
+
this.clientToken = clientToken;
|
|
196
|
+
} else {
|
|
197
|
+
this.clientToken = getCookie('client_token');
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
if (invoiceToken) {
|
|
201
|
+
this.invoiceToken = invoiceToken;
|
|
202
|
+
} else {
|
|
203
|
+
this.invoiceToken = getCookie('invoice_token');
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
if (cartToken) {
|
|
207
|
+
this.cartToken = cartToken;
|
|
208
|
+
} else {
|
|
209
|
+
this.cartToken = getCookie('cart_token');
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
// re-export configured versions of all classes
|
|
213
|
+
this.AutomaticPaymentRelationship = this.setupClass(
|
|
214
|
+
AutomaticPaymentRelationship
|
|
215
|
+
) as typeof AutomaticPaymentRelationship;
|
|
216
|
+
this.Variation = this.setupClass(Variation) as typeof Variation;
|
|
217
|
+
this.DraftComment = this.setupClass(DraftComment) as typeof DraftComment;
|
|
218
|
+
this.Component = this.setupClass(Component) as typeof Component;
|
|
219
|
+
this.ComponentVersion = this.setupClass(ComponentVersion) as typeof ComponentVersion;
|
|
220
|
+
this.Theme = this.setupClass(Theme) as typeof Theme;
|
|
221
|
+
this.ThemeCssSetting = this.setupClass(ThemeCssSetting) as typeof ThemeCssSetting;
|
|
222
|
+
this.Company = this.setupClass(Company) as typeof Company;
|
|
223
|
+
this.MenuItem = this.setupClass(MenuItem) as typeof MenuItem;
|
|
224
|
+
this.InternalTag = this.setupClass(InternalTag) as typeof InternalTag;
|
|
225
|
+
this.Inventory = this.setupClass(Inventory) as typeof Inventory;
|
|
226
|
+
this.Notification = this.setupClass(Notification) as typeof Notification;
|
|
227
|
+
this.Shipment = this.setupClass(Shipment) as typeof Shipment;
|
|
228
|
+
this.ShipmentItem = this.setupClass(ShipmentItem) as typeof ShipmentItem;
|
|
229
|
+
this.ShipmentItemFulfillment = this.setupClass(
|
|
230
|
+
ShipmentItemFulfillment
|
|
231
|
+
) as typeof ShipmentItemFulfillment;
|
|
232
|
+
this.ShipmentMethod = this.setupClass(
|
|
233
|
+
ShipmentMethod
|
|
234
|
+
) as typeof ShipmentMethod;
|
|
235
|
+
this.ShipmentMethodVariation = this.setupClass(
|
|
236
|
+
ShipmentMethodVariation
|
|
237
|
+
) as typeof ShipmentMethodVariation;
|
|
238
|
+
this.Domain = this.setupClass(Domain) as typeof Domain;
|
|
239
|
+
this.ExchangeRate = this.setupClass(ExchangeRate) as typeof ExchangeRate;
|
|
240
|
+
this.Invoice = this.setupClass(Invoice) as typeof Invoice;
|
|
241
|
+
this.Job = this.setupClass(Job) as typeof Job;
|
|
242
|
+
this.ComponentTag = this.setupClass(ComponentTag) as typeof ComponentTag;
|
|
243
|
+
this.Category = this.setupClass(Category) as typeof Category;
|
|
244
|
+
this.VariationField = this.setupClass(
|
|
245
|
+
VariationField
|
|
246
|
+
) as typeof VariationField;
|
|
247
|
+
this.InventoryUnitVariation = this.setupClass(
|
|
248
|
+
InventoryUnitVariation
|
|
249
|
+
) as typeof InventoryUnitVariation;
|
|
250
|
+
this.PhoneNumber = this.setupClass(PhoneNumber) as typeof PhoneNumber;
|
|
251
|
+
this.QuoteItem = this.setupClass(QuoteItem) as typeof QuoteItem;
|
|
252
|
+
this.Menu = this.setupClass(Menu) as typeof Menu;
|
|
253
|
+
this.Assignment = this.setupClass(Assignment) as typeof Assignment;
|
|
254
|
+
this.Draft = this.setupClass(Draft) as typeof Draft;
|
|
255
|
+
this.DraftTemplate = this.setupClass(DraftTemplate) as typeof DraftTemplate;
|
|
256
|
+
this.VariationsGroup = this.setupClass(
|
|
257
|
+
VariationsGroup
|
|
258
|
+
) as typeof VariationsGroup;
|
|
259
|
+
this.EnrolledDomain = this.setupClass(
|
|
260
|
+
EnrolledDomain
|
|
261
|
+
) as typeof EnrolledDomain;
|
|
262
|
+
this.CompanyInvitation = this.setupClass(
|
|
263
|
+
CompanyInvitation
|
|
264
|
+
) as typeof CompanyInvitation;
|
|
265
|
+
this.Quote = this.setupClass(Quote) as typeof Quote;
|
|
266
|
+
this.EmailAddress = this.setupClass(EmailAddress) as typeof EmailAddress;
|
|
267
|
+
this.SeoDomainPage = this.setupClass(SeoDomainPage) as typeof SeoDomainPage;
|
|
268
|
+
this.ProductionComment = this.setupClass(
|
|
269
|
+
ProductionComment
|
|
270
|
+
) as typeof ProductionComment;
|
|
271
|
+
this.Backup = this.setupClass(Backup) as typeof Backup;
|
|
272
|
+
this.CountryTax = this.setupClass(CountryTax) as typeof CountryTax;
|
|
273
|
+
this.ShortUrl = this.setupClass(ShortUrl) as typeof ShortUrl;
|
|
274
|
+
this.Product = this.setupClass(Product) as typeof Product;
|
|
275
|
+
this.SystemRole = this.setupClass(SystemRole) as typeof SystemRole;
|
|
276
|
+
this.CartItem = this.setupClass(CartItem) as typeof CartItem;
|
|
277
|
+
this.UserCompany = this.setupClass(UserCompany) as typeof UserCompany;
|
|
278
|
+
this.DomainTag = this.setupClass(DomainTag) as typeof DomainTag;
|
|
279
|
+
this.VariationFieldsOption = this.setupClass(
|
|
280
|
+
VariationFieldsOption
|
|
281
|
+
) as typeof VariationFieldsOption;
|
|
282
|
+
this.Address = this.setupClass(Address) as typeof Address;
|
|
283
|
+
this.Item = this.setupClass(Item) as typeof Item;
|
|
284
|
+
this.SupplyDomain = this.setupClass(SupplyDomain) as typeof SupplyDomain;
|
|
285
|
+
this.DomainInvitation = this.setupClass(
|
|
286
|
+
DomainInvitation
|
|
287
|
+
) as typeof DomainInvitation;
|
|
288
|
+
this.EmailCounter = this.setupClass(EmailCounter) as typeof EmailCounter;
|
|
289
|
+
this.Session = this.setupClass(Session) as typeof Session;
|
|
290
|
+
this.Bank = this.setupClass(Bank) as typeof Bank;
|
|
291
|
+
this.Discount = this.setupClass(Discount) as typeof Discount;
|
|
292
|
+
this.DiscountGroup = this.setupClass(DiscountGroup) as typeof DiscountGroup;
|
|
293
|
+
this.Payment = this.setupClass(Payment) as typeof Payment;
|
|
294
|
+
this.PaymentDevice = this.setupClass(PaymentDevice) as typeof PaymentDevice;
|
|
295
|
+
this.Page = this.setupClass(Page) as typeof Page;
|
|
296
|
+
this.Cart = this.setupClass(Cart) as typeof Cart;
|
|
297
|
+
this.CartShipmentGroup = this.setupClass(
|
|
298
|
+
CartShipmentGroup
|
|
299
|
+
) as typeof CartShipmentGroup;
|
|
300
|
+
this.CartShipmentQuote = this.setupClass(
|
|
301
|
+
CartShipmentQuote
|
|
302
|
+
) as typeof CartShipmentQuote;
|
|
303
|
+
this.MerchiFile = this.setupClass(MerchiFile) as typeof MerchiFile;
|
|
304
|
+
this.User = this.setupClass(User) as typeof User;
|
|
305
|
+
this.JobComment = this.setupClass(JobComment) as typeof JobComment;
|
|
306
|
+
this.VariationOption = this.setupClass(
|
|
307
|
+
VariationOption
|
|
308
|
+
) as typeof VariationOption;
|
|
309
|
+
this.MatchingInventory = this.setupClass(
|
|
310
|
+
MatchingInventory
|
|
311
|
+
) as typeof MatchingInventory;
|
|
312
|
+
this.SubscriptionPlan = this.setupClass(
|
|
313
|
+
SubscriptionPlan
|
|
314
|
+
) as typeof SubscriptionPlan;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
public authenticatedFetch = (
|
|
318
|
+
resource: string,
|
|
319
|
+
options: RequestOptions,
|
|
320
|
+
expectEmptyResponse?: boolean
|
|
321
|
+
) => {
|
|
322
|
+
if (!options.query) {
|
|
323
|
+
/* istanbul ignore next */
|
|
324
|
+
options.query = [];
|
|
325
|
+
}
|
|
326
|
+
if (this.sessionToken) {
|
|
327
|
+
/* istanbul ignore next */
|
|
328
|
+
options.query.push(['session_token', this.sessionToken]);
|
|
329
|
+
}
|
|
330
|
+
if (this.clientToken) {
|
|
331
|
+
/* istanbul ignore next */
|
|
332
|
+
options.query.push(['client_token', this.clientToken]);
|
|
333
|
+
}
|
|
334
|
+
if (this.invoiceToken) {
|
|
335
|
+
/* istanbul ignore next */
|
|
336
|
+
options.query.push(['invoice_token', this.invoiceToken]);
|
|
337
|
+
}
|
|
338
|
+
if (this.cartToken) {
|
|
339
|
+
/* istanbul ignore next */
|
|
340
|
+
options.query.push(['cart_token', this.cartToken]);
|
|
341
|
+
}
|
|
342
|
+
return apiFetch(resource, options, expectEmptyResponse);
|
|
343
|
+
};
|
|
344
|
+
|
|
345
|
+
/* istanbul ignore next */
|
|
346
|
+
public authenticatedFetchWithProgress = (
|
|
347
|
+
resource: string,
|
|
348
|
+
options: RequestOptions,
|
|
349
|
+
progressCallback?: (progress: number) => void
|
|
350
|
+
) => {
|
|
351
|
+
if (!options.query) {
|
|
352
|
+
options.query = [];
|
|
353
|
+
}
|
|
354
|
+
if (this.sessionToken) {
|
|
355
|
+
options.query.push(['session_token', this.sessionToken]);
|
|
356
|
+
}
|
|
357
|
+
if (this.clientToken) {
|
|
358
|
+
options.query.push(['client_token', this.clientToken]);
|
|
359
|
+
}
|
|
360
|
+
if (this.invoiceToken) {
|
|
361
|
+
options.query.push(['invoice_token', this.invoiceToken]);
|
|
362
|
+
}
|
|
363
|
+
if (this.cartToken) {
|
|
364
|
+
options.query.push(['cart_token', this.cartToken]);
|
|
365
|
+
}
|
|
366
|
+
return apiFetchWithProgress(resource, options, progressCallback);
|
|
367
|
+
};
|
|
368
|
+
|
|
369
|
+
public getCurrentUser = (options?: UserRequestOptions) => {
|
|
370
|
+
const { embed = {} } = options || {};
|
|
371
|
+
const defaultEmbed = { user: { enrolledDomains: { domain: {} } } };
|
|
372
|
+
if (!this.sessionToken) {
|
|
373
|
+
return Promise.resolve(null);
|
|
374
|
+
}
|
|
375
|
+
return this.Session.get(this.sessionToken, {
|
|
376
|
+
embed: { ...defaultEmbed, ...embed },
|
|
377
|
+
}).then((session: any) => session.user);
|
|
378
|
+
};
|
|
379
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { apiFetch } from './request';
|
|
2
|
+
import { ErrorType } from './constants/errors';
|
|
3
|
+
import { setup, mockFetch } from './test_util';
|
|
4
|
+
|
|
5
|
+
setup();
|
|
6
|
+
|
|
7
|
+
test('can pass through data from server', () => {
|
|
8
|
+
mockFetch(true, {'animal': 'turtle'}, 200);
|
|
9
|
+
return apiFetch('/test').then(data => {
|
|
10
|
+
expect(data.animal).toBe('turtle');
|
|
11
|
+
});
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
test('can pass through data from server with override url', () => {
|
|
15
|
+
(window as any).merchiBackendUri = 'http://override.example.com/';
|
|
16
|
+
mockFetch(true, {'animal': 'turtle'}, 200);
|
|
17
|
+
return apiFetch('/test').then(data => {
|
|
18
|
+
expect(data.animal).toBe('turtle');
|
|
19
|
+
});
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
test('404 creates ApiError', () => {
|
|
23
|
+
mockFetch(false, {'statusCode': 404, 'errorCode': ErrorType.RESOURCE_NOT_FOUND}, 404);
|
|
24
|
+
apiFetch('/test').catch(e => {
|
|
25
|
+
expect(e.statusCode).toBe(404);
|
|
26
|
+
expect(e.name).toBe('ApiError');
|
|
27
|
+
expect(e.errorCode).toBe(ErrorType.RESOURCE_NOT_FOUND);
|
|
28
|
+
expect(e.errorMessage).toBe('No error message');
|
|
29
|
+
});
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
test('will get default errorCode', () => {
|
|
33
|
+
mockFetch(false,
|
|
34
|
+
{'statusCode': 404,
|
|
35
|
+
'errorCode': -1,
|
|
36
|
+
'message': 'just a test'}, 404);
|
|
37
|
+
apiFetch('/test').catch(e => {
|
|
38
|
+
expect(e.statusCode).toBe(404);
|
|
39
|
+
expect(e.name).toBe('ApiError');
|
|
40
|
+
expect(e.errorCode).toBe(ErrorType.UNKNOWN_ERROR);
|
|
41
|
+
expect(e.errorMessage).toBe('just a test');
|
|
42
|
+
});
|
|
43
|
+
});
|
package/src/request.ts
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
// eslint-disable-next-line no-unused-vars
|
|
2
|
+
import { ErrorType, getErrorFromCode } from './constants/errors';
|
|
3
|
+
|
|
4
|
+
/* this constant are expected to be defined in webpack (or provided as
|
|
5
|
+
globals by some other means. */
|
|
6
|
+
declare const BACKEND_URI: string;
|
|
7
|
+
|
|
8
|
+
export interface RequestOptions extends RequestInit {
|
|
9
|
+
query?: string[][];
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export class ApiError extends Error {
|
|
13
|
+
public statusCode?: number;
|
|
14
|
+
public errorCode?: ErrorType;
|
|
15
|
+
public errorMessage: string;
|
|
16
|
+
public original: any;
|
|
17
|
+
public constructor(err: any) {
|
|
18
|
+
const message = JSON.stringify(err);
|
|
19
|
+
/* istanbul ignore next */
|
|
20
|
+
super(message);
|
|
21
|
+
this.statusCode = err.statusCode;
|
|
22
|
+
this.errorCode = getErrorFromCode(err.errorCode);
|
|
23
|
+
this.errorMessage = err.message ?
|
|
24
|
+
err.message : 'No error message';
|
|
25
|
+
this.name = 'ApiError';
|
|
26
|
+
this.original = err;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export const version = 'v6';
|
|
31
|
+
|
|
32
|
+
export function backendFetch(resource: string, options?: RequestOptions) {
|
|
33
|
+
const server = (window as any).merchiBackendUri
|
|
34
|
+
? (window as any).merchiBackendUri
|
|
35
|
+
: BACKEND_URI;
|
|
36
|
+
const url = new URL(server + version + resource);
|
|
37
|
+
if (options && options.query) {
|
|
38
|
+
for (const entry of options.query) {
|
|
39
|
+
url.searchParams.append(entry[0], entry[1]);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return fetch(url.toString(), options);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function apiFetch(
|
|
46
|
+
resource: string,
|
|
47
|
+
options?: RequestOptions,
|
|
48
|
+
expectEmptyResponse?: boolean
|
|
49
|
+
) {
|
|
50
|
+
return backendFetch(resource, options as RequestInit | undefined).then(
|
|
51
|
+
function (response) {
|
|
52
|
+
if (response.status < 200 || response.status > 299) {
|
|
53
|
+
return response.json().then(function (json) {
|
|
54
|
+
const err = new ApiError(json);
|
|
55
|
+
return Promise.reject(err);
|
|
56
|
+
});
|
|
57
|
+
} else {
|
|
58
|
+
return expectEmptyResponse ? '' : response.json();
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/* istanbul ignore next */
|
|
65
|
+
export function apiFetchWithProgress(
|
|
66
|
+
resource: string,
|
|
67
|
+
options?: RequestOptions,
|
|
68
|
+
progressCallback?: (progress: number) => void
|
|
69
|
+
) {
|
|
70
|
+
return backendFetch(resource, options as RequestInit | undefined).then(
|
|
71
|
+
function (response) {
|
|
72
|
+
if (!response.body) {
|
|
73
|
+
const err = new ApiError('empty response');
|
|
74
|
+
return Promise.reject(err);
|
|
75
|
+
}
|
|
76
|
+
const reader = response.body.getReader();
|
|
77
|
+
let bodyText = '';
|
|
78
|
+
let errorText = '';
|
|
79
|
+
let haveError = false;
|
|
80
|
+
const expected = '{"loadingBar": "' + '.'.repeat(100) + '"}';
|
|
81
|
+
function readChunk(): any {
|
|
82
|
+
return reader.read().then(({done, value}) => {
|
|
83
|
+
if (done) {
|
|
84
|
+
if (response.status < 200 || response.status > 299) {
|
|
85
|
+
const err = new ApiError('Unknown error');
|
|
86
|
+
return Promise.reject(err);
|
|
87
|
+
} else if(haveError) {
|
|
88
|
+
try {
|
|
89
|
+
const jsonText = JSON.parse(errorText);
|
|
90
|
+
return Promise.reject(new ApiError(jsonText));
|
|
91
|
+
} catch (e) {
|
|
92
|
+
return Promise.reject(new ApiError(errorText));
|
|
93
|
+
}
|
|
94
|
+
} else {
|
|
95
|
+
return bodyText;
|
|
96
|
+
}
|
|
97
|
+
} else {
|
|
98
|
+
const chunk = new TextDecoder().decode(value);
|
|
99
|
+
if (haveError) {
|
|
100
|
+
errorText += chunk;
|
|
101
|
+
} else {
|
|
102
|
+
for (let i = 0; i < chunk.length; ++i) {
|
|
103
|
+
const char = chunk[i];
|
|
104
|
+
const expectedChar = expected[bodyText.length];
|
|
105
|
+
if (expectedChar == char && !haveError) {
|
|
106
|
+
bodyText += char;
|
|
107
|
+
} else {
|
|
108
|
+
haveError = true;
|
|
109
|
+
errorText += char;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
if (!haveError && progressCallback) {
|
|
114
|
+
const progress = Math.min(Math.max(0, bodyText.length - 16), 100);
|
|
115
|
+
progressCallback(progress);
|
|
116
|
+
}
|
|
117
|
+
return readChunk();
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
return readChunk();
|
|
122
|
+
}
|
|
123
|
+
);
|
|
124
|
+
}
|
package/src/test_util.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export function setup() {
|
|
2
|
+
beforeAll(function() {
|
|
3
|
+
(global as any).BACKEND_URI = 'http://example.com/';
|
|
4
|
+
});
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export function mockFetch(ok: boolean, data: any, status: number) {
|
|
8
|
+
const mock = jest.fn();
|
|
9
|
+
mock.mockImplementation(() =>
|
|
10
|
+
Promise.resolve({
|
|
11
|
+
status: status,
|
|
12
|
+
ok: ok,
|
|
13
|
+
json: () => Promise.resolve(data)
|
|
14
|
+
})
|
|
15
|
+
);
|
|
16
|
+
(global as any).fetch = mock;
|
|
17
|
+
return mock;
|
|
18
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { setup, mockFetch } from './test_util';
|
|
2
|
+
import { toastNotifications } from './toasts';
|
|
3
|
+
import { Merchi } from './merchi';
|
|
4
|
+
import { NotificationSection } from './constants/notification_sections';
|
|
5
|
+
import { NotificationType } from './constants/notification_types';
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
setup();
|
|
9
|
+
|
|
10
|
+
test('fetch toast of jobs', () => {
|
|
11
|
+
const merchi = new Merchi();
|
|
12
|
+
const fetch = mockFetch(
|
|
13
|
+
true, {'notifications': [{id: 1, message: 'test'}]}, 200);
|
|
14
|
+
const entities = [{id: 1, type: 'job'}];
|
|
15
|
+
const sections = [NotificationSection.JOB_NOTIFICATIONS];
|
|
16
|
+
const notificationTypes = {[NotificationType.JOB_PAID]: true};
|
|
17
|
+
|
|
18
|
+
const options = {entities, sections, notificationTypes};
|
|
19
|
+
const invocation = toastNotifications(merchi, options as any);
|
|
20
|
+
const expectUrl = `${(global as any).BACKEND_URI}v6/notifications-check-update/`;
|
|
21
|
+
expect(fetch.mock.calls[0][0]).toEqual(expectUrl);
|
|
22
|
+
expect(fetch.mock.calls[0][1].method).toEqual('POST');
|
|
23
|
+
expect(JSON.parse(fetch.mock.calls[0][1].body.get('entities'))).toEqual(
|
|
24
|
+
[{id: 1, type: 'job'}]);
|
|
25
|
+
expect(fetch.mock.calls[0][1].body.get('sections')).toEqual(
|
|
26
|
+
`[${NotificationSection.JOB_NOTIFICATIONS}]`);
|
|
27
|
+
expect(
|
|
28
|
+
JSON.parse(fetch.mock.calls[0][1].body.get('notificationTypes'))
|
|
29
|
+
).toEqual(
|
|
30
|
+
{[NotificationType.JOB_PAID]: true}
|
|
31
|
+
);
|
|
32
|
+
invocation.then(notifications => {
|
|
33
|
+
expect(notifications.length).toBe(1);
|
|
34
|
+
expect(notifications[0].id).toBe(1);
|
|
35
|
+
expect(notifications[0].message).toBe('test');
|
|
36
|
+
});
|
|
37
|
+
return invocation;
|
|
38
|
+
});
|
package/src/toasts.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
// eslint-disable-next-line no-unused-vars
|
|
2
|
+
import { RequestOptions } from './request';
|
|
3
|
+
// eslint-disable-next-line no-unused-vars
|
|
4
|
+
import { Merchi } from './merchi';
|
|
5
|
+
// eslint-disable-next-line no-unused-vars
|
|
6
|
+
import { NotificationSection } from './constants/notification_sections';
|
|
7
|
+
// eslint-disable-next-line no-unused-vars
|
|
8
|
+
import { NotificationType } from './constants/notification_types';
|
|
9
|
+
|
|
10
|
+
interface RelatedObj {
|
|
11
|
+
id: number;
|
|
12
|
+
type: string;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
interface ToastOptions {
|
|
16
|
+
entities: RelatedObj[];
|
|
17
|
+
sections: NotificationSection[];
|
|
18
|
+
notificationTypes?: Record<NotificationType, boolean>;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export const toastNotifications = (merchi: Merchi, options: ToastOptions) => {
|
|
22
|
+
const resource = '/notifications-check-update/';
|
|
23
|
+
const data = new FormData();
|
|
24
|
+
const { entities, sections, notificationTypes } = options;
|
|
25
|
+
data.set('entities', JSON.stringify(entities));
|
|
26
|
+
data.set('sections', JSON.stringify(sections));
|
|
27
|
+
data.set('notificationTypes', JSON.stringify(notificationTypes));
|
|
28
|
+
const fetchOptions: RequestOptions = { method: 'POST', body: data };
|
|
29
|
+
return merchi
|
|
30
|
+
.authenticatedFetch(resource, fetchOptions)
|
|
31
|
+
.then((data: any) =>
|
|
32
|
+
data.notifications.map((n: any) => new merchi.Notification().fromJson(n))
|
|
33
|
+
);
|
|
34
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export function kahanSum(values: Iterable<number>) {
|
|
2
|
+
/* Return the kahan summation of the input number array */
|
|
3
|
+
let accumulator = 0;
|
|
4
|
+
let compensation = 0;
|
|
5
|
+
for (const value of values) {
|
|
6
|
+
const y = value - compensation;
|
|
7
|
+
const t = accumulator + y;
|
|
8
|
+
compensation = (t - accumulator) - y;
|
|
9
|
+
accumulator = t;
|
|
10
|
+
}
|
|
11
|
+
return accumulator;
|
|
12
|
+
}
|