transportation/server.js

import { JsonApiServer } from '../json_api'

/**
 * Facilitates interaction with the Transportation server.
 *
 * @class
 */
export class TransportationServer extends JsonApiServer {
  /**
   * Create a new Transportation server instance.
   *
   * @constructor
   * @param {ShelfNetwork} sdk Parent SDK instance.
   * @param {string} serverUrl Transportation URL.
   * @param {Object} opts
   * @param {boolean} [opts.allowHttp] Allow connecting to http servers, default: `false`. This must be set to false in production deployments!
   * @param {Object} [opts.proxy] Proxy configuration. Look [axios docs](https://github.com/axios/axios#request-config) for more info
   * @param {Object} [opts.httpBasicAuth] HTTP basic auth credentials. Look [axios docs](https://github.com/axios/axios#request-config) for more info.
   * @param {Object} [opts.customHeaders] Custom headers for request.
   * @param {boolean} [opts.withCredentials] Indicates whether or not cross-site Access-Control requests should be made using credentials.
   * @param {string} [opts.responseType='json'] Indicates the type of data that the server will respond with options are 'arraybuffer', 'blob', 'document', 'json', 'text', 'stream'.
   */
  constructor (sdk, serverUrl, opts = {}) {
    opts.alias = 'transportation_fee'
    super(sdk, serverUrl, opts)
  }

  /**
   * Get available cities.
   *
   * @return {Promise.<JsonApiResponse>} Transportation response.
   */
  async getCities () {
    return this._makeCallBuilder()
      .appendUrlSegment('cities')
      .get()
  }

  /**
   * Get transportation price.
   *
   * @param {object} query Request options.
   * @param {number} [query.cityId] City ID.
   * @param {string} [query.lotType] Lot type.
   * @param {string} [query.city] City.
   * @param {string} [query.zip] ZIP code.
   * @param {string} [query.state] State code. E.g. "CA"
   *
   * @return {JsonApiResponse}
   */
  async getPrice (query) {
    return this._makeCallBuilder()
      .appendUrlSegment('price')
      .get(query)
  }

  /**
   * Calculate multiple transportation prices.
   *
   * @param {object[]} cities Array of cities data.
   * @param {Number} [cities.id] City ID.
   * @param {string} [cities.zip] City zip code.
   * @param {string} [cities.name] City name.
   * @param {string} [cities.state] City state.
   * @param {Number} [cities.lotId] Lot ID. Will be returned in response just for frontend mapping purposes.
   * @param {string} [query.lotType] Lot type.
   *
   * @return {JsonApiResponse}
   */
  async getPriceBulk (cities) {
    return this._makeCallBuilder()
      .appendUrlSegment(`price`)
      .post({
        data: cities.map(city => ({
          type: 'cities',
          id: city.id,
          attributes: {
            zip: city.zip,
            city: city.name,
            state: city.state,
            lotId: city.lotId,
            lotType: city.lotType
          }
        }))
      })
  }
}