GithubHelp home page GithubHelp logo

doktordirk / aurelia-api Goto Github PK

View Code? Open in Web Editor NEW

This project forked from spoonx/aurelia-api

0.0 2.0 1.0 438 KB

A simple convenience wrapper around aurelia-fetch-client

License: MIT License

JavaScript 100.00%

aurelia-api's Introduction

aurelia-api

Build Status Known Vulnerabilities Gitter

This library is a plugin for the Aurelia platform and contains support for multiple endpoints, extending the functionalities supplied by aurelia-fetch-client. This library plays nice with the Sails.js framework.

Talking to your api shouldn't be difficult. You shouldn't have to repeat yourself. You shouldn't need nuclear power plants to make it easier. You should just be able to say "give me that thing!" and be done with it. If only we could do something about that...

You guessed it! We have something for that. Aurelia-api comes with a set of cool features that makes talking to APIs easy and fun.

Aurelia-api is a module wrapped around aurelia-fetch-client that allows you to:

  • Perform the usual CRUD
  • Supply criteria for your api
  • Manage more than one endpoint
  • Add defaults
  • Add interceptors
  • And more

Documentation

You can find usage examples and the documentation at aurelia-api-doc.

The changelog provides you with information about important changes.

Installation

Aureli-Cli

Run npm i aurelia-api --save from your project root.

Aurelia-api makes use of aurelia-fetch-client and extend. You might also need a (fetch)[https://github.com/github/fetch] polyfill, if targeting older browsers. So, add following to the build.bundles.dependencies section of aurelia-project/aurelia.json.

"dependencies": [
  // ...
  "extend",
  "aurelia-fetch-client",
  "aurelia-api",
  // "fetch",
  // ...
],

Jspm

Run jspm i aurelia-api

Add aurelia-api to the bundles.dist.aurelia.includes section of build/bundles.js.

If the installation results in having forks, try resolving them by running:

jspm inspect --forks
jspm resolve --only registry:package-name@version

E.g.

jspm inspect --forks
>     Installed Forks
>         npm:aurelia-dependency-injection 1.0.0-beta.1.2.3 1.0.0-beta.2.1.0

jspm resolve --only npm:[email protected]

Webpack

Run npm i aurelia-api --save from your project root.

Add 'aurelia-api' in the coreBundles.aurelia section of your webpack.config.js.

Typescript

Npm-based installations pick up the typings automatically. For Jspm-based installations, add to your typings.json:

"aurelia-api": "github:spoonx/aurelia-api",

and run typings i

or run

typings i github:spoonx/aurelia-api

Usage

Configuring

Register the plugin and some endpoints.

aurelia.use
  /* Your other plugins and init code */
  .plugin('aurelia-api', config => {

    // Register hosts
    config.registerEndpoint('api', '/mypath');
    config.registerEndpoint('other-api', '/otherpath', {headers: {'Content-Type': 'x-www-form-urlencoded'}});
  })

Get and use an endpoint

You can get endpoints with the .getEndpoint() method on the Config instance from aurelia-api.

import {inject} from 'aurelia-framework';
import {Config} from 'aurelia-api';

@inject(Config)
export class MyClass {
  constructor(config) {
    this.apiEndpoint = config.getEndpoint('api');

    this.apiEndpoint.find('users')
    .then(users => {
        // use your received users.json
    })
    .catch(console.error);
  }
}

Quick Rest api overview

All methods will:

  • stringify the body, if it is an object and the Content-Type is set to application/json (the default).
  • convert the body to querystring format, if the body is an object and the Content-Type is set to any other value.
  • leave the body unchanged, if the Content-Type is not set or when the body is not an object.
  • maintain trailing slashes of the resource parameter

All methods return on success a Promise with the server response parsed to an object if possible. On error, they reject with the server response. If possible and parseError is set true, they reject with the JSON parsed server response.

endpoint
  .client                                                           // the httpClient instance
  .endpoint                                                         // name of the endpoint
  .default                                                          // The fetch client defaults
  .find(resource, idOrCriteria, options, responseOutput)                // GET
  .findOne(resource, id, criteria, options, responseOutput)         // GET
  .post(resource, body, options, responseOutput) {                  // POST
  .update(resource, idOrCriteria, body, options, responseOutput)        // PUT
  .updateOne(resource, id, criteria, body, options, responseOutput) // PUT
  .patch(resource, idOrCriteria, body, options, responseOutput)         // PATCH
  .patchOne(resource, id, criteria, body, options, responseOutput)  // PATCH
  .destroy(resource, idOrCriteria, options, responseOutput)             // DELETE
  .destroyOne(resource, id, criteria, options, responseOutput)      // DELETE
  .create(resource, body, options, responseOutput)                  // POST
  .request(method, path, body, options, responseOutput)             // method

Note

Some months ago, we've simplified installation and usage! This plugin should now be installed using jspm i aurelia-api or (for webpack) npm i aurelia-api --save. Make sure you update all references to spoonx/aurelia-api and remove the spoonx/ prefix (don't forget your config.js, package.json, imports and bundles).

aurelia-api's People

Contributors

adamwillden avatar doktordirk avatar fedoranimus avatar greenkeeperio-bot avatar kellyethridge avatar kukks avatar nevercast avatar pfurini avatar rwoverdijk avatar yutackall avatar

Watchers

 avatar  avatar

Forkers

evgenygrishnov

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.