# forget-api

> 无需封装 Axios，无需写接口请求函数，无需维护返回值类型，把这些琐碎重复的事情交给工具来处理，让精力聚焦在核心功能的实现上。

Latest version **0.0.0-alpha.2** (published 2022-08-11) · MulanPSL-2.0 license · 0 weekly downloads

## Install

```sh
npm install forget-api
pnpm add forget-api
yarn add forget-api
bun add forget-api
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.0-alpha.2 |
| Published | 2022-08-11 |
| First published | 2022-08-11 |
| Weekly downloads | 0 |
| License | MulanPSL-2.0 |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 39.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Ambit Tsai |
| Maintainers | ambit_tsai |
| Keywords | apiless, api-less, noapi, no-api |

## Links

- npm: https://www.npmjs.com/package/forget-api
- Repository: https://github.com/ambit-tsai/forget-api
- Homepage: https://github.com/ambit-tsai/forget-api#readme
- Issues: https://github.com/ambit-tsai/forget-api/issues
- npm.io page: https://npm.io/package/forget-api

## Recent versions

- 0.0.0-alpha.2 (latest) — 2022-08-11
- 0.0.0-alpha.1 — 2022-08-11

## README

# Forget API
无需封装 Axios，无需写接口请求函数，无需维护返回值类型，把这些琐碎重复的事情交给工具来处理，让精力聚焦在核心功能的实现上。


## Install
```shell
npm i -S forget-api
```

## Usage
以`NestJS`+`Vite`技术栈为例，假设目录结构如下：
```text
root/
 │
 ├─ backend/
 │   ├─ src/
 │       ├─ user.controller.ts
 │
 ├─ frontend/
     ├─ tsconfig.json
     ├─ vite.config.ts
     ├─ src/
         ├─ main.ts
```

在控制器中，利用`Mark`标记请求参数类型，通过`defineExpose`暴露 API 方法
```ts
// "backend/src/user.controller.ts"
import { defineExpose, Mark } from 'forget-api/nest';

@Controller('user')
export class UserController {
    @Get(':id')
    getUserInfo(@Param() params: Mark<{ id: string }>) {
        return { code: 200, data: { id } };
    }
}

export default defineExpose(UserController);
```

在`frontend/tsconfig.json`配置别名映射到后端目录
```json
{
    "compilerOptions": {
        "paths": {
            "@@api/*": ["../backend/src/*"]
        }
    },
}
```

在 Vite 中引入定制插件对别名进行解析
```ts
import { vitePluginForNest } from 'forget-api/nest/vite';

export default defineConfig({
    plugins: [
        vitePluginForNest({
            alias: '@@api',
            baseUrl: 'http://localhost:3000',
        })
    ],
});
```

在前端调用接口
```ts
// "frontend/src/main.ts"
import userApis from '@@api/user.controller';

await userApis.getUserInfo({ id: '123' });
// 发起请求：GET http://localhost:3000/user/123
```

> 借助于插件的转换，前端发起请求像是直接调用控制器的方法，使用相同的方法名、参数类型、返回值类型，类似于 RPC


## Principle
1. 通过插件监听`@@api/`相关的模块导入，根据`tsconfig.json`配置将模块解析成正确的文件路径，并以**文本**的形式载入；
1. 将上述的文本解析成 TypeScript AST，查找导出的控制器类及其方法和装饰器，生成如下代码：
```ts
// "@@api/user.controller"
import { createApis } from 'forget-api/request';
export default createApis('/user', {
    getUserInfo: ['GET', '/:id'],
});
```
<a href="docs/request.md">forget-api/request</a> 是对 fetch 的简单封装。

---
_Source: https://npm.io/package/forget-api · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
