s-express
Features
- Generates OpenApi documentation at runtime in .yaml, .json and .md
- Handles async errors globally out of the box
- Dead simple API and almost no config required
- Provides a simple wrapper around Error
- Fully backward compatible with exppres.js
- Enforce json response aka contentType: application/json
It's basically express.js with a bit more sugar and opinionated stuff. I just wanted
something that would automatically generate openApi documentation based on my app
endpoints. I know there's a lot of solutions out there for that,
but I wanted something with almost no config and a super simple API not far from express. Hence why I made this little package.
Installation
npm i @appandflow/s-express
API
Creating a server (minimal example)
You might wanna do this in your index.ts
index.ts
import { createServer } from '@appandflow/s-express'
const IamAnExpressAppBasically = createServer()
require('./controller');
The returned value is basically an Express app.
You can of course pass some configs to createServer(config)
.
interface Config {
port?: string | number;
useCors?: boolean;
readyMessage?: string;
dotenvConfig?: dotenv.DotenvConfigOptions;
auth?: {
authMiddleware: RequestHandler;
secureAllRoutes?: boolean;
};
doc?: {
version?: string;
title?: string;
description?: string;
servers?: DocServer[];
headers?: any[];
};
morgan?: {
format: string;
options?: morgan.Options<any, any>;
};
uses?: any[];
controllersPath?: string;
}
interface DocServer {
url: string;
description: string;
}
Adding a route to our server (minimal example)
You can do this everywhere and the way you wish
but I'll suggest using a classic controller pattern.
controller.ts
import { addRoute } from '@appandflow/s-express'
addRoute(({ data, req, res, params }) => {
return {any: object, you: wish, to: 'return to you client'}
}, config)
const config: AddRouteConfig = {...}
export interface AddRouteConfig {
method?: HttpMethod;
path?: string;
middlewares?: RequestHandler[];
secure?: boolean;
fields?: Field[];
summary?: string;
description?: string;
}
SexpressError a basic Error wrapper.
todo.
import { SexpressError } from '@appandflow/s-express'
throw new SexpressError({
message: "Dev error msg, this will be remove when env === production if you specify a prodMessage,",
restCode: 422,
prodMessage: "If you wish to have another error for once env === production.",
data: {msg: "any data you wish to add, this will be remove when env === production."}
})
Generating OpenApi Doc
You have to set the env variable DOC_MODE
to true
.
The app will then start in doc mode and every declared endpoint
throughout the app will be hit by a request and capturing it returned value(s)
Documentation will be generated under /docs
.
You can see an example Here
I suggest adding this your scripts in your package.json
i.e: openApiDoc: "rm -rf docs && tsc && DOC_MODE=true node dist/index.js"
Todo(s)
- Write better documentation
- Add support for authenticated api calls when generating OpenApi Doc
This is an experimental package. Use at your own risk.
Thanks