@hero-js/core
TypeScript icon, indicating that this package has built-in type declarations

0.2.2 • Public • Published

@hero-js/core

@hero-js/core is a versatile library that provides tools for managing generic routes and handling HTTP requests in JavaScript and TypeScript applications. It is designed to simplify the process of creating and managing routes and request handling within your application.

Please note: @hero-js/core is a generic layer and should be used with specific web frameworks such as express or fastify to create web applications. You need to build an adapter for the framework you want to use, like express, fastify. Or you can use an already built adapter like @hero-js/express-adapter, @hero-js/fastify-adapter. The core library focuses on defining and managing routes, middleware, and request handling in a generic way.

Features

  • Define and manage generic routes for various HTTP methods (GET, POST, PUT, PATCH, DELETE).
  • Add middleware to routes for handling requests.
  • Create and configure routes with ease.

Installation

You can install @hero-js/core via npm or yarn:

npm install @hero-js/core

or

yarn add @hero-js/core

Usage

Basic Usage

Here's a simple example of how to use @hero-js/core with a specific framework (e.g., Express):

import { ExpressAdapter, Express } from '@hero-js/express-adapter';
import { Router } from '@hero-js/core';

const adapter = new ExpressAdapter();

// Create an instance of the router
const router = Router.create<Express>();

// Define routes and middleware
router.use((req, res, next) => {
  // Middleware logic
  next();
});

router.get('/', ({ response }) => {
  response.send('Health Ok!');
})

router.batchMiddleware(['LoggerMiddleware'])([
    router.get('/api', 'ArticleController.one'),
    // and other route

  ...router.batchMiddleware([
    'AuthMiddleware'
  ])([
    router.post('/api', 'ControllerName.create').middleware(['SerializerMiddleware']).register;
    // and other route
  ])
]);

// Use the ExpressAdapter to integrate with Express
const app = adapter.adapt(router);

// Start listening for requests
app.listen(3000, () => {
  console.log('Server is running on port 3000');
});

For more detailed usage examples and configurations, please refer to the documentation or examples in the repository.

Documentation

For detailed documentation, API references, and usage examples, please visit the official documentation.

Contributing

Contributions are welcome! If you'd like to contribute to this project, please follow the guidelines in CONTRIBUTING.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Changelog

For a history of changes to this module, see the CHANGELOG file.

Acknowledgments

  • Any acknowledgments or credits you want to give.

Package Sidebar

Install

npm i @hero-js/core

Weekly Downloads

1

Version

0.2.2

License

MIT

Unpacked Size

30.5 kB

Total Files

19

Last publish

Collaborators

  • sana_yasfp