Nubank API 开源项目教程
1. 项目的目录结构及介绍
nubank-api/
├── README.md
├── package.json
├── src/
│ ├── index.ts
│ ├── api/
│ │ ├── auth.ts
│ │ ├── bill.ts
│ │ └── utils.ts
│ └── types/
│ └── index.ts
└── tsconfig.json
- README.md: 项目说明文档。
- package.json: 项目依赖和脚本配置文件。
- src/: 源代码目录。
- index.ts: 项目入口文件。
- api/: API 相关模块。
- auth.ts: 认证模块。
- bill.ts: 账单查询模块。
- utils.ts: 工具函数模块。
- types/: 类型定义文件。
- tsconfig.json: TypeScript 配置文件。
2. 项目的启动文件介绍
项目的启动文件是 src/index.ts
。该文件主要负责初始化 API 连接和提供对外的接口。以下是 index.ts
的主要内容:
import { initAuth } from './api/auth';
import { getBillDetails } from './api/bill';
export async function start() {
const authToken = await initAuth('your-cpf', 'your-password');
const billDetails = await getBillDetails(authToken);
console.log(billDetails);
}
start();
- initAuth: 初始化认证,获取认证令牌。
- getBillDetails: 使用认证令牌获取账单详情。
3. 项目的配置文件介绍
项目的配置文件主要是 package.json
和 tsconfig.json
。
package.json
{
"name": "nubank-api",
"version": "1.0.0",
"scripts": {
"start": "ts-node src/index.ts"
},
"dependencies": {
"axios": "^0.21.1",
"ts-node": "^9.1.1",
"typescript": "^4.1.3"
}
}
- scripts: 定义了启动脚本
start
,使用ts-node
运行src/index.ts
。 - dependencies: 项目依赖,包括
axios
用于 HTTP 请求,ts-node
用于直接运行 TypeScript 文件,typescript
用于 TypeScript 编译。
tsconfig.json
{
"compilerOptions": {
"target": "ES6",
"module": "commonjs",
"outDir": "./dist",
"strict": true,
"esModuleInterop": true
},
"include": ["src/**/*"]
}
- compilerOptions: TypeScript 编译选项。
- target: 编译目标为 ES6。
- module: 使用 commonjs 模块系统。
- outDir: 编译输出目录为
dist
。 - strict: 开启严格模式。
- esModuleInterop: 支持 ES 模块 interoperability。
- include: 包含
src
目录下的所有文件。