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

jquery-resource

v1.4.0

Published

A jQuery plugin that abstracts the process of consuming a REST endpoint.

Downloads

149

Readme

jquery-resource

CodeQL Node.js CI Codecov LGTM Grade downloads
npm bundle size LICENSE

A jQuery plugin that abstracts the process of consuming a REST endpoint.

Features

  • ⚡ Simple and lightweight
  • 🚀 Incredible high availability
  • ✨ Customizable ajax settings for resources

Table of Contents

Installation

CDN

jquery-resource is available on jsDelivr or unpkg.

jsDelivr

Load jquery-resource from jsDelivr.

<script src="https://cdn.jsdelivr.net/npm/[email protected]"></script>

unpkg

Load jquery-resource from unpkg.

<script src="https://unpkg.com/[email protected]"></script>

Usage

var userResource = $.resource({
  endpoint: 'https://reqres.in/api/users'
});

// POST /api/users
userResource.post({
  email: '[email protected]',
  first_name: 'George',
  last_name: 'Bluth'
});

// GET /api/users/1
userResource.get(1);

// PATCH /api/users/1
userResource.patch(1, {
  email: '[email protected]',
  first_name: 'Emma',
  last_name: 'Wong'
});

// DELETE /api/users/1
userResource.delete(1);

Try it out on JSFiddle.

Methods

post()

var userResource = $.resource({
  endpoint: 'https://reqres.in/api/users'
});

// POST /api/users
userResource.post({
  email: '[email protected]',
  first_name: 'George',
  last_name: 'Bluth'
}).done(function () {
  console.log('POST /api/users');
});

get()

var userResource = $.resource({
  endpoint: 'https://reqres.in/api/users'
});

// GET /api/users/1
userResource.get(1).done(function () {
  console.log('GET /api/users/1');
});

find()

var userResource = $.resource({
  endpoint: 'https://reqres.in/api/users'
});

// GET /api/users?first_name=George
userResource.find({
  first_name: 'George'
}).done(function () {
  console.log('GET /api/users?first_name=George');
});

patch()

var userResource = $.resource({
  endpoint: 'https://reqres.in/api/users'
});

// PATCH /api/users/1
userResource.patch(1, {
  email: '[email protected]',
  first_name: 'Emma',
  last_name: 'Wong'
}).done(function () {
  console.log('PATCH /api/users/1');
});

put()

var userResource = $.resource({
  endpoint: 'https://reqres.in/api/users'
});

// PUT /api/users/1
userResource.put(1, {
  email: '[email protected]',
  first_name: 'Emma',
  last_name: 'Wong'
}).done(function () {
  console.log('PUT /api/users/1');
});

delete()

var userResource = $.resource({
  endpoint: 'https://reqres.in/api/users'
});

// DELETE /api/users/1
userResource.delete(1).done(function () {
  console.log('DELETE /api/users/1');
});

Aliases

add()

Alias of post() method.

var userResource = $.resource({
  endpoint: 'https://reqres.in/api/users'
});

// POST /api/users
userResource.add({
  email: '[email protected]',
  first_name: 'George',
  last_name: 'Bluth'
}).done(function () {
  console.log('POST /api/users');
});

create()

Alias of post() method.

var userResource = $.resource({
  endpoint: 'https://reqres.in/api/users'
});

// POST /api/users
userResource.create({
  email: '[email protected]',
  first_name: 'George',
  last_name: 'Bluth'
}).done(function () {
  console.log('POST /api/users');
});

update()

Alias of patch() method.

var userResource = $.resource({
  endpoint: 'https://reqres.in/api/users'
});

// PATCH /api/users/1
userResource.update(1, {
  email: '[email protected]',
  first_name: 'Emma',
  last_name: 'Wong'
}).done(function () {
  console.log('PATCH /api/users/1');
});

replace()

Alias of put() method.

var userResource = $.resource({
  endpoint: 'https://reqres.in/api/users'
});

// PUT /api/users/1
userResource.replace(1, {
  email: '[email protected]',
  first_name: 'Emma',
  last_name: 'Wong'
}).done(function () {
  console.log('PUT /api/users/1');
});

Custom actions

var userResource = $.resource({
  endpoint: 'https://reqres.in/api/users',
  actions: {
    // actionName: ajaxSettings
    save: {
      method: 'POST',
      url: 'https://reqres.in/api/users/save',
    },
    notify: {
      method: 'POST',
      url: 'https://reqres.in/api/users/notify',
      // use resource id as an argument to action
      useID: true,
    },
  },
});

// save(params, ajaxSettings)
userResource.save({
  email: '[email protected]',
  first_name: 'George',
  last_name: 'Bluth'
});

// notify(id, params, ajaxSettings)
userResource.notify(1, {
  message: 'some message'
});

Last request

var userResource = $.resource({
  endpoint: 'https://reqres.in/api/users',
});

userResource.get(1);
userResource.get(2);

// action's last request
userResource.get.lastRequest.done(function () {
  console.log('GET /api/users/2');
});

isPending()

var userResource = $.resource({
  endpoint: 'https://reqres.in/api/users',
});

userResource.get(1);

// check if the last request is pending
console.log(userResource.get.isPending());

Ajax settings

jQuery.ajax(settings)

Resource's ajax settings

var userResource = $.resource({
  endpoint: 'https://reqres.in/api/users',
  // use resource's ajax settings to actions
  ajaxSettings: {
    contentType: false,
    processData: false
  },
});

userResource.get(1);

// update resource's ajax settings
$.extend(userResource.ajaxSettings, {
  processData: true,
});

Action's ajax settings

var userResource = $.resource({
  endpoint: 'https://reqres.in/api/users',
  action: {
    post: {
      processData: false,
    },
  },
});

// update action's ajax settings
$.extend(userResource.post.ajaxSettings, {
  processData: true,
});

userResource.post({
  email: '[email protected]',
  first_name: 'Emma',
  last_name: 'Wong'
});

One-time ajax settings

var userResource = $.resource({
  endpoint: 'https://reqres.in/api/users',
  ajaxSettings: {
    dataType: 'text'
  },
});

// use one-time ajax settings to post
userResource.post({
  email: '[email protected]',
  first_name: 'Emma',
  last_name: 'Wong'
}, {
  dataType: 'json'
});

// use one-time ajax settings to actions:
//   - post(data, ajaxSettings)
//   - find(params, ajaxSettings)
//   - patch(id, data, ajaxSettings)
//   - put(id, data, ajaxSettings)
//   - get(id, params, ajaxSettings)
//   - delete(id, params, ajaxSettings)

Inspiration

License

MIT