I have a dto class which describes an object of kcell telephony API
Something like:
<code>/**
* ...
* */
export class KcellEventRequest {
/**
* ...
* */
cmd: 'event'
/**
* ...
* */
crm_token: string
...
}
</code>
<code>/**
* ...
* */
export class KcellEventRequest {
/**
* ...
* */
cmd: 'event'
/**
* ...
* */
crm_token: string
...
}
</code>
/**
* ...
* */
export class KcellEventRequest {
/**
* ...
* */
cmd: 'event'
/**
* ...
* */
crm_token: string
...
}
for every kcell event
When I receive data, I use serializer and got object of KcellEventRequest class
<code>private serializer (body: any): KcellEventRequest | KcellHistoryRequest | KcellContactRequest | KcellRatingRequest {
body.authorization = this.parseToken(body.crm_token)
switch (body.cmd) {
case 'event' :
return plainToInstance(KcellEventRequest, body)
case 'history':
return plainToInstance(KcellHistoryRequest, body)
case 'contact':
return plainToInstance(KcellContactRequest, body)
case 'rating':
return plainToInstance(KcellRatingRequest, body)
default:
throw Error('Incorrect cmd')
}
}
async handleRequest (body: any, headers: any): Promise<any> {
// serialize object
const callObject = this.serializer(body)
// {{read next}}
return await callObject.handle(headers)
}
</code>
<code>private serializer (body: any): KcellEventRequest | KcellHistoryRequest | KcellContactRequest | KcellRatingRequest {
body.authorization = this.parseToken(body.crm_token)
switch (body.cmd) {
case 'event' :
return plainToInstance(KcellEventRequest, body)
case 'history':
return plainToInstance(KcellHistoryRequest, body)
case 'contact':
return plainToInstance(KcellContactRequest, body)
case 'rating':
return plainToInstance(KcellRatingRequest, body)
default:
throw Error('Incorrect cmd')
}
}
async handleRequest (body: any, headers: any): Promise<any> {
// serialize object
const callObject = this.serializer(body)
// {{read next}}
return await callObject.handle(headers)
}
</code>
private serializer (body: any): KcellEventRequest | KcellHistoryRequest | KcellContactRequest | KcellRatingRequest {
body.authorization = this.parseToken(body.crm_token)
switch (body.cmd) {
case 'event' :
return plainToInstance(KcellEventRequest, body)
case 'history':
return plainToInstance(KcellHistoryRequest, body)
case 'contact':
return plainToInstance(KcellContactRequest, body)
case 'rating':
return plainToInstance(KcellRatingRequest, body)
default:
throw Error('Incorrect cmd')
}
}
async handleRequest (body: any, headers: any): Promise<any> {
// serialize object
const callObject = this.serializer(body)
// {{read next}}
return await callObject.handle(headers)
}
Each class (KcellEventRequest | KcellHistoryRequest | KcellContactRequest | KcellRatingRequest) implements TelephonyAPIHandler with method handle
This method triggers my app to do something
I don’t like the handler function being in the dto, but it simplifies my logic here
return await callObject.handle(headers)
How do I separate the handler function from the dto?