gcf-common-lib 0.13.3 → 0.14.3

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.
Files changed (3) hide show
  1. package/index.js +20 -18
  2. package/index.ts +26 -25
  3. package/package.json +1 -1
package/index.js CHANGED
@@ -59,9 +59,9 @@ class GcfCommon {
59
59
  var _a, _b, _c;
60
60
  return __awaiter(this, void 0, void 0, function* () {
61
61
  const topic = yield this.getTopic(event, context);
62
- console.log('publish:', topic === null || topic === void 0 ? void 0 : topic.name, json);
62
+ console.log('publish:', topic === null || topic === void 0 ? void 0 : topic.name, json, attributes);
63
63
  if (topic) {
64
- return yield topic.publishMessage({
64
+ return topic.publishMessage({
65
65
  json,
66
66
  attributes: Object.assign(Object.assign({}, attributes), { requestId: (_c = (_b = (_a = event) === null || _a === void 0 ? void 0 : _a.attributes) === null || _b === void 0 ? void 0 : _b.requestId) !== null && _c !== void 0 ? _c : '', type: 'response', response: '1' }),
67
67
  });
@@ -73,24 +73,26 @@ class GcfCommon {
73
73
  return __awaiter(this, void 0, void 0, function* () {
74
74
  /** t_{GUID}__{YYYY-MM-DD} */
75
75
  let topicName;
76
- if ((context === null || context === void 0 ? void 0 : context.eventType) === 'google.storage.object.finalize') {
77
- const gsEvent = event;
78
- topicName = (_a = gsEvent === null || gsEvent === void 0 ? void 0 : gsEvent.metadata) === null || _a === void 0 ? void 0 : _a.topic;
79
- if (!topicName && ((_b = context === null || context === void 0 ? void 0 : context.resource) === null || _b === void 0 ? void 0 : _b.type) === 'storage#object') {
80
- const file = exports.storage.bucket(gsEvent.bucket).file(gsEvent.name);
81
- const [meta] = yield file.getMetadata();
82
- topicName = (_c = meta === null || meta === void 0 ? void 0 : meta.metadata) === null || _c === void 0 ? void 0 : _c.topic;
83
- console.log('topic:', topicName);
76
+ switch (context === null || context === void 0 ? void 0 : context.eventType) {
77
+ case 'google.storage.object.finalize': {
78
+ const gsEvent = event;
79
+ topicName = (_a = gsEvent === null || gsEvent === void 0 ? void 0 : gsEvent.metadata) === null || _a === void 0 ? void 0 : _a.topic;
80
+ if (!topicName && ((_b = context === null || context === void 0 ? void 0 : context.resource) === null || _b === void 0 ? void 0 : _b.type) === 'storage#object') {
81
+ const file = exports.storage.bucket(gsEvent.bucket).file(gsEvent.name);
82
+ const [meta] = yield file.getMetadata();
83
+ topicName = (_c = meta === null || meta === void 0 ? void 0 : meta.metadata) === null || _c === void 0 ? void 0 : _c.topic;
84
+ }
85
+ break;
86
+ }
87
+ case 'google.pubsub.topic.publish': {
88
+ const psEvent = event;
89
+ topicName = (_e = (_d = psEvent.attributes) === null || _d === void 0 ? void 0 : _d.topic) !== null && _e !== void 0 ? _e : (_f = psEvent.data) === null || _f === void 0 ? void 0 : _f.topic;
90
+ break;
84
91
  }
85
92
  }
86
- else if ((context === null || context === void 0 ? void 0 : context.eventType) === 'google.pubsub.topic.publish') {
87
- const psEvent = event;
88
- topicName = (_e = (_d = psEvent.attributes) === null || _d === void 0 ? void 0 : _d.topic) !== null && _e !== void 0 ? _e : (_f = psEvent.data) === null || _f === void 0 ? void 0 : _f.topic;
89
- }
90
- if (topicName && !(0, isEmpty_1.default)(topicName)) {
91
- const topic = exports.pubSub.topic(topicName);
92
- yield topic.setMetadata({ labels: { date: topicName.split('__')[1] } });
93
- return topic;
93
+ console.log('topic:', topicName);
94
+ if (!(0, isEmpty_1.default)(topicName)) {
95
+ return exports.pubSub.topic(topicName);
94
96
  }
95
97
  });
96
98
  }
package/index.ts CHANGED
@@ -40,8 +40,8 @@ export type TContext = {
40
40
  eventType: 'google.storage.object.finalize' | 'google.pubsub.topic.publish';
41
41
  resource: {
42
42
  service: 'storage.googleapis.com' | 'pubsub.googleapis.com';
43
+ type: 'storage#object' | 'type.googleapis.com/google.pubsub.v1.PubsubMessage';
43
44
  name: string;
44
- type: string; // 'storage#object' | 'type.googleapis.com/google.pubsub.v1.PubsubMessage'
45
45
  }
46
46
  }
47
47
 
@@ -76,7 +76,7 @@ export class GcfCommon {
76
76
  timeoutAfter(timeout),
77
77
  handler(event, context),
78
78
  ])
79
- .then(async (res: any) => {
79
+ .then(async (res: T | undefined) => {
80
80
  // console.log('res:', res);
81
81
  await this.publish(event, context, res ?? {});
82
82
  })
@@ -91,16 +91,15 @@ export class GcfCommon {
91
91
  };
92
92
  await this.publish(event, context, response, {error: '1'}).catch(noop);
93
93
  throw err;
94
- })
95
- ;
94
+ });
96
95
  }
97
96
 
98
97
  static async publish(event: TEvent, context: TContext, json: TResponse, attributes?: any) {
99
98
  const topic = await this.getTopic(event, context);
100
- console.log('publish:', topic?.name, json);
99
+ console.log('publish:', topic?.name, json, attributes);
101
100
 
102
101
  if (topic) {
103
- return await topic.publishMessage({
102
+ return topic.publishMessage({
104
103
  json,
105
104
  attributes: {
106
105
  ...attributes,
@@ -115,28 +114,30 @@ export class GcfCommon {
115
114
  static async getTopic(event: TEvent, context: TContext) {
116
115
 
117
116
  /** t_{GUID}__{YYYY-MM-DD} */
118
- let topicName: string | undefined;
119
-
120
- if (context?.eventType === 'google.storage.object.finalize') {
121
- const gsEvent = event as TGSEvent;
122
-
123
- topicName = gsEvent?.metadata?.topic;
124
-
125
- if (!topicName && context?.resource?.type === 'storage#object') {
126
- const file: File = storage.bucket(gsEvent.bucket).file(gsEvent.name);
127
- const [meta] = await file.getMetadata();
128
- topicName = meta?.metadata?.topic;
129
- console.log('topic:', topicName);
117
+ let topicName: string;
118
+
119
+ switch (context?.eventType) {
120
+ case 'google.storage.object.finalize': {
121
+ const gsEvent = event as TGSEvent;
122
+ topicName = gsEvent?.metadata?.topic;
123
+ if (!topicName && context?.resource?.type === 'storage#object') {
124
+ const file: File = storage.bucket(gsEvent.bucket).file(gsEvent.name);
125
+ const [meta] = await file.getMetadata();
126
+ topicName = meta?.metadata?.topic;
127
+ }
128
+ break;
129
+ }
130
+ case 'google.pubsub.topic.publish' : {
131
+ const psEvent = event as TPSEvent;
132
+ topicName = psEvent.attributes?.topic ?? (psEvent.data as any)?.topic;
133
+ break;
130
134
  }
131
- } else if (context?.eventType === 'google.pubsub.topic.publish') {
132
- const psEvent = event as TPSEvent;
133
- topicName = psEvent.attributes?.topic ?? (psEvent.data as any)?.topic;
134
135
  }
135
136
 
136
- if (topicName && !isEmpty(topicName)) {
137
- const topic = pubSub.topic(topicName);
138
- await topic.setMetadata({labels: {date: topicName.split('__')[1]}});
139
- return topic;
137
+ console.log('topic:', topicName);
138
+
139
+ if (!isEmpty(topicName)) {
140
+ return pubSub.topic(topicName)
140
141
  }
141
142
  }
142
143
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "gcf-common-lib",
3
3
  "description": "",
4
- "version": "0.13.3",
4
+ "version": "0.14.3",
5
5
  "publishConfig": {
6
6
  "access": "public",
7
7
  "branches": [