19
packages/api-client/__tests__/controllers/act.test.ts
Normal file
19
packages/api-client/__tests__/controllers/act.test.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
import { mockRequestInstance } from '~/__tests__/helpers/instance'
|
||||
import { mockResponse } from '~/__tests__/helpers/response'
|
||||
import { AckController } from '~/controllers'
|
||||
|
||||
describe('test ack client', () => {
|
||||
const client = mockRequestInstance(AckController)
|
||||
|
||||
test('POST /ack', async () => {
|
||||
mockResponse('/ack', {}, 'post', {
|
||||
type: 'read',
|
||||
payload: {
|
||||
type: 'note',
|
||||
id: '11',
|
||||
},
|
||||
})
|
||||
|
||||
await expect(client.ack.read('note', '11')).resolves.not.toThrowError()
|
||||
})
|
||||
})
|
||||
@@ -7,7 +7,7 @@ import type { AxiosResponse } from 'axios'
|
||||
import { axiosAdaptor } from '~/adaptors/axios'
|
||||
import { umiAdaptor } from '~/adaptors/umi-request'
|
||||
import {
|
||||
allContollerNames,
|
||||
allControllerNames,
|
||||
allControllers,
|
||||
NoteController,
|
||||
PostController,
|
||||
@@ -83,7 +83,7 @@ describe('test client', () => {
|
||||
|
||||
it('should throw error if not inject other client', () => {
|
||||
const client = generateClient()
|
||||
allContollerNames.forEach((name) => {
|
||||
allControllerNames.forEach((name) => {
|
||||
expect(() => (client as any)[name].name).toThrow(
|
||||
`${
|
||||
name.charAt(0).toUpperCase() + name.slice(1)
|
||||
@@ -96,7 +96,7 @@ describe('test client', () => {
|
||||
const client = generateClient()
|
||||
|
||||
client.injectControllers(allControllers)
|
||||
allContollerNames.forEach((name) => {
|
||||
allControllerNames.forEach((name) => {
|
||||
expect(() => (client as any)[name].name).toBeDefined()
|
||||
})
|
||||
})
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { inspect } from 'util'
|
||||
import isEqual from 'lodash/isEqual'
|
||||
import { vi } from 'vitest'
|
||||
import type { URLSearchParams } from 'url'
|
||||
@@ -29,9 +30,9 @@ export const mockResponse = <T>(
|
||||
const { data } = options || {}
|
||||
if (!isEqual(requestBody, data)) {
|
||||
throw new Error(
|
||||
`body not equal, got: ${JSON.stringify(
|
||||
data,
|
||||
)} except: ${JSON.stringify(requestBody)}`,
|
||||
`body not equal, got: ${inspect(data)} except: ${inspect(
|
||||
requestBody,
|
||||
)}`,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
44
packages/api-client/controllers/ack.ts
Normal file
44
packages/api-client/controllers/ack.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
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,
|
||||
> {
|
||||
ack: AckController<ResponseWrapper>
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @support core >= 4.4.0
|
||||
*/
|
||||
export class AckController<ResponseWrapper> implements IController {
|
||||
base = 'ack'
|
||||
name = 'ack'
|
||||
|
||||
constructor(private client: HTTPClient) {
|
||||
autoBind(this)
|
||||
}
|
||||
|
||||
public get proxy(): IRequestHandler<ResponseWrapper> {
|
||||
return this.client.proxy(this.base)
|
||||
}
|
||||
|
||||
read(type: 'post' | 'note', id: string) {
|
||||
return this.proxy.ack.post<never>({
|
||||
data: {
|
||||
type: 'read',
|
||||
payload: {
|
||||
type,
|
||||
id,
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
import { AckController } from './ack'
|
||||
import { ActivityController } from './activity'
|
||||
import { AggregateController } from './aggregate'
|
||||
import { CategoryController } from './category'
|
||||
@@ -21,6 +22,7 @@ import { TopicController } from './topic'
|
||||
import { UserController } from './user'
|
||||
|
||||
export const allControllers = [
|
||||
AckController,
|
||||
ActivityController,
|
||||
AggregateController,
|
||||
CategoryController,
|
||||
@@ -40,7 +42,8 @@ export const allControllers = [
|
||||
UserController,
|
||||
]
|
||||
|
||||
export const allContollerNames = [
|
||||
export const allControllerNames = [
|
||||
'ack',
|
||||
'activity',
|
||||
'aggregate',
|
||||
'category',
|
||||
@@ -66,6 +69,7 @@ export const allContollerNames = [
|
||||
] as const
|
||||
|
||||
export {
|
||||
AckController,
|
||||
ActivityController,
|
||||
AggregateController,
|
||||
CategoryController,
|
||||
|
||||
@@ -12,7 +12,7 @@ import { isPlainObject } from '~/utils'
|
||||
import { camelcaseKeys } from '~/utils/camelcase-keys'
|
||||
import { resolveFullPath } from '~/utils/path'
|
||||
|
||||
import { allContollerNames } from '../controllers'
|
||||
import { allControllerNames } from '../controllers'
|
||||
import { attachRequestMethod } from './attach-request'
|
||||
import { RequestError } from './error'
|
||||
|
||||
@@ -42,7 +42,7 @@ class HTTPClient<
|
||||
}
|
||||
|
||||
private initGetClient() {
|
||||
for (const name of allContollerNames) {
|
||||
for (const name of allControllerNames) {
|
||||
Object.defineProperty(this, name, {
|
||||
get() {
|
||||
const client = Reflect.get(this, `${methodPrefix}${name}`)
|
||||
|
||||
Reference in New Issue
Block a user