feat: add api client for activity controller

This commit is contained in:
Innei
2023-08-06 19:24:00 +08:00
parent 03ca5c6592
commit 44ab9dc273
6 changed files with 66 additions and 1 deletions

View File

@@ -164,6 +164,9 @@ export class NoteController {
}
@Get('like/:id')
/**
* @deprecated
*/
async likeNote(
@Param() param: IntIdOrMongoIdDto,
@IpLocation() location: IpRecord,

View File

@@ -232,6 +232,9 @@ export class PostController {
return
}
/**
* @deprecated
*/
@Get('/_thumbs-up')
async thumbsUpArticle(
@Query() query: MongoIdDto,

View File

@@ -0,0 +1,15 @@
import { mockRequestInstance } from '~/__tests__/helpers/instance'
import { mockResponse } from '~/__tests__/helpers/response'
import { ActivityController } from '~/controllers'
describe('test activity client', () => {
const client = mockRequestInstance(ActivityController)
test('POST /like', async () => {
mockResponse('/activity/like', {}, 'post')
await expect(
client.activity.likeIt('Note', '11111111'),
).resolves.not.toThrowError()
})
})

View File

@@ -0,0 +1,39 @@
import type { IRequestAdapter } from '~/interfaces/adapter'
import type { IController } from '~/interfaces/controller'
import type { IRequestHandler } from '~/interfaces/request'
import { autoBind } from '~/utils/auto-bind'
import { HTTPClient } from '../core'
declare module '../core/client' {
interface HTTPClient<
T extends IRequestAdapter = IRequestAdapter,
ResponseWrapper = unknown,
> {
activity: ActivityController<ResponseWrapper>
}
}
/**
* @support core >= 4.3.0
*/
export class ActivityController<ResponseWrapper> implements IController {
base = 'activity'
name = 'activity'
constructor(private client: HTTPClient) {
autoBind(this)
}
public get proxy(): IRequestHandler<ResponseWrapper> {
return this.client.proxy(this.base)
}
likeIt(type: 'Post' | 'Note', id: string) {
return this.proxy.like.post<never>({
type,
id,
})
}
}

View File

@@ -9,11 +9,12 @@ import type {
TimelineData,
TimelineType,
} from '~/models/aggregate'
import type { HTTPClient } from '../core'
import { sortOrderToNumber } from '~/utils'
import { autoBind } from '~/utils/auto-bind'
import { HTTPClient } from '../core'
declare module '../core/client' {
interface HTTPClient<
T extends IRequestAdapter = IRequestAdapter,

View File

@@ -1,3 +1,4 @@
import { ActivityController } from './activity'
import { AggregateController } from './aggregate'
import { CategoryController } from './category'
import { CommentController } from './comment'
@@ -20,6 +21,7 @@ import { TopicController } from './topic'
import { UserController } from './user'
export const allControllers = [
ActivityController,
AggregateController,
CategoryController,
CommentController,
@@ -39,6 +41,7 @@ export const allControllers = [
]
export const allContollerNames = [
'activity',
'aggregate',
'category',
'comment',
@@ -63,6 +66,7 @@ export const allContollerNames = [
] as const
export {
ActivityController,
AggregateController,
CategoryController,
CommentController,