cosmicjs / cosmicjs-node

*API v2 JavaScript client for Cosmic. See important message in README
https://www.cosmicjs.com
MIT License
275 stars 38 forks source link

Node API for search #8

Closed TroutZen closed 7 years ago

TroutZen commented 7 years ago

Hi,

Are there plans for extending this API for interacting with the /search endpoint? It currently looks like the getObjectType interface does not support this case.

tonyspiro commented 7 years ago

Hey Devin! Yes there are plans to add more to this client (including sort, authentication, etc). If you'd like to add this, submit a PR, I would be more than happy to merge it 😄

TroutZen commented 7 years ago

@tonyspiro - sounds great, I'll most likely have a bit of time sometime this week to put together a small PR. Keep you posted!

weewey commented 7 years ago

@tonyspiro While I'm trying to help with this API, I discovered there is an issue with the /search endpoint with metafield_object_slug parameter.

I'm getting 502 bad gateway error when I specified the following:

https://api.cosmicjs.com/v1/secondwindnation/object-type/posts/search?metafield_key=category&metafield_object_slug=general

weewey commented 7 years ago

anyway, i have written this. I can't seem to push to the repo, it says unauthorized.

getObjectBySearch: function(config, object , callback){
    var searchParams = '/search?' + 'metafield_key=' + object.metafield_key;
    if (object.metafield_value) searchParams += '&metafield_value=' + object.metafield_value;
    else if (object.metafield_object_slug) searchParams += '&metafield_object_slug=' + object.metafield_object_slug;
    else searchParams += '&metafield_value_has=' + object.metafield_value_has;
    var endpoint = api_url + '/' + api_version + '/' + config.bucket.slug + '/object-type/' + object.type_slug + searchParams + '&read_key=' + config.bucket.read_key;
    if (object.limit) endpoint += '&limit=' + object.limit;
    if (object.skip) endpoint +=  '&skip=' + object.skip;
    fetch(endpoint)
    .then(function(response){
      if (response.status >= 400) {
        var err = {
          "message" : "There was an error with this request."
        }
        return callback(err, false);
      }
      return response.json()
    })
    .then(function(response){
      // Constructor
      var cosmic = {};
      var objects = response.objects;
      cosmic.objects = {};
      cosmic.objects.all = objects;
      cosmic.object = _.map(objects, keyMetafields);
      cosmic.object = _.keyBy(cosmic.object, "slug");
      cosmic.total = response.total;
      return callback(false, cosmic);
    });
  },
tonyspiro commented 7 years ago

@weewey thanks for the heads up on the endpoint and for adding the new method 😄

The endpoint issue is now fixed, however I noticed that you have the category Metafield key set to post_category in your Bucket, make sure it's this in the query string ?metafield_key=post_category also the Metafield must be a single Object Metafield and not a Multiple Object Metafield.

As for merging the new method, please submit a pull request and I'll merge it in.

Thanks @weewey!

weewey commented 7 years ago

I'm not too sure whether it is an error on my side or the permission issue of the repo.

YewWee@Yews-MBP:~/cosmicjs-node$ git push origin fix/get-object-by-search

ERROR: Permission to cosmicjs/cosmicjs-node.git denied to weewey.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
tonyspiro commented 7 years ago

@weewey you will need to fork a copy, make your changes, then issue a pull request here: https://github.com/cosmicjs/cosmicjs-node/pulls

tonyspiro commented 7 years ago

Thanks @weewey, the PR has been merged 🙌

tonyspiro commented 7 years ago

@weewey FYI I changed the method to indicate multiple Objects returned getObjectsBySearch.