@chevre/domain 22.5.0-alpha.40 → 22.5.0-alpha.42

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.
@@ -58,7 +58,9 @@ export async function aggregateScreeningEvent() {
58
58
  },
59
59
  notification: {
60
60
  timeout: 1000,
61
- useFetchAPI: true
61
+ useFetchAPI: true,
62
+ secretKey: 'xx',
63
+ headerIdentifier: 'xx'
62
64
  },
63
65
  userPoolIdOld: '',
64
66
  userPoolIdNew: '',
@@ -1,6 +1,7 @@
1
1
  import { LINENotifyCredentials } from '../credentials/lineNotify';
2
2
  import { SendGridCredentials } from '../credentials/sendGrid';
3
3
  import * as factory from '../factory';
4
+ import { IWebhookSettings } from '../settings';
4
5
  import type { ActionRepo } from '../repo/action';
5
6
  import type { MessageRepo } from '../repo/message';
6
7
  import type { ProjectRepo } from '../repo/project';
@@ -37,10 +38,7 @@ declare function triggerWebhook(params: factory.task.IData<factory.taskName.Trig
37
38
  project: {
38
39
  id: string;
39
40
  };
40
- }, options: {
41
- timeout: number;
42
- useFetchAPI: boolean;
43
- }): (repos: {
41
+ }, options: IWebhookSettings): (repos: {
44
42
  action: ActionRepo;
45
43
  }) => Promise<void>;
46
44
  export { lineNotify, sendEmailMessage, triggerWebhook };
@@ -11,8 +11,8 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.triggerWebhook = exports.sendEmailMessage = exports.lineNotify = void 0;
13
13
  const sgMail = require("@sendgrid/mail");
14
+ const crypto = require("crypto");
14
15
  const http_status_1 = require("http-status");
15
- // import * as request from 'request';
16
16
  const util = require("util");
17
17
  const factory = require("../factory");
