Package to create easy node.js services
Run the following command:
// npm
npm i @splashprotocol/backend
// yarn
yarn add @splashprotocol/backend
Each route has 2 section: info
and handler
.
info includes
This refers to metadata or configuration associated with a specific route in your application.
It includes details such as:
The HTTP method (e.g., GET, POST).
The path (e.g., /users/{id}).
Parameters (query, path, headers, body).
Expected response type.
The route info is used to generate OpenAPI/Swagger documentation.
This documentation describes the API's structure, including:
Endpoints.
Input parameters (query, path, headers, body).
Expected responses.
Zod is a TypeScript library for schema validation.
The route info includes Zod schemas to validate:
- Query parameters.
- Path parameters.
- Headers.
- Request body.
- Return type
This ensures that incoming data matches the expected format.
function that receives checked parameters and returns result
Example:
export const launchRoute = Route.new({
info: {
route: '/launch',
result: {
status: 200,
content: z.object({
hello: z.string()
})
},
body: {
contentType: 'multipart/form-data',
fileSizeLimit: 1024 * 1024,
payload: z.object({
image: zodFile( ['image/png', 'image/jpg', 'image/jpeg', 'image/gif']),
info: z.string()
})
},
tags: ['Launch'],
summary: 'test',
method: 'post'
},
handler: async ({ body, requestWorker }) => {
return await requestWorker<LaunchParams, LaunchResult>({
type: 'launch',
hello: '123'
});
}
})
example:
const backend = Backend.new({
routes: [
launchRoute
],
settings: {
maxBodyLength: 50 * 1024 * 1024,
swaggerPath: __dirname,
},
info: {
title: 'test',
description: 'tes',
version: '1.0.0',
},
environment: config
})
backend.run(3000);