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-dmresource

v0.6.1

Published

Wrapper for vue-resource to provide API layer in Vue web app

Downloads

5

Readme

vue-dmresource

Convenience wrapper for VueResource to provide API layer in Vue web app. Common handlers are built in. Custom route descriptors can be passed into constructor; any '?' wildcards in URL will be replaced in order with arguments passed to handler, or named params (:param) can be passed as object. Base URL can be provided as a custom route named "base" (see example below).

Request URL is concatenated like so: BASE_URL/NAME/CUSTOM_ROUTE_URL; for example: /api/work_order/?/notes (unless exact prop is set; see below).

Installation

npm i --save vue-dmresource

Basic Usage

API = require 'vue-dmresource'
MyApi = new API name [, custom_route_config]

MyApi is (in this example) how you refer to the API in your component; name will be appended to each request URL (see above) and would typically correspond to a subroute of your backend API; see example below.

You can specify an exact URL by including exact: true in your route definition. Including a leading / will cause that URL to be interpreted relative to window.location.host (the site document root); omitting the leading slash makes it a relative URL; including a protocol (http://) will allow you to specify the exact URL from start to finish.

When using named URL parameters in custom methods, verbs PATCH, POST and PUT provide a handy shortcut if all parameters are contained in the BODY of the request: in this case, pass NULL for the PARAMS argument and your parameter keys will be parsed out of the BODY (see below for examples).

The optional last argument to all methods (third for PATCH, POST and PUT; second for verbs which don't use a BODY) is the VueResource OPTIONS object.

Example component

Vue = require 'vue' # omit if Vue is loaded in <SCRIPT> tag
API = require 'vue-dmresource'
WorkOrder = new API 'work_order',
  base: # optional baseUrl prepended to all requests (default is '/api')
    url: '/api/v1'
  get_notes: # route with wildcard parameter(s)
    method: 'get'
    url: '/?/notes'
  get_items: # route with named parameter(s)
    method: 'get'
    url: '/:id/items'
  get_others: # route with exact URL
    method: 'get'
    url: '/not_api/?/all/different'
    exact: true
  get_external: # route with complete exact URL
    method: 'get'
    url: 'http://other-external-web-site.com/items/?'
    exact: true
  update_item:
    method: 'put'
    url: '/item/?'
  update_item_named:
    method: 'put'
    url: '/item/:id'

Vue.extend
  name: 'wo-component'
  data: () ->
    items: []
    notes: []
  mounted: () ->
    WorkOrder.get_notes(1234).then (notes) ->
      @notes = notes
      WorkOrder.get_items({ id: 1999 }).then (items) ->
        @items = items
    .catch (err) ->
      console.log err

    # wildcard ('?') ID param provided explicitly
    WorkOrder.update_item(1234, { name: 'Made up' }).then (data) ->
      updated_item = data

    # named PARAMS and BODY provided explicitly
    body = { name: 'Made up', status: 'active' }    
    params = { id: 1234 }
    WorkOrder.update_item_named(params, body).then (data) ->
      updated_item = data

    # no PARAMS; will be parsed from provided BODY
    body = { id: 1234, name: 'Made up', status: 'active' }
    WorkOrder.update_item_named(null, body).then (data) ->
      updated_item = data

    # optional last argument is VueResource OPTIONS
    body = { id: 1234, name: 'Made up', status: 'active' }
    options = { headers: Accept: 'application/json' }
    WorkOrder.update_item_named(null, body, options).then (data) ->
      updated_item = data

Testing

npm test