@gokiteam/dev-tools
v1.3.0
Published
Goki Team Dev Tools
Readme
Goki Dev Tools
Tools and common codes that we may use in our microservices are provided in this module.
Table of Contents
Installation
Using npm command:
npm i --save @gokiteam/dev-toolsUsage
Container
There's a Container entity that can be configured to create singleton instances of provided helpers. You can declare and instantiate it once and use that instance everywhere in your project. You can also import and instantiate one helper on demand if needed, but it's recommended to use Container entity to have a cleaner implementation and prevent redundant code.
Each config param is optional and can be defined if needed.
const container = new Container ({
logger: WinstonLogger,
config: {
redis: {/* config here */},
rollBar: {/* config here */},
errorLogger: {/* config here */},
lockProvider: {/* config here */},
endpoints: {/* config here */},
healthCheck: {/* config here */},
}
})Redis
This component uses ioredis and creates a simple interface for redis.
Endpoints
Endpoints is a component that represents how other services can communicate with this service. It includes all gRPC and REST APIs and also all messaging consumers.
Usage
- Registering an endpoint:
// batch
container.Endpoints.batchRegister([
{
group: 'group.nestedGroup',
name: 'endpointName',
schema: JoiToSwagger(JoiSchema),
isPublic: true,
responses: []
},
{
group: 'group.nestedGroup',
name: 'endpointName',
schema: JoiToSwagger(JoiSchema),
isPublic: true,
responses: []
}
])
// single
container.Endpoints.register({
group: 'group.nestedGroup',
name: 'endpointName',
schema: JoiToSwagger(JoiSchema),
isPublic: true,
responses: []
})- Getting the endpoints:
const endpoints = container.Endpoints.list()Error Logger
Logs the error appropriately and if RollBar is enabled, forwards the data to it too.
Usage
container.ErrorLogger.log(error, 'message', { /* any meta data */ })Health Check
This component is responsible for preparing the health status of the service. Don't forget to register new components to the Health Check component to be checked during calls.
Usage:
- Registering a new item:
container.HealthCheck.register({
group: 'group.nestedGroup',
name: 'TestComponent',
method: async () => {
// async method to prepare the item's health
return {}
}
})- Checking the status:
const status = await container.HealthCheck.check()Lock Provider
It creates an interface for locking and unlocking resources with RedLock.
Usage
const lock = await container.LockProvider.lock(`resource`)
// operations...
lock.unlock()Utils
There are some utils in this module that can be imported and used.
Joi To Swagger
Converts Joi Schema to swagger object.
Usage
const swaggerSchema = Utils.JoiToSwagger(joiSchema)Object Describer
This component creates a swagger schema from one or several connected models. This component can be used to describe the response of APIs.
Usage
const output = new Utils.ObjectDescriber({ schema: User, mask: 'id,firstName,lastName,email'})
.extend({ key: 'ads', objectModel: { schema: Ad, mask: 'id,category' }, array: true })
.extend({ key: 'likes', objectModel: { schema: Like, mask: 'id,link' } })
.join({ key: 'books', objectModel: { schema: Book, mask: 'id,title,author' } })
.format()The output will be something like this:
{
id: { type: 'string' },
firstName: { type: 'string' },
lastName: { type: 'string' },
email: { type: 'string' },
ads: {
type: 'array',
items: {
id: { type: 'string' },
category: { type: 'string' }
}
},
likes: {
id: { type: 'string' },
link: { type: 'string' }
},
books: {
id: { type: 'string' },
title: { type: 'string' },
author: { type: 'string' }
}
}If you set the second parameter of the constructor to { array: true }, the output will be in the following format:
{
type: 'array',
items: <ITEMS-SCHEMA>
}Promise Runner
This component receives several async functions and executes all of them and return the response.
Usage
const runner = new Utils.PromiseRunner()
runner.add(async () => {
const output1 = await task1()
// ...
})
runner.add(async () => {
const output2 = await task2()
// ...
})
await runner.run()Documentation [NEED TO CREATE and UPDATE]
Link external documents in this section
Support
Please create a task for support.
Contributing
Please contribute using Git Convention. Create a branch, add commits, and create a merge request.
