1.0.5 • Published 8 years ago
general_fetch v1.0.5
通用读写请求库
封装通用读写方便使用,居于node-fetch
和es6-promise
封装同时支持浏览器和node.js环境。
浏览器支持需要支持fetch 和 Promise,对于不支持fetch 和 Promise的浏览器请自己注入fetch 和 Promise到全局。
安装
npm i general_fetch --save
全局配置
默认使用全局配置的里的配置作为参数,构造器里传入的参数优先级比全局配置高
globalOptions = {
/**
* CGI URL
*/
baseUrl: '',
/**
* select时分页查询时的起始页
*/
page: 0,
/**
* select时分页查询时每页的数量
*/
count: 20
};
// 先配置全局`baseUrl`
const GF = require('../index');
GF.globalOptions.baseUrl = 'http://ke.qq.com/cgi-bin/crm/general_access';
使用前必须先配置全局baseUrl
Select
查询数据构造器
/**
* 查询数据构造器
* @param sourceId 业务id,对应一张表,找后端要
*/
constructor(sourceId: number);
分页查询起始页
/**
* 分页查询起始页
* @param pageIndex
* @return {Select}
*/
page(pageIndex: number): Select;
分页查询每页数量
/**
* 分页查询每页数量
* @param count
* @return {Select}
*/
count(count: number): Select;
设置选中的字段
/**
* 设置选中的字段
* @param fields
* @return {Select}
*/
fields(fields: string[]): Select;
添加字段升序排序条件
/**
* 添加字段升序排序条件
* @param field
* @return {Select}
*/
sortAsc(field: any): Select;
添加字段降序排序条件
/**
* 添加字段降序排序条件
* @param field
* @return {Select}
*/
sortDesc(field: any): Select;
发生select请求,获得Promise
在发送请求前一定要设置过滤条件。
/**
* 发生select请求,获得Promise
* @return {Promise<SelectResponse>|Promise}
*/
send(): Promise<SelectResponse>;
Select 请求返回的结果
interface SelectResponse {
/**
* 分页查询每页数量
*/
count: number;
/**
* 分页查询起始页
*/
page: number;
/**
* 对应查询条件共有的记录数量
*/
total: number;
/**
* 获取到的数据存放在这里
*/
rows: {
[key: string]: any;
}[];
}
条件过滤
在Select和Update的时候,必须设置条件过滤选中对应的记录。有以下条件过滤接口
等于
/**
* 添加过滤条件 等于
* @param field
* @param value
* @return {Select}
*/
equalTo(field: string, value: any): Filter;
大于
/**
* 添加过滤条件 大于
* @param field
* @param value
* @return {Select}
*/
greaterThan(field: string, value: any): Filter;
大于等于
/**
* 添加过滤条件 大于等于
* @param field
* @param value
* @return {Select}
*/
greaterThanOrEqualTo(field: string, value: any): Filter;
小于
/**
* 添加过滤条件 小于
* @param field
* @param value
* @return {Select}
*/
lessThan(field: string, value: any): Filter;
小于等于
/**
* 添加过滤条件 小于等于
* @param field
* @param value
* @return {Select}
*/
lessThanOrEqualTo(field: string, value: any): Filter;
不等于
/**
* 添加过滤条件 不等于
* @param field
* @param value
* @return {Select}
*/
notEqual(field: string, value: any): Filter;
值在列表里
/**
* 添加过滤条件 值在列表里
* @param field
* @param list
* @return {Select}
*/
inList(field: string, list: any[]): Filter;
值不在列表里
/**
* 添加过滤条件 值不在列表里
* @param field
* @param list
* @return {Select}
*/
notInList(field: string, list: any[]): Filter;
Update
更新请求构造器
/**
* 更新请求构造器
* @param sourceId 业务id,对应一张表,找后端要
*/
constructor(sourceId: number);
设置要更新的字段名称和对应的值
/**
* 设置要更新的字段名称和对应的值
* @param updateData
* @return {Update}
*/
data(updateData: {
[key: string]: any;
}): Update;
发送请求
/**
* 发生update请求,获得Promise
* @return {Promise<T>|Promise}
*/
send(): Promise<UpdateResponse>;
update请求成功插入后不会返回数据,如果插入出错就reject错误
Insert
Insert 插入数据构造器
/**
* Insert 插入数据构造器
* @param sourceId
*/
constructor(sourceId: number);
设置要更新的字段
/**
* 设置要更新的字段
* @param fields
* @return {Insert}
*/
fields(fields: string[]): Insert;
添加一条需要插入的数据
/**
* 添加一条需要插入的数据
* @param row
*/
row(row: any[]): Insert;
发生请求Insert请求,获得Promise
/**
* 发生请求Insert请求,获得Promise
* @return {Promise<{}>|Promise}
*/
send(): Promise<{}>;
insert请求成功插入后不会返回数据,如果插入出错就reject错误
Contributing
dev
test
test with mocha
,run npm run test
to test it
require
安装全局依赖
- typedoc to make API doc
- mocha for test
License
Copyright (c) 2016 HalWu