(apiKey: string)
| 412 | * Verify User Api Key |
| 413 | */ |
| 414 | export async function verifyUserApiKey(apiKey: string) { |
| 415 | const result = await prisma.userApiKey.findUnique({ |
| 416 | where: { |
| 417 | apiKey, |
| 418 | }, |
| 419 | select: { |
| 420 | user: true, |
| 421 | expiredAt: true, |
| 422 | }, |
| 423 | }); |
| 424 | |
| 425 | if (result?.expiredAt && result.expiredAt.valueOf() < Date.now()) { |
| 426 | throw new Error('Api Key has been expired.'); |
| 427 | } |
| 428 | |
| 429 | if (!result) { |
| 430 | throw new Error( |
| 431 | 'Api Key not found, input api key: ' + apiKey.slice(0, 10) + '...' |
| 432 | ); |
| 433 | } |
| 434 | |
| 435 | prisma.userApiKey |
| 436 | .update({ |
| 437 | where: { |
| 438 | apiKey, |
| 439 | }, |
| 440 | data: { |
| 441 | usage: { |
| 442 | increment: 1, |
| 443 | }, |
| 444 | }, |
| 445 | }) |
| 446 | .catch((err) => { |
| 447 | logger.error('Failed to update API key usage', err); |
| 448 | }); |
| 449 | |
| 450 | return result.user; |
| 451 | } |
no test coverage detected