Skip to content
This repository has been archived by the owner on Nov 18, 2023. It is now read-only.

Commit

Permalink
refactor: drop $ prefix from cacheMappings property
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Jul 3, 2020
1 parent aada504 commit fd4cc96
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 118 deletions.
104 changes: 13 additions & 91 deletions example/index.ts
Original file line number Diff line number Diff line change
@@ -1,103 +1,25 @@
import { ExtractImplementations, ExtractConfig } from '../src/Contracts'
import { ManagerContract } from '../src/Contracts'

import { Manager } from '../src/Manager'

interface MailDriverContract {
send(): void
}

type SmtpConfig = {
driver: 'smtp'
host: string
user: string
password: string
port?: number
}

type MailchimpConfig = {
driver: 'mailchimp'
apiKey: string
interface DriverContract {
run(): string
}

type MappingsList = {
transactional: {
config: SmtpConfig
implementation: SmtpDriver
}
promotional: {
config: MailchimpConfig
implementation: MailchimpDriver
}
}

class SmtpDriver {
constructor(public config: any) {}
public send() {}
}

class MailchimpDriver {
constructor(public config: any) {}
public send() {}
}

class Mailer extends Manager<MailDriverContract, ExtractImplementations<MappingsList>> {
constructor(container: any, private _config: any) {
super(container)
}

protected $cacheMappings = true

protected getDefaultMappingName(): string {
return this._config.mailer
}

protected getMappingConfig(name: string): any {
return this._config.mailers[name]
}

protected getMappingDriver(name: string): any {
return this._config.mailers[name].driver
}

protected createSmtp(_name: string, config: any) {
return new SmtpDriver(config)
}

protected createMailchimp(_name: string, config: any) {
return new MailchimpDriver(config)
}
interface Smtp extends DriverContract {
send(): void
}

type Config<Mailer extends keyof MappingsList> = {
mailer: Mailer
mailers: ExtractConfig<MappingsList>
interface Mailgun extends DriverContract {
verifyAndSend(): void
}

const config: Config<'transactional'> = {
mailer: 'transactional',
mailers: {
transactional: {
driver: 'smtp',
host: '',
user: '',
password: '',
},

promotional: {
driver: 'mailchimp',
apiKey: '',
},
},
type Mappings = {
smtp: Smtp
mailgun: Mailgun
}

const mailer = new Mailer({}, config)
class Postmark {
constructor(public _config: any) {}
public send() {}
}
type Mailer = ManagerContract<DriverContract, DriverContract, Mappings>

mailer.extend('postmark', (_container, _name, driverConfig) => {
return new Postmark(driverConfig)
})
const a = {} as Mailer
a.use()

// mailer.use('')
28 changes: 23 additions & 5 deletions src/Contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,33 @@ export type ExtendCallback<Driver extends any> = (container: any, mappingName: s
*/
export interface ManagerContract<
DriverContract extends any,
ReturnValueContract extends any = DriverContract,
MappingsList extends { [key: string]: ReturnValueContract } = { [key: string]: ReturnValueContract },
DefaultItem extends ReturnValueContract = ReturnValueContract
MappingValue extends any = DriverContract,
MappingsList extends { [key: string]: MappingValue } = { [key: string]: MappingValue }
> {
/**
* Returns concrete type when binding name is from the mappings lsit
*/
use<K extends keyof MappingsList>(name: K): MappingsList[K]
use(name: string): DefaultItem
use(): DefaultItem

/**
* Returns mapping value when an unknown key is defined. This will be done, when someone
* is trying to bypass static analysis
*/
use(name: string): MappingValue

/**
* Return a overload of mapping when no key is defined
*/
use(): { [K in keyof MappingsList]: MappingsList[K] }[keyof MappingsList]

/**
* Extend by adding a new custom driver
*/
extend(name: string, callback: ExtendCallback<DriverContract>): void

/**
* Release bindings from cache
*/
release<K extends keyof MappingsList>(name: K): void
release(name: string): void
}
Expand Down
19 changes: 8 additions & 11 deletions src/Manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ import { ManagerContract, ExtendCallback } from './Contracts'
export abstract class Manager<
DriverContract extends any,
ReturnValueContract extends any = DriverContract,
MappingsList extends { [key: string]: ReturnValueContract } = { [key: string]: ReturnValueContract },
DefaultItem extends ReturnValueContract = ReturnValueContract
> implements ManagerContract<DriverContract, ReturnValueContract, MappingsList, DefaultItem> {
MappingsList extends { [key: string]: ReturnValueContract } = { [key: string]: ReturnValueContract }
> implements ManagerContract<DriverContract, ReturnValueContract, MappingsList> {
/**
* Mappings cache (if caching is enabled)
*/
Expand All @@ -30,14 +29,12 @@ export abstract class Manager<
/**
* List of drivers added at runtime
*/
private extendedDrivers: {
[key: string]: ExtendCallback<DriverContract>
} = {}
private extendedDrivers: { [key: string]: ExtendCallback<DriverContract> } = {}

/**
* Whether or not to cache mappings
*/
protected abstract $cacheMappings: boolean
protected abstract cacheMappings: boolean

/**
* Getting the default mapping name, incase a mapping
Expand Down Expand Up @@ -71,7 +68,7 @@ export abstract class Manager<
* `cacheDrivers` attribute before entertaining the cache.
*/
private saveToCache(name: string, value: ReturnValueContract): void {
if (this.$cacheMappings) {
if (this.cacheMappings) {
this.mappingsCache.set(name, value)
}
}
Expand Down Expand Up @@ -102,7 +99,7 @@ export abstract class Manager<
* Raise error when the parent class doesn't implement the function
*/
if (typeof this[driverCreatorName] !== 'function') {
throw new Error(`${mappingName} driver is not supported by ${this.constructor.name}`)
throw new Error(`"${mappingName}" driver is not supported by "${this.constructor.name}"`)
}

const value = this.wrapDriverResponse(mappingName, this[driverCreatorName](mappingName, config))
Expand All @@ -123,10 +120,10 @@ export abstract class Manager<
*/
public use<K extends keyof MappingsList & string>(name: K): MappingsList[K]
public use(name: string): ReturnValueContract
public use(): DefaultItem
public use(): { [K in keyof MappingsList]: MappingsList[K] }[keyof MappingsList]
public use<K extends keyof MappingsList & string>(
name?: K | string
): MappingsList[K] | ReturnValueContract | DefaultItem {
): MappingsList[K] | ReturnValueContract | { [K in keyof MappingsList]: MappingsList[K] }[keyof MappingsList] {
name = name || this.getDefaultMappingName()

const cached = this.getFromCache(name)
Expand Down
22 changes: 11 additions & 11 deletions test/manager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ interface Mailable {
test.group('Manager', () => {
test("raise error when driver create function doesn't exists", (assert) => {
class Mail extends Manager<Mailable> {
protected $cacheMappings = false
protected cacheMappings = false
protected getDefaultMappingName() {
return 'smtp'
}
Expand All @@ -32,7 +32,7 @@ test.group('Manager', () => {
const mail = new Mail({})
const fn = () => mail.use()

assert.throw(fn, 'smtp driver is not supported by Mail')
assert.throw(fn, '"smtp" driver is not supported by "Mail"')
})

test('resolve default driver when no name is defined', (assert) => {
Expand All @@ -42,7 +42,7 @@ test.group('Manager', () => {
}

class Mail extends Manager<Mailable> {
protected $cacheMappings = false
protected cacheMappings = false

protected getDefaultMappingName() {
return 'smtp'
Expand Down Expand Up @@ -74,7 +74,7 @@ test.group('Manager', () => {
}

class Mail extends Manager<Mailable> {
protected $cacheMappings = false
protected cacheMappings = false
protected getDefaultMappingName() {
return 'smtp'
}
Expand Down Expand Up @@ -103,7 +103,7 @@ test.group('Manager', () => {
}

class Mail extends Manager<Mailable> {
protected $cacheMappings = false
protected cacheMappings = false
protected getDefaultMappingName() {
return 'smtp'
}
Expand Down Expand Up @@ -132,7 +132,7 @@ test.group('Manager', () => {
}

class Mail extends Manager<Mailable> {
protected $cacheMappings = false
protected cacheMappings = false
protected getDefaultMappingName() {
return 'smtp'
}
Expand Down Expand Up @@ -168,7 +168,7 @@ test.group('Manager', () => {
}

class Mail extends Manager<Mailable> {
protected $cacheMappings = true
protected cacheMappings = true
protected getDefaultMappingName() {
return 'smtp'
}
Expand Down Expand Up @@ -204,7 +204,7 @@ test.group('Manager', () => {
}

class Mail extends Manager<Mailable> {
protected $cacheMappings = true
protected cacheMappings = true
protected getDefaultMappingName() {
return 'smtp'
}
Expand Down Expand Up @@ -240,7 +240,7 @@ test.group('Manager', () => {
}

class Mail extends Manager<Mailable> {
protected $cacheMappings = true
protected cacheMappings = true
protected getDefaultMappingName() {
return 'smtp'
}
Expand Down Expand Up @@ -278,7 +278,7 @@ test.group('Manager', () => {
}

class Mail extends Manager<Mailable, { mappingName: string; driver: Mailable }> {
protected $cacheMappings = true
protected cacheMappings = true
protected getDefaultMappingName() {
return 'smtp'
}
Expand Down Expand Up @@ -320,7 +320,7 @@ test.group('Manager', () => {
}

class Mail extends Manager<Mailable, { mappingName: string; driver: Mailable }> {
protected $cacheMappings = true
protected cacheMappings = true
protected getDefaultMappingName() {
return 'smtp'
}
Expand Down

0 comments on commit fd4cc96

Please sign in to comment.