@azure/eventgrid
Version:
An isomorphic client library for the Azure Event Grid service.
131 lines • 6.63 kB
JavaScript
;
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
Object.defineProperty(exports, "__esModule", { value: true });
exports.EventGridPublisherClient = void 0;
exports.convertEventGridEventToModelType = convertEventGridEventToModelType;
exports.convertCloudEventToModelType = convertCloudEventToModelType;
const tslib_1 = require("tslib");
const core_auth_1 = require("@azure/core-auth");
const eventGridAuthenticationPolicy_js_1 = require("./eventGridAuthenticationPolicy.js");
const constants_js_1 = require("./constants.js");
const models_js_1 = require("./models.js");
const generatedClient_js_1 = require("./generated/generatedClient.js");
const cloudEventDistrubtedTracingEnricherPolicy_js_1 = require("./cloudEventDistrubtedTracingEnricherPolicy.js");
const tracing_js_1 = require("./tracing.js");
const core_util_1 = require("@azure/core-util");
const core_rest_pipeline_1 = require("@azure/core-rest-pipeline");
/**
* Client class for publishing events to the Event Grid Service.
*/
class EventGridPublisherClient {
/**
* Creates an instance of EventGridPublisherClient which sends events using the Event Grid Schema.
*
* Example usage:
* ```ts snippet:ReadmeSampleCreateClient_KeyCredential
* import { EventGridPublisherClient, AzureKeyCredential } from "@azure/eventgrid";
*
* const client = new EventGridPublisherClient(
* "<endpoint>",
* "EventGrid",
* new AzureKeyCredential("<Access Key>"),
* );
* ```
*
* @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.
*/
constructor(endpointUrl, inputSchema, credential, options = {}) {
this.endpointUrl = endpointUrl;
this.inputSchema = inputSchema;
this.client = new generatedClient_js_1.GeneratedClient(options);
const authPolicy = (0, core_auth_1.isTokenCredential)(credential)
? (0, core_rest_pipeline_1.bearerTokenAuthenticationPolicy)({ credential, scopes: constants_js_1.DEFAULT_EVENTGRID_SCOPE })
: (0, eventGridAuthenticationPolicy_js_1.eventGridCredentialPolicy)(credential);
this.client.pipeline.addPolicy(authPolicy);
this.client.pipeline.addPolicy((0, cloudEventDistrubtedTracingEnricherPolicy_js_1.cloudEventDistributedTracingEnricherPolicy)(), {
afterPolicies: [core_rest_pipeline_1.tracingPolicyName],
});
this.apiVersion = this.client.apiVersion;
}
/**
* Sends events to a topic.
*
* @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.
*/
send(events, options = {}) {
return tracing_js_1.tracingClient.withSpan("EventGridPublisherClient.send", options, (updatedOptions) => {
switch (this.inputSchema) {
case "EventGrid": {
return this.client.publishEventGridEvents(this.endpointUrl, events.map(convertEventGridEventToModelType), updatedOptions);
}
case "CloudEvent": {
// The underlying REST API expects a header named `aeg-channel-name`, and so the generated client
// expects that options bag has a property called `aegChannelName`, where as we expose it with the
// friendlier name "channelName". Fix up the impedence mismatch here
const _a = updatedOptions, { channelName } = _a, sendOptions = tslib_1.__rest(_a, ["channelName"]);
if (channelName) {
sendOptions.aegChannelName = channelName;
}
return this.client.publishCloudEventEvents(this.endpointUrl, events.map(convertCloudEventToModelType), sendOptions);
}
case "Custom": {
return this.client.publishCustomEventEvents(this.endpointUrl, events, updatedOptions);
}
default: {
throw new Error(`Unknown input schema type '${this.inputSchema}'`);
}
}
});
}
}
exports.EventGridPublisherClient = EventGridPublisherClient;
/**
* @internal
*/
function convertEventGridEventToModelType(event) {
var _a, _b;
return {
eventType: event.eventType,
eventTime: (_a = event.eventTime) !== null && _a !== void 0 ? _a : new Date(),
id: (_b = event.id) !== null && _b !== void 0 ? _b : (0, core_util_1.randomUUID)(),
subject: event.subject,
topic: event.topic,
data: event.data,
dataVersion: event.dataVersion,
};
}
/**
* @internal
*/
function convertCloudEventToModelType(event) {
var _a, _b, _c, _d;
if (event.extensionAttributes) {
for (const propName in event.extensionAttributes) {
// Per the cloud events spec: "CloudEvents attribute names MUST consist of lower-case letters ('a' to 'z') or digits ('0' to '9') from the ASCII character set"
// they also can not match an existing defined property name.
if (!/^[a-z0-9]*$/.test(propName) ||
models_js_1.cloudEventReservedPropertyNames.indexOf(propName) !== -1) {
throw new Error(`invalid extension attribute name: ${propName}`);
}
}
}
const converted = Object.assign({ specversion: "1.0", type: event.type, source: event.source, id: (_a = event.id) !== null && _a !== void 0 ? _a : (0, core_util_1.randomUUID)(), time: (_b = event.time) !== null && _b !== void 0 ? _b : new Date(), subject: event.subject, dataschema: event.dataschema }, ((_c = event.extensionAttributes) !== null && _c !== void 0 ? _c : []));
if (event.data instanceof Uint8Array) {
if (!event.datacontenttype) {
throw new Error("a data content type must be provided when sending an event with binary data");
}
converted.datacontenttype = event.datacontenttype;
converted.dataBase64 = event.data;
}
else {
converted.datacontenttype = (_d = event.datacontenttype) !== null && _d !== void 0 ? _d : "application/json";
converted.data = event.data;
}
return converted;
}
//# sourceMappingURL=eventGridClient.js.map