18
18
  function createSendEmailMessageActionAttributes(params) {
@@ -234,22 +234,44 @@ function createInformActionAttributes(params) {
234
234
  const { object, purpose, recipient, project } = params;
235
235
  return Object.assign({ agent: { id: project.id, typeOf: factory.organizationType.Project }, object, project: { id: project.id, typeOf: factory.organizationType.Project }, recipient, typeOf: factory.actionType.InformAction }, (typeof (purpose === null || purpose === void 0 ? void 0 : purpose.typeOf) === 'string') ? { purpose } : undefined);
236
236
  }
237
+ function signRequest(params) {
238
+ const { requestBody, timestamp, secretKey } = params;
239
+ const payload = `${requestBody}${timestamp.toString()}`;
240
+ return crypto.createHmac('sha256', secretKey)
241
+ .update(payload)
242
+ .digest('hex');
243
+ }
244
+ // tslint:disable-next-line:max-func-body-length
237
245
  function processInformAction(params, options) {
246
+ // tslint:disable-next-line:cyclomatic-complexity
238
247
  return (repos) => __awaiter(this, void 0, void 0, function* () {
239
248
  var _a, _b;
240
- const { timeout, useFetchAPI } = options;
249
+ const { timeout, useFetchAPI, secretKey, headerIdentifier } = options;
241
250
  const action = yield repos.action.start(createInformActionAttributes(params));
242
251
  let result = {};
243
252
  try {
244
253
  if (typeof ((_a = params.recipient) === null || _a === void 0 ? void 0 : _a.url) === 'string') {
245
254
  const url = params.recipient.url;
255
+ const requestBody = JSON.stringify(Object.assign({ data: params.object }, (typeof params.identifier === 'string' && params.identifier !== '')
256
+ ? { identifier: params.identifier }
257
+ : undefined));
258
+ const timestamp = action.startDate.valueOf();
259
+ // sign request(2024-10-28~)
260
+ let signature;
261
+ if (typeof secretKey === 'string' && secretKey !== ''
262
+ && typeof headerIdentifier === 'string' && headerIdentifier !== '') {
263
+ signature = signRequest({ requestBody, timestamp, secretKey });
264
+ }
246
265
  if (useFetchAPI) {
247
266
  try {
248
- const res = yield fetch(url, Object.assign({ method: 'POST', headers: {
249
- 'Content-Type': 'application/json'
250
- }, body: JSON.stringify(Object.assign({ data: params.object }, (typeof params.identifier === 'string' && params.identifier !== '')
251
- ? { identifier: params.identifier }
252
- : undefined)) }, (typeof timeout === 'number')
267
+ const headers = new Headers({ 'Content-Type': 'application/json' });
268
+ if (typeof headerIdentifier === 'string' && headerIdentifier !== '') {
269
+ headers.append(`X-${headerIdentifier}-Webhook-Timestamp`, String(timestamp));
270
+ if (typeof signature === 'string') {
271
+ headers.append(`X-${headerIdentifier}-Webhook-Signature`, signature);
272
+ }
273
+ }
274
+ const res = yield fetch(url, Object.assign({ method: 'POST', headers, body: requestBody }, (typeof timeout === 'number')
253
275
  ? { signal: AbortSignal.timeout(timeout) }
254
276
  : undefined));
255
277
  let body;
@@ -18,8 +18,8 @@ const notification_1 = require("../notification");
18
18
  function call(params) {
19
19
  return ({ connection, settings }) => __awaiter(this, void 0, void 0, function* () {
20
20
  const { notification } = settings;
21
- const { timeout, useFetchAPI } = notification;
22
- yield (0, notification_1.triggerWebhook)(Object.assign(Object.assign({}, params.data), { project: { id: params.project.id } }), { timeout, useFetchAPI })({
21
+ const { timeout, useFetchAPI, secretKey, headerIdentifier } = notification;
22
+ yield (0, notification_1.triggerWebhook)(Object.assign(Object.assign({}, params.data), { project: { id: params.project.id } }), { timeout, useFetchAPI, secretKey, headerIdentifier })({
23
23
  action: new action_1.ActionRepo(connection)
24
24
  });
25
25
  });
@@ -1,5 +1,5 @@
1
1
  import * as factory from './factory';
2
- import { AggregationSettings } from './settings/aggregation';
2
+ import { AggregationSettings, ICallableTaskOperation, ICallResult, IOperationExecute } from './settings/aggregation';
3
3
  interface IOnOrderStatusChanged {
4
4
  informOrder?: factory.project.IInformParams[];
5
5
  informOrder2hub?: factory.project.IInformParams[];
@@ -8,6 +8,21 @@ interface IOnReservationStatusChanged {
8
8
  informReservation?: factory.project.IInformParams[];
9
9
  informReservation2hub?: factory.project.IInformParams[];
10
10
  }
11
+ interface IWebhookSettings {
12
+ /**
13
+ * リクエストタイムアウト
14
+ */
15
+ timeout: number;
16
+ useFetchAPI: boolean;
17
+ /**
18
+ * 通知署名の鍵
19
+ */
20
+ secretKey?: string;
21
+ /**
22
+ * 固有署名ヘッダー識別子
23
+ */
24
+ headerIdentifier?: string;
25
+ }
11
26
  interface IOptions {
12
27
  onOrderStatusChanged: IOnOrderStatusChanged;
13
28
  onEventChanged: {
@@ -63,13 +78,7 @@ interface IOptions {
63
78
  };
64
79
  useAssetTransactionSyncProcessing: boolean;
65
80
  useExperimentalFeature: boolean;
66
- notification: {
67
- /**
68
- * リクエストタイムアウト
69
- */
70
- timeout: number;
71
- useFetchAPI: boolean;
72
- };
81
+ notification: IWebhookSettings;
73
82
  }
74
83
  /**
75
84
  * domain settings
@@ -135,16 +144,10 @@ declare class Settings {
135
144
  /**
136
145
  * 通知設定
137
146
  */
138
- readonly notification: {
139
- /**
140
- * リクエストタイムアウト
141
- */
142
- timeout: number;
143
- useFetchAPI: boolean;
144
- };
147
+ readonly notification: IWebhookSettings;
145
148
  constructor(options: IOptions);
146
149
  }
147
150
  export declare const MONGO_MAX_TIME_MS: number;
148
151
  export declare const MONGO_READ_PREFERENCE: string;
149
152
  export declare const MONGO_AUTO_INDEX: boolean;
150
- export { AggregationSettings, Settings };
153
+ export { AggregationSettings, Settings, ICallableTaskOperation, ICallResult, IOperationExecute, IWebhookSettings };
package/package.json CHANGED
@@ -108,5 +108,5 @@
108
108
  "postversion": "git push origin --tags",
109
109
  "prepublishOnly": "npm run clean && npm run build && npm test && npm run doc"
110
110
  },
111
- "version": "22.5.0-alpha.40"
111
+ "version": "22.5.0-alpha.42"
112
112
  }