Skip to content

Commit

Permalink
update: update contents
Browse files Browse the repository at this point in the history
  • Loading branch information
simon1uo committed Jun 10, 2022
1 parent e3a82fb commit 6ca0cc3
Show file tree
Hide file tree
Showing 6 changed files with 744 additions and 42 deletions.
54 changes: 20 additions & 34 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
---
title: 🌑 初始化 TypeScript 库项目
date: 2022-06-10 19:55:42
permalink: /pages/a07897/
categories:
- 🚶🏻 前端巩固基础
- 🚟 axios
- 💽 使用 TypeScript 封装 axios 库
tags:
-
---
🍞 相关资源:

+ **TypeScript library starter** [alexjoverm/typescript-library-starter: Starter kit with zero-config for building a library in TypeScript, featuring RollupJS, Jest, Prettier, TSLint, Semantic Release, and more! (github.com)](https://github.com/alexjoverm/typescript-library-starter)

开发 TypeScript 基础库脚手架工具,快速初始化 TypeScript 项目。



## 创建项目

clone `typescript-library-starter` 项目:

```shell
git clone [email protected]:alexjoverm/typescript-library-starter.git axios-typescript
```



> 文件目录相关:
>
> ```
> ├── CONTRIBUTING.md
> ├── LICENSE
> ├── README.md
> ├── code-of-conduct.md
> ├── node_modules
> ├── package-lock.json
> ├── package.json
> ├── rollup.config.ts // rollup 配置文件
> ├── src // 源码目录
> ├── test // 测试目录
> ├── tools // 发布到 GitHup pages 以及 发布到 npm 的一些配置脚本工具
> ├── tsconfig.json // TypeScript 编译配置文件
> └── tslint.json // TypeScript lint 文件
> ```
>
> 集成工具:
>
> + [rollup.js](https://rollupjs.org/guide/en/):打包工具;
> + [Prettier](https://github.com/prettier/prettier) / [TSLint](https://palantir.github.io/tslint/)格式化代码;
> + [TypeDoc](https://typedoc.org/):自动生成文档;
> + [Jest](https://jestjs.io/):单元测试;
> + [Commitizen](https://github.com/commitizen/cz-cli) :生成规范注释;
> + [Semantic release](https://github.com/semantic-release/semantic-release):管理版本和发布;
> + [husky](https://github.com/typicode/husky):git hooks 工具
> + [Conventional changelog ](https://github.com/conventional-changelog/conventional-changelog):生成 change log;
## 入口文件
删除原来 `src` 的文件,创建整个库入口文件 `index.ts`。先定义一个 `axios` 方法,并默认导出:
```ts
function axios(config) {
}
export default axios
```


要给 `config` 参数定义一种接口类型。创建 `types` 目录存放类型接口。

定义 `AxiosRequestConfig` 接口类型和请求方法字符串字面量类型 `Method`

```ts
export interface AxiosRequestConfig {
url: string
method?: Method
data?: any
params?: any
}

export type Method =
'get'
| 'GET'
| 'post'
| 'POST'
| 'delete'
| 'DELETE'
| 'head'
| 'HEAD'
| 'options'
| 'OPTIONS'
| 'put'
| 'PUT'
| 'patch'
| 'PATCH'
```
然后在入口文件 `index.ts` 引入类型作为 `config` 参数的类型:
Loading

0 comments on commit 6ca0cc3

Please sign in to comment.