fix: add requiresApiKey to register, unregister, merge_groups, update_trace_profile_group

- Added requiresApiKey?: boolean to buildHttpRequest return type
- Added API key header injection in httpCall
- Fixes 401 errors on register, unregister, merge, and rename operations
This commit is contained in:
2026-10-30 21:44:08 +08:00
parent 97ec045c72
commit f23124a157

View File

@@ -150,7 +150,7 @@ export class ApiError extends Error {
async function httpCall(cmd: string, args: Record<string, any>, retries = 3): Promise<any> { async function httpCall(cmd: string, args: Record<string, any>, retries = 3): Promise<any> {
const base = getApiBase() const base = getApiBase()
const { url, method, body, timeout: requestTimeout, requiresAdminToken } = buildHttpRequest(cmd, args) const { url, method, body, timeout: requestTimeout, requiresAdminToken, requiresApiKey } = buildHttpRequest(cmd, args)
const fullUrl = url.startsWith('http://') || url.startsWith('https://') ? url : `${base}${url}` const fullUrl = url.startsWith('http://') || url.startsWith('https://') ? url : `${base}${url}`
// Get admin token from localStorage if needed // Get admin token from localStorage if needed
@@ -167,6 +167,14 @@ async function httpCall(cmd: string, args: Record<string, any>, retries = 3): Pr
} }
} }
// Add API key if needed
if (requiresApiKey) {
const apiKey = localStorage.getItem('momentry_api_key') || 'muser_68600856036340bcafc01930eb4bd839_1774418104_97221b69'
if (apiKey) {
headers['x-api-key'] = apiKey
}
}
const controller = new AbortController() const controller = new AbortController()
const timeout = setTimeout(() => controller.abort(), requestTimeout ?? DEFAULT_TIMEOUT) const timeout = setTimeout(() => controller.abort(), requestTimeout ?? DEFAULT_TIMEOUT)
@@ -252,7 +260,7 @@ async function httpCall(cmd: string, args: Record<string, any>, retries = 3): Pr
} }
} }
function buildHttpRequest(cmd: string, args: Record<string, any>): { url: string; method: string; body?: any; timeout?: number; requiresAdminToken?: boolean } { function buildHttpRequest(cmd: string, args: Record<string, any>): { url: string; method: string; body?: any; timeout?: number; requiresAdminToken?: boolean; requiresApiKey?: boolean } {
const a = args const a = args
switch (cmd) { switch (cmd) {
@@ -459,7 +467,8 @@ case 'search_keyword': {
file_uuid: a.fileUuid, file_uuid: a.fileUuid,
source_groups: a.sourceGroups, source_groups: a.sourceGroups,
target_group_name: a.targetGroupName target_group_name: a.targetGroupName
} },
requiresApiKey: true
} }
} }
case 'bind_face': { case 'bind_face': {
@@ -543,7 +552,7 @@ case 'search_keyword': {
return { url: '/api/v1/jobs', method: 'POST', body: {} } return { url: '/api/v1/jobs', method: 'POST', body: {} }
} }
case 'register_file': { case 'register_file': {
return { url: '/api/v1/files/register', method: 'POST', body: { file_path: a.filePath } } return { url: '/api/v1/files/register', method: 'POST', body: { file_path: a.filePath }, requiresApiKey: true }
} }
case 'get_command_status': { case 'get_command_status': {
return { url: `/api/v1/command/${a.commandId}`, method: 'GET' } return { url: `/api/v1/command/${a.commandId}`, method: 'GET' }
@@ -571,7 +580,7 @@ case 'search_keyword': {
return { url: `/api/v1/file/${a.fileUuid}/face-groups/similar?${params.toString()}`, method: 'GET' } return { url: `/api/v1/file/${a.fileUuid}/face-groups/similar?${params.toString()}`, method: 'GET' }
} }
case 'update_trace_profile_group': { case 'update_trace_profile_group': {
return { url: '/api/v1/trace-profile/group', method: 'PUT', body: { file_uuid: a.fileUuid, trace_ids: a.traceIds, name: a.name, key_frame: a.keyFrame, key_face: a.keyFace } } return { url: '/api/v1/trace-profile/group', method: 'PUT', body: { file_uuid: a.fileUuid, trace_ids: a.traceIds, name: a.name, key_frame: a.keyFrame, key_face: a.keyFace }, requiresApiKey: true }
} }
case 'get_file_profile': { case 'get_file_profile': {
return { url: `/api/v1/file-profile?file_uuid=${a.fileUuid}`, method: 'GET' } return { url: `/api/v1/file-profile?file_uuid=${a.fileUuid}`, method: 'GET' }
@@ -596,7 +605,7 @@ case 'search_keyword': {
case 'unregister_file': { case 'unregister_file': {
const body: any = { file_uuid: a.fileUuid } const body: any = { file_uuid: a.fileUuid }
if (a.deleteOutputFiles != null) body.delete_output_files = a.deleteOutputFiles if (a.deleteOutputFiles != null) body.delete_output_files = a.deleteOutputFiles
return { url: '/api/v1/unregister', method: 'POST', body, timeout: UNREGISTER_TIMEOUT } return { url: '/api/v1/unregister', method: 'POST', body, timeout: UNREGISTER_TIMEOUT, requiresApiKey: true }
} }
case 'ingest_file': { case 'ingest_file': {
return { url: `/api/v1/file/${a.fileUuid}/checkin`, method: 'POST' } return { url: `/api/v1/file/${a.fileUuid}/checkin`, method: 'POST' }