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

jigsaw-restful

v0.2.4

Published

jigsaw-restful is a router component of jigsaw-rpc

Downloads

59

Readme

Jigsaw-Restful

jigsaw-restful is an API framework written in TypeScript, it can be used to design stable HTTP APIs through Jigsaws.

Install

in a npm project folder, run:

npm install jigsaw-restful --save

Easy-to-start Example

api.js

const { RPC } = require("jigsaw-rpc");
const Restful = require("jigsaw-restful");
const router = new Restful.Router();

router.get("/v1/test/res",{
    public:true,
    desc:"This API can get result",
    return:"string",
    vali:{
        str:"required"
    }
},async (ctx,next)=>{
    ctx.result.hello = ctx.data.str;
    await next();
});

const jg = RPC.GetJigsaw({name:"testjigsaw"});
jg.use(router.router());

then try this:

app.js

const { RPC } = require("jigsaw-rpc");

const invoker = RPC.GetJigsaw();

invoker.send("test:<get>/v1/test/res",{
    str:"world!"
}).then(console.log);

// will get a "hello":"world";

Advanced Example

endpoint.js

const { RPC } = require("jigsaw-rpc");
const adapter = new Restful.KoaAdapter(RPC.GetJigsaw());

const koa = new Koa();
koa.use(adapter.koa());
koa.listen(80);

then try access by HTTP client:

GET http://127.0.0.1/v1/test/res?path=testjigsaw
body: {str:"world!"}

You will get the HTTP Response:

{
	"error": false,
	"code": 0,
	"httpcode": 200,
	"message": "API invoked successfully",
	"detail": "",
	"type": "object",
	"data": {"hello":"world"}
}

if you don't follow the rules of validation like:

GET http://127.0.0.1/v1/test/res?path=testjigsaw
body: {}

will get a HTTP 400 Bad Request like this:

{
	"error": true,
	"code": 9005,
	"httpcode": 400,
	"message": "Your request format isn't correct.",
	"detail": {
		"str": [
			"The str field is required."
		]
	},
	"type": "object",
	"data": null
}

GET = POST

some HTTP-Client like axios don't support GET request with a body, so in Jigsaw-Restful design,

GET method is same as POST method to get a information of resource.

PUT method is used to create a resource ,

PATCH method is used to modify a resource ,

and DELETE method is used to delete a resource.

API Map

you can review all of apis you provide easily by:

jg.use(router.secretAPIMap());

the complete api.js is like:

const { RPC } = require("jigsaw-rpc");
const Restful = require("jigsaw-restful");
const router = new Restful.Router();

router.get("/v1/test/res",{
    public:true,
    desc:"This API can get result",
    return:"string",
    vali:{
        str:"required"
    }
},async (ctx,next)=>{
    ctx.result.hello = ctx.data.str;
    await next();
});

const jg = RPC.GetJigsaw({name:"testjigsaw"});
jg.use(router.secretAPIMap());
jg.use(router.router());

if you have already started the endpoint.js

you can access the API Map by HTTP Client (eg. browser).

GET http://127.0.0.1/apimap/?path=testjigsaw

and get this response:

{
	"error": true,
	"code": 9005,
	"httpcode": 400,
	"message": "Your request format isn't correct.",
	"detail": {
		"/v1/test/res": {
			"desc": {
				"get": "This API can get result",
				"post": "This API can get result"
			},
			"return": "string",
			"method": {
				"get": {
					"str": "required"
				},
				"post": {
					"str": "required"
				}
			}
		}
	},
	"type": "object",
	"data": null
}