2.5.3 • Published 7 years ago

hyper-api-client v2.5.3

Weekly downloads
4
License
MIT
Repository
github
Last release
7 years ago

hyper-api-client

Install

npm install hyper-api-client --save

Usage

Config compiler and create instance AppApiClint

import HyperApiClient from 'hyper-api-client'
import Ajv from 'ajv'
import 'whatwg-fetch'

function initAPIClient() {
  HyperApiClient.compiler = function () {
    return new Ajv()
  }

  var doRequest = function ({method, url, data}) {
    return fetch(url, {
      method,
      body: JSON.stringify(data)
    })
  }

  return HyperApiClient.createClient({
    doRequest: doRequest
  })
}

let AppApiClint = initAPIClient()

export default AppApiClint

Add schema to AppApiClint

let findUserSchema = {
  href: 'http://domain.com/api',
  title: 'findUser',
  method: 'post',
  description: 'Find user by name',
  definitions: {
    name: {
      type: 'string'
    },
    age: {
      type: 'number'
    }
  },
  schema: {
    properties: {
      name: {
        type: 'string'
      }
    }
  },
  targetSchema: {
    properties: {
      name: {
        $ref: '#/definitions/name',
      },
      age: {
        $ref: '#/definitions/age'      
      }
    }
  }
}

AppApiClint.addSchema(findUserSchema)

Use directly

AppApiClint.findUser.send({name: 'hal.zhong'}).then(user => {
  console.info(user.name)
})

Install vue-modello plugin

import VueModello from 'vue-modello'
import HyperApiClient from 'hyper-api-client'
import AppApiClient from './api_client'

let AppModello = new VueModello()
AppModello.use(HyperApiClient.VueModelloPlugin, {
  client: AppApiClient
})

if (top !== window) {
  let loc = location
  top.href.replace(loc.domain + (loc.port || '') + '//' + loc.hostname + '/#')
}

vue-modello plugin Option

OptionNameTypeExampleDescription
clientObjectHyperApiClient instance
disableResultValidateBooleanfalse默认值为 false,为 true 时将不校验响应结果
suppressResultInvalidErrorBooleanfalse默认值为 false,为 true 时将不会在响应结果校验出错时抛出错误
disableParametersValidateBooleanfalse默认值为 false,为 true 时将不校验请求参数
suppressParametersInvalidErrorBooleanfalse默认值为 false,为 true 时将不会在请求参数校验出错时抛出错误
onErrorFunctionfn(error)

vue modello hyper api plugin mix option

PropertyPriorityTypeDefaultExampleDescription
parameterObject 或 String{}'parameterPath' 等价于 { valuePath: 'parameterPath' }
parameter.valuePathString'query'请求参数在 state 中的 path,将根据它自动设置参数的其他选项
parameter.set+Functionfn(state, value)提供一个函数设置参数的值,用于将 schema 中的默认值复制给参数
parameter.get+Functionfn(state)提供一个函数从 state 中获取参数,仅用于发起请求时未传递参数
parameter.validateEnabledBooleantruewatch parameters and validate 通过 vue-modello 的 watch 选项监听参数变化并进行校验
parameter.validateErrorPathString'validateError.query'参数校验错误的保存到 state 的 path
parameter.setValidateError+Functionfn(state, error, propPath)提供一个函数保存参数校验错误
parameter.copyDefaultBooleanBe true if valuePath present 是否 copy schema 中的默认值到参数
resultObject 或 String{}'resultPath' 等价于 { valuePath: 'resultPath'}
result.valuePathString'list'保存结果到 state 的 path
result.set+Functionfn(state, value)用于保存响应结果到 state
result.filterFunctionfn(result)filter result 用于通过校验后过滤响应结果
result.copyDefaultBooleanBe true if valuePath present 是否复制默认值到响应结果

Add api to model

let UserModel = {
  modelName: 'User',

  hyperApi: {
    findUser: {}
  },

  actions: {

  },

  mutations: {

  }
}
2.5.3

7 years ago

2.5.1

7 years ago

2.5.0

7 years ago

2.4.4

7 years ago

2.4.2

7 years ago

2.4.1

7 years ago

2.4.0

7 years ago

2.3.1

7 years ago

2.3.0

7 years ago

2.1.0

7 years ago

2.0.0

7 years ago

1.2.0

7 years ago

1.1.0

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago