0.3.16 • Published 1 year ago

web-model v0.3.16

Weekly downloads
2
License
MIT
Repository
github
Last release
1 year ago

web-model

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.

0.3.16

1 year ago

0.3.15

2 years ago

0.3.14

3 years ago

0.3.13

3 years ago

0.3.12

4 years ago

0.3.11

4 years ago

0.3.10

4 years ago

0.3.9

4 years ago

0.3.8

4 years ago

0.3.7

5 years ago

0.3.6

6 years ago

0.3.5

6 years ago

0.3.4

6 years ago

0.3.3

6 years ago

0.3.2

6 years ago

0.3.1

6 years ago

0.3.0

6 years ago

0.2.16

7 years ago

0.2.15

7 years ago

0.2.14

7 years ago

0.2.13

7 years ago

0.2.12

7 years ago

0.2.11

7 years ago

0.2.10

7 years ago

0.2.9

7 years ago

0.2.8

7 years ago

0.2.7

7 years ago

0.2.6

7 years ago

0.2.5

7 years ago

0.2.4

7 years ago

0.2.3

7 years ago

0.2.2

7 years ago

0.2.1

7 years ago

0.2.0

7 years ago

0.1.16

7 years ago

0.1.15

7 years ago

0.1.14

7 years ago

0.1.13

7 years ago

0.1.12

7 years ago

0.1.11

7 years ago

0.1.10

7 years ago

0.1.9

7 years ago

0.1.8

7 years ago

0.1.7

7 years ago

0.1.5

7 years ago

0.1.4

7 years ago

0.1.3

8 years ago

0.1.2

8 years ago

0.1.1

8 years ago

0.1.0

8 years ago