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