0.10.2 • Published 10 months ago

restutils-host v0.10.2

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
10 months ago

RESTUtils Logo

RESTUtils Host (restutils-host)

Instantly convert any JavaScript library into a web-based API!

Purpose

Async? Sync? Await? Callbacks? Nonsense!

Quickly convert any basic JavaScript library into a web-based API by issuing one command. You don't even need to install it! This lil' uitlity will pull down and pull in your project, stick a web server in front of it, and setup routes for every property or function within it.

Console Output

Simplified Samples

Disclaimer

This initial version has never seen the light of day. It's been written by one guy. And, is still being tested, expanded, developed, etc., etc., etc. So, try it out and beat the snot out of it before you use it anywhere. If you run into any quesitons, send an email or open an issue on GitHub

Coding Requirement (Singular)

Yeah, yeah, yeah. So, what's the catch? Right? Well, there is one small rule:

Rule #1
Each method in your class must receive one single property and return an object, string, or other primative. Each function in your library will be wrapped with a POST operation where the entire request.body object will be sent inside. All properties in your library become GET operations.

Rule #2
There isn't a second rule... just that one above.

Simplified Data Handling

Remember Rule #1 from above? How the body of each request is passed in to your functions as a single parameter? Well, this includes querystring parameters. All data received by your function is combined into this single object parameter for simplicity. This means your calling application can send in both a JSON object as well as querstring parameters and you don't have to parse them separately.

Combined Parameters

Wildcard Routes

Maybe you don't want to worry about the incoming path structure at all? That's fine. By using a [wildcard].js file and handler function, any route you invent after the fact becomes a valid. Essentially, this means /myroute and /my/really/long/route/name will be dumped into the same function if you so desire.

Wildcard Routes

Remember Rule #1 from above?

Installation

It's best to run from npx but it can be installed globally...

npm i -g restutils-host

Usage

Launching can be as simple as ...

npx restutils-host --name "my-utility"

... or, if your project is in a Git repo, just point it there ...

npx restutils-host --repo "https://github.com/codergod/my-utility.git"

You can even use the worlds simplest JavaScript file...

npx restutils-host --path "~/my-utility.js"

Or, if you're using it in the scripts section of your package.json file you could always use the ${PWD} to pass in the current path of your library...

{
  "name"       : "data-api-lib",
  "version"    : "0.0.0",
  "description": "My Data API Library",
  "scripts"    : {
    "debug": "npx -node-options=--inspect restutils-host@latest --path ${PWD}/index.js",
    "dev"  : "npx restutils-host@latest --path ${PWD}/index.js"
  }
}

And, maybe even kick it up a notch by tossing Nodemon into the mix...

{
  "name"       : "data-api-lib",
  "version"    : "0.0.0",
  "description": "My Data API Library",
  "scripts"    : {
    "debug": "npx -node-options=--inspect restutils-host@latest --path ${PWD}/index.js",
    "dev"  : "npx restutils-host@latest --path ${PWD}/index.js",
    "watch"       : "nodemon --exec \"npm run dev\"",
    "watch:debug" : "nodemon --exec \"npm run debug\""
  }
}

Parameters

NameDescriptionTypeDefault
namePackage name in npmjs.orgstring
pathFull path to local packagestring (path)
envFull path to environment filestring (path)
repoURL to hosted Git repostring
depthMaximum levels to recursenumbernull
baseBase path of listenerstring
publishRoute to publish definition.string.restutils
capsAllow all-caps for names.booleantrue
corsAllowed CORS originstring
portPort for incoming requestsnumber3000
installInstall dependencies if neededbooleantrue
jwt-secretValidate JWT if setstring
jwt-cookieJWT cookie namestringtoken
jwt-headerJWT header namestringauthorization
jwt-paramJWT query param namestringjwt
jwt-expiredExpired JWT allowedbooleanfalse
jwt-invalidInvalid JWT allowedbooleanfalse

Debugging

Nothing changes while debugging your libraries. Passing the --node-options switch to npx causes the Node Debugger to listen as it normally would:

Debug Command

So, that command above would become...

npx --node-options=--inspect restutils-host --path "~/my-utility.js"

Then just tell your IDE to attach to the process. In VSCode this is done by creating a debug configuration:

Debug Command

Production Recommendations

Disable Console Output

Route information is output to the console in all non-production environments. The goal is to allow developers the ability to see incoming payloads without having to attach the debugger. This could be a security concern in some environments. For example, within AWS the Cloud Watch service logs console information to permanent storage. Should your APIs receive sensitive data you would not want this to be logged. To disable the output in non-production enviornments, simply ensure the NODE_ENV enviornment variable is set to something beginning with the prod prefix (ie, prod, production, prod-o-rama, etc.).

Known Limitations

Error: 'git clone' failed with status 128

When using the --repo option, and pulling directly from a hosted Git repository, the underlying git clone process may randomly fail with status 128. Online docs have not been helpful (yet). More than likely there is some async process burried in there that should be allowed to complete before the process returns. Repeating the same command a few seconds later works successfully.

Demo

RESTUtils Host Demo

Contact

Please feel free to contact me directly with any questions, comments, or enhancement requests:

Fred Lackey
fred.lackey@gmail.com
http://fredlackey.com

0.10.1

10 months ago

0.10.2

10 months ago

0.9.4

1 year ago

0.9.3

1 year ago

0.9.6

1 year ago

0.9.5

1 year ago

0.9.2

1 year ago

0.9.1

1 year ago

0.8.2

1 year ago

0.7.5

1 year ago

0.7.4

2 years ago

0.7.3

2 years ago

0.7.2

2 years ago

0.7.1

2 years ago

0.7.0

2 years ago

0.6.1

2 years ago

0.6.0

2 years ago

0.5.0

2 years ago

0.4.3

2 years ago

0.4.2

2 years ago

0.4.1

2 years ago

0.4.0

2 years ago

0.3.1

2 years ago

0.3.0

2 years ago

0.2.0

2 years ago

0.1.0

2 years ago

0.0.7

2 years ago

0.0.6

2 years ago

0.0.5

2 years ago

0.0.4

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago

0.0.0

2 years ago