补充快手OpenAPI集成日志
This commit is contained in:
@@ -58,6 +58,7 @@ export async function consumeCallback(
|
||||
apiMethod: 'integration.callback.virtual.eticket.consume',
|
||||
path: '/integration/callback/virtual/eticket/consume',
|
||||
bizParams,
|
||||
disableDefaultLog: true,
|
||||
...(input.sellerId ? { sellerId: input.sellerId } : {}),
|
||||
onRequest: (request) => {
|
||||
logInfo('[kuaishou-industry/consume-callback]', `发起核销回调 oid=${input.oid}`, {
|
||||
|
||||
@@ -41,6 +41,7 @@ export async function destroyCallback(
|
||||
apiMethod: 'integration.callback.virtual.eticket.destroy',
|
||||
path: '/integration/callback/virtual/eticket/destroy',
|
||||
bizParams,
|
||||
disableDefaultLog: true,
|
||||
...(input.sellerId ? { sellerId: input.sellerId } : {}),
|
||||
onRequest: (request) => {
|
||||
logInfo('[kuaishou-industry/destroy-callback]', `发起销毁回调 oid=${input.oid}`, {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { getKuaishouIndustryConfig } from './config.js'
|
||||
import { signKuaishouIndustryPayload, type SignMethod } from './crypto.js'
|
||||
import { ensureKuaishouIndustryAccessToken } from './token-service.js'
|
||||
import { logIntegration } from '../../../utils/logger.js'
|
||||
|
||||
export type JsonObject = Record<string, any>
|
||||
|
||||
@@ -12,6 +13,7 @@ type KuaishouIndustryOpenApiCallInput = {
|
||||
sellerId?: string
|
||||
signMethod?: SignMethod
|
||||
onRequest?: (request: JsonObject) => void
|
||||
disableDefaultLog?: boolean
|
||||
}
|
||||
|
||||
export type KuaishouIndustryOpenApiCallResult = {
|
||||
@@ -33,6 +35,11 @@ export async function requestKuaishouIndustryOpenApi(
|
||||
let config = getKuaishouIndustryConfig()
|
||||
|
||||
if (!config.enabled) {
|
||||
logKuaishouOpenApi(
|
||||
input,
|
||||
`快手 OpenAPI 未启用,跳过 ${input.apiMethod}`,
|
||||
undefined,
|
||||
)
|
||||
return { success: true, skippedReason: 'disabled' }
|
||||
}
|
||||
|
||||
@@ -47,6 +54,12 @@ export async function requestKuaishouIndustryOpenApi(
|
||||
accessToken: tokenResult.accessToken,
|
||||
}
|
||||
} catch (error) {
|
||||
logKuaishouOpenApi(
|
||||
input,
|
||||
`快手 OpenAPI token 准备失败 ${input.apiMethod}`,
|
||||
resolveKuaishouOpenApiErrorDetail(error),
|
||||
'warn',
|
||||
)
|
||||
return {
|
||||
success: false,
|
||||
skippedReason: 'token_error',
|
||||
@@ -56,6 +69,12 @@ export async function requestKuaishouIndustryOpenApi(
|
||||
}
|
||||
|
||||
if (!config.accessToken) {
|
||||
logKuaishouOpenApi(
|
||||
input,
|
||||
`快手 OpenAPI accessToken 缺失 ${input.apiMethod}`,
|
||||
undefined,
|
||||
'warn',
|
||||
)
|
||||
return {
|
||||
success: false,
|
||||
skippedReason: 'missing_access_token',
|
||||
@@ -91,6 +110,7 @@ export async function requestKuaishouIndustryOpenApi(
|
||||
})
|
||||
|
||||
input.onRequest?.(requestLog)
|
||||
logKuaishouOpenApi(input, `发起快手 OpenAPI ${input.apiMethod}`, requestLog)
|
||||
|
||||
try {
|
||||
const startedAt = Date.now()
|
||||
@@ -110,19 +130,44 @@ export async function requestKuaishouIndustryOpenApi(
|
||||
}
|
||||
|
||||
const durationMs = Date.now() - startedAt
|
||||
const success = res.ok && Number(json.result) === 1
|
||||
logKuaishouOpenApi(
|
||||
input,
|
||||
success
|
||||
? `快手 OpenAPI 调用成功 ${input.apiMethod}`
|
||||
: `快手 OpenAPI 调用失败 ${input.apiMethod}`,
|
||||
{
|
||||
durationMs,
|
||||
httpStatus: res.status,
|
||||
request: requestLog,
|
||||
response: json,
|
||||
},
|
||||
success ? 'info' : 'warn',
|
||||
)
|
||||
|
||||
return {
|
||||
success: res.ok && Number(json.result) === 1,
|
||||
success,
|
||||
response: json,
|
||||
request: requestLog,
|
||||
durationMs,
|
||||
httpStatus: res.status,
|
||||
}
|
||||
} catch (error) {
|
||||
const errorDetail = resolveKuaishouOpenApiErrorDetail(error)
|
||||
logKuaishouOpenApi(
|
||||
input,
|
||||
`快手 OpenAPI 调用异常 ${input.apiMethod}`,
|
||||
{
|
||||
...errorDetail,
|
||||
request: requestLog,
|
||||
},
|
||||
'warn',
|
||||
)
|
||||
return {
|
||||
success: false,
|
||||
error: error instanceof Error ? error.message : String(error),
|
||||
request: requestLog,
|
||||
errorDetail: resolveKuaishouOpenApiErrorDetail(error),
|
||||
errorDetail,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -193,3 +238,16 @@ function normalizeOpenApiPath(value: unknown): string {
|
||||
}
|
||||
return path.startsWith('/') ? path : `/${path}`
|
||||
}
|
||||
|
||||
function logKuaishouOpenApi(
|
||||
input: Pick<KuaishouIndustryOpenApiCallInput, 'apiMethod' | 'disableDefaultLog'>,
|
||||
message: string,
|
||||
detail?: JsonObject,
|
||||
level: 'info' | 'warn' | 'error' = 'info',
|
||||
) {
|
||||
if (input.disableDefaultLog) {
|
||||
return
|
||||
}
|
||||
|
||||
logIntegration('[kuaishou-industry/openapi]', message, detail, { level })
|
||||
}
|
||||
|
||||
@@ -49,6 +49,7 @@ export async function sendCallback(input: SendCallbackInput): Promise<{ success:
|
||||
apiMethod: 'integration.callback.virtual.eticket.send',
|
||||
path: '/integration/callback/virtual/eticket/send',
|
||||
bizParams,
|
||||
disableDefaultLog: true,
|
||||
...(input.sellerId ? { sellerId: input.sellerId } : {}),
|
||||
onRequest: (request) => {
|
||||
const requestLog = {
|
||||
|
||||
Reference in New Issue
Block a user