# alipay-mini-program-open-api-request-options
**Repository Path**: changeden/alipay-mini-program-open-api-request-options
## Basic Information
- **Project Name**: alipay-mini-program-open-api-request-options
- **Description**: 基于[Easy Open Api服务器开发框架](https://gitee.com/durcframework/easyopen)以及[alipay-mini-program-request-options](https://www.npmjs.com/package/alipay-mini-program-request-options)包的请求拓展
- **Primary Language**: Unknown
- **License**: Not specified
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 0
- **Created**: 2019-12-05
- **Last Updated**: 2022-05-23
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
# alipay-mini-program-open-api-request-options
> 基于[Easy Open Api服务器开发框架](https://gitee.com/durcframework/easyopen)以及[alipay-mini-program-request-options](https://www.npmjs.com/package/alipay-mini-program-request-options)包的请求拓展
## [alipay-mini-program-request-options](https://www.npmjs.com/package/alipay-mini-program-request-options)
> [网络请求]()基于[支付宝小程序网络请求Api](https://docs.alipay.com/mini/api/owycmh)的二次封装
> [文件上传]()基于[支付宝小程序文件上传Api](https://docs.alipay.com/mini/api/kmq4hc)的二次封装
## NPM信息
[



](https://www.npmjs.com/package/alipay-mini-program-open-api-request-options)
## 使用
### 添加默认配置
```js
const OpenApi = require('alipay-mini-program-open-api-request-options')
// 默认配置
const defaultConfig = {
version: '1.0',
app_key: 'test',
format: 'json',
app_secret: '123456',
url: `请求链接URL`
}
// 创建实例
const openApiInstance = new OpenApi(defaultConfig)
// 添加默认配置
openApiInstance.setConfig(defaultConfig)
```
### 网络请求
```js
// 创建请求实例
openApiInstance.newOptions()
.setName('apiName')
.setData({
keyword: 'test'
})
.setHeaders({
'content-type': 'application/json;charset=UTF-8'
})
.setTimeout(60000)
.request(function () {
console.log('completed');
})
.then(res => {
console.log(res);
})
.catch(err => {
console.error(err);
});
```
#### Api
|Name|Param name array|Param type array|Return|
| ------ | ------ | ------ | --- |
| setUrl | [url] | [String] | this |
| setName | [name] | [String] | this |
| setMethod | [method] | [String] | this |
| setHeaders | [headers] | [Object] | this |
| setTimeout | [timeout] | [Integer] | this |
| setData | [data] | [Object] | this |
| request | [completeCallback] | [Function] | Promise |
### 文件上传
```js
const { FileTypes } = require("alipay-mini-program-request-options");
// 目前支付宝只支持图片、视频、音频3种文件类型
const { IMAGE, VIDEO, AUDIO } = FileTypes;
openApiInstance.newUploadOptions()
.setName('apiName')
.setHeaders({})
.setFile(`文件路径`, `文件名`, IMAGE)
.upload(function () {
console.log('completed');
})
.then(res => {
console.log(res);
})
.catch(err => {
console.error(err);
});
```
#### Api
|Name|Param name array|Param type array|Return|
| ------ | ------ | ------ | --- |
| setUrl | [url] | [String] | this |
| setName | [name] | [String] | this |
| setHeaders | [headers] | [Object] | this |
| setFile | [filePath, fileName, fileType] | [String, String, String] | this |
| setFilePath | [filePath] | [String] | this |
| setFileName | [fileName] | [String] | this |
| setFileType | [fileType] | [String] | this |
| upload | [completeCallback] | [Function] | Promise |