fix: event type handling and update file paths

Signed-off-by: Innei <i@innei.in>
This commit is contained in:
Innei
2023-12-28 11:49:35 +08:00
parent bbd1a67f1c
commit c29be5177e
4 changed files with 26 additions and 15 deletions

View File

@@ -21,16 +21,31 @@ function generateGenericEventType(fileName) {
if (ts.isPropertySignature(member) && member.type) {
const key = member.name
let eventType = ''
let isLiteralType = false
if (
ts.isComputedPropertyName(key) &&
ts.isPropertyAccessExpression(key.expression)
) {
eventType = key.expression.name.text
} else if (ts.isStringLiteral(key)) {
// Handle string literal types like 'health-check'
eventType = key.text
isLiteralType = true
}
if (eventType && eventType !== '*') {
// if (eventType && eventType !== '*') {
// const payloadType = member.type.getText(sourceFile)
// genericEventType += ` | { type: BusinessEvents.${eventType}; payload: ${payloadType} }\n`
// }
if (eventType) {
const payloadType = member.type.getText(sourceFile)
genericEventType += ` | { type: BusinessEvents.${eventType}; payload: ${payloadType} }\n`
if (isLiteralType) {
// For string literals, use the literal value directly
genericEventType += ` | { type: '${eventType}'; payload: ${payloadType} }\n`
} else {
// For regular event types
genericEventType += ` | { type: BusinessEvents.${eventType}; payload: ${payloadType} }\n`
}
}
}
})

View File

@@ -20,7 +20,7 @@ function replaceContent(filePath, searchValue, replaceValue) {
}
// File paths
const files = ['dist/index.d.ts', 'dist/index.d.mts']
const files = ['dist/index.d.ts', 'dist/index.d.cts']
// The string to be replaced and its replacement
const searchValue = "import { Ref } from '@typegoose/typegoose';"

View File

@@ -1,6 +1,7 @@
import assert from 'assert'
import { createHmac, timingSafeEqual } from 'crypto'
import { EventEmitter } from 'events'
import type { GenericEvent } from 'dist'
import type { IncomingMessage, ServerResponse } from 'http'
import type { BusinessEvents } from './event.enum'
import type { ExtendedEventEmitter } from './types'
@@ -9,7 +10,7 @@ import { InvalidSignatureError } from './error'
interface CreateHandlerOptions {
secret: string
events?: 'all' | BusinessEvents[]
// events?: 'all' | BusinessEvents[]
}
export type RequestWithJSONBody = IncomingMessage & Request & { body: object }
type Handler = {
@@ -25,14 +26,7 @@ export const createHandler = (options: CreateHandlerOptions): Handler => {
try {
const data = await readDataFromRequest({ req, secret })
const { event, payload } = data
if (event === 'health_check') {
res.statusCode = 200
res.setHeader('Content-Type', 'application/json')
res.end(JSON.stringify({ ok: 1 }))
return
}
const { type: event, payload } = data
handler.emitter.emit(event as BusinessEvents, payload)
handler.emitter.emit('*', {
@@ -84,9 +78,9 @@ export const readDataFromRequest = async ({
if (isValid) {
return {
event: event as BusinessEvents | 'health_check',
payload: obj,
}
type: event as BusinessEvents,
payload: obj as any,
} as GenericEvent
} else {
console.error('revice a invalidate webhook payload', req.headers)
throw new InvalidSignatureError()

View File

@@ -45,6 +45,7 @@ export interface EventPayloadMapping {
[BusinessEvents.COMMENT_CREATE]: Omit<CommentModel, 'ref'> & {
ref: Id | PostModel | PageModel | NoteModel | RecentlyModel
}
'health-check': {}
}
export interface IActivityLike {
@@ -77,3 +78,4 @@ export type GenericEvent =
ref: Id | PostModel | PageModel | NoteModel | RecentlyModel
}
}
| { type: 'health-check'; payload: {} }