@azure/eventgrid 5.10.0-alpha.20250131.1 → 5.10.0-alpha.20250204.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
- The MIT License (MIT)
1
+ Copyright (c) Microsoft Corporation.
2
2
 
3
- Copyright (c) 2020 Microsoft
3
+ MIT License
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
@@ -12,7 +12,7 @@ furnished to do so, subject to the following conditions:
12
12
  The above copyright notice and this permission notice shall be included in all
13
13
  copies or substantial portions of the Software.
14
14
 
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
16
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
17
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
18
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
package/README.md CHANGED
@@ -72,12 +72,12 @@ az eventgrid topic key list --resource-group <your-resource-group-name> --name <
72
72
 
73
73
  Once you have an API key and endpoint, you can use the `AzureKeyCredential` class to authenticate the client as follows:
74
74
 
75
- ```js
76
- const { EventGridPublisherClient, AzureKeyCredential } = require("@azure/eventgrid");
75
+ ```ts snippet:ReadmeSampleCreateClient_KeyCredential
76
+ import { EventGridPublisherClient, AzureKeyCredential } from "@azure/eventgrid";
77
77
 
78
78
  const client = new EventGridPublisherClient(
79
79
  "<endpoint>",
80
- "<endpoint schema>",
80
+ "EventGrid",
81
81
  new AzureKeyCredential("<Access Key>"),
82
82
  );
83
83
  ```
@@ -86,20 +86,20 @@ const client = new EventGridPublisherClient(
86
86
 
87
87
  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:
88
88
 
89
- ```js
90
- const { EventGridPublisherClient, AzureSASCredential } = require("@azure/eventgrid");
89
+ ```ts snippet:ReadmeSampleCreateClient_SASCredential
90
+ import { EventGridPublisherClient, AzureSASCredential } from "@azure/eventgrid";
91
91
 
92
92
  const client = new EventGridPublisherClient(
93
93
  "<endpoint>",
94
- "<endpoint schema>",
94
+ "EventGrid",
95
95
  new AzureSASCredential("<SAS Token>"),
96
96
  );
97
97
  ```
98
98
 
99
99
  You can generate a SAS token by using the `generateSharedAccessSigniture` function.
100
100
 
101
- ```js
102
- const { generateSharedAccessSignature, AzureKeyCredential } = require("@azure/eventgrid");
101
+ ```ts snippet:ReadmeSample_GenerateSasToken
102
+ import { generateSharedAccessSignature, AzureKeyCredential } from "@azure/eventgrid";
103
103
 
104
104
  // Create a SAS Token which expires on 2020-01-01 at Midnight.
