0.0.1 • Published 6 years ago

itingluo-api v0.0.1

Weekly downloads
-
License
MIT
Repository
gitlab
Last release
6 years ago

wscn-ivanka-api

WSCN Ivanka API

Features

  • 继承 teleman
  • 自动设置token
  • 自动添加请求头X-Ivanka-TokenX-Client-TypeX-Ivanka-PlatformX-Taotie-Device-IdX-Device-Id
  • 自动抛出非20000响应错误

Install

yarn add wscn-ivanka-api --registry=http://mavenhk.wallstcn.com/repository/npm-all/

Usage

import WscnIvankaAPI from 'wscn-ivanka-api'

const api = new WscnIvankaAPI({
  env: 'prod', // sit, stage, prod
  appId: 'com.wallstreetcn.web',
  appVersion: '1.2.3',

  onSafeError(e) {
    // 现实中应该使用toast等友好的UI实现
    console.error(e.code, e.message)
  }
})

api.get('/user/info/me')

API

WscnIvankaApi(options)

Options:

env

类型:String

可选,默认为prod。base默认值将从env对应的值取得。

envbase
sithttps://api-sit.wallstreetcn.com
stagehttps://api-stage.wallstreetcn.com
prodhttps://api-prod.wallstreetcn.com

base

类型:String

可选,默认为env指定的值。相对于请求URL的基础URL。

apiVersion

类型:String

API地址前缀。可选,默认为 apiv1

appId

类型:String

业务appId,例如com.wallstreetcn.web

参见:http://115.159.108.254:4000/gui-ze/chang-liang.html

appVersion

类型:String

app版本,例如1.2.3

cache

类型:Boolean

可选。如果该项为真,将会缓存请求,并且将请求结果内联至head,供下次初始化使用,请配合cacheUntil使用。

cacheUntil

类型:Number

可选,默认为400ms。第一个api发出并且成功响应后,如果在指定的毫秒内没有新的请求发出,那么结束缓存。

onReady(callback)

类型:Function

配合cache使用。当cache开启时,我们需要app在拉取到api缓存后替换DOM节点,以避免页面加载时闪屏的问题。

const api = const api = new WscnIvankaAPI({
  env: 'prod', // sit, stage, prod
  appId: 'com.wallstreetcn.web',
  appVersion: '1.2.3',
  cache: true,
  onReady: replaceDocument
})

const app = new Vue(...).$mount()

function replaceDocument() {
  document.body.replaceChild(app.$el, document.getElementById('app'))
}

ddcEnv

类型:String

/ddc/开头的API,URL会被改写未DDC服务的地址。ddcEnv指定DDC服务环境。

默认为prodddcBase默认值将从ddcEnv对应的值取得。 由于ddc没有stage环境,因此stage也使用prod地址。

envbase
sithttp://api-ddc-sit.wallstreetcn.com
stagehttp://api-ddc.wallstreetcn.com
prodhttp://api-ddc.wallstreetcn.com

ddcBase

类型:String

可选,默认为ddcEnv指定的值。相对于请求URL的基础URL。

onSafeError

类型: Function

api.safe.* 出错后的回调函数。

实例方法

api.fetch(url, { method, headers, query, body, type })

Params: url: String. The url of the request. The final url will be base + url + querystring. method: String. HTTP methods. 'GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'HEAD'. Defaults to 'GET'. headers: Object | Headers. HTTP headers. query: String | Object | Array | URLSearchParams. The URL queries. String/Object/Array type will be used to create a URLSearchParams instance. body: Object | FormData | Blob | BufferSource | URLSearchParams | String. Any body that you want to add to your request. Note that a request using the GET or HEAD method cannot have a body. type: String. 'json' or 'form'.

  • If type is not defined and body is a plain object, body will be transformed to JSON.
  • If type is 'json', body will be transformed to JSON.
  • If type is 'form' and body is a key-value object, body will be transformed to FormData with formData.append(name, value).

Request method aliases

api.get(url, query, options)
api.post(url, body, options)
api.put(url, body, options)
api.patch(url, body, options)
api.delete(url, query, options)
api.head(url, query, options)

api.safe

提供了安全方法,出错后不会执行之后的代码。并且调用 onSafeError 函数,你可以在函数内弹 toast 等方式提示错误信息。

const result = await api.safe.get(...)
// 如果拉取数据失败,下面代码不会执行
console.log(result)

经常使用的方法:

  • api.safe.get()
  • api.safe.post()
0.0.1

6 years ago