@azure/eventgrid
Advanced tools
Comparing version 3.0.0-alpha.20210127.1 to 3.0.0-alpha.20210209.1
# Release History | ||
## 3.0.0-beta.4 (Unreleased) | ||
### Breaking Changes | ||
- `EventGridSharedAccessCredential` has been removed, in favor of `AzureSASCredential`. Code which is using `EventGridSharedAccessCredential` should | ||
now use `AzureSASCredential` instead. | ||
- When constructing the client, you must now include the schema type your topic is configured to expect (one of "EventGrid", "CloudEvent" or "Custom"). | ||
- The `sendEvents` methods have been collapsed into a single method on the client called `send` which uses the input schema that was configured on the client. | ||
## 3.0.0-beta.3 (2020-10-06) | ||
@@ -4,0 +13,0 @@ |
@@ -11,7 +11,7 @@ // Copyright (c) Microsoft Corporation. | ||
/** | ||
* EventGridConsumer is used to aid in processing events delivered by EventGrid. It can deserialize a JSON encoded payload | ||
* EventGridDeserializer is used to aid in processing events delivered by EventGrid. It can deserialize a JSON encoded payload | ||
* of either a single event or batch of events as well as be used to convert the result of `JSON.parse` into an | ||
* `EventGridEvent` or `CloudEvent` like object. | ||
* | ||
* Unlike normal JSON deseralization, EventGridConsumer does some additional conversions: | ||
* Unlike normal JSON deseralization, EventGridDeserializer does some additional conversions: | ||
* | ||
@@ -22,12 +22,4 @@ * - The consumer parses the event time property into a `Date` object, for ease of use. | ||
* - The `data` payload from system events is converted to match the interfaces this library defines. | ||
* | ||
* When constructing an `EventGridConsumer`, a map of event types to custom deserializers may be provided. When | ||
* deserializing, if a custom deserializer has been registered for a given event type, it will be called with the | ||
* data object. The object this deserializer returns will replace the existing data object. | ||
*/ | ||
export class EventGridConsumer { | ||
constructor(options) { | ||
var _a; | ||
this.customDeserializers = (_a = options === null || options === void 0 ? void 0 : options.customDeserializers) !== null && _a !== void 0 ? _a : {}; | ||
} | ||
export class EventGridDeserializer { | ||
deserializeEventGridEvents(encodedEvents) { | ||
@@ -43,5 +35,2 @@ return __awaiter(this, void 0, void 0, function* () { | ||
} | ||
else if (this.customDeserializers[deserialized.eventType]) { | ||
deserialized.data = yield this.customDeserializers[deserialized.eventType](deserialized.data); | ||
} | ||
events.push(deserialized); | ||
@@ -96,5 +85,2 @@ } | ||
} | ||
else if (this.customDeserializers[modelEvent.type]) { | ||
modelEvent.data = yield this.customDeserializers[modelEvent.type](modelEvent.data); | ||
} | ||
// Build the "extensionsAttributes" property bag by removing all known top level properties. | ||
@@ -101,0 +87,0 @@ const extensionAttributes = Object.assign({}, deserialized); |
@@ -30,3 +30,3 @@ // Copyright (c) Microsoft Corporation. | ||
else { | ||
request.headers.set(SAS_TOKEN_HEADER_NAME, credential.signature()); | ||
request.headers.set(SAS_TOKEN_HEADER_NAME, credential.signature); | ||
} | ||
@@ -33,0 +33,0 @@ return next(request); |
@@ -18,3 +18,3 @@ // Copyright (c) Microsoft Corporation. | ||
/** | ||
* Creates an instance of EventGridPublisherClient. | ||
* Creates an instance of EventGridPublisherClient which sends events using the Event Grid Schema. | ||
* | ||
@@ -27,2 +27,3 @@ * Example usage: | ||
* "<service endpoint>", | ||
* "EventGrid", | ||
* new AzureKeyCredential("<api key>") | ||
@@ -32,8 +33,10 @@ * ); | ||
* | ||
* @param endpointUrl - The URL to the EventGrid endpoint, e.g. https://eg-topic.westus2-1.eventgrid.azure.net/api/events | ||
* @param endpointUrl - The URL to the Event Grid endpoint, e.g. https://eg-topic.westus2-1.eventgrid.azure.net/api/events. | ||
* @param inputSchema - The schema that the Event Grid endpoint is configured to accept. One of "EventGrid", "CloudEvent", or "Custom". | ||
* @param credential - Used to authenticate requests to the service. | ||
* @param options - Used to configure the Event Grid Client | ||
* @param options - Used to configure the Event Grid Client. | ||
*/ | ||
constructor(endpointUrl, credential, options = {}) { | ||
constructor(endpointUrl, inputSchema, credential, options = {}) { | ||
this.endpointUrl = endpointUrl; | ||
this.inputSchema = inputSchema; | ||
const libInfo = `azsdk-js-eventgrid/${SDK_VERSION}`; | ||
@@ -58,11 +61,25 @@ const pipelineOptions = Object.assign({}, options); | ||
/** | ||
* Publishes events in the Event Grid schema. The topic must be configured to expect events in the Event Grid schema. | ||
* Sends events to a topic. | ||
* | ||
* @param message - One or more events to publish | ||
* @param events - The events to send. The events should be in the schema used when constructing the client. | ||
* @param options - Options to control the underlying operation. | ||
*/ | ||
sendEvents(events, options) { | ||
send(events, options) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const { span, updatedOptions } = createSpan("EventGridPublisherClient-sendEvents", options || {}); | ||
const { span, updatedOptions } = createSpan("EventGridPublisherClient-send", options || {}); | ||
try { | ||
return yield this.client.publishEvents(this.endpointUrl, (events || []).map(convertEventGridEventToModelType), updatedOptions); | ||
switch (this.inputSchema) { | ||
case "EventGrid": { | ||
return yield this.client.publishEvents(this.endpointUrl, events.map(convertEventGridEventToModelType), updatedOptions); | ||
} | ||
case "CloudEvent": { | ||
return yield this.client.publishCloudEventEvents(this.endpointUrl, events.map(convertCloudEventToModelType), updatedOptions); | ||
} | ||
case "Custom": { | ||
return yield this.client.publishCustomEventEvents(this.endpointUrl, events, updatedOptions); | ||
} | ||
default: { | ||
throw new Error(`Unknown input schema type '${this.inputSchema}'`); | ||
} | ||
} | ||
} | ||
@@ -78,42 +95,2 @@ catch (e) { | ||
} | ||
/** | ||
* Publishes events in the Cloud Events 1.0 schema. The topic must be configured to expect events in the Cloud Events 1.0 schema. | ||
* | ||
* @param message - One or more events to publish | ||
*/ | ||
sendCloudEvents(events, options) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const { span, updatedOptions } = createSpan("EventGridPublisherClient-sendCloudEvents", options || {}); | ||
try { | ||
return yield this.client.publishCloudEventEvents(this.endpointUrl, (events || []).map(convertCloudEventToModelType), updatedOptions); | ||
} | ||
catch (e) { | ||
span.setStatus({ code: CanonicalCode.UNKNOWN, message: e.message }); | ||
throw e; | ||
} | ||
finally { | ||
span.end(); | ||
} | ||
}); | ||
} | ||
/** | ||
* Publishes events written using a custom schema. The topic must be configured to expect events in a custom schema. | ||
* | ||
* @param message - One or more events to publish | ||
*/ | ||
sendCustomSchemaEvents(events, options) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const { span, updatedOptions } = createSpan("EventGridPublisherClient-sendCustomSchemaEvents", options || {}); | ||
try { | ||
return yield this.client.publishCustomEventEvents(this.endpointUrl, events || [], updatedOptions); | ||
} | ||
catch (e) { | ||
span.setStatus({ code: CanonicalCode.UNKNOWN, message: e.message }); | ||
throw e; | ||
} | ||
finally { | ||
span.end(); | ||
} | ||
}); | ||
} | ||
} | ||
@@ -120,0 +97,0 @@ /** |
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT license. | ||
export { AzureKeyCredential } from "@azure/core-auth"; | ||
export { AzureKeyCredential, AzureSASCredential } from "@azure/core-auth"; | ||
export { EventGridPublisherClient } from "./eventGridClient"; | ||
export { EventGridSharedAccessSignatureCredential } from "./sharedAccessSignitureCredential"; | ||
export { EventGridConsumer } from "./consumer"; | ||
export { EventGridDeserializer } from "./consumer"; | ||
export { generateSharedAccessSignature } from "./generateSharedAccessSignature"; | ||
export { isSystemEvent } from "./predicates"; | ||
//# sourceMappingURL=index.js.map |
@@ -6,3 +6,3 @@ { | ||
"description": "An isomorphic client library for the Azure Event Grid service.", | ||
"version": "3.0.0-alpha.20210127.1", | ||
"version": "3.0.0-alpha.20210209.1", | ||
"keywords": [ | ||
@@ -9,0 +9,0 @@ "node", |
102
README.md
@@ -78,3 +78,7 @@ # Azure Event Grid client library for JavaScript | ||
const client = new EventGridPublisherClient("<endpoint>", new AzureKeyCredential("<Access Key>")); | ||
const client = new EventGridPublisherClient( | ||
"<endpoint>", | ||
"<endpoint schema>", | ||
new AzureKeyCredential("<Access Key>") | ||
); | ||
``` | ||
@@ -84,13 +88,11 @@ | ||
Like an access key, a SAS token allows access to sending events to an Event Grid topic. Unlike an access key, which can be used until it is regenerated, a SAS token has an experation time, at which point it is no longer valid. To use a SAS token for authentication, use the `EventGridSharedAccesSignatureCredential` as follows: | ||
Like an access key, a SAS token allows access to sending events to an Event Grid topic. Unlike an access key, which can be used until it is regenerated, a SAS token has an experation time, at which point it is no longer valid. To use a SAS token for authentication, use the `AzureSASCredential` as follows: | ||
```js | ||
const { | ||
EventGridPublisherClient, | ||
EventGridSharedAccessSignatureCredential | ||
} = require("@azure/eventgrid"); | ||
const { EventGridPublisherClient, AzureSASCredential } = require("@azure/eventgrid"); | ||
const client = new EventGridPublisherClient( | ||
"<endpoint>", | ||
new EventGridSharedAccessSignatureCredential("<SAS Token>") | ||
"<endpoint schema>", | ||
new AzureSASCredential("<SAS Token>") | ||
); | ||
@@ -120,28 +122,58 @@ ``` | ||
Event Grid supports multiple schemas for encoding events. When a Custom Topic or Domain is created, you specify the schema that will be used when publishing events. While you may configure your topic to use a _custom schema_ it is more common to use the already defined _Event Grid schema_ or _CloudEvents 1.0 schema_. [CloudEvents](https://cloudevents.io/) is a Cloud Native Computing Foundation project which produces a specification for describing event data in a common way. Regardless of what schmea your topic or domain is configured to use, `EventGridPublisherClient` will be used to publish events to it. However, you must use the correct method for publishing: | ||
Event Grid supports multiple schemas for encoding events. When a Custom Topic or Domain is created, you specify the schema that will be used when publishing events. While you may configure your topic to use a _custom schema_ it is more common to use the already defined _Event Grid schema_ or _CloudEvents 1.0 schema_. [CloudEvents](https://cloudevents.io/) is a Cloud Native Computing Foundation project which produces a specification for describing event data in a common way. When you construct the EventGridPublisherClient you must specify which schema your topic is configured to use: | ||
| Schema | Publishing Method | | ||
| ------------ | --------------------- | | ||
| Event Grid | `publishEvents` | | ||
| Cloud Events | `publishCloudEvents` | | ||
| Custom | `publishCustomEvents` | | ||
If your topic is configured to use the Event Grid Schema, set "EventGrid" as the schema type: | ||
Using the wrong method will result in an error from the service and your events will not be published. | ||
```js | ||
const client = new EventGridPublisherClient( | ||
"<endpoint>", | ||
"EventGrid", | ||
new AzureKeyCredential("<API Key>") | ||
); | ||
``` | ||
### EventGridConsumer | ||
If your topic is configured to use the Cloud Event Schema, set "CloudEvent" as the schema type: | ||
Events delivered to consumers by Event Grid are delivered as JSON. Depending on the type of consumer being delivered to, the Event Grid service may deliver one or more events as part of a single payload. While these events may be deserialized using normal JavaScript methods like `JSON.parse`, this library offers a helper type for deserializing events, called `EventGridConsumer`. | ||
```js | ||
const client = new EventGridPublisherClient( | ||
"<endpoint>", | ||
"CloudEvent", | ||
new AzureKeyCredential("<API Key>") | ||
); | ||
``` | ||
Compared with using `JSON.parse` directly, `EventGridConsumer` does some additional conversions while deserializng events: | ||
If your topic is configured to use a Custom Event Schema, set "Custom" as the schema type: | ||
1. `EventGridConsumer` validates that the required properties of an event are present and are the right types. | ||
2. `EventGridConsumer` converts the event time property into a JavaScript `Date` object. | ||
3. When using Cloud Events, binary data may be used for an event's data property (by using `Uint8Array`). When the event is sent through Event Grid, it is encoded in Base 64. `EventGridConsumer` will decode this data back into an instance of `Uint8Array`. | ||
4. When deserilizing a _System Event_ (an event generated by another Azure service), `EventGridConsumer` will do additional conversions so that the `data` object matches the corresponding interface which describes its data. When using TypeScript, these interfaces ensure you have strong typing when access properties of the data object for a system event. | ||
```js | ||
const client = new EventGridPublisherClient( | ||
"<endpoint>", | ||
"Custom", | ||
new AzureKeyCredential("<API Key>") | ||
); | ||
``` | ||
When creating an instance of `EventGridConsumer` you may supply custom deserializers that are used to further convert the `data` object. | ||
Constructing the client with a different schema than what the topic is configured to expect will result in an error from the service and your events will not be published. | ||
You can see what input schema has been configured for an Event Grid topic by using the [Azure CLI][azure_cli] snippet below: | ||
```bash | ||
az eventgrid topic show --name <your-resource-name> --resource-group <your-resource-group-name> --query "inputSchema" | ||
``` | ||
### EventGridDeserializer | ||
Events delivered to consumers by Event Grid are delivered as JSON. Depending on the type of consumer being delivered to, the Event Grid service may deliver one or more events as part of a single payload. While these events may be deserialized using normal JavaScript methods like `JSON.parse`, this library offers a helper type for deserializing events, called `EventGridDeserializer`. | ||
Compared with using `JSON.parse` directly, `EventGridDeserializer` does some additional conversions while deserializng events: | ||
1. `EventGridDeserializer` validates that the required properties of an event are present and are the right types. | ||
2. `EventGridDeserializer` converts the event time property into a JavaScript `Date` object. | ||
3. When using Cloud Events, binary data may be used for an event's data property (by using `Uint8Array`). When the event is sent through Event Grid, it is encoded in Base 64. `EventGridDeserializer` will decode this data back into an instance of `Uint8Array`. | ||
4. When deserilizing a _System Event_ (an event generated by another Azure service), `EventGridDeserializer` will do additional conversions so that the `data` object matches the corresponding interface which describes its data. When using TypeScript, these interfaces ensure you have strong typing when access properties of the data object for a system event. | ||
When creating an instance of `EventGridDeserializer` you may supply custom deserializers that are used to further convert the `data` object. | ||
## Examples | ||
### Publish a Custom Event to an Event Grid Topic | ||
### Publish a Custom Event to an Event Grid Topic using the Event Grid Schema | ||
@@ -151,5 +183,9 @@ ```js | ||
const client = new EventGridPublisherClient("<endpoint>", new AzureKeyCredential("<API key>")); | ||
const client = new EventGridPublisherClient( | ||
"<endpoint>", | ||
"EventGrid", | ||
new AzureKeyCredential("<API key>") | ||
); | ||
await client.sendEvents([ | ||
await client.send([ | ||
{ | ||
@@ -166,3 +202,3 @@ eventType: "Azure.Sdk.SampleEvent", | ||
### Publish a Custom Event to a Topic in an Event Grid Domain | ||
### Publish a Custom Event to a Topic in an Event Grid Domain using the Event Grid Schema | ||
@@ -174,5 +210,9 @@ Publishing events to an Event Grid Domain is similar to publish to an Event Grid Topic, except that when using the Event Grid schema for events, you must include the `topic` property. When publishing events in the Cloud Events 1.0 schema, the required `source` property is used as the name of the topic in the domain to publish to: | ||
const client = new EventGridPublisherClient("<endpoint>", new AzureKeyCredential("<API key>")); | ||
const client = new EventGridPublisherClient( | ||
"<endpoint>", | ||
"EventGrid", | ||
new AzureKeyCredential("<API key>") | ||
); | ||
await client.sendEvents([ | ||
await client.send([ | ||
{ | ||
@@ -192,3 +232,3 @@ topic: "my-sample-topic", | ||
`EventGridConsumer` can be used to deserialize events delivered by Event Grid. When deserializing an event, you need to know the schema used to deliver the event. In this example we have events being delivered to an Azure Service Bus Topic in the Cloud Events schema. Using the Service Bus SDK we can recieve these events from the Service Bus Topic and then deserialize them using `EventGridConsumer` and use `isSystemEvent` to detect what type of events they are. | ||
`EventGridDeserializer` can be used to deserialize events delivered by Event Grid. When deserializing an event, you need to know the schema used to deliver the event. In this example we have events being delivered to an Azure Service Bus Topic in the Cloud Events schema. Using the Service Bus SDK we can recieve these events from the Service Bus Topic and then deserialize them using `EventGridDeserializer` and use `isSystemEvent` to detect what type of events they are. | ||
@@ -198,3 +238,3 @@ ```js | ||
const { DefaultAzureCredential } = require("@azure/identity"); | ||
const { EventGridConsumer, isSystemEvent } = require("@azure/eventgrid"); | ||
const { EventGridDeserializer, isSystemEvent } = require("@azure/eventgrid"); | ||
@@ -205,3 +245,3 @@ const client = new ServiceBusClient("<service bus hostname>", new DefaultAzureCredential()); | ||
const consumer = new EventGridConsumer(); | ||
const consumer = new EventGridDeserializer(); | ||
@@ -208,0 +248,0 @@ async function processMessage(message) { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
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
30186
301
22
39
1303083
49
14947