gcf-common-lib 0.12.1 → 0.13.0
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/index.js +17 -6
- package/index.ts +23 -6
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -50,21 +50,32 @@ class GcfCommon {
|
|
|
50
50
|
stack: err.stack,
|
|
51
51
|
},
|
|
52
52
|
};
|
|
53
|
-
|
|
53
|
+
const attr = {
|
|
54
|
+
error: {
|
|
55
|
+
name: err.name,
|
|
56
|
+
message: `GCF [${fname}]: ${err.message}`,
|
|
57
|
+
},
|
|
58
|
+
};
|
|
59
|
+
yield this.publish(event, context, response, attr).catch(noop_1.default);
|
|
54
60
|
throw err;
|
|
55
61
|
}));
|
|
56
62
|
});
|
|
57
63
|
}
|
|
58
|
-
static publish(event, context, json) {
|
|
64
|
+
static publish(event, context, json, attributes) {
|
|
65
|
+
var _a, _b;
|
|
59
66
|
return __awaiter(this, void 0, void 0, function* () {
|
|
60
67
|
const topic = yield this.getTopic(event, context);
|
|
61
68
|
console.log('publish:', topic === null || topic === void 0 ? void 0 : topic.name, json);
|
|
62
|
-
if (topic)
|
|
63
|
-
return yield topic.publishMessage({
|
|
69
|
+
if (topic) {
|
|
70
|
+
return yield topic.publishMessage({
|
|
71
|
+
json,
|
|
72
|
+
attributes: Object.assign(Object.assign({}, attributes), { requestId: (_b = (_a = event) === null || _a === void 0 ? void 0 : _a.attributes) === null || _b === void 0 ? void 0 : _b.requestId, type: 'response', response: true }),
|
|
73
|
+
});
|
|
74
|
+
}
|
|
64
75
|
});
|
|
65
76
|
}
|
|
66
77
|
static getTopic(event, context) {
|
|
67
|
-
var _a, _b, _c, _d;
|
|
78
|
+
var _a, _b, _c, _d, _e, _f;
|
|
68
79
|
return __awaiter(this, void 0, void 0, function* () {
|
|
69
80
|
/** t_{GUID}__{YYYY-MM-DD} */
|
|
70
81
|
let topicName;
|
|
@@ -80,7 +91,7 @@ class GcfCommon {
|
|
|
80
91
|
}
|
|
81
92
|
else if ((context === null || context === void 0 ? void 0 : context.eventType) === 'google.pubsub.topic.publish') {
|
|
82
93
|
const psEvent = event;
|
|
83
|
-
topicName = (_d = psEvent.
|
|
94
|
+
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;
|
|
84
95
|
}
|
|
85
96
|
if (topicName && !(0, isEmpty_1.default)(topicName)) {
|
|
86
97
|
const topic = exports.pubSub.topic(topicName);
|
package/index.ts
CHANGED
|
@@ -28,8 +28,8 @@ export type TGSEvent = {
|
|
|
28
28
|
|
|
29
29
|
export type TPSEvent = {
|
|
30
30
|
'@type': string; // 'type.googleapis.com/google.pubsub.v1.PubsubMessage'
|
|
31
|
-
attributes?: string
|
|
32
|
-
data
|
|
31
|
+
attributes?: { [k: string]: any };
|
|
32
|
+
data?: string | { [k: string]: any };
|
|
33
33
|
}
|
|
34
34
|
|
|
35
35
|
export type TEvent = TGSEvent | TPSEvent;
|
|
@@ -89,16 +89,33 @@ export class GcfCommon {
|
|
|
89
89
|
stack: err.stack,
|
|
90
90
|
},
|
|
91
91
|
};
|
|
92
|
-
|
|
92
|
+
const attr = {
|
|
93
|
+
error: {
|
|
94
|
+
name: err.name,
|
|
95
|
+
message: `GCF [${fname}]: ${err.message}`,
|
|
96
|
+
},
|
|
97
|
+
};
|
|
98
|
+
await this.publish(event, context, response, attr).catch(noop);
|
|
93
99
|
throw err;
|
|
94
100
|
})
|
|
95
101
|
;
|
|
96
102
|
}
|
|
97
103
|
|
|
98
|
-
static async publish(event: TEvent, context: TContext, json: TResponse) {
|
|
104
|
+
static async publish(event: TEvent, context: TContext, json: TResponse, attributes?: any) {
|
|
99
105
|
const topic = await this.getTopic(event, context);
|
|
100
106
|
console.log('publish:', topic?.name, json);
|
|
101
|
-
|
|
107
|
+
|
|
108
|
+
if (topic) {
|
|
109
|
+
return await topic.publishMessage({
|
|
110
|
+
json,
|
|
111
|
+
attributes: {
|
|
112
|
+
...attributes,
|
|
113
|
+
requestId: (event as TPSEvent)?.attributes?.requestId,
|
|
114
|
+
type: 'response',
|
|
115
|
+
response: true,
|
|
116
|
+
},
|
|
117
|
+
});
|
|
118
|
+
}
|
|
102
119
|
}
|
|
103
120
|
|
|
104
121
|
static async getTopic(event: TEvent, context: TContext) {
|
|
@@ -119,7 +136,7 @@ export class GcfCommon {
|
|
|
119
136
|
}
|
|
120
137
|
} else if (context?.eventType === 'google.pubsub.topic.publish') {
|
|
121
138
|
const psEvent = event as TPSEvent;
|
|
122
|
-
topicName = (psEvent.data as any)?.topic;
|
|
139
|
+
topicName = psEvent.attributes?.topic ?? (psEvent.data as any)?.topic;
|
|
123
140
|
}
|
|
124
141
|
|
|
125
142
|
if (topicName && !isEmpty(topicName)) {
|