hippofish/packages/backend/src/server/api/endpoints/i/authorized-apps.ts
2022-09-18 03:27:08 +09:00

50 lines
1.3 KiB
TypeScript

import { Inject, Injectable } from '@nestjs/common';
import { Endpoint } from '@/server/api/endpoint-base.js';
import { AccessTokensRepository } from '@/models/index.js';
import { AppEntityService } from '@/core/entities/AppEntityService.js';
import { DI } from '@/di-symbols.js';
export const meta = {
requireCredential: true,
secure: true,
} as const;
export const paramDef = {
type: 'object',
properties: {
limit: { type: 'integer', minimum: 1, maximum: 100, default: 10 },
offset: { type: 'integer', default: 0 },
sort: { type: 'string', enum: ['desc', 'asc'], default: 'desc' },
},
required: [],
} as const;
// eslint-disable-next-line import/no-default-export
@Injectable()
export default class extends Endpoint<typeof meta, typeof paramDef> {
constructor(
@Inject(DI.accessTokensRepository)
private accessTokensRepository: AccessTokensRepository,
private appEntityService: AppEntityService,
) {
super(meta, paramDef, async (ps, me) => {
// Get tokens
const tokens = await this.accessTokensRepository.find({
where: {
userId: me.id,
},
take: ps.limit,
skip: ps.offset,
order: {
id: ps.sort === 'asc' ? 1 : -1,
},
});
return await Promise.all(tokens.map(token => this.appEntityService.pack(token.appId, me, {
detail: true,
})));
});
}
}