Files
core/packages/api-client
Innei 99781842e1 chore(deps): update dependencies for improved compatibility and functionality
- Upgraded various dependencies across multiple package.json files, including @babel/core to 7.28.0, @keyv/redis to 4.6.0, and mongoose to 8.16.2.
- Updated axios to version 1.10.0 and nodemailer to 7.0.5 for enhanced performance.
- Adjusted types for @types/lodash and @types/node to their latest versions for better type safety.
- Incremented better-auth to 1.2.12 and updated other related packages for stability.

Signed-off-by: Innei <tukon479@gmail.com>
2025-07-10 22:22:49 +08:00
..
2025-04-06 22:18:48 +08:00
2025-04-06 22:18:48 +08:00
2025-04-06 22:18:48 +08:00
2023-06-28 14:43:52 +08:00
2024-04-29 16:00:20 +08:00
2025-04-06 22:18:48 +08:00
2022-12-23 14:59:20 +08:00
2025-04-06 22:18:48 +08:00
2025-04-06 22:18:48 +08:00
2025-04-06 22:18:48 +08:00
2025-04-06 22:18:48 +08:00
2024-04-29 16:00:20 +08:00

MApi Client

这是一个适用于 MServer v3 的 JS SDK封装了常用接口请求方法以及返回类型的声明以快速开发前端应用。

迁移到 v1

不再提供 camelcase-keys 的 re-export此库不再依赖 camelcase-keys 库,如有需要可自行安装。

- import { camelcaseKeysDeep, camelcaseKeys } from '@mx-space/api-client'
+ import { simpleCamelcaseKeys as camelcaseKeysDeep } from '@mx-space/api-client'

如何使用

此 SDK 框架无关,不捆绑任何一个网络请求库,只需要提供适配器。你需要手动传入符合接口标准的适配器。

此项目提供 axiosumi-request 两个适配器。

axios 为例。

import {
  AggregateController,
  allControllers, // ...
  CategoryController,
  createClient,
  NoteController,
  PostController,
} from '@mx-space/api-client'
import { axiosAdaptor } from '@mx-space/api-client/adaptors/axios'

const endpoint = 'https://api.innei.dev/v2'
const client = createClient(axiosAdaptor)(endpoint)

// `default` is AxiosInstance
// you can do anything else on this
// interceptor or re-configure
const $axios = axiosAdaptor.default
// re-config (optional)
$axios.defaults.timeout = 10000
// set interceptors (optional)
$axios.interceptors.request.use(
  (config) => {
    const token = getToken()
    if (token) {
      config.headers!.Authorization = `bearer ${  getToken()}`
    }

    return config
  },
  (error) => {
    if (__DEV__) {
      console.log(error.message)
    }

    return Promise.reject(error)
  },
)

// inject controller first.
client.injectControllers([
  PostController,
  NoteController,
  AggregateController,
  CategoryController,
])

// or you can inject allControllers
client.injectControllers(allControllers)

// then you can request `post` `note` and `aggregate` controller

client.post.post.getList(page, 10, { year }).then((data) => {
  // do anything
})

为什么要手动注入控制器

按需加载,可以减少打包体积 (Tree Shake)

为什么不依赖请求库

可以防止项目中出现两个请求库,减少打包体积

如果不使用 axios应该如何编写适配器

参考 src/adaptors/axios.tssrc/adaptors/umi-request.ts

如何使用 proxy 来访问 sdk 内未包含的请求

如请求 GET /notes/something/other/123456/info,可以使用

client.note.proxy.something.other('123456').info.get()

从 proxy 获取请求地址但不发出

client.note.proxy.something.other('123456').info.toString() // /notes/something/other/123456/info

client.note.proxy.something.other('123456').info.toString(true) // http://localhost:2333/notes/something/other/123456/info