Coder Social home page Coder Social logo

victor-am / rails-ranger Goto Github PK

View Code? Open in Web Editor NEW
32.0 2.0 4.0 1.94 MB

🀠 An opinionated AJAX client for Ruby on Rails APIs

Home Page: https://victor-am.github.io/rails-ranger/

License: MIT License

JavaScript 98.17% HTML 1.83%
rails-api rails axios javascript ajax ajax-client rest-client

rails-ranger's People

Contributors

codacy-badger avatar dependabot[bot] avatar ryanmk54 avatar timgates42 avatar valdemarua avatar victor-am avatar waffle-iron avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

rails-ranger's Issues

Support for inflections

Allow things like api.show('user', { id: 1 }) instead of api.show('users', { id: 1 }) through Rails-like inflections.

Integrate with Rails authentication libraries

The idea is to make Rails Ranger also a solution to handle authenticated requests on the front-end. The idea is very rough so I'm not sure how much Rails Ranger and still keeping consistency to what it is.

I'm keeping this issue as a reminder of a possible evolution path for the library.

API calls fail for Rails apps that respond to HTML and JSON

Given the following controller:

class ThingController < ApplicationController
  def index
    @things = Things.all
    respond_to do |format|
      format.html
      format.json { render json: @things }
    end
  end

and the following JavaScript:

api.list('things').then((response) => {
  console.log(response.data)
})

then response.data contains the HTML of /things rather JSON. If I visit /things.json in the browser I get the expected JSON.

I can't see how to configure rails-ranger or axios to append .json to the URL. Is this possible? Sadly, calling api.list('things.json') produces a request to things_json, which doesn't help much.

(I realise that rails-ranger's really intended for use with API-only Rails backends, but it would be extremely useful to be able to have it work for this case, even if that's not the default behaviour, as we don't always have control over the other end…)

Thanks!

Reduce build size

At the time of writing the build size is 90kb (minified), which is too big for such a simple library (even when accounting for Axios that weights 12.4kb minified).

If we get to around 45kb (minified but without gzip) we can consider closing this issue.

Get method doesn't use existing resource

I have the route current_sbm_employees_path | GET | /merchandising/sbm_employees/current(.:format)
I tried to access it with api.resource('sbm_employees').get('current')
but that gives me /merchandising/current
It works correctly if I use api.resource('sbm_employees').list('current')

Based on the documentation it appears that accessing the url with the get method should work too.

Fix the documentation

Currently the RailsRanger#index method is not showing up in the docs and the RailsRanger#list method is showing up as global.

Support for singular resources

Currently we don't support singular resources through the api.action('resource') API, but it's a desirable feature.

I've played around with some APIs for this but I didn't feel comfortable with any of them.

Suggestions are welcome πŸ˜„

Wrong parsing when passing nested objects

After this code

        list(context, { filters }) {
          return api.list('characters', { filters }).then((response) => {
            context.commit('setItems', response.data)
          })
        }

Rails received

<ActionController::Parameters {"filters"=>"[object Object]", (...)}>

Interchangeable API adapters

Allow a set of official adapters (like ActiveModelSerializer's JSON API, Attributes, etc) and the possibility of passing a custom adapter to the Rails Ranger client.

I thought something like that:

import RailsRanger from 'rails-ranger'

//
// Official adapter
//
const api = new RailsRanger({
  adapter: 'officialAdapterName'
})

//
// Custom adapter
//
const myAdapter = {
  // Some object describing the adapter
}

const api = new RailsRanger({
  adapter: myAdapter
})

But as always I'm open to suggestions πŸ˜„

Parameter substitution only seems to work for one parameter

I'm having trouble getting the parameter substitution to work with more than one parameter:

api.get('/first/:a/second/:b/third/:c', { a: 'A', b: 'B', c: 'C', d: 'D' }).then((r) => {
  console.log(r)
}).catch((e) => {
  console.log(e)
})

should perform a GET to /first/A/second/B/third/C?d=D, but instead tries to GET /first/:a/second/:b/third/C?d=D

I've managed to work around this by using api.get(`first/${a}/second/${b}/third/:c`) for now, but it doesn't look very clean (just like attempting to put backticks in a code block in GitHub πŸ™„ )

I can't see anything in _paramsToPath that could be causing this β€”Β do you know what could be going wrong here?

(I did try replicating this with the RouteBuilder, but the import in the README seems to be wrong:

import { RouteBuilder } from RailsRanger
const routes = new RouteBuilder

produces Uncaught Error: Module build failed: SyntaxError: Unexpected token. Replacing the import with

import { RouteBuilder } from 'rails-ranger'

produces TypeError: __WEBPACK_IMPORTED_MODULE_3_rails_ranger__.RouteBuilder is not a constructor. What is the correct way to import the RouteBuilder?)

Weird behavior with update params

I didn't had the time to investigate deeper, but the following is happening:

πŸ‘ Works fine

api.update('characters', { id: 1 })

❌ Doesn't work

api.update('characters', { id: 1, character: { firstName: 'John' } })

The second scenario tries tries to request the /characters/:id URL (seems like it didn't replace the :id with the given id).

this.client is undefined in readme example

I am trying to copy the example in the readme, with the following code, but I get the error: this.client is undefined. If I change this.client to client it works fine.

import RailsRanger from 'rails-ranger';
const client = new RailsRanger;
export default {
  client,

  orders: {
    update(params) {
      return this.client.update('orders', params) // <-- error on this line
        .then(response => response.data)
    } 
  }
};

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.