# api的组合配置

在Router和Interceptor配置章节已经详细介绍了各个配置项，其中api配置项支持类型比较多，并且使用会比较灵活，下面通过示例说明多种配置组合的方式。

#### 字符串类型值

```javascript
{
  api: '/api/articles',
  handle(data) {
    const list = data.articles; // data key由apiDataName配置决定

    return { list };
  }
}
```

api接口配置为一个地址，handle方法的data key按照[apiDataName(apiUrl)](/maty-js/api-fang-fa/maty-options.md#apidataname-apiurl-function)的规则，默认取api地址的最后一个单词，即'/api/articles'中的articles。

api地址前可以使用:get :post :put :delete前缀特别指定请求方法。未指定的默认按照ctx.method来请求。

#### 对象类型值

```javascript
{
  api: {
    api: '/api/articles',
    name: 'articleList',
    series: false,
    cache: false,
    query() { //... },
    body() { // ... },
    handle() { // ... }
  },
  handle(data) {
    const list = data.articleList; // data key优先由api.name配置决定

    return { list };
  }
}
```

api配置为对象，可以设置name项，handle方法的data key优先以name命名，否则使用apiDataName配置的规则生成。

api对象可拥有部分router对象的同名属性，其中api.name为独有，api.handle方法仅处理当前api接口返回的数据。api.handle方法返回的数据会传入router.handle继续处理。对于一个路由只有一个api接口的情况下，无需配置两个handle处理方法。

#### 数组类型值

```javascript
{
  api: [
    '/api/article/detail',
    {
      api: '/api/user/info',
      name: 'userInfo',
      series: true,
      handle(data, ctx) {
        return data;
      }
    },
    {
      api: '/api/author/info',
      query(ctx) {
        return { id: ctx.query.articleId }
      },
      handle(data, ctx) {
        return data;
      }
    },
    (ctx) => {
      if (ctx.query.commentShow) {
        return '/api/comments'
      }

      return null;
    },
    (ctx) => {
      return {
        api: '/api/relation-articles',
        name: 'articles'
      }
    }
  ],
  query(ctx) {
    return {
      userId: ctx.query.id,
      articleId: ctx.query.aid
    }
  },
  handle(data) {
    const articleDetail = data.detail;
    const userInfo = data.userInfo;
    const author = data.info;
    const comments = data.comments || [];
    const relationArticles = data.articles;

    return { articleDetail, userInfo, author, comments, relationArticles };
  }
}
```

当api为Array类型，Array item可以是String、 Object、 Function三种类型的集合，适用于一个路由有多个api接口提供数据的情况。其中Function类型只能返回String、Object两种类型值，可以为null值，表示不请求api接口。

#### 函数类型值

```javascript
{
  api(ctx) {
    const apis = [];

    if (ctx.query.show) {
      apis.push('/api/show/v3');
    }

    apis.push({
      api: '/api/list/v3',
      name: 'list'
    });

    return apis;
  }
}
```

当api为Function类型，可以返回以上3种类型，接收ctx为参数。使用Function类型，可以加入某些业务逻辑判断，从而有选择的返回相应api接口。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://tofishes.gitbook.io/maty-js/xiang-mu-shi-jian/api-de-zu-he-pei-zhi.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
