npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@belloai/fetch

v0.0.15

Published

api 请求工具

Downloads

36

Readme

fetch

api 请求工具

Usage

import { initApiFactory, checkInterceptConfig } from '@belloai/fetch'
import { getFormUtcToLocalMoment } from '@belloai/moment'
import axios from 'axios'
import { get } from 'lodash'

import { ApisConfigMap } from '@/interface/config'

import Store from '@/store'
import { msgPost } from '@/utils/delayMsg'
import LS from '@/utils/local-storage'
import { setPathForwarded } from '@/utils/request/getApiSource'

import { openBindWechatDailog } from '@/components/BelloBindWechat/utils'

export const isExpire = (apiExpireAtProp = '') => {
  const userInfo = LS.get('userInfo')
  const { expire_at } = userInfo || {}
  const expireAt = apiExpireAtProp || expire_at

  if (expireAt) {
    const isExpire = getFormUtcToLocalMoment(expireAt).diff(new Date())
    if (isExpire <= 0) {
      LS.clearAllExcept()
      return true
    }
  }
  return false
}

export const instance = axios.create()

// Add a request interceptor
instance.interceptors.request.use(
  function (config) {
    // 看是否有需要替换的 api
    const interceptApis = get(Store.getters.serverConfig, 'intercept_apis', {})
    return checkInterceptConfig(config, interceptApis)
  },
  function (error) {
    console.error('request error: ', error)
    return Promise.reject(error)
  }
)

// Add a response interceptor
instance.interceptors.response.use(
  function (response) {
    setPathForwarded(response)
    return response
  },
  function (error) {
    setPathForwarded(error.response)
    console.error('response error: ', error)
    return Promise.reject(error)
  }
)

export const isNoUseAuthorization = (url, token) => {
  const [orgUrl] = url.split('?')
  return !token || ['user/login'].includes(orgUrl)
}

export const getAuthorization = props => {
  const token = LS.get('token')
  const { url } = props || {}
  if (isNoUseAuthorization(url, token)) {
    return {
      Authorization: token
    }
  }
  return {}
}

export const hostnameMap = {
  localhost: '',
  '127.0.0.1': '',
  'www.belloai.com': 'https://www.belloai.com',
  'belloai.com': 'https://www.belloai.com'
}

export const getSynchronizeApisMap = (): ApisConfigMap => {
  const { synchronize_apis } = Store.getters.serverConfig || {}
  return synchronize_apis || {}
}

export const getMethodAndUrl = (props: FetchProps) => {
  const { url, method = 'get' } = props || {}
  return `${method} ${url}`.toLowerCase()
}

export const getNeedSynchronizeApis = (props: FetchProps) => {
  return getSynchronizeApisMap()[getMethodAndUrl(props)] || []
}

export const getSynchronizeApis = async props => {
  return getNeedSynchronizeApis(props)
}

export const apiFactory: any = initApiFactory({
  // debug: true, // 是否需要打印内部的关键方法信息
  // errorKeyPolicy: {}, // 异常策略的key值推断策略,可以重写任意一个key值推断策略
  // errorPolicy: {}, // 异常策略,可以重写任意一个key策略
  // apiPre: '', // 拼接api 请求链接时的api前缀,默认值 '/api/',例如: http://t.com/api/a, `${host}${apiPre}${path}`
  // lsApiKey: '', // 拼接api 请求链接时的优先读取local storage中的这个key作为host,默认值 'apiServer'
  // getDefHeaders, // 获取默认的headers 的方法
  // getBaseUrl, // 替换内置的 getBaseUrl 的方法
  // needThrowResError, // 替换内置的 needThrowResError 的方法
  getSynchronizeApis, // 获取需要同步请求的配置对象
  instance, // 核心的请求工具 这里是 axios 的实例
  LS, // 获取一些 local storage 内的参数的工具实例
  errorPolicyProps: {
    experienceAccountExpiredFunc: () =>
      openBindWechatDailog('账号到期提醒', {
        componentName: 'service-qrCode',
        tips: '您的账号已到期,扫码联系客服获取免费使用时长'
      })
  }, // 异常时提供给异常策略的参数

  hostnameMap, // 域名和api的映射
  msgPost, // 异常时提示的方法
  getAuthorization, // 请求时获取token加到headers的方法
  getCancelSource: () => {
    return axios.CancelToken.source()
  }, // 取消请求的key获取方法,根据 instance 而定
  isExpire // 判断用户过期的方法
})

API

导出的方法已 src/index.ts 为准

目前有

resUtil

resUtil 对象内是 src/error.ts 内的方法

具体方法的应用请看 error.test.ts

initApiFactory

initApiFactory 为核心的请求实例构建函数,根据上面示例参考调用

initApiFactory.fetchUtil 为核心的请求实例

initApiFactory.getRestfulApi(entry: string) 是获取符合 RestfulApi 规范的实例,可以便捷的调用 add,del,update,list,key 的 Restful 规范请求,详情请看 src/Restful.ts

checkInterceptConfig

checkInterceptConfig 是用于做配置形式替换某个请求的行为

以上面的 axios 示例举例

// Add a request interceptor
instance.interceptors.request.use(
  function (config) {
    // 看是否有需要替换的 api
    const interceptApis = get(Store.getters.serverConfig, 'intercept_apis', {})
    return checkInterceptConfig(config, interceptApis)
  },
  function (error) {
    console.error('request error: ', error)
    return Promise.reject(error)
  }
)