Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
@doodle/subscriptions-api-connector / src / api / subscriptionApi.js
Size: Mime:
import fetch from 'fetch-everywhere';
import { errorHandler, errorHandlerMonolith, responseHandler, responseHandlerMonolith } from '../helpers/fetch.js';
import convertSubscription from '../helpers/converter';

const SUBDOMAIN_ENDPOINT = '/np/utils/validateUrlPrefix';

/**
 * Only adds truthy values to the headers object.
 *
 * @method
 * @private
 * @param {string} authToken
 * @param {string} experimentId Id of an AB test experiment defined in the MongoDB for svc-billing
 */
export function buildHeaders(authToken, experimentId) {
  return {
    ...(Boolean(authToken) && { Authorization: `Bearer ${authToken}` }),
    ...(Boolean(experimentId) && { 'billing-experiment-key': experimentId }),
    'Content-Type': 'application/json',
    Accept: 'application/json',
  };
}

/**
 * Creates a subscription for the user.
 * @method
 * @public
 * @param {string} planId The id of the subscription plan
 * @param {SubscriptionPayload} subscriptionPayload The data of the subscription to create
 * @param {string} authToken
 * @param {string} experimentId Id of an AB test experiment defined in the MongoDB for svc-billing
 * @param {Object} options Additional options
 * @param {string} options.endpoint The endpoint's base URL
 */
export function createSubscription(planId, subscriptionPayload, authToken, experimentId, options) {
  const headers = buildHeaders(authToken, experimentId);
  return fetch(`${options.endpoint}/plans/${planId}/subscribe`, {
    method: 'POST',
    headers,
    body: JSON.stringify(subscriptionPayload),
  })
    .catch(errorHandler)
    .then(responseHandler);
}

/**
 * Updates the user's subscription.
 * @method
 * @public
 * @param {string} planId The id of the subscription plan
 * @param {SubscriptionPayload} subscriptionPayload The data of the subscription to create
 * @param {string} authToken
 * @param {string} experimentId Id of an AB test experiment defined in the MongoDB for svc-billing
 * @param {Object} options Additional options
 * @param {string} options.endpoint The endpoint's base URL
 */
export function changeSubscriptionPlan(planId, subscriptionPayload, authToken, experimentId, options) {
  const headers = buildHeaders(authToken, experimentId);
  return fetch(`${options.endpoint}/plans/${planId}/change`, {
    method: 'POST',
    headers,
    body: JSON.stringify(subscriptionPayload),
  })
    .catch(errorHandler)
    .then(responseHandler);
}

/**
 * Fetches a user subscription.
 * @method
 * @public
 * @param {string} authToken
 * @param {Object} options Additional options
 * @param {string} options.endpoint The endpoint's base URL
 */
export function fetchSubscription(authToken, options) {
  return fetch(`${options.endpoint}`, {
    headers: {
      'Access-Token': authToken,
      Accept: 'application/json',
    },
  })
    .catch(errorHandler)
    .then(responseHandler)
    .then(response => (response && response.document && response.document.length > 0 ? response.document[0] : null))
    .then(convertSubscription);
}

/**
 * Fetches all user subscriptions, including those that expired.
 * @method
 * @public
 * @param {string} authToken
 * @param {Object} options Additional options
 * @param {string} options.endpoint The endpoint's base URL
 */
export function fetchAllSubscriptions(authToken, options) {
  return fetch(`${options.endpoint}/list`, {
    headers: {
      'Access-Token': authToken,
      Accept: 'application/json',
    },
  })
    .catch(errorHandler)
    .then(responseHandler)
    .then(response => (response && response.document ? response.document : []))
    .then(subscriptions => subscriptions.map(convertSubscription));
}

/**
 * Checks whether a subdomain is available.
 * Note: this method still uses the NP API.
 * @method
 * @public
 * @param {string} subdomain The subdomain to validate
 * @param {string} locale The user's locale
 * @param {string} mandatorId The id of the user's mandator
 */
export function validateSubdomain(subdomain, locale, mandatorId) {
  const details = { urlPrefix: subdomain, locale, mandatorId };
  const formData = Object.keys(details)
    .map(key => `${encodeURIComponent(key)}=${encodeURIComponent(details[key])}`)
    .join('&');

  return fetch(SUBDOMAIN_ENDPOINT, {
    method: 'POST',
    headers: {
      Accept: 'application/json',
      'Content-Type': 'application/x-www-form-urlencoded',
    },
    body: formData,
  })
    .catch(errorHandlerMonolith)
    .then(responseHandlerMonolith);
}

/**
 * Checks whether a VAT ID is valid.
 * @method
 * @public
 * @param {string} vatId The VAT ID to check for validity
 * @param {string} country The Country provided by the user
 * @param {string} authToken
 * @param {Object} options Additional options
 * @param {string} options.endpoint The endpoint's base URL
 */
export function validateVatId(vatId, country, authToken, options) {
  return fetch(`${options.endpoint}/validate/${country}/${vatId}`, {
    headers: {
      'Access-Token': authToken,
      Accept: 'application/json',
    },
  })
    .catch(errorHandler)
    .then(responseHandler)
    .then(response => (response.document ? response.document.valid : false));
}

/**
 * Saves billing info and checks if vat is valiid
 * @method
 * @public
 * @param {string} authToken
 * @param {Object} billing information
 * @param {string} options.endpoint The endpoint's base URL
 */
export function saveAndValidateBillingInfo(authToken, billingInfo, options) {
  return fetch(`${options.endpoint}/billingInfo`, {
    method: 'POST',
    headers: {
      'Access-Token': authToken,
      Accept: 'application/json',
      'Content-Type': 'application/json',
    },
    body: JSON.stringify(billingInfo),
  })
    .catch(errorHandler)
    .then(responseHandler)
    .then(response => (response.document ? response.document.valid : false));
}

/**
 * Sets the autorenwal state of the given subscription
 *
 * @param {string} subscriptionId
 * @param {boolean} autoRenewalOn
 */
export function changeAutoRenewal(authToken, subscriptionId, autoRenewalOn, options) {
  const autoRenewalState = autoRenewalOn ? 'ON' : 'OFF';
  return fetch(`${options.endpoint}/${subscriptionId}/autorenewal/${autoRenewalState}`, {
    method: 'PUT',
    headers: {
      'Access-Token': authToken,
      Accept: 'application/json',
    },
    body: JSON.stringify({}),
  })
    .catch(errorHandler)
    .then(responseHandler);
}