Giter Club home page Giter Club logo

xhip's Introduction

Xhip

CircleCI npm version

Xhip is a modern, isomorphic, operation-based web API framework built top on express.

Concept

TL;DR: See slides.

Core concept is do everything with only one-single-endpoint. Benefit is doing same things with less requests. In xhip, all function will become into one-single-endpoint. They are callable from frontend and become single-endpoint API. Also, Xhip integrates heavily with WebSocket. All operation will be callable from both of normal POST request and WebSocket request.

And also, we heavily focus on integration with TypeScript.

You can see full React example in xhip-example.

Install

npm install xhip xhip-server xhip-client

Example

App(app.js):

import { op, load, Application } from "xhip"
const request = load("request")

export class App extends Application {
  @op showAppName() {
    return {
      appName: "xhip example"
    }
  }
  @op showAppVersion() {
    return {
      appVersion: 1
    }
  }
  @op getServerIP() {
    return new Promise((resolve, reject) =>
      request.get('https://api.ipify.org?format=json', (error, response, body) => {
        if (error) reject(error)
        resolve({ ip: JSON.parse(body).ip })
      })
    ).catch(err => {
      console.log(err)
    })
  }
  @op echo(say) {
    return { say }
  }
}
export const app = new App()

Server-side(server.js):

import { Server } from "xhip-server"
import { app } from "./app.js"

new xhip.Server(app, {
  cors: {
    origin: 'http://localhost:21000', // for CORS support
    credentials: true,
  }
}).app.listen(8080)

Then you can access to Xhip server via xhip-client.

Client-side(client.js):

import { Client } from "xhip-client"

const client = new xhip.Client("http://localhost:8080/", { ssl: false })
client.exec([
  app.showAppName(),
  app.showAppVersion(),
  app.echo("hi"),
  app.getServerIP()
]).then(res => {
  // res will be like this:
  // {
  //  appName: "xhip example",
  //  appVersion: 1,
  //  say: hi,
  //  ip: ***.***.***.***
  // }
})

How it works

When you see those codes you will notice there is wired load function. This is a function for isomorphic require. From server side, it will become normal require function, defined by commonjs. And from frontend, it will become useless proxy object. So that both can require same application code.

Also, there is a decorator object, named op. Decorator op automatically generate server/client compatible function. From server side, it will become endpoint definition. From client side, it will become to argument generator which will be posted into server.

Server Specification

Normal POST Connection

Xhip do everything in POST method.

You can inquiry this way:

{
  "__xhip": true,
  "operations": {
    "your_operation": argument
  }
}

then server must return this way:

{
  "your_operation": result  
}

Those argument and result can be any.

WebSocket

Most is same but no need to append "__xhip" mark.

Limitation

  • Any operation must returns JSON object which can be combined with other operations.
  • Not as usual importation, we have to use Xhip.load for support isomorphism inside app.

xhip's People

Contributors

minamorl avatar

Watchers

 avatar  avatar  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.