Skip to content

Commit

Permalink
update single gp params and publish new version
Browse files Browse the repository at this point in the history
  • Loading branch information
meng.wang committed Nov 8, 2018
1 parent 344c78c commit 7e97cee
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 95 deletions.
6 changes: 3 additions & 3 deletions build/tar/ws/package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"name": "water-service",
"version": "0.0.2",
"version": "0.0.3",
"license": "MIT",
"homepage": "https://confluence.tendcloud.com/display/VD/water-service",
"homepage": "https://github.com/water-design/fe-microservice-base/blob/master/WATER.md",
"main": "./index.js",
"typings": "./index.d.ts",
"dependencies": {
"es6-promise": "^4.2.4"
}
}
}
196 changes: 117 additions & 79 deletions build/tar/ws/readme.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Water-Service

# 前言
提供统一的数据库接口配置管理.独立的管道和中间件机制,从请求开始、进行、结束各个环节的流式管道处理过程.同时支持数据加工中间件的自注入,达到理想的所要即所得的状态.

Expand Down Expand Up @@ -47,108 +49,137 @@ export interface SchemaApi {
npm i water-service
```
# 使用示例

```js
import waterService, { WaterService } from "water-service";

//多实例的创建
let ot = WaterService.create();
...

//在angular下使用示例(ng2+)-------------------------------
//创建全局service,引入water-service
//provider服务到根组件
import { Injectable } from "@angular/core";
import waterService,{ WaterService,ConfDataBase,requestType,responseFilters } from "water-service";
import { DATABASE } from "./service/config.database";

@Injectable()
- 在angular下使用示例
1. 创建全局service,引入water-service,示例:
```ts
/**
* 基于water-service ng-crud服务封装
* 本服务不需要provider,使用时直接注入
* root
* tree-shaking
*/
import { Injectable } from '@angular/core';
import waterService, { WaterService, ConfDataBase, requestType, responseFilters } from "water-service";
import { NzNotificationService } from 'ng-cosmos-ui';
import { DATABASE } from "../config/app.database";

@Injectable({
providedIn: 'root'
})
export class AppService {
confDataBase:ConfDataBase;
api:any;
constructor() {
super();
private confDataBase: ConfDataBase;
readonly api = DATABASE.database;
constructor(private notification: NzNotificationService) {
this.confDataBase = DATABASE;
this.api = this.confDataBase.database;
let headers = {
"x-client-token": "Be_HkRG5IpDL5AgCeXIEDJUhciIDDfxnFZT53Xl5sU4"
};

//注册数据库接口配置
waterService.provider(this.confDataBase);

//设置请求超时时间
waterService.timeout = 6000;

//注册全局数据流中间件
let token = Math.random() * 10000;
waterService.interceptors.request.use(conf => {
//设置请求header头(无需设置content-type)
conf.headers = { access_token: Math.random() * 10000 };
conf.headers = { Authorization: "Bearer " + token };
return conf;
});
waterService.interceptors.response.use(res => {
res.over = 1000;
return res;
});

}


//common
//main rewrite request
request(type:requestType,filters?: responseFilters): Promise<any>{


/**
* common
* main rewrite request
* @param type
* @param filters
*/
request(type: requestType, filters?: responseFilters): Promise<any> {
return waterService
.request(
type,
filters
)
.request(
type,
filters
).catch((e: any) => this.handlerError(e))
}
/**
* common
* handler error
* @param e
*/
handlerError(e: any) {
this.notification.create('info', 'Error', `status:${e.status}\nmsg:${e.msg}`)
}

/**
* accept urlBase
* @param url
*/
setUrlBase(url: string) {
this.confDataBase.baseUrl = url
}

}


//接口配置文件config.database.ts
export const DATABASE = {
baseUrl: "http://5990367be1e4470011c46fa8.mockapi.io",
database: {
schema1: {
api1: {
suffix: "/meng/user",
method: "get"
},
api2: {
suffix: "/meng/user",
method: "post"
}
```
2. 配置water-service接口,示例:
```ts
/**
* water-service 接口配置
* config water-service database
* https://confluence.tendcloud.com/display/VD/water-service
* base end database
* schema
* table
* api
*/
export const DATABASE = {
baseUrl: "http://172.26.126.90:3000",
// baseUrl: "",
database: {
//module | table
user: {
login: {
prefix: "/user/login",
method: "post"
}
},
project: {
projectlist: {
prefix: "/project/list",
method: "get"
},
projectNew: {
prefix: "/project/save",
method: "post"
},
schema2: {
api1: {
suffix: "/smart",
method: "get"
},
api2: {
suffix: "/smart",
method: "post"
}
errorhandler: {
prefix: "/error/handler",
method: "put"
},
projectDelete: {
prefix: "/project/delete/:id",
method: 'delete'
}
}
};

}
};

//依赖注入(业务模块调用)
```
3. 在业务模块中调用,示例:
```ts
import { Component, OnInit} from '@angular/core';
import { AppService } from './service/app.service';
@Component({...})
export class DemoComponent implements OnInit{
//注入service
constructor(public appService:AppService){

}
constructor(public appService:AppService){}

ngOnInit() {
//数据请求
this.appService
.request(
{
api: this.appService.api.schema1.api1,
api: this.appService.api.user.login,
params: { name: "test" }
}
)
Expand All @@ -160,18 +191,25 @@ export class DemoComponent implements OnInit{
);
}
}
```
- 其它独立请求调用(目前只支持get、post方法),示例:

//end---------------------------------
```ts
import waterService from "water-service";

...
let url = "http://5ab211b762a6ae001408c1d0.mockapi.io/ng/heroes";
waterService.get(url,{params:'params'}).then(res => console.log("get:", res));
...
```


//其它方式----------------------------
//独立请求调用
let conf = {
method: "get",
url: "http://5ab211b762a6ae001408c1d0.mockapi.io/ng/heroes"
};
waterService.get(conf.url).then(res => console.log("get:", res));
- 多实例支持,示例:
```ts
import { WaterService } from "water-service";

//多实例的创建
let otherService = WaterService.create();
...
```

```
参考:[微服务使用示例](https://github.com/water-design/fe-microservice-base)
3 changes: 2 additions & 1 deletion src/http/ajax.base.abstract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ export abstract class AjaxBase {
* @param params
* @param contentType
*/
abstract get(url: string, params: object): Promise<any>;
abstract get(url: string, params: object, headers: object): Promise<any>;

abstract post(
url: string,
params: object,
headers: object,
contentType?: string
): Promise<any>;

Expand Down
22 changes: 11 additions & 11 deletions src/http/http.main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ export default class HttpMain extends AjaxBase {
this.confManager.Database = database;
}

get(url: string, params?: object): Promise<any> {
return this.init({ method: this.method[0], url, params });
get(url: string, params?: object, headers?: object): Promise<any> {
return this.init({ method: this.method[0], url, params, headers });
}

post(url: string, params: object, contentType?: string): Promise<any> {
return this.init({ method: this.method[1], url, params, contentType });
post(url: string, params: object, headers?: object, contentType?: string): Promise<any> {
return this.init({ method: this.method[1], url, params, headers, contentType });
}

adapter = (config: HttpRequest) => {
Expand All @@ -36,10 +36,10 @@ export default class HttpMain extends AjaxBase {
init(
config: HttpRequest
): // method: string,
// url: string,
// params: any,
// contentType?: string
Promise<any> {
// url: string,
// params: any,
// contentType?: string
Promise<any> {
let { method, url, params, contentType, headers } = config;
return new Promise((resolve, reject) => {
let xhr: XMLHttpRequest = new XMLHttpRequest();
Expand Down Expand Up @@ -133,14 +133,14 @@ export default class HttpMain extends AjaxBase {
};

xhr.timeout = this.timeout;
xhr.ontimeout = function() {
xhr.ontimeout = function () {
response = {
status: xhr.status,
msg: "Link timeout!"
};
reject(response);
};
xhr.onerror = function() {
xhr.onerror = function () {
response = {
status: xhr.status,
msg: "Network Error!"
Expand All @@ -149,7 +149,7 @@ export default class HttpMain extends AjaxBase {
//clear
xhr = null;
};
xhr.onabort = function() {
xhr.onabort = function () {
response = {
status: xhr.status,
msg: "Request cancellation!"
Expand Down
3 changes: 2 additions & 1 deletion test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ function run(waterService) {
method: "get",
url: "http://5ab211b762a6ae001408c1d0.mockapi.io/ng/heroes"
};
waterService.get(conf.url).then(res => console.log("get:", res));
waterService.get(conf.url, null, { access_token: '232' }).then(res => console.log("get:", res));
// waterService.post(conf.url, { name: 'meng100' }, { access_token: '1232' })
}

export default run;

0 comments on commit 7e97cee

Please sign in to comment.