0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2025-01-27 21:39:16 -05:00

refactor(core,schemas): move webhook event payload type definition to schemas (#5878)

move webhook event payload type definition to schemas
This commit is contained in:
simeng-li 2024-05-17 16:25:19 +08:00 committed by GitHub
parent 76fd33b7ed
commit a1091aee20
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 82 additions and 85 deletions

View file

@ -1,8 +1,12 @@
import { InteractionEvent, InteractionHookEvent, type DataHookEvent } from '@logto/schemas';
import {
InteractionEvent,
InteractionHookEvent,
type DataHookEvent,
type InteractionApiMetadata,
type ManagementApiContext,
} from '@logto/schemas';
import { type Optional } from '@silverhand/essentials';
import type { InteractionApiMetadata, ManagementApiContext } from './type.js';
type DataHookMetadata = {
userAgent?: string;
ip: string;

View file

@ -1,10 +1,13 @@
import {
LogResult,
userInfoSelectFields,
type DataHookEventPayload,
type Hook,
type HookConfig,
type HookEvent,
type HookEventPayload,
type HookTestErrorResponseData,
type InteractionHookEventPayload,
} from '@logto/schemas';
import { generateStandardId, normalizeError, type ConsoleLog } from '@logto/shared';
import { conditional, pick, trySafe } from '@silverhand/essentials';
@ -19,11 +22,6 @@ import {
type DataHookContextManager,
type InteractionHookContextManager,
} from './context-manager.js';
import type {
DataHookEventPayload,
HookEventPayload,
InteractionHookEventPayload,
} from './type.js';
import { generateHookTestPayload, parseResponse, sendWebhookRequest } from './utils.js';
type BetterOmit<T, Ignore> = {

View file

@ -1,74 +0,0 @@
import {
type Application,
type DataHookEvent,
type InteractionEvent,
type InteractionHookEvent,
type User,
type userInfoSelectFields,
} from '@logto/schemas';
/**
* The interaction API context for triggering InteractionHook and DataHook events.
* In the `koaInteractionHooks` middleware,
* we will store the context before processing the interaction and consume it after the interaction is processed if needed.
*/
export type InteractionApiMetadata = {
/** The application ID if the hook is triggered by interaction API. */
applicationId?: string;
/** The session ID if the hook is triggered by interaction API. */
sessionId?: string;
/** The InteractionEvent if the hook is triggered by interaction API. */
interactionEvent: InteractionEvent;
};
type InteractionApiContextPayload = {
/** Fetch application detail by application ID before sending the hook event */
application?: Pick<Application, 'id' | 'type' | 'name' | 'description'>;
sessionId?: string;
interactionEvent?: InteractionEvent;
};
export type InteractionHookEventPayload = {
event: InteractionHookEvent;
createdAt: string;
hookId: string;
userAgent?: string;
userIp?: string;
/** InteractionHook result */
userId?: string;
/** Fetch user detail by user ID before sending the hook event */
user?: Pick<User, (typeof userInfoSelectFields)[number]>;
} & InteractionApiContextPayload &
Record<string, unknown>;
/**
* The API context for management API triggered data hooks.
* In the `koaManagementApiHooks` middleware,
* we will store the context of management API requests that triggers the DataHook events.
* Can't put it in the DataHookMetadata because the matched API context is only available after the request is processed.
*/
export type ManagementApiContext = {
/** Request route params. */
params?: Record<string, string>;
/** Request route path. */
path: string;
/** Matched route used as the identifier to trigger the hook. */
matchedRoute?: string;
/** Request method. */
method: string;
/** Response status code. */
status: number;
};
export type DataHookEventPayload = {
event: DataHookEvent;
createdAt: string;
hookId: string;
ip?: string;
userAgent?: string;
data?: unknown;
} & Partial<InteractionApiContextPayload> &
Partial<ManagementApiContext> &
Record<string, unknown>;
export type HookEventPayload = InteractionHookEventPayload | DataHookEventPayload;

View file

@ -3,6 +3,7 @@ import {
managementApiHooksRegistration,
type HookConfig,
type HookEvent,
type HookEventPayload,
} from '@logto/schemas';
import { conditional, trySafe } from '@silverhand/essentials';
import { type IRouterParamContext } from 'koa-router';
@ -10,8 +11,6 @@ import ky, { type KyResponse } from 'ky';
import { sign } from '#src/utils/sign.js';
import { type HookEventPayload } from './type.js';
export const parseResponse = async (response: KyResponse) => {
const body = await response.text();
return {

View file

@ -1,6 +1,10 @@
import { z } from 'zod';
import { Hooks } from '../db-entries/index.js';
import { Hooks, type Application, type User } from '../db-entries/index.js';
import { type DataHookEvent, type InteractionHookEvent } from '../foundations/index.js';
import { type InteractionEvent } from './interactions.js';
import { type userInfoSelectFields } from './user.js';
const hookExecutionStatsGuard = z.object({
successCount: z.number(),
@ -21,3 +25,69 @@ export const hookTestErrorResponseDataGuard = z.object({
});
export type HookTestErrorResponseData = z.infer<typeof hookTestErrorResponseDataGuard>;
/**
* The interaction API context for triggering InteractionHook and DataHook events.
* In the `koaInteractionHooks` middleware,
* we will store the context before processing the interaction and consume it after the interaction is processed if needed.
*/
export type InteractionApiMetadata = {
/** The application ID if the hook is triggered by interaction API. */
applicationId?: string;
/** The session ID if the hook is triggered by interaction API. */
sessionId?: string;
/** The InteractionEvent if the hook is triggered by interaction API. */
interactionEvent: InteractionEvent;
};
type InteractionApiContextPayload = {
/** Fetch application detail by application ID before sending the hook event */
application?: Pick<Application, 'id' | 'type' | 'name' | 'description'>;
sessionId?: string;
interactionEvent?: InteractionEvent;
};
export type InteractionHookEventPayload = {
event: InteractionHookEvent;
createdAt: string;
hookId: string;
userAgent?: string;
userIp?: string;
/** InteractionHook result */
userId?: string;
/** Fetch user detail by user ID before sending the hook event */
user?: Pick<User, (typeof userInfoSelectFields)[number]>;
} & InteractionApiContextPayload &
Record<string, unknown>;
/**
* The API context for management API triggered data hooks.
* In the `koaManagementApiHooks` middleware,
* we will store the context of management API requests that triggers the DataHook events.
* Can't put it in the DataHookMetadata because the matched API context is only available after the request is processed.
*/
export type ManagementApiContext = {
/** Request route params. */
params?: Record<string, string>;
/** Request route path. */
path: string;
/** Matched route used as the identifier to trigger the hook. */
matchedRoute?: string;
/** Request method. */
method: string;
/** Response status code. */
status: number;
};
export type DataHookEventPayload = {
event: DataHookEvent;
createdAt: string;
hookId: string;
ip?: string;
userAgent?: string;
data?: unknown;
} & Partial<InteractionApiContextPayload> &
Partial<ManagementApiContext> &
Record<string, unknown>;
export type HookEventPayload = InteractionHookEventPayload | DataHookEventPayload;