Comparing version 0.0.1 to 0.0.2
143
lib/index.js
@@ -6,52 +6,99 @@ 'use strict'; | ||
const validate = require('validate.js'); | ||
const { | ||
promisify, | ||
ElarianService, | ||
ElarianMessages, | ||
} = require('./utils'); | ||
const HOST = 'api.elarian.com:443'; | ||
const SANDBOX_HOST = 'api.elarian.dev:443'; | ||
const { promisify, ElarianService } = require('./utils'); | ||
function Elarian(options) { | ||
const voice = require('./voice'); | ||
const payment = require('./payment'); | ||
const customer = require('./customer'); | ||
const messaging = require('./messaging'); | ||
const auth = require('./authentication'); | ||
const notification = require('./notification'); | ||
this.options = _.cloneDeep(options); | ||
const HOST = 'api.elarian.dev:443'; | ||
const constraints = { | ||
apiKey: { | ||
type: 'string', | ||
}, | ||
authToken: { | ||
type: 'string', | ||
}, | ||
sandbox: { | ||
type: 'boolean' | ||
class Client { | ||
constructor(options) { | ||
this.options = _.cloneDeep(options); | ||
const constraints = { | ||
apiKey: { | ||
type: 'string', | ||
}, | ||
authToken: { | ||
type: 'string', | ||
}, | ||
appId: { | ||
type: 'string', | ||
} | ||
}; | ||
const error = validate(this.options, constraints); | ||
if (error) { | ||
throw error; | ||
} | ||
}; | ||
const error = validate(this.options, constraints); | ||
if (error) { | ||
throw error; | ||
} | ||
const { apiKey, authToken, appId } = this.options; | ||
if (!apiKey && !authToken) { | ||
throw new Error('Either one of apiKey or authToken is required'); | ||
} | ||
const { apiKey, authToken, sandbox } = this.options; | ||
if (!apiKey && !authToken) { | ||
throw new Error('Either one of apiKey or authToken is required'); | ||
if (!appId) { | ||
throw new Error('appId is required'); | ||
} | ||
const credentials = grpc.credentials.combineChannelCredentials( | ||
grpc.credentials.createSsl(), | ||
grpc.credentials.createFromMetadataGenerator((params, callback) => { | ||
const metadata = new grpc.Metadata(); | ||
if (apiKey) { | ||
metadata.set('api-key', apiKey); | ||
} | ||
if (authToken) { | ||
metadata.set('auth-token', authToken); | ||
} | ||
callback(null, metadata); | ||
}), | ||
); | ||
this._stub = promisify(new ElarianService(HOST, credentials)); | ||
// Auth | ||
const authClient = new auth.Client(this.options, this._stub); | ||
this.authToken = authClient.authToken; | ||
// Customer | ||
const customerClient = new customer.Client(this.options, this._stub); | ||
this.getCustomerState = customerClient.getCustomerState; | ||
this.adoptCustomerState = customerClient.adoptCustomerState; | ||
this.addCustomerReminder = customerClient.addCustomerReminder; | ||
this.addCustomerReminderByTag = customerClient.addCustomerReminderByTag; | ||
this.cancelCustomerReminder = customerClient.cancelCustomerReminder; | ||
this.cancelCustomerReminderByTag = customerClient.cancelCustomerReminderByTag; | ||
this.updateCustomerTag = customerClient.updateCustomerTag; | ||
this.deleteCustomerTag = customerClient.deleteCustomerTag; | ||
this.updateCustomerSecondaryId = customerClient.updateCustomerSecondaryId; | ||
this.deleteCustomerSecondaryId = customerClient.deleteCustomerSecondaryId; | ||
this.updateCustomerMetadata = customerClient.updateCustomerMetadata; | ||
this.deleteCustomerMetadata = customerClient.deleteCustomerMetadata; | ||
// Messaging | ||
const messagingClient = new messaging.Client(this.options, this._stub); | ||
this.sendMessage = messagingClient.sendMessage; | ||
this.sendMessageByTag = messagingClient.sendMessageByTag; | ||
this.replyToMessage = messagingClient.replyToMessage; | ||
this.messagingConsent = messagingClient.messagingConsent; | ||
// Payment | ||
const paymentClient = new payment.Client(this.options, this._stub); | ||
this.sendPayment = paymentClient.sendPayment; | ||
this.checkoutPayment = paymentClient.checkoutPayment; | ||
// Voice | ||
const voiceClient = new voice.Client(this.options, this._stub); | ||
this.makeVoiceCall = voiceClient.makeVoiceCall; | ||
// Notifcation | ||
const notificationClient = new notification.Client(this.options, this._stub); | ||
this.sendWebhookResponse = notificationClient.sendWebhookResponse; | ||
this.streamNotifications = notificationClient.streamNotifications; | ||
} | ||
const host = sandbox ? SANDBOX_HOST : HOST; | ||
const credentials = grpc.credentials.combineChannelCredentials( | ||
grpc.credentials.createSsl(), | ||
grpc.credentials.createFromMetadataGenerator((params, callback) => { | ||
const metadata = new grpc.Metadata(); | ||
if (apiKey) { | ||
metadata.set('api-key', apiKey); | ||
} | ||
if (authToken) { | ||
metadata.set('auth-token', authToken); | ||
} | ||
callback(null, metadata); | ||
}), | ||
); | ||
this._stub = promisify(new ElarianService(host, credentials)); | ||
} | ||
@@ -61,7 +108,9 @@ | ||
module.exports = { | ||
...ElarianMessages, | ||
Client: function (options) { | ||
const elarian = new Elarian(options); | ||
return elarian._stub; | ||
}, | ||
Client, | ||
...auth.Enums, | ||
...voice.Enums, | ||
...payment.Enums, | ||
...customer.Enums, | ||
...messaging.Enums, | ||
...notification.Enums, | ||
}; |
const util = require('util'); | ||
const google_protobuf_wrappers_pb = require('google-protobuf/google/protobuf/wrappers_pb.js'); | ||
const google_protobuf_timestamp_pb = require('google-protobuf/google/protobuf/timestamp_pb.js'); | ||
const messages = require('./service/web_pb'); | ||
@@ -8,2 +11,17 @@ const service = require('./service/web_grpc_pb'); | ||
const { | ||
Cash, | ||
IndexMapping, | ||
CustomerIndex, | ||
CustomerNumber, | ||
TextMessageBody, | ||
MediaMessageBody, | ||
CustomerReminder, | ||
CustomerMessageBody, | ||
LocationMessageBody, | ||
TextMessageTemplate, | ||
MessagingChannelNumber, | ||
} = ElarianMessages; | ||
const promisify = (client) => { | ||
@@ -24,2 +42,133 @@ const to = {}; | ||
const addTag = (req, { tag } = {}) => { | ||
const tagValue = new google_protobuf_wrappers_pb.StringValue().setValue(tag.value); | ||
const tagObj = new IndexMapping() | ||
.setKey(tag.key) | ||
.setValue(tagValue); | ||
return req.setTag(tagObj); | ||
} | ||
const addIndices = (req, params, key, keyFn) => { | ||
const valueObj = (params[key] || []).map((i) => { | ||
const mapping = new IndexMapping() | ||
.setKey(i.key) | ||
.setValue(new google_protobuf_wrappers_pb.StringValue().setValue(i.value)); | ||
return mapping; | ||
}) | ||
return req[keyFn](valueObj); | ||
} | ||
const addCustomerIndices = (req, params, key, keyFn) => { | ||
const valueObj = (params[key] || []).map((i) => { | ||
let idx = new CustomerIndex(); | ||
const mapping = new IndexMapping() | ||
.setKey(i.mapping.key) | ||
.setValue(new google_protobuf_wrappers_pb.StringValue().setValue(i.mapping.value)); | ||
idx = idx.setMapping(mapping); | ||
idx = idx.setExpiration(new google_protobuf_timestamp_pb.Timestamp().setSeconds(i.expiration)); | ||
return idx; | ||
}) | ||
return req[keyFn](valueObj); | ||
} | ||
const addReminder = (req, { reminder } = {}) => { | ||
const expirationValue = new google_protobuf_timestamp_pb.Timestamp().setSeconds(reminder.expiration); | ||
const payloadValue = new google_protobuf_wrappers_pb.StringValue().setValue(reminder.payload); | ||
const reminderObj = new CustomerReminder() | ||
.setProductId(reminder.productId) | ||
.setKey(reminder.key) | ||
.setExpiration(expirationValue) | ||
.setPayload(payloadValue); | ||
return req.setReminder(reminderObj); | ||
} | ||
const addCustomer = (req, { customerId, customerNumber } = {}) => { | ||
if (customerId) { | ||
return req.setCustomerId(customerId); | ||
} | ||
if (customerNumber) { | ||
const customerNumberObj = new CustomerNumber() | ||
.setNumber(customerNumber.number) | ||
.setProvider(customerNumber.provider || 0) | ||
return req.setCustomerNumber(customerNumberObj); | ||
} | ||
return req; | ||
} | ||
const addOtherCustomer = (req, { otherCustomerId, otherCustomerNumber } = {}) => { | ||
if (otherCustomerId) { | ||
return req.setOtherCustomerId(otherCustomerId); | ||
} | ||
if (otherCustomerNumber) { | ||
const customerNumberObj = new CustomerNumber() | ||
.setNumber(customerNumber.number) | ||
.setProvider(customerNumber.provider || 0) | ||
return req.setOtherCustomerNumber(customerNumberObj); | ||
} | ||
return req; | ||
} | ||
const addChannelNumber = (req, { channelNumber } = {}, Class = MessagingChannelNumber) => { | ||
const channelNumberObj = new Class() | ||
.setNumber(channelNumber.number) | ||
.setChannel(channelNumber.channel || 0); | ||
return req.setChannelNumber(channelNumberObj); | ||
} | ||
const addCashValue = (req, { value } = {}) => { | ||
const cashObj = new Cash() | ||
.setCurrencyCode(value.currencyCode) | ||
.setAmount(value.amount); | ||
return req.setValue(cashObj); | ||
} | ||
const addCustomerMessageBody = (req, { body } = {}) => { | ||
if (!body.text && !body.media && !body.location) { | ||
throw new Error('One of text, media or location is required in the body'); | ||
} | ||
let bodyObj = new CustomerMessageBody(); | ||
if (body.text) { | ||
let { template = { name: null, params: [] } } = body.text; | ||
if (template.name) { | ||
template = new TextMessageTemplate() | ||
.setName(body.text.template.name) | ||
.setParamsList(body.text.template.params); | ||
} else { | ||
template = null; | ||
} | ||
const textValue = new google_protobuf_wrappers_pb.StringValue().setValue(body.text.text); | ||
const bodyText = new TextMessageBody() | ||
.setText(textValue) | ||
.setTemplate(template); | ||
bodyObj = bodyObj.setText(bodyText); | ||
} | ||
if (body.media) { | ||
const mediaObj = new MediaMessageBody() | ||
.setUrl(body.media.url) | ||
.setMedia(body.media.type || 0); | ||
bodyObj = bodyObj.setMedia(mediaObj); | ||
} | ||
if (body.location) { | ||
const { latitude, longitude } = body.location; | ||
const locationObj = new LocationMessageBody() | ||
.setLatitude(latitude) | ||
.setLongitude(longitude); | ||
bodyObj.setLocation(locationObj); | ||
} | ||
return req.setBody(bodyObj); | ||
}; | ||
module.exports = { | ||
@@ -29,2 +178,12 @@ promisify, | ||
ElarianMessages, | ||
addTag, | ||
addIndices, | ||
addReminder, | ||
addCustomer, | ||
addCashValue, | ||
addChannelNumber, | ||
addOtherCustomer, | ||
addCustomerIndices, | ||
addCustomerMessageBody, | ||
}; |
{ | ||
"name": "elarian", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "Elrian JavaScript SDK", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -24,27 +24,27 @@ # Elarian JavaScript SDK | ||
```javascript | ||
const { Client, StreamNotificationRequest } = require('elarian'); | ||
const { Client, NumberProvider, MessagingChannel } = require('elarian'); | ||
const elarian = new Client({ | ||
apiKey: 'test_api_key', | ||
sandbox: true, | ||
appId: 'test_app' | ||
}); | ||
const req = new StreamNotificationRequest() | ||
.setAppId('test_app'); | ||
const stream = elarian.streamNotifications(req); | ||
stream.on('data', (notification) => { | ||
console.log(notification); | ||
}); | ||
stream.on('end', () => { | ||
// The server has finished sending | ||
}); | ||
stream.on('error', (err) => { | ||
// An error has occurred and the stream has been closed. | ||
console.error(err); | ||
}); | ||
stream.on('status', (status) => { | ||
// process status | ||
}); | ||
elarian.sendMessage({ | ||
productId: 'product-j90HNs', | ||
customerNumber: { | ||
number: '+254700000000', | ||
provider: NumberProvider.TELCO, | ||
}, | ||
channelNumber: { | ||
number: '41011', | ||
channel: MessagingChannel.SMS, // MessagingChannel.WHATSAPP, MessagingChannel.TELEGRAM, MessagingChannel.FACEBOOK_MESSENGER, MessagingChannel.GOOGLE_RCS | ||
}, | ||
body: { | ||
text: { | ||
text: 'Hello Boss' | ||
} | ||
} | ||
}) | ||
.then(res => console.log(res)) | ||
.catch(ex => console.error(ex)); | ||
``` | ||
@@ -51,0 +51,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
933793
15
23941
2