Security News
RubyGems.org Adds New Maintainer Role
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
@poppinss/manager
Advanced tools
Implementation of the Manager pattern used by AdonisJS
Manager pattern is a way to ease the construction of objects of similar nature. To understand it better, we will follow an imaginary example through out this document.
Let's imagine we are creating a mailing library and it supports multiple drivers like: SMTP, Mailgun, PostMark and so on. Also, we want the users of our library to use each driver for multiple times using different configuration. For example:
Using the Mailgun driver with different accounts. Maybe one account for sending promotional emails and another account for sending transactional emails.
The simplest way to expose the drivers, is to export them directly and let the consumer construct instances of them. For example:
import { Mailgun } from 'my-mailer-library'
const promotional = new Mailgun(configForPromtional)
const transactional = new Mailgun(configForTransactional)
promotional.send()
transactional.send()
The above approach works perfect, but has few drawbacks
promotional
and transactional
in this case.What we really need is a Manager to manage and construct these objects in the most ergonomic way.
First step is to move the mappings to a configuration file. The config mimics the same behavior we were trying to achieve earlier (in the Basic example), but defines it declaratively this time.
const mailersConfig = {
default: 'transactional',
list: {
transactional: {
driver: 'mailgun'
},
promotional: {
driver: 'mailgun'
},
}
}
import { Manager } from '@poppinss/manager'
class MailManager implements Manager {
protected singleton = true
constructor (private config) {
super({})
}
}
The Base Manager class will do all the heavy lifting for you. However, you will have to define certain methods to resolve the values from the config file.
import { Manager } from '@poppinss/manager'
class MailManager implements Manager {
protected singleton = true
protected getDefaultMappingName() {
return this.config.default
}
protected getMappingConfig(mappingName: string) {
return this.config.list[mappingName]
}
protected getMappingDriver(mappingName: string) {
return this.config.list[mappingName].driver
}
constructor (private config) {
super({})
}
}
The final step is to write the code for constructing drivers. The Base Manager uses a convention for this. Anytime the consumer will ask for the mailgun
driver, it will invoke createMailgun
method. So the convention here is to prefix create
followed by the camelCase driver name.
import { Manager } from '@poppinss/manager'
class MailManager implements Manager {
// ... The existing code
public createMailgun (mappingName, config) {
return new Mailgun(config)
}
public createSmtp (mappingName, config) {
return new Smtp(config)
}
}
Once done, the consumer of the Mailer class just needs to define the mappings config and they are good to go.
const mailersConfig = {
default: 'transactional',
list: {
transactional: {
driver: 'mailgun'
},
promotional: {
driver: 'mailgun'
},
}
}
const mailer = new MailManager(mailersConfig)
mailer.use('transactional').send()
mailer.use('promotional').send()
mailer.use()
as many times as they want, without worrying about creating too many un-used objects.The Base Manager class comes with first class support for adding custom drivers from outside-in using the extend
method.
const mailer = new MailManager(mailersConfig)
mailer.extend('postmark', (manager, mappingName, config) => {
return new PostMark(config)
})
The extend
method receives a total of three arguments:
manager
object is the reference to the mailer
object.callback
should return an instance of the Driver.The Manager class can also leverage static Typescript types to have better intellisense support and also ensure that the drivers added using the extend
method adhere to a given interface.
Following is a dummy interface, we expect all drivers to adhere too
interface DriverContract {
send (): Promise<void>
}
import { Manager } from '@poppinss/manager'
import { DriverContract } from './Contracts'
class MailManager implements Manager<
DriverContract
> {
}
The mappings config currently has any
type and hence, the mailer.use
method cannot infer correct return types.
In order to improve intellisense for the use
method. You will have to define a type for the mappings too.
type MailerMappings = {
transactional: Mailgun,
promotional: Mailgun
}
type MailerConfig = {
default: keyof MailerMappings,
list: {
[K in keyof MailerMappings]: any
}
}
const mailerConfig: MailerConfig = {
default: 'transactional',
list: {
transactional: {
driver: 'mailgun',
// ...
},
promotional: {
driver: 'mailgun',
// ...
},
}
}
Finally, pass the MailerMappings
to the Base Manager class
import { DriverContract, MailerMappings } from './Contracts'
class MailManager implements Manager<
DriverContract,
DriverContract,
MailerMappings
> {
}
Once mailer mappings have been defined, the use
method will have proper return types.
FAQs
The builder (Manager) pattern implementation
The npm package @poppinss/manager receives a total of 3,199 weekly downloads. As such, @poppinss/manager popularity was classified as popular.
We found that @poppinss/manager demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.
Security News
Research
Socket's threat research team has detected five malicious npm packages targeting Roblox developers, deploying malware to steal credentials and personal data.