npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

vue-easy-rest

v5.2.14

Published

Advanced model layer for a VueJS app with a REST API

Downloads

181

Readme

vue-easy-rest by @Destygo

What is vue-easy-rest

Vue-easy-rest is a NPM package for VueJs for managing data served by a CRUD REST API.

Why we created it

We created vue-easy-rest because existing packages did not fit our needs :

  • HTTP clients (VueResource, Axios, ..) : They are good at providing functions to read/write a data, but they return plain objects, for which all the business logic of maintaining them must be handeled manually.
  • Model vue packages (vue-model, vue-mc, ...) or wrappers around other collection based packages (Backbone collections/models, ...) : They force us to maintain a descriptive state of the data and the relations in the front-end, which has to be in implicit sync with the backend. Moreover, most of them do not actually provide automatic gestion of relations between resources (Backbone schema is an exception, but is not easily compatible with VueJS).

What we want is to have a model layer which adresses these issues :

  1. We want to READ a "high level" resource in one chunck (with nested resources inside it). This enables us to retrieve a form without having to make several requests and reconnect the results together.
  2. We want to be able to UPDATE/CREATE/DESTROY each resource individually, regardless of how that resource was read alone or as being nested in a "bigger" resource.
  3. We want to be able to UPDATE/CREATE/DESTROY links between resources without having to know in the front-end how the link is actually represented in the database (for instance, we use polymorphic tables for some relationships, which require a slightly different data than just [key]_id, but VueJS should not have to care about that).
  4. We want to be able to do all of that without knowing in advance the structure of the data (if the structure of the data changes, we need to update the backend and the components of the VM layer only, NOT the model layer, NOT the update logic)
  5. We want objects exposed by our model layer to behave like plain objects and be as close as possible to 'pure state', so we can use them directly and very easily in our templating.

NPM Package

https://www.npmjs.com/package/vue-easy-rest

Core concepts

They are 2 main classes you will work with with vue-easy-rest : ResourceType and Resource. A Resource represents an individual object in the API, and a ResourceType is just a collection of resources.

How to use

ResourceType

The ResourceType (or Collection), is a list of items representing resources in the API (and most of the time lines in the database).

Declare a ResourceType

The constructor of ResourceType takes two parameters :

  • route (string): URL of the collection (absolute or relative)
  • options (Object, optional): Accepted keys =>
    • scope : The scope of the collection, which is an object of parameters that will be sent to the API when getting the collection on the index route. No parameters if left empty.
    • autosaveDisabled : Boolean to trigger off the autosave functionanlity (default : false)

Examples :

answers = new ResourceType(`${window.location.origin}/answers`, { scope: {by_machine_node: 13} })
livechecks = new ResourceType(`/vue_api/livechecks`, { autosaveDisabled: true })

Get the list of items in a collection

The method fetch of a ResourceType makes a GET HTTP call on the index route of the collection. Upon getting a response, all local resources will be erased and replaced by the result of the request.

Examples :

  • With async/await :
await answers.fetch()
try {
    await livechecks.fetch()
} catch (e) {
    console.warn('Livechecks unavailable')
}
  • With promises :
answers.fetch().then(reponse => { etc... })
livechecks.fetch().then(reponse => { etc... }).catch(err => { console.warn('Livechecks unavailable') })

Access the list of items of a collection

Examples :

  • answers.models (returns a javascript Array of Resource)
  • for (let answer of answers.models)
  • <div v-for="answer of answers.models" :key="answer.id">

Create a new object (high level resource)

Examples :

  • let instance = await instances.create({name: "New instance"})
  • let case_statement = await case_staments.create({name: "My super case statement", answer_id: 12})

Create a new object (nested sub-resource)

Example : let input = await answers.inputs.push({name: "My super input"})

Resource

Test whether the data of a Resource has been loaded or not

When fetching a ResourceType, for each resource, we only get the data that the index route sends. Most of the time, the individual GET route of a resource will send more information which is not sent in the index to prevent the response from being too big.

To test whether the resource has been individually loaded or not : answer.fullyLoaded() (returns uan boolean)

Fetch a resource

Exemple : await answer.fetch()

Delete a resource

Exemples :

  • await answer.delete()
  • await answer.destroy() (exact same result, destroy is just an alias for delete) Pro tip : Be careful not to use .delete() in the template of a vue component. You could be surprised. Now I know you will try. Go ahead

Set a resource data

Exemples :

  • answer.name = 'New name'
  • answer.inputs[2].reference = 'other'
  • <input v-model="answer.name">

PUT requests are automatically performed by vue-easy-rest, except if the autosaveDisabled option has been declared. In that case, the saveAll method will force saving the object with all its children.

Call a custom endpoint

Resource and ResourceType provide a call method that enable to make a call to a custom non-CRUD route

Exemples :

  • answer.call('check_integrity', 'GET', { anAdditionalParam: 42 }
  • (new ResourceType('instances')).call('all_available_languages', 'GET')
  • imageTemplate.call('deploy', 'PUT')

Fetch a resource without fetching the collection

Sometimes, you already know which resource you need, but you don't need to actually perform a GET to the index of that resource and THEN fetch the resource individually.

Exemples :

let answers = new ResourceType('/vue_api/answers');
let answer = answers.createModel({id: 12});
await answer.fetch();

createModel locally pushes a new resource in the ResourceType with the given attributes. Simillarily, there is a local findOrCreateModel.

Vue-mc

Now that you know more about vue-easy-rest, it's time for you to know that it's a wrapper around vue-mc. Resource is an extension of Model and ResourceType is an extension for Collection. That means you also have access to methods provided by vue-mc in their documentation : http://vuemc.io/#introduction