All files / src merchi.ts

40.72% Statements 90/221
0% Branches 0/32
14.28% Functions 1/7
40.63% Lines 89/219

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458          1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x                                           1x   1x                                                                                                                                                                                       1x                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   1x  
import {
  Entity,
  // eslint-disable-next-line no-unused-vars
  EmbedDescriptor,
} from './entity.js';
import { AutomaticPaymentRelationship } from './entities/automatic_payment_relationship.js';
import { Session } from './entities/session.js';
import { JobComment } from './entities/job_comment.js';
import { JobNote } from './entities/job_note.js';
import { JobOperationLog } from './entities/job_operation_log.js';
import { Domain } from './entities/domain.js';
import { ExchangeRate } from './entities/exchange_rate.js';
import { Job } from './entities/job.js';
import { Menu } from './entities/menu.js';
import { VariationField } from './entities/variation_field.js';
import { VariationOption } from './entities/variation_option.js';
import { ProductionComment } from './entities/production_comment.js';
import { Product } from './entities/product.js';
import { ProductReview } from './entities/product_review.js';
import { InternalTag } from './entities/internal_tag.js';
import { Inventory } from './entities/inventory.js';
import { InventoryGroup } from './entities/inventory_group.js';
import { QuoteItem } from './entities/quote_item.js';
import { Category } from './entities/category.js';
import { Invoice } from './entities/invoice.js';
import { UserCompany } from './entities/user_company.js';
import { InventoryUnitVariation } from './entities/inventory_unit_variation.js';
import { VariationFieldsOption } from './entities/variation_fields_option.js';
import { Bank } from './entities/bank.js';
import { Shipment } from './entities/shipment.js';
import { ShipmentLog } from './entities/shipment_log.js';
import { ShipmentItem } from './entities/shipment_item.js';
import { ShipmentItemFulfillment } from './entities/shipment_item_fulfillment.js';
import { ShipmentMethod } from './entities/shipment_method.js';
import { ShipmentMethodVariation } from './entities/shipment_method_variation.js';
import { DomainInvitation } from './entities/domain_invitation.js';
import { EmailCounter } from './entities/email_counter.js';
import { MenuItem } from './entities/menu_item.js';
import { SupplyDomain } from './entities/supply_domain.js';
import { Cart } from './entities/cart.js';
import { CartShipmentGroup } from './entities/cart_shipment_group.js';
import { CartShipmentQuote } from './entities/cart_shipment_quote.js';
import { Theme } from './entities/theme.js';
import { ThemeCssSetting } from './entities/theme_css_setting.js';
import { Component } from './entities/component.js';
import { ComponentVersion } from './entities/component_version.js';
import { MerchiFile } from './entities/file.js';
import { EmailAddress } from './entities/email_address.js';
import { SeoDomainPage } from './entities/seo_domain_page.js';
import { ShortUrl } from './entities/short_url.js';
import { VariationsGroup } from './entities/variations_group.js';
import { Quote } from './entities/quote.js';
import { Reminder } from './entities/reminder.js';
import { Draft } from './entities/draft.js';
import { DraftPreview } from './entities/draft_preview.js';
import { DraftPreviewLayer } from './entities/draft_preview_layer.js';
import { DraftTemplate } from './entities/draft_template.js';
import { Discount } from './entities/discount.js';
import { DiscountGroup } from './entities/discount_group.js';
import { User } from './entities/user.js';
import { Company } from './entities/company.js';
import { ComponentTag } from './entities/component_tag.js';
import { EnrolledDomain } from './entities/enrolled_domain.js';
import { CountryTax } from './entities/country_tax.js';
import { Item } from './entities/item.js';
import { DomainTag } from './entities/domain_tag.js';
import { DraftComment } from './entities/draft_comment.js';
import { Notification } from './entities/notification.js';
import { Payment } from './entities/payment.js';
import { Page } from './entities/page.js';
import { CompanyInvitation } from './entities/company_invitation.js';
import { SystemRole } from './entities/system_role.js';
import { PaymentDevice } from './entities/payment_device.js';
import { PhoneNumber } from './entities/phone_number.js';
import { Variation } from './entities/variation.js';
import { CartItem } from './entities/cart_item.js';
import { Address } from './entities/address.js';
import { AgentConversation } from './entities/agent_conversation.js';
import { AgentSkill } from './entities/agent_skill.js';
import { AgentSkillVersion } from './entities/agent_skill_version.js';
import { AgentSkillApproval } from './entities/agent_skill_approval.js';
import { DomainChatSettings } from './entities/domain_chat_settings.js';
import { SupportConversation } from './entities/support_conversation.js';
import { SupportMessage } from './entities/support_message.js';
import { Assignment } from './entities/assignment.js';
import { MatchingInventory } from './entities/matching_inventory.js';
import { SubscriptionPlan } from './entities/subscription_plan.js';
import { generateUUID } from './uuid.js';
// eslint-disable-next-line no-unused-vars
import { RequestOptions, apiFetch, apiFetchWithProgress } from './request.js';
import { getCookie } from './cookie.js';
 
