marvelous

0.30.6 • Public • Published

marvelous npm Build Status

the marvelous framework - microservices for node.js

microservices

Sample Application

Task Service

This RPC service reads/writes tasks for a to-do list:

Tip: this service is accessible via http://localhost:4000

class TaskService extends Service {
  calls = {
    createTask: CreateTaskCall,
    listTasks: ListTasksCall
  };
}
 
class CreateTaskCall extends ServiceCall {
  handler: ICreateTaskHandler = async request => {
    const task = {
      ...request,
      id: nextTaskId
    };
    tasks.push(task);
    nextTaskId++;
    return {
      id: task.id
    };
  };
}
 
class ListTasksCall extends ServiceCall {
  handler: IListTasksHandler = async () => {
    return {
      tasks: tasks.map((task: IListTasksResponseTask) => ({
        id: task.id,
        text: task.text
      }))
    };
  };
}
 
const taskService = new TaskService({
  url: 'http://localhost:4000'
});
taskService.start();

Public Gateway

This REST gateway exposes the Task Service functionality:

Tip: this service is accessible via http://localhost:3000

class PublicGateway extends Gateway {
  routes = [TasksRoute];
}
 
class TasksRoute extends GatewayRoute {
  uri = '/tasks';
  methods = {
    get: GetTasksMethod,
    post: PostTasksMethod
  };
}
 
class GetTasksMethod extends GatewayMethod {
  handler: any = async () => {
    const result = await taskServiceClient.listTasks();
    return {
      status: 200,
      body: result.tasks
    };
  };
}
 
class PostTasksMethod extends GatewayMethod {
  handler: any = async (req: Request) => {
    const result = await taskServiceClient.createTask({
      text: req.body.text
    });
    return {
      status: 201,
      body: {
        id: result.id
      }
    };
  };
}
 
const publicGateway = new PublicGateway({
  url: 'http://localhost:3000'
});
publicGateway.start();

API Reference

Gateway

Properties

Name Example
environment string
the gateway environment
"production"
enableLogging string
enables gateway logging
true
knownErrors GatewayError[]
an array of gateway errors
[ AuthServiceError ]
onLoad Function
fires when gateway is loaded
onStart Function
fires when gateway is started
onStop Function
fires when gateway is stopped
routes GatewayRoute[]
an array of gateway routes
[ UsersRoute ]
url string
the gateway url
"http://localhost:3000"

GatewayError

Properties

Name Example
code number
the error status code
401
name string
the error name
"AuthGatewayError"
message Function
the error message
"You must be logged in."

GatewayMethod

Properties

Name Example
handler Function
the gateway method handler
schema object
the gateway method schema

GatewayRoute

Properties

Name Example
uri string
the gateway route uri
"/users"
delete GatewayMethod
DELETE method
DeleteUserMethod
get GatewayMethod
GET method
GetUsersMethod
options GatewayMethod
OPTIONS method
OptionsUsersMethod
patch GatewayMethod
PATCH method
PatchUserMethod
post GatewayMethod
POST method
PostUsersMethod
put GatewayMethod
PUT method
PutUserMethod

Service

Properties

Name Example
calls object
the service calls
{ createUser: CreateUserCall }
environment string
the service environment
"production"
enableLogging string
enables service logging
true
onLoad Function
fires when service is loaded
onStart Function
fires when service is started
onStop Function
fires when service is stopped
url string
the service url
"http://localhost:3000"

ServiceCall

Properties

Name Example
handler Function
the service call handler
schema object
the service call schema

ServiceError

Properties

Name Example
name string
the error name
"AuthGatewayError"
message Function
the error message
"You must be logged in."

Readme

Keywords

none

Package Sidebar

Install

npm i marvelous

Weekly Downloads

188

Version

0.30.6

License

none

Unpacked Size

149 kB

Total Files

96

Last publish

Collaborators

  • ctate
  • zappjs