105
105
  const token = generateSharedAccessSignature(
@@ -119,13 +119,13 @@ With the `@azure/identity` package, you can seamlessly authorize requests in bot
119
119
 
120
120
  For example, use can use `DefaultAzureCredential` to construct a client which will authenticate using Azure Active Directory:
121
121
 
122
- ```js
123
- const { EventGridPublisherClient } = require("@azure/eventgrid");
124
- const { DefaultAzureCredential } = require("@azure/identity");
122
+ ```ts snippet:ReadmeSampleCreateClient_TokenCredential
123
+ import { EventGridPublisherClient } from "@azure/eventgrid";
124
+ import { DefaultAzureCredential } from "@azure/identity";
125
125
 
126
126
  const client = new EventGridPublisherClient(
127
127
  "<endpoint>",
128
- "<endpoint schema>",
128
+ "CloudEvent",
129
129
  new DefaultAzureCredential(),
130
130
  );
131
131
  ```
@@ -142,7 +142,9 @@ Event Grid supports multiple schemas for encoding events. When a Custom Topic or
142
142
 
143
143
  If your topic is configured to use the Event Grid Schema, set "EventGrid" as the schema type:
144
144
 
145
- ```js
145
+ ```ts snippet:ReadmeSampleCreateClient_EventGrid
146
+ import { EventGridPublisherClient, AzureKeyCredential } from "@azure/eventgrid";
147
+
146
148
  const client = new EventGridPublisherClient(
147
149
  "<endpoint>",
148
150
  "EventGrid",
@@ -152,7 +154,9 @@ const client = new EventGridPublisherClient(
152
154
 
153
155
  If your topic is configured to use the Cloud Event Schema, set "CloudEvent" as the schema type:
154
156
 
155
- ```js
157
+ ```ts snippet:ReadmeSampleCreateClient_CloudEvent
158
+ import { EventGridPublisherClient, AzureKeyCredential } from "@azure/eventgrid";
159
+
156
160
  const client = new EventGridPublisherClient(
157
161
  "<endpoint>",
158
162
  "CloudEvent",
@@ -162,7 +166,9 @@ const client = new EventGridPublisherClient(
162
166
 
163
167
  If your topic is configured to use a Custom Event Schema, set "Custom" as the schema type:
164
168
 
165
- ```js
169
+ ```ts snippet:ReadmeSampleCreateClient_Custom
170
+ import { EventGridPublisherClient, AzureKeyCredential } from "@azure/eventgrid";
171
+
166
172
  const client = new EventGridPublisherClient(
167
173
  "<endpoint>",
168
174
  "Custom",
@@ -203,8 +209,8 @@ This library has been tested and validated on [Kubernetes using Azure Arc][event
203
209
 
204
210
  ### Publish a Custom Event to an Event Grid Topic using the Event Grid Schema
205
211
 
206
- ```js
207
- const { EventGridPublisherClient, AzureKeyCredential } = require("@azure/eventgrid");
212
+ ```ts snippet:ReadmeSample_PublishCustomEvent
213
+ import { EventGridPublisherClient, AzureKeyCredential } from "@azure/eventgrid";
208
214
 
209
215
  const client = new EventGridPublisherClient(
210
216
  "<endpoint>",
@@ -228,8 +234,8 @@ await client.send([
228
234
 
229
235
  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:
230
236
 
231
- ```js
232
- const { EventGridPublisherClient, AzureKeyCredential } = require("@azure/eventgrid");
237
+ ```ts snippet:ReadmeSample_PublishCustomEventToDomain
238
+ import { EventGridPublisherClient, AzureKeyCredential } from "@azure/eventgrid";
233
239
 
234
240
  const client = new EventGridPublisherClient(
235
241
  "<endpoint>",
@@ -254,36 +260,31 @@ await client.send([
254
260
 
255
261
  `EventGridDeserializer` can be used to deserialize events delivered by Event Grid. In this example we have a cloud event that is deserialized using `EventGridDeserializer` and use `isSystemEvent` to detect what type of events they are.
256
262
 
257
- ```js
258
- const { EventGridDeserializer, isSystemEvent } = require("@azure/eventgrid");
259
-
260
- async function main() {
261
- const deserializer = new EventGridDeserializer();
262
- const message = {
263
- id: "5bc888aa-c2f4-11ea-b3de-0242ac130004",
264
- source:
265
- "/subscriptions/<subscriptionid>/resourceGroups/dummy-rg/providers/Microsoft.EventGrid/topics/dummy-topic",
266
- specversion: "1.0",
267
- type: "Microsoft.ContainerRegistry.ImagePushed",
268
- subject: "Test Subject",
269
- time: "2020-07-10T21:27:12.925Z",
270
- data: {
271
- hello: "world",
272
- },
273
- };
274
- const deserializedMessage = await deserializer.deserializeCloudEvents(message);
275
- console.log(deserializedMessage);
276
-
277
- if (
278
- deserializedMessage != null &&
279
- deserializedMessage.length !== 0 &&
280
- isSystemEvent("Microsoft.ContainerRegistry.ImagePushed", deserializedMessage[0])
281
- ) {
282
- console.log("This is a Microsoft.ContainerRegistry.ImagePushed event");
283
- }
263
+ ```ts snippet:ReadmeSample_DeserializeEvent
264
+ import { EventGridDeserializer, isSystemEvent } from "@azure/eventgrid";
265
+
266
+ const deserializer = new EventGridDeserializer();
267
+ const message = {
268
+ id: "5bc888aa-c2f4-11ea-b3de-0242ac130004",
269
+ source:
270
+ "/subscriptions/<subscriptionid>/resourceGroups/dummy-rg/providers/Microsoft.EventGrid/topics/dummy-topic",
271
+ specversion: "1.0",
272
+ type: "Microsoft.ContainerRegistry.ImagePushed",
273
+ subject: "Test Subject",
274
+ time: "2020-07-10T21:27:12.925Z",
275
+ data: {
276
+ hello: "world",
277
+ },
278
+ };
279
+ const deserializedMessage = await deserializer.deserializeCloudEvents(message);
280
+ console.log(deserializedMessage);
281
+ if (
282
+ deserializedMessage != null &&
283
+ deserializedMessage.length !== 0 &&
284
+ isSystemEvent("Microsoft.ContainerRegistry.ImagePushed", deserializedMessage[0])
285
+ ) {
286
+ console.log("This is a Microsoft.ContainerRegistry.ImagePushed event");
284
287
  }
285
-
286
- main();
287
288
  ```
288
289
 
289
290
  ## Troubleshooting
@@ -292,8 +293,8 @@ main();
292
293
 
293
294
  Enabling logging may help uncover useful information about failures. In order to see a log of HTTP requests and responses, set the `AZURE_LOG_LEVEL` environment variable to `info`. Alternatively, logging can be enabled at runtime by calling `setLogLevel` in the `@azure/logger`:
294
295
 
295
- ```javascript
296
- const { setLogLevel } = require("@azure/logger");
296
+ ```ts snippet:SetLogLevel
297
+ import { setLogLevel } from "@azure/logger";
297
298
 
298
299
  setLogLevel("info");
299
300
  ```
@@ -82,13 +82,13 @@ export declare class EventGridPublisherClient<T extends InputSchema> {
82
82
  * Creates an instance of EventGridPublisherClient which sends events using the Event Grid Schema.
83
83
  *
84
84
  * Example usage:
85
- * ```ts
85
+ * ```ts snippet:ReadmeSampleCreateClient_KeyCredential
86
86
  * import { EventGridPublisherClient, AzureKeyCredential } from "@azure/eventgrid";
87
87
  *
88
88
  * const client = new EventGridPublisherClient(
89
- * "<service endpoint>",
90
- * "EventGrid",
91
- * new AzureKeyCredential("<api key>")
89
+ * "<endpoint>",
90
+ * "EventGrid",
91
+ * new AzureKeyCredential("<Access Key>"),
92
92
  * );
93
93
  * ```
94
94
  *
@@ -18,13 +18,13 @@ export class EventGridPublisherClient {
18
18
  * Creates an instance of EventGridPublisherClient which sends events using the Event Grid Schema.
19
19
  *
20
20
  * Example usage:
21
- * ```ts
21
+ * ```ts snippet:ReadmeSampleCreateClient_KeyCredential
22
22
  * import { EventGridPublisherClient, AzureKeyCredential } from "@azure/eventgrid";
23
23
  *
24
24
  * const client = new EventGridPublisherClient(
25
- * "<service endpoint>",
26
- * "EventGrid",
27
- * new AzureKeyCredential("<api key>")
25
+ * "<endpoint>",
26
+ * "EventGrid",
27
+ * new AzureKeyCredential("<Access Key>"),
28
28
  * );
29
29
  * ```
30
30
  *
@@ -1 +1 @@
1
- {"version":3,"file":"eventGridClient.js","sourceRoot":"","sources":["../../src/eventGridClient.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC;;AAGlC,OAAO,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AAErD,OAAO,EAAE,yBAAyB,EAAE,MAAM,oCAAoC,CAAC;AAC/E,OAAO,EAAE,uBAAuB,EAAE,MAAM,gBAAgB,CAAC;AAEzD,OAAO,EAAE,+BAA+B,EAAE,MAAM,aAAa,CAAC;AAC9D,OAAO,EAAE,eAAe,EAAE,MAAM,gCAAgC,CAAC;AAMjE,OAAO,EAAE,0CAA0C,EAAE,MAAM,gDAAgD,CAAC;AAC5G,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAC7C,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAE9C,OAAO,EAAE,+BAA+B,EAAE,iBAAiB,EAAE,MAAM,2BAA2B,CAAC;AA+D/F;;GAEG;AACH,MAAM,OAAO,wBAAwB;IAqBnC;;;;;;;;;;;;;;;;;;OAkBG;IACH,YACE,WAAmB,EACnB,WAAc,EACd,UAA2D,EAC3D,UAA2C,EAAE;QAE7C,IAAI,CAAC,WAAW,GAAG,WAAW,CAAC;QAC/B,IAAI,CAAC,WAAW,GAAG,WAAW,CAAC;QAE/B,IAAI,CAAC,MAAM,GAAG,IAAI,eAAe,CAAC,OAAO,CAAC,CAAC;QAE3C,MAAM,UAAU,GAAG,iBAAiB,CAAC,UAAU,CAAC;YAC9C,CAAC,CAAC,+BAA+B,CAAC,EAAE,UAAU,EAAE,MAAM,EAAE,uBAAuB,EAAE,CAAC;YAClF,CAAC,CAAC,yBAAyB,CAAC,UAAU,CAAC,CAAC;QAE1C,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,SAAS,CAAC,UAAU,CA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AK,CAAC,UAAU,IACzB,CAAC,MAAA,KAAK,CAAC,mBAAmB,mCAAI,EAAE,CAAC,CACrC,CAAC;IAEF,IAAI,KAAK,CAAC,IAAI,YAAY,UAAU,EAAE,CAAC;QACrC,IAAI,CAAC,KAAK,CAAC,eAAe,EAAE,CAAC;YAC3B,MAAM,IAAI,KAAK,CACb,6EAA6E,CAC9E,CAAC;QACJ,CAAC;QAED,SAAS,CAAC,eAAe,GAAG,KAAK,CAAC,eAAe,CAAC;QAClD,SAAS,CAAC,UAAU,GAAG,KAAK,CAAC,IAAI,CAAC;IACpC,CAAC;SAAM,CAAC;QACN,SAAS,CAAC,eAAe,GAAG,MAAA,KAAK,CAAC,eAAe,mCAAI,kBAAkB,CAAC;QACxE,SAAS,CAAC,IAAI,GAAG,KAAK,CAAC,IAAI,CAAC;IAC9B,CAAC;IAED,OAAO,SAAS,CAAC;AACnB,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT License.\n\nimport type { KeyCredential, SASCredential } from \"@azure/core-auth\";\nimport { isTokenCredential } from \"@azure/core-auth\";\nimport type { OperationOptions, CommonClientOptions } from \"@azure/core-client\";\nimport { eventGridCredentialPolicy } from \"./eventGridAuthenticationPolicy.js\";\nimport { DEFAULT_EVENTGRID_SCOPE } from \"./constants.js\";\nimport type { SendCloudEventInput, SendEventGridEventInput } from \"./models.js\";\nimport { cloudEventReservedPropertyNames } from \"./models.js\";\nimport { GeneratedClient } from \"./generated/generatedClient.js\";\nimport type {\n CloudEvent as CloudEventWireModel,\n EventGridEvent as EventGridEventWireModel,\n GeneratedClientPublishCloudEventEventsOptionalParams,\n} from \"./generated/models/index.js\";\nimport { cloudEventDistributedTracingEnricherPolicy } from \"./cloudEventDistrubtedTracingEnricherPolicy.js\";\nimport { tracingClient } from \"./tracing.js\";\nimport { randomUUID } from \"@azure/core-util\";\nimport type { TokenCredential } from \"@azure/core-auth\";\nimport { bearerTokenAuthenticationPolicy, tracingPolicyName } from \"@azure/core-rest-pipeline\";\n\n/**\n * Options for the Event Grid Client.\n */\nexport type EventGridPublisherClientOptions = CommonClientOptions;\n\n/**\n * Options for the send events operation.\n */\nexport type SendOptions = OperationOptions;\n\n/**\n * Options for the send events operation, when the input schema is cloud event.\n */\nexport interface CloudEventSendOptions extends SendOptions {\n /**\n * The name of the channel to send the event to (only valid for Partner Namespaces and Topics).\n */\n channelName?: string;\n}\n\n/**\n * A map of input schema names to shapes of the input for the send method on EventGridPublisherClient.\n */\nexport interface InputSchemaToInputTypeMap {\n /**\n * The shape of the input to `send` when the client is configured to send events using the Event Grid schema.\n */\n EventGrid: SendEventGridEventInput<unknown>;\n /**\n * The shape of the input to `send` when the client is configured to send events using the Cloud Event schema.\n */\n CloudEvent: SendCloudEventInput<unknown>;\n /**\n * The shape of the input to `send` when the client is configured to send events using a custom schema.\n */\n Custom: Record<string, unknown>;\n}\n\n/**\n * A map of input schema names to shapes of the options bag for the send method on EventGridPublisherClient.\n */\nexport interface InputSchemaToOptionsTypeMap {\n /**\n * The shape of the options parameter for `send` when the client is configured to send events using the Event Grid schema.\n */\n EventGrid: SendOptions;\n /**\n * The shape of the options parameter for `send` when the client is configured to send events using the Cloud Event schema.\n */\n CloudEvent: CloudEventSendOptions;\n /**\n * The shape of the options parameter for `send` when the client is configured to send events using a custom schema.\n */\n Custom: SendOptions;\n}\n\n/**\n * Allowed schema types, to be used when constructing the EventGridPublisherClient.\n */\nexport type InputSchema = keyof InputSchemaToInputTypeMap;\n\n/**\n * Client class for publishing events to the Event Grid Service.\n */\nexport class EventGridPublisherClient<T extends InputSchema> {\n /**\n * The URL to the Event Grid endpoint.\n */\n public readonly endpointUrl: string;\n\n /**\n * The version of the Even Grid service.\n */\n public readonly apiVersion: string;\n\n /**\n * The AutoRest generated client for the EventGrid dataplane.\n */\n private readonly client: GeneratedClient;\n\n /**\n * The schema that will be used when sending events.\n */\n private readonly inputSchema: InputSchema;\n\n /**\n * Creates an instance of EventGridPublisherClient which sends events using the Event Grid Schema.\n *\n * Example usage:\n * ```ts\n * import { EventGridPublisherClient, AzureKeyCredential } from \"@azure/eventgrid\";\n *\n * const client = new EventGridPublisherClient(\n * \"<service endpoint>\",\n * \"EventGrid\",\n * new AzureKeyCredential(\"<api key>\")\n * );\n * ```\n *\n * @param endpointUrl - The URL to the Event Grid endpoint, e.g. https://eg-topic.westus2-1.eventgrid.azure.net/api/events.\n * @param inputSchema - The schema that the Event Grid endpoint is configured to accept. One of \"EventGrid\", \"CloudEvent\", or \"Custom\".\n * @param credential - Used to authenticate requests to the service.\n * @param options - Used to configure the Event Grid Client.\n */\n constructor(\n endpointUrl: string,\n inputSchema: T,\n credential: KeyCredential | SASCredential | TokenCredential,\n options: EventGridPublisherClientOptions = {},\n ) {\n this.endpointUrl = endpointUrl;\n this.inputSchema = inputSchema;\n\n this.client = new GeneratedClient(options);\n\n const authPolicy = isTokenCredential(credential)\n ? bearerTokenAuthenticationPolicy({ credential, scopes: DEFAULT_EVENTGRID_SCOPE })\n : eventGridCredentialPolicy(credential);\n\n this.client.pipeline.addPolicy(authPolicy);\n this.client.pipeline.addPolicy(cloudEventDistributedTracingEnricherPolicy(), {\n afterPolicies: [tracingPolicyName],\n });\n this.apiVersion = this.client.apiVersion;\n }\n\n /**\n * Sends events to a topic.\n *\n * @param events - The events to send. The events should be in the schema used when constructing the client.\n * @param options - Options to control the underlying operation.\n */\n send(\n events: InputSchemaToInputTypeMap[T][],\n options: InputSchemaToOptionsTypeMap[T] = {},\n ): Promise<void> {\n return tracingClient.withSpan(\"EventGridPublisherClient.send\", options, (updatedOptions) => {\n switch (this.inputSchema) {\n case \"EventGrid\": {\n return this.client.publishEventGridEvents(\n this.endpointUrl,\n (events as InputSchemaToInputTypeMap[\"EventGrid\"][]).map(\n convertEventGridEventToModelType,\n ),\n updatedOptions,\n );\n }\n case \"CloudEvent\": {\n // The underlying REST API expects a header named `aeg-channel-name`, and so the generated client\n // expects that options bag has a property called `aegChannelName`, where as we expose it with the\n // friendlier name \"channelName\". Fix up the impedence mismatch here\n const {\n channelName,\n ...sendOptions\n }: { channelName?: string } & GeneratedClientPublishCloudEventEventsOptionalParams =\n updatedOptions as CloudEventSendOptions;\n\n if (channelName) {\n sendOptions.aegChannelName = channelName;\n }\n\n return this.client.publishCloudEventEvents(\n this.endpointUrl,\n (events as InputSchemaToInputTypeMap[\"CloudEvent\"][]).map(convertCloudEventToModelType),\n sendOptions,\n );\n }\n case \"Custom\": {\n return this.client.publishCustomEventEvents(\n this.endpointUrl,\n events as InputSchemaToInputTypeMap[\"Custom\"][],\n updatedOptions,\n );\n }\n default: {\n throw new Error(`Unknown input schema type '${this.inputSchema}'`);\n }\n }\n });\n }\n}\n\n/**\n * @internal\n */\nexport function convertEventGridEventToModelType(\n event: SendEventGridEventInput<any>,\n): EventGridEventWireModel {\n return {\n eventType: event.eventType,\n eventTime: event.eventTime ?? new Date(),\n id: event.id ?? randomUUID(),\n subject: event.subject,\n topic: event.topic,\n data: event.data,\n dataVersion: event.dataVersion,\n };\n}\n\n/**\n * @internal\n */\nexport function convertCloudEventToModelType(event: SendCloudEventInput<any>): CloudEventWireModel {\n if (event.extensionAttributes) {\n for (const propName in event.extensionAttributes) {\n // 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\"\n // they also can not match an existing defined property name.\n\n if (\n !/^[a-z0-9]*$/.test(propName) ||\n cloudEventReservedPropertyNames.indexOf(propName) !== -1\n ) {\n throw new Error(`invalid extension attribute name: ${propName}`);\n }\n }\n }\n\n const converted: CloudEventWireModel = {\n specversion: \"1.0\",\n type: event.type,\n source: event.source,\n id: event.id ?? randomUUID(),\n time: event.time ?? new Date(),\n subject: event.subject,\n dataschema: event.dataschema,\n ...(event.extensionAttributes ?? []),\n };\n\n if (event.data instanceof Uint8Array) {\n if (!event.datacontenttype) {\n throw new Error(\n \"a data content type must be provided when sending an event with binary data\",\n );\n }\n\n converted.datacontenttype = event.datacontenttype;\n converted.dataBase64 = event.data;\n } else {\n converted.datacontenttype = event.datacontenttype ?? \"application/json\";\n converted.data = event.data;\n }\n\n return converted;\n}\n"]}
1
+ {"version":3,"file":"eventGridClient.js","sourceRoot":"","sources":["../../src/eventGridClient.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC;;AAGlC,OAAO,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AAErD,OAAO,EAAE,yBAAyB,EAAE,MAAM,oCAAoC,CAAC;AAC/E,OAAO,EAAE,uBAAuB,EAAE,MAAM,gBAAgB,CAAC;AAEzD,OAAO,EAAE,+BAA+B,EAAE,MAAM,aAAa,CAAC;AAC9D,OAAO,EAAE,eAAe,EAAE,MAAM,gCAAgC,CAAC;AAMjE,OAAO,EAAE,0CAA0C,EAAE,MAAM,gDAAgD,CAAC;AAC5G,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAC7C,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAE9C,OAAO,EAAE,+BAA+B,EAAE,iBAAiB,EAAE,MAAM,2BAA2B,CAAC;AA+D/F;;GAEG;AACH,MAAM,OAAO,wBAAwB;IAqBnC;;;;;;;;;;;;;;;;;;OAkBG;IACH,YACE,WAAmB,EACnB,WAAc,EACd,UAA2D,EAC3D,UAA2C,EAAE;QAE7C,IAAI,CAAC,WAAW,GAAG,WAAW,CAAC;QAC/B,IAAI,CAAC,WAAW,GAAG,WAAW,CAAC;QAE/B,IAAI,CAAC,MAAM,GAAG,IAAI,eAAe,CAAC,OAAO,CAAC,CAAC;QAE3C,MAAM,UAAU,GAAG,iBAAiB,CAAC,UAAU,CAAC;YAC9C,CAAC,CAAC,+BAA+B,CAAC,EAAE,UAAU,EAAE,MAAM,EAAE,uBAAuB,EAAE,CAAC;YAClF,CAAC,CAAC,yBAAyB,CAAC,UAAU,CAAC,CAAC;QAE1C,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,SAAS,CAAC,UAAU,CAAC,CA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AC,UAAU,IACzB,CAAC,MAAA,KAAK,CAAC,mBAAmB,mCAAI,EAAE,CAAC,CACrC,CAAC;IAEF,IAAI,KAAK,CAAC,IAAI,YAAY,UAAU,EAAE,CAAC;QACrC,IAAI,CAAC,KAAK,CAAC,eAAe,EAAE,CAAC;YAC3B,MAAM,IAAI,KAAK,CACb,6EAA6E,CAC9E,CAAC;QACJ,CAAC;QAED,SAAS,CAAC,eAAe,GAAG,KAAK,CAAC,eAAe,CAAC;QAClD,SAAS,CAAC,UAAU,GAAG,KAAK,CAAC,IAAI,CAAC;IACpC,CAAC;SAAM,CAAC;QACN,SAAS,CAAC,eAAe,GAAG,MAAA,KAAK,CAAC,eAAe,mCAAI,kBAAkB,CAAC;QACxE,SAAS,CAAC,IAAI,GAAG,KAAK,CAAC,IAAI,CAAC;IAC9B,CAAC;IAED,OAAO,SAAS,CAAC;AACnB,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT License.\n\nimport type { KeyCredential, SASCredential } from \"@azure/core-auth\";\nimport { isTokenCredential } from \"@azure/core-auth\";\nimport type { OperationOptions, CommonClientOptions } from \"@azure/core-client\";\nimport { eventGridCredentialPolicy } from \"./eventGridAuthenticationPolicy.js\";\nimport { DEFAULT_EVENTGRID_SCOPE } from \"./constants.js\";\nimport type { SendCloudEventInput, SendEventGridEventInput } from \"./models.js\";\nimport { cloudEventReservedPropertyNames } from \"./models.js\";\nimport { GeneratedClient } from \"./generated/generatedClient.js\";\nimport type {\n CloudEvent as CloudEventWireModel,\n EventGridEvent as EventGridEventWireModel,\n GeneratedClientPublishCloudEventEventsOptionalParams,\n} from \"./generated/models/index.js\";\nimport { cloudEventDistributedTracingEnricherPolicy } from \"./cloudEventDistrubtedTracingEnricherPolicy.js\";\nimport { tracingClient } from \"./tracing.js\";\nimport { randomUUID } from \"@azure/core-util\";\nimport type { TokenCredential } from \"@azure/core-auth\";\nimport { bearerTokenAuthenticationPolicy, tracingPolicyName } from \"@azure/core-rest-pipeline\";\n\n/**\n * Options for the Event Grid Client.\n */\nexport type EventGridPublisherClientOptions = CommonClientOptions;\n\n/**\n * Options for the send events operation.\n */\nexport type SendOptions = OperationOptions;\n\n/**\n * Options for the send events operation, when the input schema is cloud event.\n */\nexport interface CloudEventSendOptions extends SendOptions {\n /**\n * The name of the channel to send the event to (only valid for Partner Namespaces and Topics).\n */\n channelName?: string;\n}\n\n/**\n * A map of input schema names to shapes of the input for the send method on EventGridPublisherClient.\n */\nexport interface InputSchemaToInputTypeMap {\n /**\n * The shape of the input to `send` when the client is configured to send events using the Event Grid schema.\n */\n EventGrid: SendEventGridEventInput<unknown>;\n /**\n * The shape of the input to `send` when the client is configured to send events using the Cloud Event schema.\n */\n CloudEvent: SendCloudEventInput<unknown>;\n /**\n * The shape of the input to `send` when the client is configured to send events using a custom schema.\n */\n Custom: Record<string, unknown>;\n}\n\n/**\n * A map of input schema names to shapes of the options bag for the send method on EventGridPublisherClient.\n */\nexport interface InputSchemaToOptionsTypeMap {\n /**\n * The shape of the options parameter for `send` when the client is configured to send events using the Event Grid schema.\n */\n EventGrid: SendOptions;\n /**\n * The shape of the options parameter for `send` when the client is configured to send events using the Cloud Event schema.\n */\n CloudEvent: CloudEventSendOptions;\n /**\n * The shape of the options parameter for `send` when the client is configured to send events using a custom schema.\n */\n Custom: SendOptions;\n}\n\n/**\n * Allowed schema types, to be used when constructing the EventGridPublisherClient.\n */\nexport type InputSchema = keyof InputSchemaToInputTypeMap;\n\n/**\n * Client class for publishing events to the Event Grid Service.\n */\nexport class EventGridPublisherClient<T extends InputSchema> {\n /**\n * The URL to the Event Grid endpoint.\n */\n public readonly endpointUrl: string;\n\n /**\n * The version of the Even Grid service.\n */\n public readonly apiVersion: string;\n\n /**\n * The AutoRest generated client for the EventGrid dataplane.\n */\n private readonly client: GeneratedClient;\n\n /**\n * The schema that will be used when sending events.\n */\n private readonly inputSchema: InputSchema;\n\n /**\n * Creates an instance of EventGridPublisherClient which sends events using the Event Grid Schema.\n *\n * Example usage:\n * ```ts snippet:ReadmeSampleCreateClient_KeyCredential\n * import { EventGridPublisherClient, AzureKeyCredential } from \"@azure/eventgrid\";\n *\n * const client = new EventGridPublisherClient(\n * \"<endpoint>\",\n * \"EventGrid\",\n * new AzureKeyCredential(\"<Access Key>\"),\n * );\n * ```\n *\n * @param endpointUrl - The URL to the Event Grid endpoint, e.g. https://eg-topic.westus2-1.eventgrid.azure.net/api/events.\n * @param inputSchema - The schema that the Event Grid endpoint is configured to accept. One of \"EventGrid\", \"CloudEvent\", or \"Custom\".\n * @param credential - Used to authenticate requests to the service.\n * @param options - Used to configure the Event Grid Client.\n */\n constructor(\n endpointUrl: string,\n inputSchema: T,\n credential: KeyCredential | SASCredential | TokenCredential,\n options: EventGridPublisherClientOptions = {},\n ) {\n this.endpointUrl = endpointUrl;\n this.inputSchema = inputSchema;\n\n this.client = new GeneratedClient(options);\n\n const authPolicy = isTokenCredential(credential)\n ? bearerTokenAuthenticationPolicy({ credential, scopes: DEFAULT_EVENTGRID_SCOPE })\n : eventGridCredentialPolicy(credential);\n\n this.client.pipeline.addPolicy(authPolicy);\n this.client.pipeline.addPolicy(cloudEventDistributedTracingEnricherPolicy(), {\n afterPolicies: [tracingPolicyName],\n });\n this.apiVersion = this.client.apiVersion;\n }\n\n /**\n * Sends events to a topic.\n *\n * @param events - The events to send. The events should be in the schema used when constructing the client.\n * @param options - Options to control the underlying operation.\n */\n send(\n events: InputSchemaToInputTypeMap[T][],\n options: InputSchemaToOptionsTypeMap[T] = {},\n ): Promise<void> {\n return tracingClient.withSpan(\"EventGridPublisherClient.send\", options, (updatedOptions) => {\n switch (this.inputSchema) {\n case \"EventGrid\": {\n return this.client.publishEventGridEvents(\n this.endpointUrl,\n (events as InputSchemaToInputTypeMap[\"EventGrid\"][]).map(\n convertEventGridEventToModelType,\n ),\n updatedOptions,\n );\n }\n case \"CloudEvent\": {\n // The underlying REST API expects a header named `aeg-channel-name`, and so the generated client\n // expects that options bag has a property called `aegChannelName`, where as we expose it with the\n // friendlier name \"channelName\". Fix up the impedence mismatch here\n const {\n channelName,\n ...sendOptions\n }: { channelName?: string } & GeneratedClientPublishCloudEventEventsOptionalParams =\n updatedOptions as CloudEventSendOptions;\n\n if (channelName) {\n sendOptions.aegChannelName = channelName;\n }\n\n return this.client.publishCloudEventEvents(\n this.endpointUrl,\n (events as InputSchemaToInputTypeMap[\"CloudEvent\"][]).map(convertCloudEventToModelType),\n sendOptions,\n );\n }\n case \"Custom\": {\n return this.client.publishCustomEventEvents(\n this.endpointUrl,\n events as InputSchemaToInputTypeMap[\"Custom\"][],\n updatedOptions,\n );\n }\n default: {\n throw new Error(`Unknown input schema type '${this.inputSchema}'`);\n }\n }\n });\n }\n}\n\n/**\n * @internal\n */\nexport function convertEventGridEventToModelType(\n event: SendEventGridEventInput<any>,\n): EventGridEventWireModel {\n return {\n eventType: event.eventType,\n eventTime: event.eventTime ?? new Date(),\n id: event.id ?? randomUUID(),\n subject: event.subject,\n topic: event.topic,\n data: event.data,\n dataVersion: event.dataVersion,\n };\n}\n\n/**\n * @internal\n */\nexport function convertCloudEventToModelType(event: SendCloudEventInput<any>): CloudEventWireModel {\n if (event.extensionAttributes) {\n for (const propName in event.extensionAttributes) {\n // 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\"\n // they also can not match an existing defined property name.\n\n if (\n !/^[a-z0-9]*$/.test(propName) ||\n cloudEventReservedPropertyNames.indexOf(propName) !== -1\n ) {\n throw new Error(`invalid extension attribute name: ${propName}`);\n }\n }\n }\n\n const converted: CloudEventWireModel = {\n specversion: \"1.0\",\n type: event.type,\n source: event.source,\n id: event.id ?? randomUUID(),\n time: event.time ?? new Date(),\n subject: event.subject,\n dataschema: event.dataschema,\n ...(event.extensionAttributes ?? []),\n };\n\n if (event.data instanceof Uint8Array) {\n if (!event.datacontenttype) {\n throw new Error(\n \"a data content type must be provided when sending an event with binary data\",\n );\n }\n\n converted.datacontenttype = event.datacontenttype;\n converted.dataBase64 = event.data;\n } else {\n converted.datacontenttype = event.datacontenttype ?? \"application/json\";\n converted.data = event.data;\n }\n\n return converted;\n}\n"]}
@@ -82,13 +82,13 @@ export declare class EventGridPublisherClient<T extends InputSchema> {
82
82
  * Creates an instance of EventGridPublisherClient which sends events using the Event Grid Schema.
83
83
  *
84
84
  * Example usage:
85
- * ```ts
85
+ * ```ts snippet:ReadmeSampleCreateClient_KeyCredential
86
86
  * import { EventGridPublisherClient, AzureKeyCredential } from "@azure/eventgrid";
87
87
  *
88
88
  * const client = new EventGridPublisherClient(
89
- * "<service endpoint>",
90
- * "EventGrid",
91
- * new AzureKeyCredential("<api key>")
89
+ * "<endpoint>",
90
+ * "EventGrid",
91
+ * new AzureKeyCredential("<Access Key>"),
92
92
  * );
93
93
  * ```
94
94
  *
@@ -23,13 +23,13 @@ class EventGridPublisherClient {
23
23
  * Creates an instance of EventGridPublisherClient which sends events using the Event Grid Schema.
24
24
  *
25
25
  * Example usage:
26
- * ```ts
26
+ * ```ts snippet:ReadmeSampleCreateClient_KeyCredential
27
27
  * import { EventGridPublisherClient, AzureKeyCredential } from "@azure/eventgrid";
28
28
  *
29
29
  * const client = new EventGridPublisherClient(
30
- * "<service endpoint>",
31
- * "EventGrid",
32
- * new AzureKeyCredential("<api key>")
30
+ * "<endpoint>",
31
+ * "EventGrid",
32
+ * new AzureKeyCredential("<Access Key>"),
33
33
  * );
34
34
  * ```
35
35
  *
@@ -1 +1 @@
1
- {"version":3,"file":"eventGridClient.js","sourceRoot":"","sources":["../../src/eventGridClient.ts"],"names":[],"mappings":";AAAA,uCAAuC;AACvC,kCAAkC;;;AA8MlC,4EAYC;AAKD,oEAyCC;;AArQD,gDAAqD;AAErD,yFAA+E;AAC/E,iDAAyD;AAEzD,2CAA8D;AAC9D,uEAAiE;AAMjE,iHAA4G;AAC5G,6CAA6C;AAC7C,gDAA8C;AAE9C,kEAA+F;AA+D/F;;GAEG;AACH,MAAa,wBAAwB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/J,6DAA6D;YAE7D,IACE,CAAC,aAAa,CAAC,IAAI,CAAC,QAAQ,CAAC;gBAC7B,2CAA+B,CAAC,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,EACxD,CAAC;gBACD,MAAM,IAAI,KAAK,CAAC,qCAAqC,QAAQ,EAAE,CAAC,CAAC;YACnE,CAAC;QACH,CAAC;IACH,CAAC;IAED,MAAM,SAAS,mBACb,WAAW,EAAE,KAAK,EAClB,IAAI,EAAE,KAAK,CAAC,IAAI,EAChB,MAAM,EAAE,KAAK,CAAC,MAAM,EACpB,EAAE,EAAE,MAAA,KAAK,CAAC,EAAE,mCAAI,IAAA,sBAAU,GAAE,EAC5B,IAAI,EAAE,MAAA,KAAK,CAAC,IAAI,mCAAI,IAAI,IAAI,EAAE,EAC9B,OAAO,EAAE,KAAK,CAAC,OAAO,EACtB,UAAU,EAAE,KAAK,CAAC,UAAU,IACzB,CAAC,MAAA,KAAK,CAAC,mBAAmB,mCAAI,EAAE,CAAC,CACrC,CAAC;IAEF,IAAI,KAAK,CAAC,IAAI,YAAY,UAAU,EAAE,CAAC;QACrC,IAAI,CAAC,KAAK,CAAC,eAAe,EAAE,CAAC;YAC3B,MAAM,IAAI,KAAK,CACb,6EAA6E,CAC9E,CAAC;QACJ,CAAC;QAED,SAAS,CAAC,eAAe,GAAG,KAAK,CAAC,eAAe,CAAC;QAClD,SAAS,CAAC,UAAU,GAAG,KAAK,CAAC,IAAI,CAAC;IACpC,CAAC;SAAM,CAAC;QACN,SAAS,CAAC,eAAe,GAAG,MAAA,KAAK,CAAC,eAAe,mCAAI,kBAAkB,CAAC;QACxE,SAAS,CAAC,IAAI,GAAG,KAAK,CAAC,IAAI,CAAC;IAC9B,CAAC;IAED,OAAO,SAAS,CAAC;AACnB,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT License.\n\nimport type { KeyCredential, SASCredential } from \"@azure/core-auth\";\nimport { isTokenCredential } from \"@azure/core-auth\";\nimport type { OperationOptions, CommonClientOptions } from \"@azure/core-client\";\nimport { eventGridCredentialPolicy } from \"./eventGridAuthenticationPolicy.js\";\nimport { DEFAULT_EVENTGRID_SCOPE } from \"./constants.js\";\nimport type { SendCloudEventInput, SendEventGridEventInput } from \"./models.js\";\nimport { cloudEventReservedPropertyNames } from \"./models.js\";\nimport { GeneratedClient } from \"./generated/generatedClient.js\";\nimport type {\n CloudEvent as CloudEventWireModel,\n EventGridEvent as EventGridEventWireModel,\n GeneratedClientPublishCloudEventEventsOptionalParams,\n} from \"./generated/models/index.js\";\nimport { cloudEventDistributedTracingEnricherPolicy } from \"./cloudEventDistrubtedTracingEnricherPolicy.js\";\nimport { tracingClient } from \"./tracing.js\";\nimport { randomUUID } from \"@azure/core-util\";\nimport type { TokenCredential } from \"@azure/core-auth\";\nimport { bearerTokenAuthenticationPolicy, tracingPolicyName } from \"@azure/core-rest-pipeline\";\n\n/**\n * Options for the Event Grid Client.\n */\nexport type EventGridPublisherClientOptions = CommonClientOptions;\n\n/**\n * Options for the send events operation.\n */\nexport type SendOptions = OperationOptions;\n\n/**\n * Options for the send events operation, when the input schema is cloud event.\n */\nexport interface CloudEventSendOptions extends SendOptions {\n /**\n * The name of the channel to send the event to (only valid for Partner Namespaces and Topics).\n */\n channelName?: string;\n}\n\n/**\n * A map of input schema names to shapes of the input for the send method on EventGridPublisherClient.\n */\nexport interface InputSchemaToInputTypeMap {\n /**\n * The shape of the input to `send` when the client is configured to send events using the Event Grid schema.\n */\n EventGrid: SendEventGridEventInput<unknown>;\n /**\n * The shape of the input to `send` when the client is configured to send events using the Cloud Event schema.\n */\n CloudEvent: SendCloudEventInput<unknown>;\n /**\n * The shape of the input to `send` when the client is configured to send events using a custom schema.\n */\n Custom: Record<string, unknown>;\n}\n\n/**\n * A map of input schema names to shapes of the options bag for the send method on EventGridPublisherClient.\n */\nexport interface InputSchemaToOptionsTypeMap {\n /**\n * The shape of the options parameter for `send` when the client is configured to send events using the Event Grid schema.\n */\n EventGrid: SendOptions;\n /**\n * The shape of the options parameter for `send` when the client is configured to send events using the Cloud Event schema.\n */\n CloudEvent: CloudEventSendOptions;\n /**\n * The shape of the options parameter for `send` when the client is configured to send events using a custom schema.\n */\n Custom: SendOptions;\n}\n\n/**\n * Allowed schema types, to be used when constructing the EventGridPublisherClient.\n */\nexport type InputSchema = keyof InputSchemaToInputTypeMap;\n\n/**\n * Client class for publishing events to the Event Grid Service.\n */\nexport class EventGridPublisherClient<T extends InputSchema> {\n /**\n * The URL to the Event Grid endpoint.\n */\n public readonly endpointUrl: string;\n\n /**\n * The version of the Even Grid service.\n */\n public readonly apiVersion: string;\n\n /**\n * The AutoRest generated client for the EventGrid dataplane.\n */\n private readonly client: GeneratedClient;\n\n /**\n * The schema that will be used when sending events.\n */\n private readonly inputSchema: InputSchema;\n\n /**\n * Creates an instance of EventGridPublisherClient which sends events using the Event Grid Schema.\n *\n * Example usage:\n * ```ts\n * import { EventGridPublisherClient, AzureKeyCredential } from \"@azure/eventgrid\";\n *\n * const client = new EventGridPublisherClient(\n * \"<service endpoint>\",\n * \"EventGrid\",\n * new AzureKeyCredential(\"<api key>\")\n * );\n * ```\n *\n * @param endpointUrl - The URL to the Event Grid endpoint, e.g. https://eg-topic.westus2-1.eventgrid.azure.net/api/events.\n * @param inputSchema - The schema that the Event Grid endpoint is configured to accept. One of \"EventGrid\", \"CloudEvent\", or \"Custom\".\n * @param credential - Used to authenticate requests to the service.\n * @param options - Used to configure the Event Grid Client.\n */\n constructor(\n endpointUrl: string,\n inputSchema: T,\n credential: KeyCredential | SASCredential | TokenCredential,\n options: EventGridPublisherClientOptions = {},\n ) {\n this.endpointUrl = endpointUrl;\n this.inputSchema = inputSchema;\n\n this.client = new GeneratedClient(options);\n\n const authPolicy = isTokenCredential(credential)\n ? bearerTokenAuthenticationPolicy({ credential, scopes: DEFAULT_EVENTGRID_SCOPE })\n : eventGridCredentialPolicy(credential);\n\n this.client.pipeline.addPolicy(authPolicy);\n this.client.pipeline.addPolicy(cloudEventDistributedTracingEnricherPolicy(), {\n afterPolicies: [tracingPolicyName],\n });\n this.apiVersion = this.client.apiVersion;\n }\n\n /**\n * Sends events to a topic.\n *\n * @param events - The events to send. The events should be in the schema used when constructing the client.\n * @param options - Options to control the underlying operation.\n */\n send(\n events: InputSchemaToInputTypeMap[T][],\n options: InputSchemaToOptionsTypeMap[T] = {},\n ): Promise<void> {\n return tracingClient.withSpan(\"EventGridPublisherClient.send\", options, (updatedOptions) => {\n switch (this.inputSchema) {\n case \"EventGrid\": {\n return this.client.publishEventGridEvents(\n this.endpointUrl,\n (events as InputSchemaToInputTypeMap[\"EventGrid\"][]).map(\n convertEventGridEventToModelType,\n ),\n updatedOptions,\n );\n }\n case \"CloudEvent\": {\n // The underlying REST API expects a header named `aeg-channel-name`, and so the generated client\n // expects that options bag has a property called `aegChannelName`, where as we expose it with the\n // friendlier name \"channelName\". Fix up the impedence mismatch here\n const {\n channelName,\n ...sendOptions\n }: { channelName?: string } & GeneratedClientPublishCloudEventEventsOptionalParams =\n updatedOptions as CloudEventSendOptions;\n\n if (channelName) {\n sendOptions.aegChannelName = channelName;\n }\n\n return this.client.publishCloudEventEvents(\n this.endpointUrl,\n (events as InputSchemaToInputTypeMap[\"CloudEvent\"][]).map(convertCloudEventToModelType),\n sendOptions,\n );\n }\n case \"Custom\": {\n return this.client.publishCustomEventEvents(\n this.endpointUrl,\n events as InputSchemaToInputTypeMap[\"Custom\"][],\n updatedOptions,\n );\n }\n default: {\n throw new Error(`Unknown input schema type '${this.inputSchema}'`);\n }\n }\n });\n }\n}\n\n/**\n * @internal\n */\nexport function convertEventGridEventToModelType(\n event: SendEventGridEventInput<any>,\n): EventGridEventWireModel {\n return {\n eventType: event.eventType,\n eventTime: event.eventTime ?? new Date(),\n id: event.id ?? randomUUID(),\n subject: event.subject,\n topic: event.topic,\n data: event.data,\n dataVersion: event.dataVersion,\n };\n}\n\n/**\n * @internal\n */\nexport function convertCloudEventToModelType(event: SendCloudEventInput<any>): CloudEventWireModel {\n if (event.extensionAttributes) {\n for (const propName in event.extensionAttributes) {\n // 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\"\n // they also can not match an existing defined property name.\n\n if (\n !/^[a-z0-9]*$/.test(propName) ||\n cloudEventReservedPropertyNames.indexOf(propName) !== -1\n ) {\n throw new Error(`invalid extension attribute name: ${propName}`);\n }\n }\n }\n\n const converted: CloudEventWireModel = {\n specversion: \"1.0\",\n type: event.type,\n source: event.source,\n id: event.id ?? randomUUID(),\n time: event.time ?? new Date(),\n subject: event.subject,\n dataschema: event.dataschema,\n ...(event.extensionAttributes ?? []),\n };\n\n if (event.data instanceof Uint8Array) {\n if (!event.datacontenttype) {\n throw new Error(\n \"a data content type must be provided when sending an event with binary data\",\n );\n }\n\n converted.datacontenttype = event.datacontenttype;\n converted.dataBase64 = event.data;\n } else {\n converted.datacontenttype = event.datacontenttype ?? \"application/json\";\n converted.data = event.data;\n }\n\n return converted;\n}\n"]}
1
+ {"version":3,"file":"eventGridClient.js","sourceRoot":"","sources":["../../src/eventGridClient.ts"],"names":[],"mappings":";AAAA,uCAAuC;AACvC,kCAAkC;;;AA8MlC,4EAYC;AAKD,oEAyCC;;AArQD,gDAAqD;AAErD,yFAA+E;AAC/E,iDAAyD;AAEzD,2CAA8D;AAC9D,uEAAiE;AAMjE,iHAA4G;AAC5G,6CAA6C;AAC7C,gDAA8C;AAE9C,kEAA+F;AA+D/F;;GAEG;AACH,MAAa,wBAAwB;IAqBnC;;;;;;;;;;;;;;;;;;OAkBG;IACH,YACE,WAAmB,EACnB,WAAc,EACd,UAA2D,EAC3D,UAA2C,EAAE;QAE7C,IAAI,CAAC,WAAW,GAAG,WAAW,CAAC;QAC/B,IAAI,CAAC,WAAW,GAAG,WAAW,CAAC;QAE/B,IAAI,CAAC,MAAM,GAAG,IAAI,oCAAe,CAAC,OAAO,CAAC,CAAC;QAE3C,MAAM,UAAU,GAAG,IAA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,EAClB,IAAI,EAAE,KAAK,CAAC,IAAI,EAChB,MAAM,EAAE,KAAK,CAAC,MAAM,EACpB,EAAE,EAAE,MAAA,KAAK,CAAC,EAAE,mCAAI,IAAA,sBAAU,GAAE,EAC5B,IAAI,EAAE,MAAA,KAAK,CAAC,IAAI,mCAAI,IAAI,IAAI,EAAE,EAC9B,OAAO,EAAE,KAAK,CAAC,OAAO,EACtB,UAAU,EAAE,KAAK,CAAC,UAAU,IACzB,CAAC,MAAA,KAAK,CAAC,mBAAmB,mCAAI,EAAE,CAAC,CACrC,CAAC;IAEF,IAAI,KAAK,CAAC,IAAI,YAAY,UAAU,EAAE,CAAC;QACrC,IAAI,CAAC,KAAK,CAAC,eAAe,EAAE,CAAC;YAC3B,MAAM,IAAI,KAAK,CACb,6EAA6E,CAC9E,CAAC;QACJ,CAAC;QAED,SAAS,CAAC,eAAe,GAAG,KAAK,CAAC,eAAe,CAAC;QAClD,SAAS,CAAC,UAAU,GAAG,KAAK,CAAC,IAAI,CAAC;IACpC,CAAC;SAAM,CAAC;QACN,SAAS,CAAC,eAAe,GAAG,MAAA,KAAK,CAAC,eAAe,mCAAI,kBAAkB,CAAC;QACxE,SAAS,CAAC,IAAI,GAAG,KAAK,CAAC,IAAI,CAAC;IAC9B,CAAC;IAED,OAAO,SAAS,CAAC;AACnB,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT License.\n\nimport type { KeyCredential, SASCredential } from \"@azure/core-auth\";\nimport { isTokenCredential } from \"@azure/core-auth\";\nimport type { OperationOptions, CommonClientOptions } from \"@azure/core-client\";\nimport { eventGridCredentialPolicy } from \"./eventGridAuthenticationPolicy.js\";\nimport { DEFAULT_EVENTGRID_SCOPE } from \"./constants.js\";\nimport type { SendCloudEventInput, SendEventGridEventInput } from \"./models.js\";\nimport { cloudEventReservedPropertyNames } from \"./models.js\";\nimport { GeneratedClient } from \"./generated/generatedClient.js\";\nimport type {\n CloudEvent as CloudEventWireModel,\n EventGridEvent as EventGridEventWireModel,\n GeneratedClientPublishCloudEventEventsOptionalParams,\n} from \"./generated/models/index.js\";\nimport { cloudEventDistributedTracingEnricherPolicy } from \"./cloudEventDistrubtedTracingEnricherPolicy.js\";\nimport { tracingClient } from \"./tracing.js\";\nimport { randomUUID } from \"@azure/core-util\";\nimport type { TokenCredential } from \"@azure/core-auth\";\nimport { bearerTokenAuthenticationPolicy, tracingPolicyName } from \"@azure/core-rest-pipeline\";\n\n/**\n * Options for the Event Grid Client.\n */\nexport type EventGridPublisherClientOptions = CommonClientOptions;\n\n/**\n * Options for the send events operation.\n */\nexport type SendOptions = OperationOptions;\n\n/**\n * Options for the send events operation, when the input schema is cloud event.\n */\nexport interface CloudEventSendOptions extends SendOptions {\n /**\n * The name of the channel to send the event to (only valid for Partner Namespaces and Topics).\n */\n channelName?: string;\n}\n\n/**\n * A map of input schema names to shapes of the input for the send method on EventGridPublisherClient.\n */\nexport interface InputSchemaToInputTypeMap {\n /**\n * The shape of the input to `send` when the client is configured to send events using the Event Grid schema.\n */\n EventGrid: SendEventGridEventInput<unknown>;\n /**\n * The shape of the input to `send` when the client is configured to send events using the Cloud Event schema.\n */\n CloudEvent: SendCloudEventInput<unknown>;\n /**\n * The shape of the input to `send` when the client is configured to send events using a custom schema.\n */\n Custom: Record<string, unknown>;\n}\n\n/**\n * A map of input schema names to shapes of the options bag for the send method on EventGridPublisherClient.\n */\nexport interface InputSchemaToOptionsTypeMap {\n /**\n * The shape of the options parameter for `send` when the client is configured to send events using the Event Grid schema.\n */\n EventGrid: SendOptions;\n /**\n * The shape of the options parameter for `send` when the client is configured to send events using the Cloud Event schema.\n */\n CloudEvent: CloudEventSendOptions;\n /**\n * The shape of the options parameter for `send` when the client is configured to send events using a custom schema.\n */\n Custom: SendOptions;\n}\n\n/**\n * Allowed schema types, to be used when constructing the EventGridPublisherClient.\n */\nexport type InputSchema = keyof InputSchemaToInputTypeMap;\n\n/**\n * Client class for publishing events to the Event Grid Service.\n */\nexport class EventGridPublisherClient<T extends InputSchema> {\n /**\n * The URL to the Event Grid endpoint.\n */\n public readonly endpointUrl: string;\n\n /**\n * The version of the Even Grid service.\n */\n public readonly apiVersion: string;\n\n /**\n * The AutoRest generated client for the EventGrid dataplane.\n */\n private readonly client: GeneratedClient;\n\n /**\n * The schema that will be used when sending events.\n */\n private readonly inputSchema: InputSchema;\n\n /**\n * Creates an instance of EventGridPublisherClient which sends events using the Event Grid Schema.\n *\n * Example usage:\n * ```ts snippet:ReadmeSampleCreateClient_KeyCredential\n * import { EventGridPublisherClient, AzureKeyCredential } from \"@azure/eventgrid\";\n *\n * const client = new EventGridPublisherClient(\n * \"<endpoint>\",\n * \"EventGrid\",\n * new AzureKeyCredential(\"<Access Key>\"),\n * );\n * ```\n *\n * @param endpointUrl - The URL to the Event Grid endpoint, e.g. https://eg-topic.westus2-1.eventgrid.azure.net/api/events.\n * @param inputSchema - The schema that the Event Grid endpoint is configured to accept. One of \"EventGrid\", \"CloudEvent\", or \"Custom\".\n * @param credential - Used to authenticate requests to the service.\n * @param options - Used to configure the Event Grid Client.\n */\n constructor(\n endpointUrl: string,\n inputSchema: T,\n credential: KeyCredential | SASCredential | TokenCredential,\n options: EventGridPublisherClientOptions = {},\n ) {\n this.endpointUrl = endpointUrl;\n this.inputSchema = inputSchema;\n\n this.client = new GeneratedClient(options);\n\n const authPolicy = isTokenCredential(credential)\n ? bearerTokenAuthenticationPolicy({ credential, scopes: DEFAULT_EVENTGRID_SCOPE })\n : eventGridCredentialPolicy(credential);\n\n this.client.pipeline.addPolicy(authPolicy);\n this.client.pipeline.addPolicy(cloudEventDistributedTracingEnricherPolicy(), {\n afterPolicies: [tracingPolicyName],\n });\n this.apiVersion = this.client.apiVersion;\n }\n\n /**\n * Sends events to a topic.\n *\n * @param events - The events to send. The events should be in the schema used when constructing the client.\n * @param options - Options to control the underlying operation.\n */\n send(\n events: InputSchemaToInputTypeMap[T][],\n options: InputSchemaToOptionsTypeMap[T] = {},\n ): Promise<void> {\n return tracingClient.withSpan(\"EventGridPublisherClient.send\", options, (updatedOptions) => {\n switch (this.inputSchema) {\n case \"EventGrid\": {\n return this.client.publishEventGridEvents(\n this.endpointUrl,\n (events as InputSchemaToInputTypeMap[\"EventGrid\"][]).map(\n convertEventGridEventToModelType,\n ),\n updatedOptions,\n );\n }\n case \"CloudEvent\": {\n // The underlying REST API expects a header named `aeg-channel-name`, and so the generated client\n // expects that options bag has a property called `aegChannelName`, where as we expose it with the\n // friendlier name \"channelName\". Fix up the impedence mismatch here\n const {\n channelName,\n ...sendOptions\n }: { channelName?: string } & GeneratedClientPublishCloudEventEventsOptionalParams =\n updatedOptions as CloudEventSendOptions;\n\n if (channelName) {\n sendOptions.aegChannelName = channelName;\n }\n\n return this.client.publishCloudEventEvents(\n this.endpointUrl,\n (events as InputSchemaToInputTypeMap[\"CloudEvent\"][]).map(convertCloudEventToModelType),\n sendOptions,\n );\n }\n case \"Custom\": {\n return this.client.publishCustomEventEvents(\n this.endpointUrl,\n events as InputSchemaToInputTypeMap[\"Custom\"][],\n updatedOptions,\n );\n }\n default: {\n throw new Error(`Unknown input schema type '${this.inputSchema}'`);\n }\n }\n });\n }\n}\n\n/**\n * @internal\n */\nexport function convertEventGridEventToModelType(\n event: SendEventGridEventInput<any>,\n): EventGridEventWireModel {\n return {\n eventType: event.eventType,\n eventTime: event.eventTime ?? new Date(),\n id: event.id ?? randomUUID(),\n subject: event.subject,\n topic: event.topic,\n data: event.data,\n dataVersion: event.dataVersion,\n };\n}\n\n/**\n * @internal\n */\nexport function convertCloudEventToModelType(event: SendCloudEventInput<any>): CloudEventWireModel {\n if (event.extensionAttributes) {\n for (const propName in event.extensionAttributes) {\n // 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\"\n // they also can not match an existing defined property name.\n\n if (\n !/^[a-z0-9]*$/.test(propName) ||\n cloudEventReservedPropertyNames.indexOf(propName) !== -1\n ) {\n throw new Error(`invalid extension attribute name: ${propName}`);\n }\n }\n }\n\n const converted: CloudEventWireModel = {\n specversion: \"1.0\",\n type: event.type,\n source: event.source,\n id: event.id ?? randomUUID(),\n time: event.time ?? new Date(),\n subject: event.subject,\n dataschema: event.dataschema,\n ...(event.extensionAttributes ?? []),\n };\n\n if (event.data instanceof Uint8Array) {\n if (!event.datacontenttype) {\n throw new Error(\n \"a data content type must be provided when sending an event with binary data\",\n );\n }\n\n converted.datacontenttype = event.datacontenttype;\n converted.dataBase64 = event.data;\n } else {\n converted.datacontenttype = event.datacontenttype ?? \"application/json\";\n converted.data = event.data;\n }\n\n return converted;\n}\n"]}
@@ -5,7 +5,7 @@
5
5
  "toolPackages": [
6
6
  {
7
7
  "packageName": "@microsoft/api-extractor",
8
- "packageVersion": "7.49.1"
8
+ "packageVersion": "7.49.2"
9
9
  }
10
10
  ]
