From 79daf253a822a5d8e60b663371805c5c62b9c6a2 Mon Sep 17 00:00:00 2001 From: Xiao Yijun Date: Mon, 29 May 2023 11:37:00 +0800 Subject: [PATCH] feat(core): add message id for the webhook payload (#3913) --- packages/core/src/libraries/hook/index.test.ts | 17 ++++++++++------- packages/core/src/libraries/hook/utils.test.ts | 9 ++++++++- packages/core/src/libraries/hook/utils.ts | 2 ++ 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/packages/core/src/libraries/hook/index.test.ts b/packages/core/src/libraries/hook/index.test.ts index 03af40db2..9c2dc3b94 100644 --- a/packages/core/src/libraries/hook/index.test.ts +++ b/packages/core/src/libraries/hook/index.test.ts @@ -13,9 +13,8 @@ const { mockEsmWithActual, mockEsm } = createMockUtils(jest); const nanoIdMock = 'mockId'; await mockEsmWithActual('@logto/shared', () => ({ - // eslint-disable-next-line unicorn/consistent-function-scoping - buildIdGenerator: () => () => nanoIdMock, - generateStandardId: () => nanoIdMock, + buildIdGenerator: jest.fn().mockReturnValue(nanoIdMock), + generateStandardId: jest.fn().mockReturnValue(nanoIdMock), })); const mockSignature = 'mockSignature'; @@ -53,11 +52,15 @@ const findHookById = jest.fn().mockResolvedValue(hook); const { createHookLibrary } = await import('./index.js'); const { triggerInteractionHooksIfNeeded, attachExecutionStatsToHook, testHook } = createHookLibrary( new MockQueries({ - // @ts-expect-error - users: { findUserById: () => ({ id: 'user_id', username: 'user', extraField: 'not_ok' }) }, + users: { + findUserById: jest.fn().mockReturnValue({ + id: 'user_id', + username: 'user', + extraField: 'not_ok', + }), + }, applications: { - // @ts-expect-error - findApplicationById: async () => ({ id: 'app_id', extraField: 'not_ok' }), + findApplicationById: jest.fn().mockResolvedValue({ id: 'app_id', extraField: 'not_ok' }), }, logs: { insertLog, getHookExecutionStatsByHookId }, hooks: { findAllHooks, findHookById }, diff --git a/packages/core/src/libraries/hook/utils.test.ts b/packages/core/src/libraries/hook/utils.test.ts index 644165919..039cb555c 100644 --- a/packages/core/src/libraries/hook/utils.test.ts +++ b/packages/core/src/libraries/hook/utils.test.ts @@ -4,7 +4,7 @@ import { got } from 'got'; const { jest } = import.meta; -const { mockEsm } = createMockUtils(jest); +const { mockEsm, mockEsmWithActual } = createMockUtils(jest); const post = jest .spyOn(got, 'post') @@ -16,6 +16,12 @@ mockEsm('#src/utils/sign.js', () => ({ sign: () => mockSignature, })); +const mockNanoId = 'mockNanoidId'; +await mockEsmWithActual('@logto/shared', () => ({ + buildIdGenerator: jest.fn().mockReturnValue(mockNanoId), + generateStandardId: jest.fn().mockReturnValue(mockNanoId), +})); + const { generateHookTestPayload, sendWebhookRequest } = await import('./utils.js'); describe('sendWebhookRequest', () => { @@ -41,6 +47,7 @@ describe('sendWebhookRequest', () => { 'user-agent': 'Logto (https://logto.io/)', foo: 'bar', 'logto-signature-sha-256': mockSignature, + 'logto-message-id': mockNanoId, }, json: testPayload, retry: { limit: 3 }, diff --git a/packages/core/src/libraries/hook/utils.ts b/packages/core/src/libraries/hook/utils.ts index 5d82c9c9b..c7d7528ff 100644 --- a/packages/core/src/libraries/hook/utils.ts +++ b/packages/core/src/libraries/hook/utils.ts @@ -4,6 +4,7 @@ import { ApplicationType, type HookConfig, } from '@logto/schemas'; +import { generateStandardId } from '@logto/shared'; import { conditional, trySafe } from '@silverhand/essentials'; import { got, type Response } from 'got'; @@ -33,6 +34,7 @@ export const sendWebhookRequest = async ({ 'user-agent': 'Logto (https://logto.io/)', ...headers, ...conditional(signingKey && { 'logto-signature-sha-256': sign(signingKey, payload) }), + 'logto-message-id': generateStandardId(), }, json: payload, retry: { limit: retries ?? 3 },