Coder Social home page Coder Social logo

naeemo / web-model Goto Github PK

View Code? Open in Web Editor NEW
7.0 2.0 2.0 1.94 MB

API layer for the web, organize your api requests nicely.

Home Page: https://naeemo.github.io/web-model/

License: MIT License

JavaScript 100.00%
ajax request model superagent ajax-interceptor singleton-ajax

web-model's Introduction

web-model

Version License

API layer for the web, organize your api requests nicely.

Instead of repeating api requests everywhere in your SPA, a maintainable model layer for backend's api service seems more reasonable. Web-model has many useful features: request/response guards, request caching(web storage), singleton request, and more to come. Web-model rely superagent as ajax tool.

Install

yarn add web-model

or

npm i web-model

Notice, if you are using babel(like babel-loader with webpack 4+), make sure to include node_modules/web-model in the compile process.

Example for webpack 4+

module.exports = {
    // ...
    module: {
        rules: [
            {
                test: /\.js$/,
                loader: 'babel-loader',
                exclude: {
                    test: /node_modules/,
                    exclude: /web-model/
                },
                options: {}
            }
        ]
    }
    // ...
}

Basic usage

Define the model

import Model from 'web-model';
export default new Model({
    base: 'https://xxx.xxxx.com',
    api: {
        getApples() {
            return this.request.get('/apple')
        },
        getApple(id) {
            return this.request.get(`/apple/${id}`)
        },
        saveApple(apple) {
            return this.request.post('/apple').send(apple)
        },
        updateApple(apple) {
            return this.request.put(`/apple/${id}`).send(apple)
        },
        deleteApple(id) {
            return this.request.del(`/apple/${id}`)
        },
    }
})

then

import appleModel from './path/to/appleModel.js'

appleModel.getApples().then(({body: {apples}}) => {
    console.log('all my apples are here: ', apples);
})

Documents

web-model


Supported by JetBrains open source program.

web-model's People

Contributors

dependabot[bot] avatar naeemo avatar vpsvl avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

web-model's Issues

build configures

  • webpack build config: a rough build config for now.
  • more detailed doc;
  • 中文文档;
  • test config with karma;
  • tests

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.