refactor: migrate api key module to DML (#10450)

Fixes: FRMW-2827
This commit is contained in:
Harminder Virk
2024-12-05 22:07:54 +05:30
committed by GitHub
parent 559fc6587a
commit 70d77ea22f
5 changed files with 115 additions and 117 deletions

View File

@@ -5,6 +5,7 @@ import {
FilterableApiKeyProps,
FindConfig,
IApiKeyModuleService,
InferEntityType,
InternalModuleDeclaration,
ModuleJoinerConfig,
ModulesSdkTypes,
@@ -46,7 +47,9 @@ export class ApiKeyModuleService
implements IApiKeyModuleService
{
protected baseRepository_: DAL.RepositoryService
protected readonly apiKeyService_: ModulesSdkTypes.IMedusaInternalService<ApiKey>
protected readonly apiKeyService_: ModulesSdkTypes.IMedusaInternalService<
InferEntityType<typeof ApiKey>
>
constructor(
{ baseRepository, apiKeyService }: InjectedDependencies,
@@ -138,7 +141,7 @@ export class ApiKeyModuleService
protected async createApiKeys_(
data: ApiKeyTypes.CreateApiKeyDTO[],
@MedusaContext() sharedContext: Context = {}
): Promise<[ApiKey[], TokenDTO[]]> {
): Promise<[InferEntityType<typeof ApiKey>[], TokenDTO[]]> {
await this.validateCreateApiKeys_(data, sharedContext)
const normalizedInput: CreateApiKeyDTO[] = []
@@ -276,7 +279,7 @@ export class ApiKeyModuleService
protected async updateApiKeys_(
normalizedInput: UpdateApiKeyInput[],
@MedusaContext() sharedContext: Context = {}
): Promise<ApiKey[]> {
): Promise<InferEntityType<typeof ApiKey>[]> {
const updateRequest = normalizedInput.map((k) => ({
id: k.id,
title: k.title,
@@ -387,7 +390,7 @@ export class ApiKeyModuleService
async revoke_(
normalizedInput: RevokeApiKeyInput[],
@MedusaContext() sharedContext: Context = {}
): Promise<ApiKey[]> {
): Promise<InferEntityType<typeof ApiKey>[]> {
await this.validateRevokeApiKeys_(normalizedInput)
const updateRequest = normalizedInput.map((k) => {
@@ -433,7 +436,7 @@ export class ApiKeyModuleService
protected async authenticate_(
token: string,
@MedusaContext() sharedContext: Context = {}
): Promise<ApiKey | false> {
): Promise<InferEntityType<typeof ApiKey> | false> {
// Since we only allow up to 2 active tokens, getitng the list and checking each token isn't an issue.
// We can always filter on the redacted key if we add support for an arbitrary number of tokens.
const secretKeys = await this.apiKeyService_.list(
@@ -617,8 +620,8 @@ export class ApiKeyModuleService
// We are mutating the object here as what microORM relies on non-enumerable fields for serialization, among other things.
const omitToken = (
// We have to make salt optional before deleting it (and we do want it required in the DB)
key: Omit<ApiKey, "salt"> & { salt?: string }
): Omit<ApiKey, "salt"> => {
key: Omit<InferEntityType<typeof ApiKey>, "salt"> & { salt?: string }
): Omit<InferEntityType<typeof ApiKey>, "salt"> => {
key.token = key.type === ApiKeyType.SECRET ? "" : key.token
delete key.salt
return key