@simpleview/sv-pubsub 1.0.20 → 1.0.22

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.
@@ -34,6 +34,23 @@ class PubSubClient {
34
34
  throw error;
35
35
  }
36
36
  }
37
+ async publishMessage(topic, message) {
38
+ const topicName = typeof topic === "string" ? topic : topic.name;
39
+ if (typeof topicName !== "string") {
40
+ throw new Error("Topic name must be specified.");
41
+ }
42
+ const existingTopic = this.pubsub.topic(topicName);
43
+ try {
44
+ return await existingTopic.publishMessage(message);
45
+ }
46
+ catch (e) {
47
+ if (e.code === 5) { // NOT_FOUND
48
+ const [newTopic] = await this.pubsub.createTopic(topic);
49
+ return await newTopic.publishMessage(message);
50
+ }
51
+ throw e;
52
+ }
53
+ }
37
54
  close() {
38
55
  return this.pubsub.close();
39
56
  }
@@ -1,4 +1,5 @@
1
1
  import { PubSub, Subscription } from "@google-cloud/pubsub";
2
+ import { MessageOptions, TopicMetadata } from "@google-cloud/pubsub/build/src/topic";
2
3
  export type PubSubClientOptions = {
3
4
  projectId: string;
4
5
  credentials: {
@@ -11,5 +12,6 @@ export default class PubSubClient {
11
12
  constructor(options: PubSubClientOptions);
12
13
  createTopic(topicName: string): Promise<import("@google-cloud/pubsub").Topic>;
13
14
  createSubscription(topicName: string, subscriptionName: string, retentionDuration?: number): Promise<Subscription>;
15
+ publishMessage(topic: string | TopicMetadata, message: MessageOptions): Promise<string>;
14
16
  close(): Promise<void>;
15
17
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@simpleview/sv-pubsub",
3
- "version": "1.0.20",
3
+ "version": "1.0.22",
4
4
  "description": "Client for communicating with Google Pub/Sub",
5
5
  "author": "Paul Riding <paul.riding@granicus.com>",
6
6
  "devDependencies": {