apigen
Auto generate api routes for development and production. Use mocks to build api routes for local envs. Works perfect for next.js and deno. The fastest way to build your API and test with confidence.
Getting Started
Make sure to create a mocks and a schemas directory. Check out the schema section to learn how to build your api. The idea is that the api routes are always checked in .gitignore
and set at the build level for systems. Its better to setup the module to be used via script to run with pre-install scripts or pre-build scripts, checkout build.sh for an example. Make sure to add a environmental(.env) variable named API_HOST
with the url of your api endpoint. If you want to build a mock api set API_HOST=localhost
to make all endpoints return mock data. If you need to change the route generation paths use the API_BUILD_PATH
env variable targeting your location.
import { codegen } from "https://deno.land/x/apigen/mod.ts";
// optional param
await codegen({
apiBuildPath: "./src/pages/api",
schemasPath: "./schemas",
mocksPath: "./mocks/",
});
Configuration
Json files named of the lambda functions that you are going to create in the schemasPath
location.
Schema
{
"endpoints": [
{
"path": "/login",
"url": "auth/api/token",
"method": "POST",
"mock": {
"data": {
"access_token": "Bearer @Something",
"refresh_token": ""
},
"data:error": {
"message": "Authentication error, please check your email or password and try again."
},
"validator": {
"fields": [
{ "name": "password", "message": "Password is required." },
{ "name": "username", "message": "User name is required." }
]
}
}
}
]
}
Mocks
In order to import a mock add @import filename
under the mock.data
key and replace filename with the mock path in the mocks folder. The mock has to be in the form of json to work.
[
{
"id": "0",
"name": "John",
"email": "something@email.com"
}
]
CLI
command line args
--apiBuildPath
path to create lambda routes
--apiSchemasPath
path of schemas to read
--apiMocksPath
path of mocks to read
--formula
:options (init)
Env Variables
- API_HOST
- API_BUILD_PATH
- API_SCHEMAS_PATH
- API_MOCKS_PATH
- DENO_RUNTIME
Languages/Support
- Deno
- Node
- Vercel/Next.js
- Rust - Todo