All files / platform/packages/partial-response/src/Bumblebee index.js

100% Statements 38/38
100% Branches 10/10
100% Functions 8/8
100% Lines 38/38

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    1x 1x 1x 1x                               394x     394x 394x 394x 394x 394x                       394x     394x 394x 394x 394x     394x 394x 394x     394x               380x   380x 380x               392x 392x                   188x 187x   188x 188x                 18x 17x   18x               287x     287x 283x 89x   194x     287x         1x  
'use strict';
 
const AdonisBumblebee = require('adonis-bumblebee/src/Bumblebee');
const Manager = require('adonis-bumblebee/src/Bumblebee/Manager');
const PartialFieldManager = require('./PartialFieldManager');
const { ioc } = require('@adonisjs/fold');
 
/**
 * Bumblebee class
 *
 * @class Bumblebee
 * @constructor
 */
class Bumblebee extends AdonisBumblebee {
  /**
   * Create a new Bumblebee instance.
   * An instance of Manager has to be passed
   *
   * @param {Manager} manager
   */
  constructor(manager) {
    super(manager);
 
    // Set partial fields manager
    const Config = ioc.use('Adonis/Src/Config');
    this._partialFieldManager = new PartialFieldManager();
    this._enabledPartialResponse = !!Config.get('bumblebee.usingHeaderFields', false);
    this._requestHeaderFields = Config.get('bumblebee.headerFieldsKey', 'X-Fields');
    return this;
  }
 
  /**
   * Override AdonisBumblebee.create static method since the AdonisBumblebee not using
   * this when create new instance.
   *
   * @param {*} data
   * @param {TransformerAbstract} transformer
   */
  static create(data = null, transformer = null) {
    // create an instance of Bumblebee and pass a new instance of Manager
    const instance = new Bumblebee(new Manager());
 
    // initialize data and transformer properties
    instance._data = data;
    instance._dataType = instance._determineDataType(data);
    instance._transformer = transformer;
    instance._variant = null;
 
    // set pagination, context and meta properties to null
    instance._pagination = null;
    instance._context = null;
    instance._meta = null;
 
    // return the instance for the fluid interface
    return instance;
  }
 
  /**
   * @Override AdonisBumblebee.withContext
   * @param {Context} ctx
   */
  withContext(ctx) {
    super.withContext(ctx);
    // Setup partial fields from context
    this.setPartialFieldsString(ctx.request.header(this._requestHeaderFields));
    return this;
  }
 
  /**
   * Additional resources that should be included as partial
   * @param {string} partialFieldsString
   */
  setPartialFieldsString(partialFieldsString) {
    this._partialFieldManager.setPartialFieldsString(partialFieldsString);
    return this;
  }
 
  /**
   * Additional resources that should be included and that are defined as
   * 'availableInclude' on the transformer.
   *
   * @param {Array|String} includeResources
   */
  include(includeResources) {
    if (this._enabledPartialResponse) {
      includeResources = this._partialFieldManager.reduceIncludes(includeResources);
    }
    this._manager.parseIncludes(includeResources);
    return this;
  }
 
  /**
   * Return the required includes with partial fields
   * @param {Array|string} includeResources
   * @returns
   */
  getPartialIncludes(includeResources) {
    if (this._enabledPartialResponse) {
      includeResources = this._partialFieldManager.reduceIncludes(includeResources);
    }
    return includeResources;
  }
 
  /**
   * Override AdonisBumblebee.toJSON
   * Terminates the fluid interface and returns the transformed data.
   */
  toJSON() {
    return this._createData()
      .toJSON()
      .then((data) => {
        if (this._enabledPartialResponse) {
          if (this._pagination) {
            data.data = this._partialFieldManager.toJSON(data.data);
          } else {
            data = this._partialFieldManager.toJSON(data);
          }
        }
        return data;
      });
  }
}
 
module.exports = Bumblebee;