refactor: api key response dto (#30887)

This commit is contained in:
Jason Rasmussen 2026-08-20 11:23:12 -04:00 committed by GitHub
parent a066b5f301
commit 47c5a3dbf6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 104 additions and 37 deletions

View file

@ -1,4 +1,5 @@
import { createZodDto } from 'nestjs-zod';
import { HistoryBuilder } from 'src/decorators';
import { Permission } from 'src/enum';
import { isoDatetimeToDate } from 'src/validation';
import z from 'zod';
@ -31,8 +32,9 @@ const ApiKeyResponseSchema = z
const ApiKeyCreateResponseSchema = z
.object({
...ApiKeyResponseSchema.shape,
secret: z.string().describe('API key secret (only shown once)'),
apiKey: ApiKeyResponseSchema,
apiKey: ApiKeyResponseSchema.meta({ ...new HistoryBuilder().added('v1').deprecated('v3.2.0').getExtensions() }),
})
.meta({ id: 'ApiKeyCreateResponseDto' });

View file

@ -23,8 +23,9 @@ export class ApiKeyService extends BaseService {
userId: auth.user.id,
permissions: dto.permissions,
});
const apiKey = this.map(entity);
return { secret: token, apiKey: this.map(entity) };
return { ...apiKey, secret: token, apiKey };
}
async update(auth: AuthDto, id: string, dto: ApiKeyUpdateDto): Promise<ApiKeyResponseDto> {
@ -58,10 +59,10 @@ export class ApiKeyService extends BaseService {
const token = this.cryptoRepository.randomBytesAsText(32);
const hashed = this.cryptoRepository.hashSha256(token);
const newKey = await this.apiKeyRepository.update(auth.user.id, id, { key: hashed });
const apiKey = this.map(newKey);
return { secret: token, apiKey: this.map(newKey) };
return { ...apiKey, secret: token, apiKey };
}
async delete(auth: AuthDto, id: string): Promise<void> {