1.0.13 • Published 1 year ago

will-api v1.0.13

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

will-api

Web API gateway adapter for moleculer

Installation

npm install will-api

Configuration

This module require configuration (config) setting by config/default.json under project and will-sql, will-db, moleculer, moleculer-web

npm install config
npm install moleculer
npm install moleculer-web
npm install will-sql
npm install will-db

KnAPI

KnAPI handle for default setting web api gateway

Usage

import { ServiceBroker } from "moleculer";
import { APIError, JSONReply } from "will-api";
import KnAPI from "will-api";

const broker = new ServiceBroker({
    logLevel: "debug"
});

broker.createService({
    name: "test",
    actions: {
        hello() {
            return "Hello API Gateway!"
        },
        hi(context:any) {
            return "Hi, "+context.params.name;
        },
        err() {
            //this error on server but client result {}
            //throw new Error("My Exception");
            //this is no error but result {}
            //return new Error("My Exception");
            //this notify onError
            return Promise.reject("My Exception");
        },
        error() {
            return Promise.reject(new APIError("API Error",-20010));
        },
        rs() {
            return {rows: { affectedRows: 0 }, columns: null };
        },
        reply() {
            let ans = new JSONReply();
            ans.head.composeNoError();
            ans.head.modeling("api","test.reply");
            ans.body = { data: "API Gateway" };
            return ans;
        },
        plain(ctx: any) {
            //in order to ignore reponse out with default application/json
            //make meta.$responseType as text/plain or text/html
            ctx.meta.$responseType = "text/plain";
            return "Hello API";
        },
        html(ctx: any) {
            ctx.meta.$responseType = "text/html; charset=utf-8";
            return "<html><head><title>test</title></head><body>world</body></html>";
        },
        raw(ctx: any) {
            //this make response raw data for user defined
            //set meta.$responseRaw = true;
            ctx.meta.$responseRaw = true;
            return {rows: { affectedRows: 0 }, columns: null };
        }
    }
});

broker.createService({
    name: "api",
    mixins: [KnAPI],
});

broker.start()
.then(() => broker.call("test.hi",{name: "tester" }))
.then(res => console.log("response",res) )

API Request

API gateway can request by http method GET and POST with form submit or json data format

ex.
curl http://localhost:8080/api/test/hello
curl http://localhost:8080/api/test/hi?name=test

curl -X POST http://localhost:8080/api/test/hi -d name=testing
curl -X POST -H "Content-Type: application/json" http://localhost:8080/api/test/hi -d "{\"name\":\"testing\"}"

and support CORS
curl -X POST -H "Origin: https://example.com/" http://localhost:8080/api/test/hi?name=test

API Response

JSONReply response as result always compose in format with head and body

{
    "head": {
        //this is service name or alias name
        "model":"api", 
        //this is action name or method call
        "method":"test.hi",
        //this is error code default 0 with no error
        "errorcode":"0",
        //this is error flag (N = No error, Y = error)
        "errorflag":"N",
        //this is error message
        "errordesc":""
    },
    "body":{
        //this is body attributes depending on object values
        //if result from action call is plain text then it is in data attribute
        "data":"Hi, tester"
    }
}

Result from action call can be JSONReply object to response out directly.

1.0.9

1 year ago

1.0.8

1 year ago

1.0.11

1 year ago

1.0.10

1 year ago

1.0.13

1 year ago

1.0.12

1 year ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago