@serverless/platform-client
Advanced tools
Comparing version 0.10.1 to 0.11.0
@@ -6,6 +6,7 @@ /** | ||
const WS = require('ws') | ||
const utils = require('./utils') | ||
class Connection { | ||
constructor(client) { | ||
this.client = client | ||
constructor(sdk) { | ||
this.sdk = sdk | ||
this.connection = null | ||
@@ -26,3 +27,3 @@ this.context = {} | ||
// Validate org | ||
if (!options.orgName && !options.orgUid && !this.client.context.orgName && !this.client.context.orgUid) { | ||
if (!options.orgName && !options.orgUid && !this.sdk.context.orgName && !this.sdk.context.orgUid) { | ||
throw new Error(`You must specify an "orgName" or "orgUid" to connect`) | ||
@@ -32,9 +33,9 @@ } | ||
// Set context, which adds contextual details to every event for convenience | ||
if (options.orgName) this.client.context.orgName = options.orgName | ||
if (options.orgUid) this.client.context.orgUid = options.orgUid | ||
if (options.stageName) this.client.context.stageName = options.stageName | ||
if (options.appName) this.client.context.appName = options.appName | ||
if (options.instanceName) this.client.context.instanceName = options.instanceName | ||
if (options.componentName) this.client.context.componentName = options.componentName | ||
if (options.componentVersion) this.client.context.componentVersion = options.componentVersion | ||
if (options.orgName) this.sdk.context.orgName = options.orgName | ||
if (options.orgUid) this.sdk.context.orgUid = options.orgUid | ||
if (options.stageName) this.sdk.context.stageName = options.stageName | ||
if (options.appName) this.sdk.context.appName = options.appName | ||
if (options.instanceName) this.sdk.context.instanceName = options.instanceName | ||
if (options.componentName) this.sdk.context.componentName = options.componentName | ||
if (options.componentVersion) this.sdk.context.componentVersion = options.componentVersion | ||
@@ -48,3 +49,3 @@ // Sanitize filter | ||
const headers = { | ||
'AUTHORIZATION': `Bearer ${this.client.accessKey}`, | ||
'AUTHORIZATION': `Bearer ${this.sdk.accessKey}`, | ||
'X-SERVERLESS-PLATFORM-STREAMING-FILTER': JSON.stringify(options.filter) | ||
@@ -57,3 +58,3 @@ } | ||
this.connection = new WS( | ||
this.client.getDomain('events-streaming'), | ||
this.sdk.getDomain('events-streaming'), | ||
{ headers } | ||
@@ -115,3 +116,3 @@ ) | ||
/** | ||
* Sends a message via the websockets connection. Validation should be handled in client.publish() | ||
* Sends a message via the websockets connection. Validation should be handled in sdk.publish() | ||
*/ | ||
@@ -130,4 +131,4 @@ send(event) { | ||
} | ||
event.orgUid = event.orgUid || this.client.context.orgUid | ||
event.orgName = event.orgName || this.client.context.orgName | ||
event.orgUid = event.orgUid || this.sdk.context.orgUid | ||
event.orgName = event.orgName || this.sdk.context.orgName | ||
if (!event.orgName && !event.orgUid) { | ||
@@ -140,3 +141,3 @@ throw new Error(`event must have an "orgName" or "orgUid"`) | ||
const safeEvent = {} | ||
safeEvent.accessKey = this.client.accessKey | ||
safeEvent.accessKey = this.sdk.accessKey | ||
safeEvent.event = event.event.trim() | ||
@@ -150,9 +151,9 @@ if (event.orgName) safeEvent.orgName = event.orgName.trim() | ||
// Auto add to event body if set in context, but don't override above values | ||
if (this.client.context.orgUid) safeEvent.orgUid = event.orgUid || this.client.context.orgUid | ||
if (this.client.context.orgName) safeEvent.orgName = event.orgName || this.client.context.orgName | ||
if (this.client.context.stageName) safeEvent.stageName = this.client.context.stageName | ||
if (this.client.context.appName) safeEvent.appName = this.client.context.appName | ||
if (this.client.context.instanceName) safeEvent.instanceName = this.client.context.instanceName | ||
if (this.client.context.componentName) safeEvent.componentName = this.client.context.componentName | ||
if (this.client.context.componentVersion) safeEvent.componentVersion = this.client.context.componentVersion | ||
if (this.sdk.context.orgUid) safeEvent.orgUid = event.orgUid || this.sdk.context.orgUid | ||
if (this.sdk.context.orgName) safeEvent.orgName = event.orgName || this.sdk.context.orgName | ||
if (this.sdk.context.stageName) safeEvent.stageName = this.sdk.context.stageName | ||
if (this.sdk.context.appName) safeEvent.appName = this.sdk.context.appName | ||
if (this.sdk.context.instanceName) safeEvent.instanceName = this.sdk.context.instanceName | ||
if (this.sdk.context.componentName) safeEvent.componentName = this.sdk.context.componentName | ||
if (this.sdk.context.componentVersion) safeEvent.componentVersion = this.sdk.context.componentVersion | ||
@@ -163,4 +164,39 @@ safeEvent.data = event.data || {} | ||
} | ||
/** | ||
* Publish an event synchornously, via the HTTP API | ||
* @param {*} sdk | ||
* @param {*} event | ||
*/ | ||
static async publishSync(sdk, event) { | ||
// Validate | ||
if (!event) { | ||
throw new Error(`An "event" is required`) | ||
} | ||
if (typeof event !== 'object') { | ||
throw new Error(`The "event" argument must be an object`) | ||
} | ||
if (!event.event || typeof event.event !== 'string') { | ||
throw new Error(`The "event" property containing the event type is required and must be a string`) | ||
} | ||
event.orgUid = event.orgUid || sdk.context.orgUid | ||
event.orgName = event.orgName || sdk.context.orgName | ||
if (!event.orgName && !event.orgUid) { | ||
throw new Error(`event must have an "orgName" or "orgUid"`) | ||
} | ||
// Add accessKey to event body (in addition to headers) | ||
event.accessKey = event.accessKey || sdk.accessKey || null | ||
// Send request | ||
return await utils.request({ | ||
endpoint: `${sdk.getDomain('events')}/publish`, | ||
accessKey: sdk.accessKey, | ||
method: 'POST', | ||
data: event, | ||
}) | ||
} | ||
} | ||
module.exports = Connection |
{ | ||
"dev": { | ||
"events-streaming": "wss://events-streaming-v1.serverless-platform-dev.com", | ||
"events": "https://events-v1.serverless-platform-dev.com", | ||
"registry": "https://registry-v1.serverless-platform-dev.com", | ||
@@ -9,2 +10,3 @@ "engine": "https://engine-v1.serverless-platform-dev.com" | ||
"events-streaming": "wss://events-streaming-v1.serverless-platform.com", | ||
"events": "https://events-v1.serverless-platform.com", | ||
"registry": "https://registry-v1.serverless-platform.com", | ||
@@ -11,0 +13,0 @@ "engine": "https://engine-v1.serverless-platform.com" |
@@ -99,3 +99,3 @@ const domains = require('./domains') | ||
/** | ||
* Publishes a Serverless Platform Event | ||
* Publishes a Serverless Platform Event via Websockets. The use-case for this is asynchronous publishing, where you do not want to synchronous auth requests, where every message must be authorized first, adding latency. | ||
* @return {null} | ||
@@ -111,2 +111,10 @@ */ | ||
/** | ||
* Publishes a Serverless Platform Event via HTTP API. The use-case for this is synchronous publishing, where you do not want to open a websockets connection. | ||
* @return {null} | ||
*/ | ||
async publishSync(event) { | ||
return await Connection.publishSync(this, event) | ||
} | ||
/** | ||
* | ||
@@ -152,2 +160,14 @@ * Components | ||
/** | ||
* Returns a peroperly formatted ID for an Instance | ||
* @param {*} orgName The name of the Serverless Platform Organization. | ||
* @param {*} stageName The name of the Serverless Platform Stage. | ||
* @param {*} appName The name of the Serverless Platform Application. | ||
* @param {*} instanceName The name of the Serverless Platform Instance. | ||
* @return {object} Returns a new Instance definition as a Javascript Object. | ||
*/ | ||
generateInstanceId(orgName = null, stageName = null, appName = null, instanceName = null) { | ||
return instance.generateId(orgName, stageName, appName, instanceName) | ||
} | ||
/** | ||
* Returns a new Instance as a Javascript object. | ||
@@ -154,0 +174,0 @@ * @param {*} orgName The name of the Serverless Platform Organization. |
@@ -448,2 +448,3 @@ /* | ||
module.exports = { | ||
generateId, | ||
create, | ||
@@ -450,0 +451,0 @@ save, |
{ | ||
"name": "@serverless/platform-client", | ||
"version": "0.10.1", | ||
"version": "0.11.0", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "./lib/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
54916
1161