@serverless/platform-client
Advanced tools
Comparing version 0.0.6 to 0.1.0
@@ -11,2 +11,3 @@ /** | ||
this.connection = null | ||
this.context = {} | ||
} | ||
@@ -24,28 +25,13 @@ | ||
// Validate options | ||
if (!options.org) { | ||
return reject('connect() requires an "org" to be specified as an option') | ||
const headers = { | ||
'AUTHORIZATION': `Bearer ${this.sdk.accessKey}`, | ||
'X-SERVERLESS-PLATFORM-STREAMING-FILTER': JSON.stringify(options.filter) | ||
} | ||
if (options.orgName) headers['X-SERVERLESS-PLATFORM-STREAMING-ORG-NAME'] = options.orgName | ||
if (options.orgUid) headers['X-SERVERLESS-PLATFORM-STREAMING-ORG-ID'] = options.orgUid | ||
// If no event listener is offered, create an empty one | ||
if (!options.onEvent) { | ||
options.onEvent = () => {} | ||
} | ||
// Sanitize filter | ||
options.filter = options.filter || {} | ||
options.filter.stageName = options.filter.stageName ? options.filter.stageName.trim() : null | ||
options.filter.appName = options.filter.appName ? options.filter.appName.trim() : null | ||
options.filter.instanceName = options.filter.instanceName ? options.filter.instanceName.trim() : null | ||
// Instantiate websockets library | ||
this.connection = new WS( | ||
this.sdk.domains.events, | ||
{ | ||
headers: { | ||
'AUTHORIZATION': `Bearer ${this.sdk.accessKey}`, | ||
'X-SERVERLESS-PLATFORM-STREAMING-ORG': options.org, | ||
'X-SERVERLESS-PLATFORM-STREAMING-FILTER': JSON.stringify(options.filter) | ||
} | ||
} | ||
{ headers } | ||
) | ||
@@ -66,3 +52,3 @@ | ||
self.connection.on('error', (error) => { | ||
console.log(`Serverless Streaming: ${error}`) | ||
console.log(`Serverless Platform Streaming: ${error}`) | ||
}) | ||
@@ -106,5 +92,4 @@ | ||
} | ||
} | ||
module.exports = Connection |
42
index.js
@@ -24,2 +24,3 @@ /** | ||
this.platformStage = process.env.SERVERLESS_PLATFORM_STAGE || config.platformStage || 'prod' | ||
this.context = config.context || this.context || {} | ||
this.debugMode = config.debug || false | ||
@@ -35,2 +36,23 @@ } | ||
async connect(options) { | ||
// Validate org | ||
if ( | ||
(!options.orgName && !this.context.orgName) && | ||
(!options.orgUid && !this.context.orgUid) | ||
) | ||
{ | ||
throw new Error(`You must specify an "orgName" or "orgUid" to connect`) | ||
} | ||
// If no event listener is offered, create an empty one | ||
if (!options.onEvent) { | ||
options.onEvent = () => {} | ||
} | ||
// Sanitize filter | ||
options.filter = options.filter || {} | ||
options.filter.stageName = options.filter.stageName ? options.filter.stageName.trim() : null | ||
options.filter.appName = options.filter.appName ? options.filter.appName.trim() : null | ||
options.filter.instanceName = options.filter.instanceName ? options.filter.instanceName.trim() : null | ||
// Create connection | ||
this.connection = new Connection(this) | ||
@@ -80,4 +102,8 @@ await this.connection.connect(options) | ||
} | ||
if (!event.orgName) { | ||
throw new Error(`event must have an "orgName"`) | ||
if ( | ||
(!event.orgName && !this.context.orgName) && | ||
(!event.orgUid && !this.context.orgUid) | ||
) | ||
{ | ||
throw new Error(`event must have an "orgName" or "orgUid"`) | ||
} | ||
@@ -90,8 +116,16 @@ | ||
safeEvent.event = event.event.trim() | ||
safeEvent.orgName = event.orgName.trim() | ||
if (event.orgName) safeEvent.orgName = event.orgName.trim() | ||
if (event.orgUid) safeEvent.orgUid = event.orgUid.trim() | ||
if (event.stageName) safeEvent.stageName = event.stageName.trim() | ||
if (event.appName) safeEvent.appName = event.appName.trim() | ||
if (event.instanceName) safeEvent.instanceName = event.instanceName.trim() | ||
// Auto add to event body if set in context, but don't override above values | ||
if (this.context.orgUid) safeEvent.orgUid = event.orgUid || this.context.orgUid | ||
if (this.context.orgName) safeEvent.orgName = event.orgName || this.context.orgName | ||
if (this.context.stageName) safeEvent.stageName = this.context.stageName | ||
if (this.context.appName) safeEvent.appName = this.context.appName | ||
if (this.context.instanceName) safeEvent.instanceName = this.context.instanceName | ||
safeEvent.data = event.data || {} | ||
return this.connection.send(safeEvent) | ||
@@ -98,0 +132,0 @@ } |
{ | ||
"name": "@serverless/platform-client", | ||
"version": "0.0.6", | ||
"version": "0.1.0", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
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
10600
333