egirlskey/packages/backend/src/server/api/endpoints/i/authorized-apps.ts
2022-09-24 07:12:11 +09:00

52 lines
1.4 KiB
TypeScript

import { Inject, Injectable } from '@nestjs/common';
import { IsNull, Not } from 'typeorm';
import { Endpoint } from '@/server/api/endpoint-base.js';
import type { 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,
appId: Not(IsNull()),
},
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,
})));
});
}
}