GithubHelp home page GithubHelp logo

ctsstc / rapid.js Goto Github PK

View Code? Open in Web Editor NEW

This project forked from rapidjs/rapid.js

0.0 2.0 0.0 3.83 MB

An ORM-like Interface and a Router For Your API Requests

Home Page: https://rapidjs.io

JavaScript 100.00%

rapid.js's Introduction

npm npm npm

An ORM-like Interface and a Router For Your API Requests
Create simple, reusable, and cleaner wrappers, define custom routes, and more for your API requests.

Read the official docs at https://rapidjs.io.

Installation

Pick your poison:

yarn add rapid.js
npm i -S rapid.js
npm install --save rapid.js

Overview

Define Simple Models

const post = new Rapid({ modelName: 'Post' });

post.find(1).then((response) => {
    // GET => /api/post/1
});

post.collection.findBy('category', 'featured').then((response) => {
    // GET => /api/posts/category/featured
});

post.withParams({ limit: 20, order: 'desc' }).all().then((response) => {
    // GET => /api/posts?limit=20&order=desc
});

post.update(25, { title: 'Rapid JS Is Awesome!' })
    // POST => /api/posts/25/update

post.destroy(9)
    // POST => /api/posts/9/destroy

post.restore(9)
    // POST => /api/posts/9/restore    

Read more about Rapid Basics.

Easily Customize Your API Requests

const post = new Rapid({
    modelName: 'Post',
    suffixes: {
        destroy: '',
        update: 'save'
    },
    methods: {
        destroy: 'delete'
    },
    trailingSlash: true
 });

post.update(25, { title: 'Rapid JS Is Awesome!' })
    // POST => /api/posts/25/save/

post.destroy(9)
    // DELETE => /api/posts/9/

Read more about Customizing Your Requests.

Create Reusable Base Models

class Base extends Rapid {
    boot () {
        this.baseURL = 'https://myapp.com/api';
        this.config.globalParameters = { key: 'MY_API_KEY' }
    }
}

const photo = new Base({ modelName: 'Photo' });
const gallery = new Base({ modelName: 'Gallery' });
const tag = new Base({ modelName: 'Tag' });

photo.find(1)
    // GET => https://myapp.com/api/photo/1?key=MY_API_KEY

tag.collection.findBy('color', 'red')
    // GET => https://myapp.com/api/tags/color/red?key=MY_API_KEY

gallery.id(23).get('tags', 'nature')
    // GET => https://myapp.com/api/gallery/23/tag/nature?key=MY_API_KEY

Read more about Base Models.

Write API Wrappers For Your Endpoints

class GalleryWrapper extends Rapid {
    boot () {
        this.baseURL = 'https://myapp.com/gallery/api';
        this.modelName = 'Gallery';
    }

    tagSearch (query) {
        return this.url('tagsearch').withParam('query', query);
    }

    json () {
        return this.url('json');
    }
}

const gallery = new GalleryWrapper({
    globalParameters: { key: 'MY_API_KEY' }
});

gallery.tagSearch('nature').json().get().then(...);
    // GET https://myapp.com/gallery/api/tagsearch/json?query=nature&key=MY_API_KEY
    // GET https://myapp.com/gallery/api/tagsearch/json?query=nature&key=MY_API_KEY

Read more about Making a Wrapper.

Define Custom Routes

const customRoutes = [
    {
        name: 'web_get_user_preferences',
        type: 'get',
        url: '/user/preferences',
    },

    {
        name: 'web_save_user_preferences',
        type: 'post',
        url: '/user/{id}/save/preferences'
    }
];

const router = new Rapid({ customRoutes, baseURL: '/api' });

router.route('web_get_user_preferences').then((response) => {}); 
// GET => /api/user/preferences

router.route('web_save_user_preferences', { id: 12 }, /* { request data } */).then((response) => {}); 
// POST => /api/user/12/save/preferences

Using Your Own HTTP Service

import http from 'some-http-service';

const customRoutes = [
    {
        name: 'web_login',
        url: '/login'
    },

    {
        name: 'api_save_user_preferences',,
        url: '/user/{id}/save/preferences'
    }
];

const rapid = new Rapid({ customRoutes, baseURL: '' });

rapid.generate('web_login')
// returns '/login'

// use your own service
http.post(rapid.generate('api_save_user_preferences', { id: 1 }), { data }).then()...

Read more about Custom Routes.

Read the official docs at https://rapidjs.io.

rapid.js's People

Contributors

drewjbartlett avatar mgred avatar ricklancee avatar brainmaestro avatar raybenefield avatar tbroadley avatar

Watchers

Cody Swartz avatar James Cloos avatar

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.