// the type of classes
export interface Type<T, A extends any[]> extends Function {
  new (...args: A): T;
}
 
function cloneClass<T, A extends []>(
  original: Type<T, A>,
  arg: any
): Type<T, A> {
  // copy the constructor, but use the empty object as `this`
  const copy = original.bind({}, arg);
  // pick up any static members (this is shallow, the members are not copied)
  Object.assign(copy, original);
  return copy;
}
 
interface UserRequestOptions {
  embed?: EmbedDescriptor;
}
 
export const API_VERSION = 'v6';
 
export class Merchi {
  public id: string = generateUUID();
 
  public sessionToken?: string;
  public invoiceToken?: string;
  public clientToken?: string;
  public cartToken?: string;
  public readonly backendUri: string;
 
  public AutomaticPaymentRelationship: typeof AutomaticPaymentRelationship;
  public Notification: typeof Notification;
  public EnrolledDomain: typeof EnrolledDomain;
  public SystemRole: typeof SystemRole;
  public UserCompany: typeof UserCompany;
  public Variation: typeof Variation;
  public CountryTax: typeof CountryTax;
  public MenuItem: typeof MenuItem;
  public VariationField: typeof VariationField;
  public Assignment: typeof Assignment;
  public InternalTag: typeof InternalTag;
  public Inventory: typeof Inventory;
  public InventoryGroup: typeof InventoryGroup;
  public JobComment: typeof JobComment;
  public JobNote: typeof JobNote;
  public JobOperationLog: typeof JobOperationLog;
  public VariationOption: typeof VariationOption;
  public SupplyDomain: typeof SupplyDomain;
  public ProductionComment: typeof ProductionComment;
  public DraftComment: typeof DraftComment;
  public Shipment: typeof Shipment;
  public ShipmentLog: typeof ShipmentLog;
  public ShipmentItem: typeof ShipmentItem;
  public ShipmentItemFulfillment: typeof ShipmentItemFulfillment;
  public ShipmentMethod: typeof ShipmentMethod;
  public ShipmentMethodVariation: typeof ShipmentMethodVariation;
  public Draft: typeof Draft;
  public DraftPreview: typeof DraftPreview;
  public DraftPreviewLayer: typeof DraftPreviewLayer;
  public DraftTemplate: typeof DraftTemplate;
  public VariationFieldsOption: typeof VariationFieldsOption;
  public Category: typeof Category;
  public MerchiFile: typeof MerchiFile;
  public Invoice: typeof Invoice;
  public CompanyInvitation: typeof CompanyInvitation;
  public Bank: typeof Bank;
  public Job: typeof Job;
  public DomainInvitation: typeof DomainInvitation;
  public Product: typeof Product;
  public ProductReview: typeof ProductReview;
  public DomainTag: typeof DomainTag;
  public EmailAddress: typeof EmailAddress;
  public PhoneNumber: typeof PhoneNumber;
  public Company: typeof Company;
  public Address: typeof Address;
  public AgentConversation: typeof AgentConversation;
  public AgentSkill: typeof AgentSkill;
  public AgentSkillVersion: typeof AgentSkillVersion;
  public AgentSkillApproval: typeof AgentSkillApproval;
  public DomainChatSettings: typeof DomainChatSettings;
  public SupportConversation: typeof SupportConversation;
  public SupportMessage: typeof SupportMessage;
  public ComponentTag: typeof ComponentTag;
  public Discount: typeof Discount;
  public DiscountGroup: typeof DiscountGroup;
  public User: typeof User;
  public Session: typeof Session;
  public Theme: typeof Theme;
  public ThemeCssSetting: typeof ThemeCssSetting;
  public Item: typeof Item;
  public EmailCounter: typeof EmailCounter;
  public SeoDomainPage: typeof SeoDomainPage;
  public Domain: typeof Domain;
  public ExchangeRate: typeof ExchangeRate;
  public Payment: typeof Payment;
  public PaymentDevice: typeof PaymentDevice;
  public Page: typeof Page;
  public ShortUrl: typeof ShortUrl;
  public CartItem: typeof CartItem;
  public InventoryUnitVariation: typeof InventoryUnitVariation;
  public VariationsGroup: typeof VariationsGroup;
  public Menu: typeof Menu;
  public Cart: typeof Cart;
  public CartShipmentGroup: typeof CartShipmentGroup;
  public CartShipmentQuote: typeof CartShipmentQuote;
  public Quote: typeof Quote;
  public Component: typeof Component;
  public ComponentVersion : typeof ComponentVersion;
  public QuoteItem: typeof QuoteItem;
  public Reminder: typeof Reminder;
  public MatchingInventory: typeof MatchingInventory;
  public SubscriptionPlan: typeof SubscriptionPlan;
 
