This is my first package. I'm currently using it for one of my other personal project and decided to make its own package, for others to use.
I'm a little nervous as I haven't really shared my worked publicly but I've also written about facing your fears before.
This package is meant to make route setup clean and configurable, while adding the ability to add middleware for not just all routes, but individual routes as well.
Setup is simple as just a few lines.
const {default: autoRoute, RouteRequestJsonSchemaValidator, RouteResponseJsonSchemaValidator} = require('express-auto-route');
//express server
const server = ...;
// see exmple below.
const routes = ...;
autoRoute({
server,
routes,
middlewares: [
RouteRequestJsonSchemaValidator,
RouteResponseJsonSchemaValidator,
// your custom middleware
],
});
Then just add your settings via a config file.
const routes = [
{
id: "resourceGet",
method: "GET",
modulePath: "/routes/resource/get.js",
path: "/resources",
responseJsonSchema: "/path/to/schemas/resources.schema.json",
},
{
id: "resourceGetUuid",
method: "GET",
modulePath: "/routes/resource/get.js",
path: "/resources/:uuid",
requestJsonSchema: {
query: "/path/to/schemas/getByUuid.schema.json"
},
responseJsonSchema: "/path/to/schemas/resource.schema.json"
}
]
Then just add our schemas, as you can see below that we have the full ability to combine schemas for powerful clean solutions.
/path/to/schemas/resources.schema.json
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "resources",
"type": "array",
"items": {
"$ref": "schema/resource.schema.json"
}
}
/path/to/schemas/resources.schema.json
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$ref": "#/definitions/resource",
"definitions": {
"resource": {
"title": "resource",
"type": "object",
"required": [
"name",
"path",
"description"
],
"additionalProperties": false,
"properties": {
"uuid": {
"$ref": "schema/uuid.schema.json"
},
"name": {
"type": "string",
"title": "Name",
"minLength": 2,
"maxLength": 60
},
"path": {
"type": "string",
"title": "Path",
"minLength": 1,
"pattern": "^\\/[\\w\\-.~%!$&'()*+,;=:@\\/]*$"
},
"description": {
"type": "string",
"title": "Description",
"minLength": 1,
"maxLength": 254
},
"createdAt": {
"type": "string",
"format": "date-time"
},
"updatedAt": {
"type": "string",
"format": "date-time"
}
}
}
}
}
/path/to/schemas/getByUuid.schema.json
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "getByUuid",
"type": "object",
"required": [
"uuid"
],
"properties": {
"uuid": {
"$ref": "schema/uuid.schema.json"
}
}
}
/path/to/schemas/uuid.schema.json
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "UUID",
"$ref": "#/definitions/uuid",
"definitions": {
"uuid": {
"type": "string",
"pattern": "^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$"
}
}
}
The schema's paths are pulled via the config, via the middleware. So you can use the schemas or if you rather, drop the middleware and no need for schemas.
As I mentioned above, you have the option for individual route middleware as well!
const routes = [
{
id: "resourceGet",
method: "GET",
modulePath: "/routes/resource/get.js",
path: "/resources",
middleware: "/path/to/your/customMiddleware.js",
}
]
Hope you enjoy it! Please let me know your thoughts!
Top comments (0)