nicklandgrebe / active-resource.js

ActiveResource.js - API resource relational mapping in JavaScript
https://active-resource.js.org
MIT License
133 stars 20 forks source link

JSONAPI meta field #49

Open mati-a opened 5 years ago

mati-a commented 5 years ago

Thanks for the library!

Is there a way to access to meta field? Im searching in the code but couldnt find any.

aidan-casey commented 5 years ago

Agreed that this would be a big nice-have.

Randagio13 commented 4 years ago

Yes I'm agree 🙂

cliffrowley commented 4 years ago

+1 from me. Only thing that has kept me looking for other libraries currently.

daniffig commented 4 years ago

meta is a trivial but useful field in the JSON:API specification. It's essential to fulfill the requirements of some major libraries, for example, Vuetify server-served datatables. I could include it easily overriding the get method in the interface like this.

import _ from "underscore";
import ActiveResource from "active-resource";
import MyCollectionResponse from "./MyCollectionResponse";

class MyCustomInterface extends ActiveResource.Interfaces.JsonApi {
  get = function(url, queryParams = {}) {
    var queryParams =
      arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};

    var _this, data;

    data = {};

    if (queryParams["filter"] != null) {
      data["filter"] = this.buildFilters(queryParams["filter"]);
    }

    if (queryParams["fields"] != null) {
      data["fields"] = this.buildSparseFieldset(
        queryParams["fields"],
        queryParams
      );
    }

    if (queryParams["include"] != null) {
      data["include"] = this.buildIncludeTree(queryParams["include"]);
    }

    if (queryParams["sort"] != null) {
      data["sort"] = this.buildSortList(queryParams["sort"]);
    }

    if (queryParams["page"] != null) {
      data["page"] = queryParams["page"];
    }

    if (queryParams["limit"] != null) {
      data["limit"] = queryParams["limit"];
    }

    if (queryParams["offset"] != null) {
      data["offset"] = queryParams["offset"];
    }

    _this = this;
    return this.request(url, "GET", data).then(
      function(response) {
        var built;
        built = MyCollectionResponse.build(_.flatten([response.data])).map(
          function(object) {
            object = _this.buildResource(object, response.included, {});
            object.assignResourceRelatedQueryParams(queryParams);
            return object;
          }
        );
        built.links(response.links);
        built.meta(response.meta);

        console.log(built.meta());

        if (_.isArray(response.data)) {
          return built;
        } else {
          return built.first();
        }
      },
      function(errors) {
        return Promise.reject(
          _this.parameterErrors(errors.response.data["errors"])
        );
      }
    );
  };
}

export default MyCustomInterface;

I've coded my own CollectionResponse child class to include the meta field.

import _ from "underscore";
import ActiveResource from "active-resource";

class MyCollectionResponse extends ActiveResource.CollectionResponse {
  meta(data = {}) {
    if (!_.isEmpty(data) || !this.__meta) this.__meta = data;

    return this.__meta;
  }
}

export default MyCollectionResponse;

I don't use CoffeScript so my code is plain JavaScript. It could be great if @nicklandgrebe could include this enhancement.