npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

will-api

v1.0.13

Published

Web API gateway adapter for moleculer

Downloads

8

Readme

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.