  public setupClass(cls: typeof Entity) {
    const result = cloneClass(cls, this) as typeof Entity;
    result.merchi = this;
    return result;
  }
 
  public constructor(
    sessionToken?: string,
    clientToken?: string,
    invoiceToken?: string,
    cartToken?: string,
    backendUri?: string
  ) {
    if (sessionToken) {
      this.sessionToken = sessionToken;
    } else {
      this.sessionToken = getCookie('session_token');
    }
 
    if (clientToken) {
      this.clientToken = clientToken;
    } else {
      this.clientToken = getCookie('client_token');
    }
 
    if (invoiceToken) {
      this.invoiceToken = invoiceToken;
    } else {
      this.invoiceToken = getCookie('invoice_token');
    }
 
    if (cartToken) {
      this.cartToken = cartToken;
    } else {
      this.cartToken = getCookie('cart_token');
    }
 
    let determinedBaseUri = backendUri;
    Iif (!determinedBaseUri) {
      const clientBackendUri = typeof window !== 'undefined' &&
        (window as any).merchiBackendUri ?
        (window as any).merchiBackendUri : undefined;
      Iif (clientBackendUri) {
        determinedBaseUri = clientBackendUri;
      }
    }
    Iif (!determinedBaseUri) {
      const envBackendUri = typeof process !== 'undefined' &&
        process.env.MERCHI_BACKEND_URI ?
        process.env.MERCHI_BACKEND_URI : undefined;
      Iif (envBackendUri) {
        determinedBaseUri = envBackendUri;
      }
    }
    Iif (!determinedBaseUri) {
      determinedBaseUri = 'https://api.merchi.co/';
    }
 
    // Ensure determinedBaseUri ends with a slash
    Iif (!determinedBaseUri.endsWith('/')) {
      determinedBaseUri += '/';
    }
 
    this.backendUri = determinedBaseUri;
 
    // re-export configured versions of all classes
    this.AutomaticPaymentRelationship = this.setupClass(
      AutomaticPaymentRelationship
    ) as typeof AutomaticPaymentRelationship;
    this.Variation = this.setupClass(Variation) as typeof Variation;
    this.DraftComment = this.setupClass(DraftComment) as typeof DraftComment;
    this.Component = this.setupClass(Component) as typeof Component;
    this.ComponentVersion = this.setupClass(ComponentVersion) as typeof ComponentVersion;
    this.Theme = this.setupClass(Theme) as typeof Theme;
    this.ThemeCssSetting = this.setupClass(ThemeCssSetting) as typeof ThemeCssSetting;
    this.Company = this.setupClass(Company) as typeof Company;
    this.MenuItem = this.setupClass(MenuItem) as typeof MenuItem;
    this.InternalTag = this.setupClass(InternalTag) as typeof InternalTag;
    this.Inventory = this.setupClass(Inventory) as typeof Inventory;
    this.InventoryGroup = this.setupClass(InventoryGroup) as typeof InventoryGroup;
    this.Notification = this.setupClass(Notification) as typeof Notification;
    this.Shipment = this.setupClass(Shipment) as typeof Shipment;
    this.ShipmentLog = this.setupClass(ShipmentLog) as typeof ShipmentLog;
    this.ShipmentItem = this.setupClass(ShipmentItem) as typeof ShipmentItem;
    this.ShipmentItemFulfillment = this.setupClass(
      ShipmentItemFulfillment
    ) as typeof ShipmentItemFulfillment;
    this.ShipmentMethod = this.setupClass(
      ShipmentMethod
    ) as typeof ShipmentMethod;
    this.ShipmentMethodVariation = this.setupClass(
      ShipmentMethodVariation
    ) as typeof ShipmentMethodVariation;
    this.Domain = this.setupClass(Domain) as typeof Domain;
    this.ExchangeRate = this.setupClass(ExchangeRate) as typeof ExchangeRate;
    this.Invoice = this.setupClass(Invoice) as typeof Invoice;
    this.Job = this.setupClass(Job) as typeof Job;
    this.ComponentTag = this.setupClass(ComponentTag) as typeof ComponentTag;
    this.Category = this.setupClass(Category) as typeof Category;
    this.VariationField = this.setupClass(
      VariationField
    ) as typeof VariationField;
    this.InventoryUnitVariation = this.setupClass(
      InventoryUnitVariation
    ) as typeof InventoryUnitVariation;
    this.PhoneNumber = this.setupClass(PhoneNumber) as typeof PhoneNumber;
    this.QuoteItem = this.setupClass(QuoteItem) as typeof QuoteItem;
    this.Menu = this.setupClass(Menu) as typeof Menu;
    this.Assignment = this.setupClass(Assignment) as typeof Assignment;
    this.Draft = this.setupClass(Draft) as typeof Draft;
    this.DraftPreview = this.setupClass(DraftPreview) as typeof DraftPreview;
    this.DraftPreviewLayer = this.setupClass(DraftPreviewLayer) as typeof DraftPreviewLayer;
    this.DraftTemplate = this.setupClass(DraftTemplate) as typeof DraftTemplate;
    this.VariationsGroup = this.setupClass(
      VariationsGroup
    ) as typeof VariationsGroup;
    this.EnrolledDomain = this.setupClass(
      EnrolledDomain
    ) as typeof EnrolledDomain;
    this.CompanyInvitation = this.setupClass(
      CompanyInvitation
    ) as typeof CompanyInvitation;
    this.Quote = this.setupClass(Quote) as typeof Quote;
    this.Reminder = this.setupClass(Reminder) as typeof Reminder;
    this.EmailAddress = this.setupClass(EmailAddress) as typeof EmailAddress;
    this.SeoDomainPage = this.setupClass(SeoDomainPage) as typeof SeoDomainPage;
    this.ProductionComment = this.setupClass(
      ProductionComment
    ) as typeof ProductionComment;
    this.CountryTax = this.setupClass(CountryTax) as typeof CountryTax;
    this.ShortUrl = this.setupClass(ShortUrl) as typeof ShortUrl;
    this.Product = this.setupClass(Product) as typeof Product;
    this.ProductReview = this.setupClass(
      ProductReview
    ) as typeof ProductReview;
    this.SystemRole = this.setupClass(SystemRole) as typeof SystemRole;
    this.CartItem = this.setupClass(CartItem) as typeof CartItem;
    this.UserCompany = this.setupClass(UserCompany) as typeof UserCompany;
    this.DomainTag = this.setupClass(DomainTag) as typeof DomainTag;
    this.VariationFieldsOption = this.setupClass(
      VariationFieldsOption
    ) as typeof VariationFieldsOption;
    this.Address = this.setupClass(Address) as typeof Address;
    this.AgentConversation = this.setupClass(AgentConversation) as typeof AgentConversation;
    this.AgentSkill = this.setupClass(AgentSkill) as typeof AgentSkill;
    this.AgentSkillVersion = this.setupClass(AgentSkillVersion) as typeof AgentSkillVersion;
    this.AgentSkillApproval = this.setupClass(AgentSkillApproval) as typeof AgentSkillApproval;
    this.DomainChatSettings = this.setupClass(DomainChatSettings) as typeof DomainChatSettings;
    this.SupportConversation = this.setupClass(SupportConversation) as typeof SupportConversation;
    this.SupportMessage = this.setupClass(SupportMessage) as typeof SupportMessage;
    this.Item = this.setupClass(Item) as typeof Item;
    this.SupplyDomain = this.setupClass(SupplyDomain) as typeof SupplyDomain;
    this.DomainInvitation = this.setupClass(
      DomainInvitation
    ) as typeof DomainInvitation;
    this.EmailCounter = this.setupClass(EmailCounter) as typeof EmailCounter;
    this.Session = this.setupClass(Session) as typeof Session;
    this.Bank = this.setupClass(Bank) as typeof Bank;
    this.Discount = this.setupClass(Discount) as typeof Discount;
    this.DiscountGroup = this.setupClass(DiscountGroup) as typeof DiscountGroup;
    this.Payment = this.setupClass(Payment) as typeof Payment;
    this.PaymentDevice = this.setupClass(PaymentDevice) as typeof PaymentDevice;
    this.Page = this.setupClass(Page) as typeof Page;
    this.Cart = this.setupClass(Cart) as typeof Cart;
    this.CartShipmentGroup = this.setupClass(
      CartShipmentGroup
    ) as typeof CartShipmentGroup;
    this.CartShipmentQuote = this.setupClass(
      CartShipmentQuote
    ) as typeof CartShipmentQuote;
    this.MerchiFile = this.setupClass(MerchiFile) as typeof MerchiFile;
    this.User = this.setupClass(User) as typeof User;
    this.JobComment = this.setupClass(JobComment) as typeof JobComment;
    this.JobNote = this.setupClass(JobNote) as typeof JobNote;
    this.JobOperationLog = this.setupClass(JobOperationLog) as typeof JobOperationLog;
    this.VariationOption = this.setupClass(
      VariationOption
    ) as typeof VariationOption;
    this.MatchingInventory = this.setupClass(
      MatchingInventory
    ) as typeof MatchingInventory;
    this.SubscriptionPlan = this.setupClass(
      SubscriptionPlan
    ) as typeof SubscriptionPlan;
  }
 
  public authenticatedFetch = (
    resource: string,
    options: RequestOptions,
    expectEmptyResponse?: boolean
  ) => {
    Iif (!options.query) {
      /* istanbul ignore next */
      options.query = [];
    }
    Iif (this.sessionToken) {
      /* istanbul ignore next */
      options.query.push(['session_token', this.sessionToken]);
    }
    Iif (this.clientToken) {
      /* istanbul ignore next */
      options.query.push(['client_token', this.clientToken]);
    }
    Iif (this.invoiceToken) {
      /* istanbul ignore next */
      options.query.push(['invoice_token', this.invoiceToken]);
    }
    Iif (this.cartToken) {
      /* istanbul ignore next */
      options.query.push(['cart_token', this.cartToken]);
    }
    // Pass the full backendUri, and resource is now just the endpoint
    return apiFetch(this.backendUri, resource, options, expectEmptyResponse);
  };
 
  /* istanbul ignore next */
  public authenticatedFetchWithProgress = (
    resource: string,
    options: RequestOptions,
    progressCallback?: (progress: number) => void
  ) => {
    if (!options.query) {
      options.query = [];
    }
    if (this.sessionToken) {
      options.query.push(['session_token', this.sessionToken]);
    }
    if (this.clientToken) {
      options.query.push(['client_token', this.clientToken]);
    }
    if (this.invoiceToken) {
      options.query.push(['invoice_token', this.invoiceToken]);
    }
    if (this.cartToken) {
      options.query.push(['cart_token', this.cartToken]);
    }
    // Pass the full backendUri, and resource is now just the endpoint
    return apiFetchWithProgress(this.backendUri, resource, options, progressCallback);
  };
 
  public getCurrentUser = (options?: UserRequestOptions) => {
    const { embed = {} } = options || {};
    const defaultEmbed = { user: { enrolledDomains: { domain: {} } } };
    Iif (!this.sessionToken) {
      return Promise.resolve(null);
    }
    return this.Session.get(this.sessionToken, {
      embed: { ...defaultEmbed, ...embed },
    }).then((session: any) => session.user);
  };
}