0.0.2 • Published 1 year ago

tinie v0.0.2

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

Tinie 🪄

Tinie is tiny framework that simply converts JavaScript functions into Restful APIs inspired by Tanmoy741127/lumi

Feature

  • Create Restful API route from javascript functions
  • Validate parameter with type or types
  • Support depth(/) and dash(-) route from function's name
  • Tinie server wrtten with Fastify

How to start 🚀

  1. Install tinie from npm
 $ npm install tinie
  1. Write functions and join function into Tinie
import Tinie from "./tinie";

// uppercase replace with dash(-), ex) /helloWorld -> /hello-world
// underscore(_) replace with slash(/), ex) /hello_world -> /hello/world
// So, CalculatorExample_test -> /calculator-example/test
const CalculatorExample_test = (
  operation: "add" | "subtract" | "multiply" | "divide" = "divide", // if you set default value on parameter, it's optional
  a: number,
  b: number
) => {
  switch (operation) {
    case "add":
      return a + b;
    case "subtract":
      return a - b;
    case "multiply":
      return a * b;
    case "divide":
      return a / b;
  }
}

const calculatorParamTypes = {
  operation: ["string"], // if you set array on value, it's meant "or"
  a: "number",
  b: "number"
}

const tinie = new Tinie()

tinie.register(
  CalculatorExample_test,
  {
    types: calculatorParamTypes
  }
)

tinie.listen()
  1. Done, Let's check!
 $ curl -X POST -H "Content-Type: application/json" -d '{"operation": "add", "a": 4, "b": 2}' http://127.0.0.1:3000/calculator

TODO

  • 1-Dimensional parameter parsing
  • Multi-Dimensional parameter parsing
  • Advanced type checker