11
11
  }
@@ -82,13 +82,13 @@ export declare class EventGridPublisherClient<T extends InputSchema> {
82
82
  * Creates an instance of EventGridPublisherClient which sends events using the Event Grid Schema.
83
83
  *
84
84
  * Example usage:
85
- * ```ts
85
+ * ```ts snippet:ReadmeSampleCreateClient_KeyCredential
86
86
  * import { EventGridPublisherClient, AzureKeyCredential } from "@azure/eventgrid";
87
87
  *
88
88
  * const client = new EventGridPublisherClient(
89
- * "<service endpoint>",
90
- * "EventGrid",
91
- * new AzureKeyCredential("<api key>")
89
+ * "<endpoint>",
90
+ * "EventGrid",
91
+ * new AzureKeyCredential("<Access Key>"),
92
92
  * );
93
93
  * ```
94
94
  *
@@ -18,13 +18,13 @@ export class EventGridPublisherClient {
18
18
  * Creates an instance of EventGridPublisherClient which sends events using the Event Grid Schema.
19
19
  *
20
20
  * Example usage:
21
- * ```ts
21
+ * ```ts snippet:ReadmeSampleCreateClient_KeyCredential
22
22
  * import { EventGridPublisherClient, AzureKeyCredential } from "@azure/eventgrid";
23
23
  *
24
24
  * const client = new EventGridPublisherClient(
25
- * "<service endpoint>",
26
- * "EventGrid",
27
- * new AzureKeyCredential("<api key>")
25
+ * "<endpoint>",
26
+ * "EventGrid",
27
+ * new AzureKeyCredential("<Access Key>"),
28
28
  * );
29
29
  * ```
30
30
  *
@@ -1 +1 @@
1
- {"version":3,"file":"eventGridClient.js","sourceRoot":"","sources":["../../src/eventGridClient.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC;;AAGlC,OAAO,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AAErD,OAAO,EAAE,yBAAyB,EAAE,MAAM,oCAAoC,CAAC;AAC/E,OAAO,EAAE,uBAAuB,EAAE,MAAM,gBAAgB,CAAC;AAEzD,OAAO,EAAE,+BAA+B,EAAE,MAAM,aAAa,CAAC;AAC9D,OAAO,EAAE,eAAe,EAAE,MAAM,gCAAgC,CAAC;AAMjE,OAAO,EAAE,0CAA0C,EAAE,MAAM,gDAAgD,CAAC;AAC5G,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAC7C,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAE9C,OAAO,EAAE,+BAA+B,EAAE,iBAAiB,EAAE,MAAM,2BAA2B,CAAC;AA+D/F;;GAEG;AACH,MAAM,OAAO,wBAAwB;IAqBnC;;;;;;;;;;;;;;;;;;OAkBG;IACH,YACE,WAAmB,EACnB,WAAc,EACd,UAA2D,EAC3D,UAA2C,EAAE;QAE7C,IAAI,CAAC,WAAW,GAAG,WAAW,CAAC;QAC/B,IAAI,CAAC,WAAW,GAAG,WAAW,CAAC;QAE/B,IAAI,CAAC,MAAM,GAAG,IAAI,eAAe,CAAC,OAAO,CAAC,CAAC;QAE3C,MAAM,UAAU,GAAG,iBAAiB,CAAC,UAAU,CAAC;YAC9C,CAAC,CAAC,+BAA+B,CAAC,EAAE,UAAU,EAAE,MAAM,EAAE,uBAAuB,EAAE,CAAC;YAClF,CAAC,CAAC,yBAAyB,CAAC,UAAU,CAAC,CAAC;QAE1C,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,SAAS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,UAAU,EAAE,KAAK,CAAC,UAAU,IACzB,CAAC,MAAA,KAAK,CAAC,mBAAmB,mCAAI,EAAE,CAAC,CACrC,CAAC;IAEF,IAAI,KAAK,CAAC,IAAI,YAAY,UAAU,EAAE,CAAC;QACrC,IAAI,CAAC,KAAK,CAAC,eAAe,EAAE,CAAC;YAC3B,MAAM,IAAI,KAAK,CACb,6EAA6E,CAC9E,CAAC;QACJ,CAAC;QAED,SAAS,CAAC,eAAe,GAAG,KAAK,CAAC,eAAe,CAAC;QAClD,SAAS,CAAC,UAAU,GAAG,KAAK,CAAC,IAAI,CAAC;IACpC,CAAC;SAAM,CAAC;QACN,SAAS,CAAC,eAAe,GAAG,MAAA,KAAK,CAAC,eAAe,mCAAI,kBAAkB,CAAC;QACxE,SAAS,CAAC,IAAI,GAAG,KAAK,CAAC,IAAI,CAAC;IAC9B,CAAC;IAED,OAAO,SAAS,CAAC;AACnB,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT License.\n\nimport type { KeyCredential, SASCredential } from \"@azure/core-auth\";\nimport { isTokenCredential } from \"@azure/core-auth\";\nimport type { OperationOptions, CommonClientOptions } from \"@azure/core-client\";\nimport { eventGridCredentialPolicy } from \"./eventGridAuthenticationPolicy.js\";\nimport { DEFAULT_EVENTGRID_SCOPE } from \"./constants.js\";\nimport type { SendCloudEventInput, SendEventGridEventInput } from \"./models.js\";\nimport { cloudEventReservedPropertyNames } from \"./models.js\";\nimport { GeneratedClient } from \"./generated/generatedClient.js\";\nimport type {\n CloudEvent as CloudEventWireModel,\n EventGridEvent as EventGridEventWireModel,\n GeneratedClientPublishCloudEventEventsOptionalParams,\n} from \"./generated/models/index.js\";\nimport { cloudEventDistributedTracingEnricherPolicy } from \"./cloudEventDistrubtedTracingEnricherPolicy.js\";\nimport { tracingClient } from \"./tracing.js\";\nimport { randomUUID } from \"@azure/core-util\";\nimport type { TokenCredential } from \"@azure/core-auth\";\nimport { bearerTokenAuthenticationPolicy, tracingPolicyName } from \"@azure/core-rest-pipeline\";\n\n/**\n * Options for the Event Grid Client.\n */\nexport type EventGridPublisherClientOptions = CommonClientOptions;\n\n/**\n * Options for the send events operation.\n */\nexport type SendOptions = OperationOptions;\n\n/**\n * Options for the send events operation, when the input schema is cloud event.\n */\nexport interface CloudEventSendOptions extends SendOptions {\n /**\n * The name of the channel to send the event to (only valid for Partner Namespaces and Topics).\n */\n channelName?: string;\n}\n\n/**\n * A map of input schema names to shapes of the input for the send method on EventGridPublisherClient.\n */\nexport interface InputSchemaToInputTypeMap {\n /**\n * The shape of the input to `send` when the client is configured to send events using the Event Grid schema.\n */\n EventGrid: SendEventGridEventInput<unknown>;\n /**\n * The shape of the input to `send` when the client is configured to send events using the Cloud Event schema.\n */\n CloudEvent: SendCloudEventInput<unknown>;\n /**\n * The shape of the input to `send` when the client is configured to send events using a custom schema.\n */\n Custom: Record<string, unknown>;\n}\n\n/**\n * A map of input schema names to shapes of the options bag for the send method on EventGridPublisherClient.\n */\nexport interface InputSchemaToOptionsTypeMap {\n /**\n * The shape of the options parameter for `send` when the client is configured to send events using the Event Grid schema.\n */\n EventGrid: SendOptions;\n /**\n * The shape of the options parameter for `send` when the client is configured to send events using the Cloud Event schema.\n */\n CloudEvent: CloudEventSendOptions;\n /**\n * The shape of the options parameter for `send` when the client is configured to send events using a custom schema.\n */\n Custom: SendOptions;\n}\n\n/**\n * Allowed schema types, to be used when constructing the EventGridPublisherClient.\n */\nexport type InputSchema = keyof InputSchemaToInputTypeMap;\n\n/**\n * Client class for publishing events to the Event Grid Service.\n */\nexport class EventGridPublisherClient<T extends InputSchema> {\n /**\n * The URL to the Event Grid endpoint.\n */\n public readonly endpointUrl: string;\n\n /**\n * The version of the Even Grid service.\n */\n public readonly apiVersion: string;\n\n /**\n * The AutoRest generated client for the EventGrid dataplane.\n */\n private readonly client: GeneratedClient;\n\n /**\n * The schema that will be used when sending events.\n */\n private readonly inputSchema: InputSchema;\n\n /**\n * Creates an instance of EventGridPublisherClient which sends events using the Event Grid Schema.\n *\n * Example usage:\n * ```ts\n * import { EventGridPublisherClient, AzureKeyCredential } from \"@azure/eventgrid\";\n *\n * const client = new EventGridPublisherClient(\n * \"<service endpoint>\",\n * \"EventGrid\",\n * new AzureKeyCredential(\"<api key>\")\n * );\n * ```\n *\n * @param endpointUrl - The URL to the Event Grid endpoint, e.g. https://eg-topic.westus2-1.eventgrid.azure.net/api/events.\n * @param inputSchema - The schema that the Event Grid endpoint is configured to accept. One of \"EventGrid\", \"CloudEvent\", or \"Custom\".\n * @param credential - Used to authenticate requests to the service.\n * @param options - Used to configure the Event Grid Client.\n */\n constructor(\n endpointUrl: string,\n inputSchema: T,\n credential: KeyCredential | SASCredential | TokenCredential,\n options: EventGridPublisherClientOptions = {},\n ) {\n this.endpointUrl = endpointUrl;\n this.inputSchema = inputSchema;\n\n this.client = new GeneratedClient(options);\n\n const authPolicy = isTokenCredential(credential)\n ? bearerTokenAuthenticationPolicy({ credential, scopes: DEFAULT_EVENTGRID_SCOPE })\n : eventGridCredentialPolicy(credential);\n\n this.client.pipeline.addPolicy(authPolicy);\n this.client.pipeline.addPolicy(cloudEventDistributedTracingEnricherPolicy(), {\n afterPolicies: [tracingPolicyName],\n });\n this.apiVersion = this.client.apiVersion;\n }\n\n /**\n * Sends events to a topic.\n *\n * @param events - The events to send. The events should be in the schema used when constructing the client.\n * @param options - Options to control the underlying operation.\n */\n send(\n events: InputSchemaToInputTypeMap[T][],\n options: InputSchemaToOptionsTypeMap[T] = {},\n ): Promise<void> {\n return tracingClient.withSpan(\"EventGridPublisherClient.send\", options, (updatedOptions) => {\n switch (this.inputSchema) {\n case \"EventGrid\": {\n return this.client.publishEventGridEvents(\n this.endpointUrl,\n (events as InputSchemaToInputTypeMap[\"EventGrid\"][]).map(\n convertEventGridEventToModelType,\n ),\n updatedOptions,\n );\n }\n case \"CloudEvent\": {\n // The underlying REST API expects a header named `aeg-channel-name`, and so the generated client\n // expects that options bag has a property called `aegChannelName`, where as we expose it with the\n // friendlier name \"channelName\". Fix up the impedence mismatch here\n const {\n channelName,\n ...sendOptions\n }: { channelName?: string } & GeneratedClientPublishCloudEventEventsOptionalParams =\n updatedOptions as CloudEventSendOptions;\n\n if (channelName) {\n sendOptions.aegChannelName = channelName;\n }\n\n return this.client.publishCloudEventEvents(\n this.endpointUrl,\n (events as InputSchemaToInputTypeMap[\"CloudEvent\"][]).map(convertCloudEventToModelType),\n sendOptions,\n );\n }\n case \"Custom\": {\n return this.client.publishCustomEventEvents(\n this.endpointUrl,\n events as InputSchemaToInputTypeMap[\"Custom\"][],\n updatedOptions,\n );\n }\n default: {\n throw new Error(`Unknown input schema type '${this.inputSchema}'`);\n }\n }\n });\n }\n}\n\n/**\n * @internal\n */\nexport function convertEventGridEventToModelType(\n event: SendEventGridEventInput<any>,\n): EventGridEventWireModel {\n return {\n eventType: event.eventType,\n eventTime: event.eventTime ?? new Date(),\n id: event.id ?? randomUUID(),\n subject: event.subject,\n topic: event.topic,\n data: event.data,\n dataVersion: event.dataVersion,\n };\n}\n\n/**\n * @internal\n */\nexport function convertCloudEventToModelType(event: SendCloudEventInput<any>): CloudEventWireModel {\n if (event.extensionAttributes) {\n for (const propName in event.extensionAttributes) {\n // 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\"\n // they also can not match an existing defined property name.\n\n if (\n !/^[a-z0-9]*$/.test(propName) ||\n cloudEventReservedPropertyNames.indexOf(propName) !== -1\n ) {\n throw new Error(`invalid extension attribute name: ${propName}`);\n }\n }\n }\n\n const converted: CloudEventWireModel = {\n specversion: \"1.0\",\n type: event.type,\n source: event.source,\n id: event.id ?? randomUUID(),\n time: event.time ?? new Date(),\n subject: event.subject,\n dataschema: event.dataschema,\n ...(event.extensionAttributes ?? []),\n };\n\n if (event.data instanceof Uint8Array) {\n if (!event.datacontenttype) {\n throw new Error(\n \"a data content type must be provided when sending an event with binary data\",\n );\n }\n\n converted.datacontenttype = event.datacontenttype;\n converted.dataBase64 = event.data;\n } else {\n converted.datacontenttype = event.datacontenttype ?? \"application/json\";\n converted.data = event.data;\n }\n\n return converted;\n}\n"]}
1
+ {"version":3,"file":"eventGridClient.js","sourceRoot":"","sources":["../../src/eventGridClient.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC;;AAGlC,OAAO,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AAErD,OAAO,EAAE,yBAAyB,EAAE,MAAM,oCAAoC,CAAC;AAC/E,OAAO,EAAE,uBAAuB,EAAE,MAAM,gBAAgB,CAAC;AAEzD,OAAO,EAAE,+BAA+B,EAAE,MAAM,aAAa,CAAC;AAC9D,OAAO,EAAE,eAAe,EAAE,MAAM,gCAAgC,CAAC;AAMjE,OAAO,EAAE,0CAA0C,EAAE,MAAM,gDAAgD,CAAC;AAC5G,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAC7C,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAE9C,OAAO,EAAE,+BAA+B,EAAE,iBAAiB,EAAE,MAAM,2BAA2B,CAAC;AA+D/F;;GAEG;AACH,MAAM,OAAO,wBAAwB;IAqBnC;;;;;;;;;;;;;;;;;;OAkBG;IACH,YACE,WAAmB,EACnB,WAAc,EACd,UAA2D,EAC3D,UAA2C,EAAE;QAE7C,IAAI,CAAC,WAAW,GAAG,WAAW,CAAC;QAC/B,IAAI,CAAC,WAAW,GAAG,WAAW,CAAC;QAE/B,IAAI,CAAC,MAAM,GAAG,IAAI,eAAe,CAAC,OAAO,CAAC,CAAC;QAE3C,MAAM,UAAU,GAAG,iBAAiB,CAAC,UAAU,CAAC;YAC9C,CAAC,CAAC,+BAA+B,CAAC,EAAE,UAAU,EAAE,MAAM,EAAE,uBAAuB,EAAE,CAAC;YAClF,CAAC,CAAC,yBAAyB,CAAC,UAAU,CAAC,CAAC;QAE1C,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,SAAS,CAAC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,EAAE,KAAK,CAAC,UAAU,IACzB,CAAC,MAAA,KAAK,CAAC,mBAAmB,mCAAI,EAAE,CAAC,CACrC,CAAC;IAEF,IAAI,KAAK,CAAC,IAAI,YAAY,UAAU,EAAE,CAAC;QACrC,IAAI,CAAC,KAAK,CAAC,eAAe,EAAE,CAAC;YAC3B,MAAM,IAAI,KAAK,CACb,6EAA6E,CAC9E,CAAC;QACJ,CAAC;QAED,SAAS,CAAC,eAAe,GAAG,KAAK,CAAC,eAAe,CAAC;QAClD,SAAS,CAAC,UAAU,GAAG,KAAK,CAAC,IAAI,CAAC;IACpC,CAAC;SAAM,CAAC;QACN,SAAS,CAAC,eAAe,GAAG,MAAA,KAAK,CAAC,eAAe,mCAAI,kBAAkB,CAAC;QACxE,SAAS,CAAC,IAAI,GAAG,KAAK,CAAC,IAAI,CAAC;IAC9B,CAAC;IAED,OAAO,SAAS,CAAC;AACnB,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT License.\n\nimport type { KeyCredential, SASCredential } from \"@azure/core-auth\";\nimport { isTokenCredential } from \"@azure/core-auth\";\nimport type { OperationOptions, CommonClientOptions } from \"@azure/core-client\";\nimport { eventGridCredentialPolicy } from \"./eventGridAuthenticationPolicy.js\";\nimport { DEFAULT_EVENTGRID_SCOPE } from \"./constants.js\";\nimport type { SendCloudEventInput, SendEventGridEventInput } from \"./models.js\";\nimport { cloudEventReservedPropertyNames } from \"./models.js\";\nimport { GeneratedClient } from \"./generated/generatedClient.js\";\nimport type {\n CloudEvent as CloudEventWireModel,\n EventGridEvent as EventGridEventWireModel,\n GeneratedClientPublishCloudEventEventsOptionalParams,\n} from \"./generated/models/index.js\";\nimport { cloudEventDistributedTracingEnricherPolicy } from \"./cloudEventDistrubtedTracingEnricherPolicy.js\";\nimport { tracingClient } from \"./tracing.js\";\nimport { randomUUID } from \"@azure/core-util\";\nimport type { TokenCredential } from \"@azure/core-auth\";\nimport { bearerTokenAuthenticationPolicy, tracingPolicyName } from \"@azure/core-rest-pipeline\";\n\n/**\n * Options for the Event Grid Client.\n */\nexport type EventGridPublisherClientOptions = CommonClientOptions;\n\n/**\n * Options for the send events operation.\n */\nexport type SendOptions = OperationOptions;\n\n/**\n * Options for the send events operation, when the input schema is cloud event.\n */\nexport interface CloudEventSendOptions extends SendOptions {\n /**\n * The name of the channel to send the event to (only valid for Partner Namespaces and Topics).\n */\n channelName?: string;\n}\n\n/**\n * A map of input schema names to shapes of the input for the send method on EventGridPublisherClient.\n */\nexport interface InputSchemaToInputTypeMap {\n /**\n * The shape of the input to `send` when the client is configured to send events using the Event Grid schema.\n */\n EventGrid: SendEventGridEventInput<unknown>;\n /**\n * The shape of the input to `send` when the client is configured to send events using the Cloud Event schema.\n */\n CloudEvent: SendCloudEventInput<unknown>;\n /**\n * The shape of the input to `send` when the client is configured to send events using a custom schema.\n */\n Custom: Record<string, unknown>;\n}\n\n/**\n * A map of input schema names to shapes of the options bag for the send method on EventGridPublisherClient.\n */\nexport interface InputSchemaToOptionsTypeMap {\n /**\n * The shape of the options parameter for `send` when the client is configured to send events using the Event Grid schema.\n */\n EventGrid: SendOptions;\n /**\n * The shape of the options parameter for `send` when the client is configured to send events using the Cloud Event schema.\n */\n CloudEvent: CloudEventSendOptions;\n /**\n * The shape of the options parameter for `send` when the client is configured to send events using a custom schema.\n */\n Custom: SendOptions;\n}\n\n/**\n * Allowed schema types, to be used when constructing the EventGridPublisherClient.\n */\nexport type InputSchema = keyof InputSchemaToInputTypeMap;\n\n/**\n * Client class for publishing events to the Event Grid Service.\n */\nexport class EventGridPublisherClient<T extends InputSchema> {\n /**\n * The URL to the Event Grid endpoint.\n */\n public readonly endpointUrl: string;\n\n /**\n * The version of the Even Grid service.\n */\n public readonly apiVersion: string;\n\n /**\n * The AutoRest generated client for the EventGrid dataplane.\n */\n private readonly client: GeneratedClient;\n\n /**\n * The schema that will be used when sending events.\n */\n private readonly inputSchema: InputSchema;\n\n /**\n * Creates an instance of EventGridPublisherClient which sends events using the Event Grid Schema.\n *\n * Example usage:\n * ```ts snippet:ReadmeSampleCreateClient_KeyCredential\n * import { EventGridPublisherClient, AzureKeyCredential } from \"@azure/eventgrid\";\n *\n * const client = new EventGridPublisherClient(\n * \"<endpoint>\",\n * \"EventGrid\",\n * new AzureKeyCredential(\"<Access Key>\"),\n * );\n * ```\n *\n * @param endpointUrl - The URL to the Event Grid endpoint, e.g. https://eg-topic.westus2-1.eventgrid.azure.net/api/events.\n * @param inputSchema - The schema that the Event Grid endpoint is configured to accept. One of \"EventGrid\", \"CloudEvent\", or \"Custom\".\n * @param credential - Used to authenticate requests to the service.\n * @param options - Used to configure the Event Grid Client.\n */\n constructor(\n endpointUrl: string,\n inputSchema: T,\n credential: KeyCredential | SASCredential | TokenCredential,\n options: EventGridPublisherClientOptions = {},\n ) {\n this.endpointUrl = endpointUrl;\n this.inputSchema = inputSchema;\n\n this.client = new GeneratedClient(options);\n\n const authPolicy = isTokenCredential(credential)\n ? bearerTokenAuthenticationPolicy({ credential, scopes: DEFAULT_EVENTGRID_SCOPE })\n : eventGridCredentialPolicy(credential);\n\n this.client.pipeline.addPolicy(authPolicy);\n this.client.pipeline.addPolicy(cloudEventDistributedTracingEnricherPolicy(), {\n afterPolicies: [tracingPolicyName],\n });\n this.apiVersion = this.client.apiVersion;\n }\n\n /**\n * Sends events to a topic.\n *\n * @param events - The events to send. The events should be in the schema used when constructing the client.\n * @param options - Options to control the underlying operation.\n */\n send(\n events: InputSchemaToInputTypeMap[T][],\n options: InputSchemaToOptionsTypeMap[T] = {},\n ): Promise<void> {\n return tracingClient.withSpan(\"EventGridPublisherClient.send\", options, (updatedOptions) => {\n switch (this.inputSchema) {\n case \"EventGrid\": {\n return this.client.publishEventGridEvents(\n this.endpointUrl,\n (events as InputSchemaToInputTypeMap[\"EventGrid\"][]).map(\n convertEventGridEventToModelType,\n ),\n updatedOptions,\n );\n }\n case \"CloudEvent\": {\n // The underlying REST API expects a header named `aeg-channel-name`, and so the generated client\n // expects that options bag has a property called `aegChannelName`, where as we expose it with the\n // friendlier name \"channelName\". Fix up the impedence mismatch here\n const {\n channelName,\n ...sendOptions\n }: { channelName?: string } & GeneratedClientPublishCloudEventEventsOptionalParams =\n updatedOptions as CloudEventSendOptions;\n\n if (channelName) {\n sendOptions.aegChannelName = channelName;\n }\n\n return this.client.publishCloudEventEvents(\n this.endpointUrl,\n (events as InputSchemaToInputTypeMap[\"CloudEvent\"][]).map(convertCloudEventToModelType),\n sendOptions,\n );\n }\n case \"Custom\": {\n return this.client.publishCustomEventEvents(\n this.endpointUrl,\n events as InputSchemaToInputTypeMap[\"Custom\"][],\n updatedOptions,\n );\n }\n default: {\n throw new Error(`Unknown input schema type '${this.inputSchema}'`);\n }\n }\n });\n }\n}\n\n/**\n * @internal\n */\nexport function convertEventGridEventToModelType(\n event: SendEventGridEventInput<any>,\n): EventGridEventWireModel {\n return {\n eventType: event.eventType,\n eventTime: event.eventTime ?? new Date(),\n id: event.id ?? randomUUID(),\n subject: event.subject,\n topic: event.topic,\n data: event.data,\n dataVersion: event.dataVersion,\n };\n}\n\n/**\n * @internal\n */\nexport function convertCloudEventToModelType(event: SendCloudEventInput<any>): CloudEventWireModel {\n if (event.extensionAttributes) {\n for (const propName in event.extensionAttributes) {\n // 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\"\n // they also can not match an existing defined property name.\n\n if (\n !/^[a-z0-9]*$/.test(propName) ||\n cloudEventReservedPropertyNames.indexOf(propName) !== -1\n ) {\n throw new Error(`invalid extension attribute name: ${propName}`);\n }\n }\n }\n\n const converted: CloudEventWireModel = {\n specversion: \"1.0\",\n type: event.type,\n source: event.source,\n id: event.id ?? randomUUID(),\n time: event.time ?? new Date(),\n subject: event.subject,\n dataschema: event.dataschema,\n ...(event.extensionAttributes ?? []),\n };\n\n if (event.data instanceof Uint8Array) {\n if (!event.datacontenttype) {\n throw new Error(\n \"a data content type must be provided when sending an event with binary data\",\n );\n }\n\n converted.datacontenttype = event.datacontenttype;\n converted.dataBase64 = event.data;\n } else {\n converted.datacontenttype = event.datacontenttype ?? \"application/json\";\n converted.data = event.data;\n }\n\n return converted;\n}\n"]}
@@ -82,13 +82,13 @@ export declare class EventGridPublisherClient<T extends InputSchema> {
82
82
  * Creates an instance of EventGridPublisherClient which sends events using the Event Grid Schema.
83
83
  *
84
84
  * Example usage:
85
- * ```ts
85
+ * ```ts snippet:ReadmeSampleCreateClient_KeyCredential
86
86
  * import { EventGridPublisherClient, AzureKeyCredential } from "@azure/eventgrid";
87
87
  *
88
88
  * const client = new EventGridPublisherClient(
89
- * "<service endpoint>",
90
- * "EventGrid",
91
- * new AzureKeyCredential("<api key>")
89
+ * "<endpoint>",
90
+ * "EventGrid",
91
+ * new AzureKeyCredential("<Access Key>"),
92
92
  * );
93
93
  * ```
94
94
  *
@@ -18,13 +18,13 @@ export class EventGridPublisherClient {
18
18
  * Creates an instance of EventGridPublisherClient which sends events using the Event Grid Schema.
19
19
  *
20
20
  * Example usage:
21
- * ```ts
21
+ * ```ts snippet:ReadmeSampleCreateClient_KeyCredential
22
22
  * import { EventGridPublisherClient, AzureKeyCredential } from "@azure/eventgrid";
23
23
  *
24
24
  * const client = new EventGridPublisherClient(
25
- * "<service endpoint>",
26
- * "EventGrid",
27
- * new AzureKeyCredential("<api key>")
25
+ * "<endpoint>",
26
+ * "EventGrid",
27
+ * new AzureKeyCredential("<Access Key>"),
28
28
  * );
29
29
  * ```
30
30
  *
@@ -1 +1 @@
1
- {"version":3,"file":"eventGridClient.js","sourceRoot":"","sources":["../../src/eventGridClient.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC;;AAGlC,OAAO,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AAErD,OAAO,EAAE,yBAAyB,EAAE,MAAM,oCAAoC,CAAC;AAC/E,OAAO,EAAE,uBAAuB,EAAE,MAAM,gBAAgB,CAAC;AAEzD,OAAO,EAAE,+BAA+B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SAAS,CAAC,eAAe,GAAG,MAAA,KAAK,CAAC,eAAe,mCAAI,kBAAkB,CAAC;QACxE,SAAS,CAAC,IAAI,GAAG,KAAK,CAAC,IAAI,CAAC;IAC9B,CAAC;IAED,OAAO,SAAS,CAAC;AACnB,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT License.\n\nimport type { KeyCredential, SASCredential } from \"@azure/core-auth\";\nimport { isTokenCredential } from \"@azure/core-auth\";\nimport type { OperationOptions, CommonClientOptions } from \"@azure/core-client\";\nimport { eventGridCredentialPolicy } from \"./eventGridAuthenticationPolicy.js\";\nimport { DEFAULT_EVENTGRID_SCOPE } from \"./constants.js\";\nimport type { SendCloudEventInput, SendEventGridEventInput } from \"./models.js\";\nimport { cloudEventReservedPropertyNames } from \"./models.js\";\nimport { GeneratedClient } from \"./generated/generatedClient.js\";\nimport type {\n CloudEvent as CloudEventWireModel,\n EventGridEvent as EventGridEventWireModel,\n GeneratedClientPublishCloudEventEventsOptionalParams,\n} from \"./generated/models/index.js\";\nimport { cloudEventDistributedTracingEnricherPolicy } from \"./cloudEventDistrubtedTracingEnricherPolicy.js\";\nimport { tracingClient } from \"./tracing.js\";\nimport { randomUUID } from \"@azure/core-util\";\nimport type { TokenCredential } from \"@azure/core-auth\";\nimport { bearerTokenAuthenticationPolicy, tracingPolicyName } from \"@azure/core-rest-pipeline\";\n\n/**\n * Options for the Event Grid Client.\n */\nexport type EventGridPublisherClientOptions = CommonClientOptions;\n\n/**\n * Options for the send events operation.\n */\nexport type SendOptions = OperationOptions;\n\n/**\n * Options for the send events operation, when the input schema is cloud event.\n */\nexport interface CloudEventSendOptions extends SendOptions {\n /**\n * The name of the channel to send the event to (only valid for Partner Namespaces and Topics).\n */\n channelName?: string;\n}\n\n/**\n * A map of input schema names to shapes of the input for the send method on EventGridPublisherClient.\n */\nexport interface InputSchemaToInputTypeMap {\n /**\n * The shape of the input to `send` when the client is configured to send events using the Event Grid schema.\n */\n EventGrid: SendEventGridEventInput<unknown>;\n /**\n * The shape of the input to `send` when the client is configured to send events using the Cloud Event schema.\n */\n CloudEvent: SendCloudEventInput<unknown>;\n /**\n * The shape of the input to `send` when the client is configured to send events using a custom schema.\n */\n Custom: Record<string, unknown>;\n}\n\n/**\n * A map of input schema names to shapes of the options bag for the send method on EventGridPublisherClient.\n */\nexport interface InputSchemaToOptionsTypeMap {\n /**\n * The shape of the options parameter for `send` when the client is configured to send events using the Event Grid schema.\n */\n EventGrid: SendOptions;\n /**\n * The shape of the options parameter for `send` when the client is configured to send events using the Cloud Event schema.\n */\n CloudEvent: CloudEventSendOptions;\n /**\n * The shape of the options parameter for `send` when the client is configured to send events using a custom schema.\n */\n Custom: SendOptions;\n}\n\n/**\n * Allowed schema types, to be used when constructing the EventGridPublisherClient.\n */\nexport type InputSchema = keyof InputSchemaToInputTypeMap;\n\n/**\n * Client class for publishing events to the Event Grid Service.\n */\nexport class EventGridPublisherClient<T extends InputSchema> {\n /**\n * The URL to the Event Grid endpoint.\n */\n public readonly endpointUrl: string;\n\n /**\n * The version of the Even Grid service.\n */\n public readonly apiVersion: string;\n\n /**\n * The AutoRest generated client for the EventGrid dataplane.\n */\n private readonly client: GeneratedClient;\n\n /**\n * The schema that will be used when sending events.\n */\n private readonly inputSchema: InputSchema;\n\n /**\n * Creates an instance of EventGridPublisherClient which sends events using the Event Grid Schema.\n *\n * Example usage:\n * ```ts\n * import { EventGridPublisherClient, AzureKeyCredential } from \"@azure/eventgrid\";\n *\n * const client = new EventGridPublisherClient(\n * \"<service endpoint>\",\n * \"EventGrid\",\n * new AzureKeyCredential(\"<api key>\")\n * );\n * ```\n *\n * @param endpointUrl - The URL to the Event Grid endpoint, e.g. https://eg-topic.westus2-1.eventgrid.azure.net/api/events.\n * @param inputSchema - The schema that the Event Grid endpoint is configured to accept. One of \"EventGrid\", \"CloudEvent\", or \"Custom\".\n * @param credential - Used to authenticate requests to the service.\n * @param options - Used to configure the Event Grid Client.\n */\n constructor(\n endpointUrl: string,\n inputSchema: T,\n credential: KeyCredential | SASCredential | TokenCredential,\n options: EventGridPublisherClientOptions = {},\n ) {\n this.endpointUrl = endpointUrl;\n this.inputSchema = inputSchema;\n\n this.client = new GeneratedClient(options);\n\n const authPolicy = isTokenCredential(credential)\n ? bearerTokenAuthenticationPolicy({ credential, scopes: DEFAULT_EVENTGRID_SCOPE })\n : eventGridCredentialPolicy(credential);\n\n this.client.pipeline.addPolicy(authPolicy);\n this.client.pipeline.addPolicy(cloudEventDistributedTracingEnricherPolicy(), {\n afterPolicies: [tracingPolicyName],\n });\n this.apiVersion = this.client.apiVersion;\n }\n\n /**\n * Sends events to a topic.\n *\n * @param events - The events to send. The events should be in the schema used when constructing the client.\n * @param options - Options to control the underlying operation.\n */\n send(\n events: InputSchemaToInputTypeMap[T][],\n options: InputSchemaToOptionsTypeMap[T] = {},\n ): Promise<void> {\n return tracingClient.withSpan(\"EventGridPublisherClient.send\", options, (updatedOptions) => {\n switch (this.inputSchema) {\n case \"EventGrid\": {\n return this.client.publishEventGridEvents(\n this.endpointUrl,\n (events as InputSchemaToInputTypeMap[\"EventGrid\"][]).map(\n convertEventGridEventToModelType,\n ),\n updatedOptions,\n );\n }\n case \"CloudEvent\": {\n // The underlying REST API expects a header named `aeg-channel-name`, and so the generated client\n // expects that options bag has a property called `aegChannelName`, where as we expose it with the\n // friendlier name \"channelName\". Fix up the impedence mismatch here\n const {\n channelName,\n ...sendOptions\n }: { channelName?: string } & GeneratedClientPublishCloudEventEventsOptionalParams =\n updatedOptions as CloudEventSendOptions;\n\n if (channelName) {\n sendOptions.aegChannelName = channelName;\n }\n\n return this.client.publishCloudEventEvents(\n this.endpointUrl,\n (events as InputSchemaToInputTypeMap[\"CloudEvent\"][]).map(convertCloudEventToModelType),\n sendOptions,\n );\n }\n case \"Custom\": {\n return this.client.publishCustomEventEvents(\n this.endpointUrl,\n events as InputSchemaToInputTypeMap[\"Custom\"][],\n updatedOptions,\n );\n }\n default: {\n throw new Error(`Unknown input schema type '${this.inputSchema}'`);\n }\n }\n });\n }\n}\n\n/**\n * @internal\n */\nexport function convertEventGridEventToModelType(\n event: SendEventGridEventInput<any>,\n): EventGridEventWireModel {\n return {\n eventType: event.eventType,\n eventTime: event.eventTime ?? new Date(),\n id: event.id ?? randomUUID(),\n subject: event.subject,\n topic: event.topic,\n data: event.data,\n dataVersion: event.dataVersion,\n };\n}\n\n/**\n * @internal\n */\nexport function convertCloudEventToModelType(event: SendCloudEventInput<any>): CloudEventWireModel {\n if (event.extensionAttributes) {\n for (const propName in event.extensionAttributes) {\n // 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\"\n // they also can not match an existing defined property name.\n\n if (\n !/^[a-z0-9]*$/.test(propName) ||\n cloudEventReservedPropertyNames.indexOf(propName) !== -1\n ) {\n throw new Error(`invalid extension attribute name: ${propName}`);\n }\n }\n }\n\n const converted: CloudEventWireModel = {\n specversion: \"1.0\",\n type: event.type,\n source: event.source,\n id: event.id ?? randomUUID(),\n time: event.time ?? new Date(),\n subject: event.subject,\n dataschema: event.dataschema,\n ...(event.extensionAttributes ?? []),\n };\n\n if (event.data instanceof Uint8Array) {\n if (!event.datacontenttype) {\n throw new Error(\n \"a data content type must be provided when sending an event with binary data\",\n );\n }\n\n converted.datacontenttype = event.datacontenttype;\n converted.dataBase64 = event.data;\n } else {\n converted.datacontenttype = event.datacontenttype ?? \"application/json\";\n converted.data = event.data;\n }\n\n return converted;\n}\n"]}
1
+ {"version":3,"file":"eventGridClient.js","sourceRoot":"","sources":["../../src/eventGridClient.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC;;AAGlC,OAAO,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AAErD,OAAO,EAAE,yBAAyB,EAAE,MAAM,oCAAoC,CAAC;AAC/E,OAAO,EAAE,uBAAuB,EAAE,MAAM,gBAAgB,CAAC;AAEzD,OAAO,EAAE,+BAA+B,EAAE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CAAC,eAAe,GAAG,MAAA,KAAK,CAAC,eAAe,mCAAI,kBAAkB,CAAC;QACxE,SAAS,CAAC,IAAI,GAAG,KAAK,CAAC,IAAI,CAAC;IAC9B,CAAC;IAED,OAAO,SAAS,CAAC;AACnB,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT License.\n\nimport type { KeyCredential, SASCredential } from \"@azure/core-auth\";\nimport { isTokenCredential } from \"@azure/core-auth\";\nimport type { OperationOptions, CommonClientOptions } from \"@azure/core-client\";\nimport { eventGridCredentialPolicy } from \"./eventGridAuthenticationPolicy.js\";\nimport { DEFAULT_EVENTGRID_SCOPE } from \"./constants.js\";\nimport type { SendCloudEventInput, SendEventGridEventInput } from \"./models.js\";\nimport { cloudEventReservedPropertyNames } from \"./models.js\";\nimport { GeneratedClient } from \"./generated/generatedClient.js\";\nimport type {\n CloudEvent as CloudEventWireModel,\n EventGridEvent as EventGridEventWireModel,\n GeneratedClientPublishCloudEventEventsOptionalParams,\n} from \"./generated/models/index.js\";\nimport { cloudEventDistributedTracingEnricherPolicy } from \"./cloudEventDistrubtedTracingEnricherPolicy.js\";\nimport { tracingClient } from \"./tracing.js\";\nimport { randomUUID } from \"@azure/core-util\";\nimport type { TokenCredential } from \"@azure/core-auth\";\nimport { bearerTokenAuthenticationPolicy, tracingPolicyName } from \"@azure/core-rest-pipeline\";\n\n/**\n * Options for the Event Grid Client.\n */\nexport type EventGridPublisherClientOptions = CommonClientOptions;\n\n/**\n * Options for the send events operation.\n */\nexport type SendOptions = OperationOptions;\n\n/**\n * Options for the send events operation, when the input schema is cloud event.\n */\nexport interface CloudEventSendOptions extends SendOptions {\n /**\n * The name of the channel to send the event to (only valid for Partner Namespaces and Topics).\n */\n channelName?: string;\n}\n\n/**\n * A map of input schema names to shapes of the input for the send method on EventGridPublisherClient.\n */\nexport interface InputSchemaToInputTypeMap {\n /**\n * The shape of the input to `send` when the client is configured to send events using the Event Grid schema.\n */\n EventGrid: SendEventGridEventInput<unknown>;\n /**\n * The shape of the input to `send` when the client is configured to send events using the Cloud Event schema.\n */\n CloudEvent: SendCloudEventInput<unknown>;\n /**\n * The shape of the input to `send` when the client is configured to send events using a custom schema.\n */\n Custom: Record<string, unknown>;\n}\n\n/**\n * A map of input schema names to shapes of the options bag for the send method on EventGridPublisherClient.\n */\nexport interface InputSchemaToOptionsTypeMap {\n /**\n * The shape of the options parameter for `send` when the client is configured to send events using the Event Grid schema.\n */\n EventGrid: SendOptions;\n /**\n * The shape of the options parameter for `send` when the client is configured to send events using the Cloud Event schema.\n */\n CloudEvent: CloudEventSendOptions;\n /**\n * The shape of the options parameter for `send` when the client is configured to send events using a custom schema.\n */\n Custom: SendOptions;\n}\n\n/**\n * Allowed schema types, to be used when constructing the EventGridPublisherClient.\n */\nexport type InputSchema = keyof InputSchemaToInputTypeMap;\n\n/**\n * Client class for publishing events to the Event Grid Service.\n */\nexport class EventGridPublisherClient<T extends InputSchema> {\n /**\n * The URL to the Event Grid endpoint.\n */\n public readonly endpointUrl: string;\n\n /**\n * The version of the Even Grid service.\n */\n public readonly apiVersion: string;\n\n /**\n * The AutoRest generated client for the EventGrid dataplane.\n */\n private readonly client: GeneratedClient;\n\n /**\n * The schema that will be used when sending events.\n */\n private readonly inputSchema: InputSchema;\n\n /**\n * Creates an instance of EventGridPublisherClient which sends events using the Event Grid Schema.\n *\n * Example usage:\n * ```ts snippet:ReadmeSampleCreateClient_KeyCredential\n * import { EventGridPublisherClient, AzureKeyCredential } from \"@azure/eventgrid\";\n *\n * const client = new EventGridPublisherClient(\n * \"<endpoint>\",\n * \"EventGrid\",\n * new AzureKeyCredential(\"<Access Key>\"),\n * );\n * ```\n *\n * @param endpointUrl - The URL to the Event Grid endpoint, e.g. https://eg-topic.westus2-1.eventgrid.azure.net/api/events.\n * @param inputSchema - The schema that the Event Grid endpoint is configured to accept. One of \"EventGrid\", \"CloudEvent\", or \"Custom\".\n * @param credential - Used to authenticate requests to the service.\n * @param options - Used to configure the Event Grid Client.\n */\n constructor(\n endpointUrl: string,\n inputSchema: T,\n credential: KeyCredential | SASCredential | TokenCredential,\n options: EventGridPublisherClientOptions = {},\n ) {\n this.endpointUrl = endpointUrl;\n this.inputSchema = inputSchema;\n\n this.client = new GeneratedClient(options);\n\n const authPolicy = isTokenCredential(credential)\n ? bearerTokenAuthenticationPolicy({ credential, scopes: DEFAULT_EVENTGRID_SCOPE })\n : eventGridCredentialPolicy(credential);\n\n this.client.pipeline.addPolicy(authPolicy);\n this.client.pipeline.addPolicy(cloudEventDistributedTracingEnricherPolicy(), {\n afterPolicies: [tracingPolicyName],\n });\n this.apiVersion = this.client.apiVersion;\n }\n\n /**\n * Sends events to a topic.\n *\n * @param events - The events to send. The events should be in the schema used when constructing the client.\n * @param options - Options to control the underlying operation.\n */\n send(\n events: InputSchemaToInputTypeMap[T][],\n options: InputSchemaToOptionsTypeMap[T] = {},\n ): Promise<void> {\n return tracingClient.withSpan(\"EventGridPublisherClient.send\", options, (updatedOptions) => {\n switch (this.inputSchema) {\n case \"EventGrid\": {\n return this.client.publishEventGridEvents(\n this.endpointUrl,\n (events as InputSchemaToInputTypeMap[\"EventGrid\"][]).map(\n convertEventGridEventToModelType,\n ),\n updatedOptions,\n );\n }\n case \"CloudEvent\": {\n // The underlying REST API expects a header named `aeg-channel-name`, and so the generated client\n // expects that options bag has a property called `aegChannelName`, where as we expose it with the\n // friendlier name \"channelName\". Fix up the impedence mismatch here\n const {\n channelName,\n ...sendOptions\n }: { channelName?: string } & GeneratedClientPublishCloudEventEventsOptionalParams =\n updatedOptions as CloudEventSendOptions;\n\n if (channelName) {\n sendOptions.aegChannelName = channelName;\n }\n\n return this.client.publishCloudEventEvents(\n this.endpointUrl,\n (events as InputSchemaToInputTypeMap[\"CloudEvent\"][]).map(convertCloudEventToModelType),\n sendOptions,\n );\n }\n case \"Custom\": {\n return this.client.publishCustomEventEvents(\n this.endpointUrl,\n events as InputSchemaToInputTypeMap[\"Custom\"][],\n updatedOptions,\n );\n }\n default: {\n throw new Error(`Unknown input schema type '${this.inputSchema}'`);\n }\n }\n });\n }\n}\n\n/**\n * @internal\n */\nexport function convertEventGridEventToModelType(\n event: SendEventGridEventInput<any>,\n): EventGridEventWireModel {\n return {\n eventType: event.eventType,\n eventTime: event.eventTime ?? new Date(),\n id: event.id ?? randomUUID(),\n subject: event.subject,\n topic: event.topic,\n data: event.data,\n dataVersion: event.dataVersion,\n };\n}\n\n/**\n * @internal\n */\nexport function convertCloudEventToModelType(event: SendCloudEventInput<any>): CloudEventWireModel {\n if (event.extensionAttributes) {\n for (const propName in event.extensionAttributes) {\n // 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\"\n // they also can not match an existing defined property name.\n\n if (\n !/^[a-z0-9]*$/.test(propName) ||\n cloudEventReservedPropertyNames.indexOf(propName) !== -1\n ) {\n throw new Error(`invalid extension attribute name: ${propName}`);\n }\n }\n }\n\n const converted: CloudEventWireModel = {\n specversion: \"1.0\",\n type: event.type,\n source: event.source,\n id: event.id ?? randomUUID(),\n time: event.time ?? new Date(),\n subject: event.subject,\n dataschema: event.dataschema,\n ...(event.extensionAttributes ?? []),\n };\n\n if (event.data instanceof Uint8Array) {\n if (!event.datacontenttype) {\n throw new Error(\n \"a data content type must be provided when sending an event with binary data\",\n );\n }\n\n converted.datacontenttype = event.datacontenttype;\n converted.dataBase64 = event.data;\n } else {\n converted.datacontenttype = event.datacontenttype ?? \"application/json\";\n converted.data = event.data;\n }\n\n return converted;\n}\n"]}
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "sdk-type": "client",
4
4
  "author": "Microsoft Corporation",
5
5
  "description": "An isomorphic client library for the Azure Event Grid service.",
6
- "version": "5.10.0-alpha.20250131.1",
6
+ "version": "5.10.0-alpha.20250204.1",
7
7
  "keywords": [
8
8
  "node",
9
9
  "azure",
@@ -80,14 +80,14 @@
80
80
  "unit-test": "npm run unit-test:node && npm run unit-test:browser",
81
81
  "unit-test:browser": "npm run clean && dev-tool run build-package && dev-tool run build-test && dev-tool run test:vitest --browser",
82
82
  "unit-test:node": "dev-tool run test:vitest",
83
- "update-snippets": "echo skipped"
83
+ "update-snippets": "dev-tool run update-snippets"
84
84
  },
85
85
  "sideEffects": false,
86
86
  "autoPublish": false,
87
87
  "dependencies": {
88
88
  "@azure/core-auth": "^1.9.0",
89
89
  "@azure/core-client": "^1.9.2",
90
- "@azure/core-rest-pipeline": "^1.18.0",
90
+ "@azure/core-rest-pipeline": "^1.18.2",
91
91
  "@azure/core-tracing": "^1.2.0",
92
92
  "@azure/core-util": "^1.11.0",
93
93
  "@azure/logger": "^1.1.4",
@@ -99,18 +99,20 @@
99
99
  "@azure-tools/test-utils-vitest": ">=1.0.0-alpha <1.0.0-alphb",
100
100
  "@azure/dev-tool": ">=1.0.0-alpha <1.0.0-alphb",
101
101
  "@azure/eslint-plugin-azure-sdk": ">=3.0.0-alpha <3.0.0-alphb",
102
+ "@azure/identity": "^4.6.0",
102
103
  "@azure/service-bus": "^7.9.5",
103
104
  "@types/node": "^18.0.0",
104
105
  "@vitest/browser": "^3.0.3",
105
106
  "@vitest/coverage-istanbul": "^3.0.3",
106
107
  "dotenv": "^16.0.0",
107
108
  "eslint": "^9.9.0",
108
- "playwright": "^1.49.0",
109
+ "playwright": "^1.50.1",
109
110
  "typescript": "~5.7.2",
110
111
  "vitest": "^3.0.3"
111
112
  },
112
113
  "type": "module",
113
114
  "tshy": {
115
+ "project": "./tsconfig.src.json",
114
116
  "exports": {
115
117
  "./package.json": "./package.json",
116
118
  ".": "./src/index.ts"
@@ -123,8 +125,7 @@
123
125
  "browser",
124
126
  "react-native"
125
127
  ],
126
- "selfLink": false,
127
- "project": "./tsconfig.src.json"
128
+ "selfLink": false
128
129
  },
129
130
  "exports": {
130
131
  "./package.json": "./package.json",
@@ -146,5 +147,6 @@
146
147
  "default": "./dist/commonjs/index.js"
147
148
  }
148
149
  }
149
- }
150
+ },
151
+ "react-native": "./dist/react-native/index.js"
150
152
  }