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

@sinolink/http

v1.1.6-1

Published

http

Downloads

5

Readme

@sinolink/http

axios-v0.19.2 npm-v6.13 rollup-v1.19

基于axioshttp库,依赖node服务core,封装了自动登录逻辑,session过期时自动调用登录方法。预设配置项,可调整行为。

目录

背景

项目中,数据接口参数带有身份信息(如pidfundAccount等)时,前端需要使用占位符 ($passportId)以使用node中的用户信息缓存。

若node session过期,无法正常请求接口,项目逻辑中需要处理此类情况。

该情况下的处理逻辑基本一致,故使用该组件封装axios interceptors,实现session过期时的自动登录并重试接口请求。

特性

  • 基于 node-service/core 转发数据接口,在 node 返回用户身份权限失效时,自动调用 login 方法唤起客户登录。
  • 基于 node-service/core 转发数据接口,解析并统一化 response 格式。

安装

  1. 组件已发布在内部npm仓库,可安装使用:

    npm install @sinolink/http --registry http://192.168.23.21
  2. 建议先配置hostnpm环境:

    //配置host文件
    192.168.23.21 npm.sinolink.com
       
    //命令行运行
    npm config set @sinolink:registry http://npm.sinolink.com

    然后可以直接安装

    npm install @sinolink/http

用法

普通 GET 请求:

import http from '@sinolink/http';

http.get('/api', {
    params: {
        id: 123
    }
}).then(response=>{})
    .catch(error=>{});

普通 POST 请求

import http from '@sinolink/http';

http.post('/api', {
    id: 123
},{
    "content-type": "application/json"
}).then(response=>{})
    .catch(error=>{});

http模块基于 axios ,更多用法参考 axios

带用户信息占位符的 POST 请求 (基于 node-service/core

const API_URL = `${location.origin}/yjbwebservice/core/api`;

http.post(API_URL, {
    UniqueKey: "serviceName.client.profile.getclientinfo",
    clientId: "($clientId)",
}).then(response=>{})
    .catch(error=>{});

NOTE: 使用用户信息占位符作为入参,若 node session 过期,http 会自动调用登录方法。

登录方法基于 @sinolink/login

IMPORTANT: 即使 http 代理了登录行为,依旧有 code === -1 的接口错误,页面需要处理该类场景。

可通过配置 关闭 http 的自动登录行为

const API_URL = `${location.origin}/yjbwebservice/core/api`;

http.post(API_URL, {
    UniqueKey: "serviceName.client.profile.getclientinfo",
    clientId: "($clientId)",
}, {
    directResponse: true,
}).then(response=>{})
    .catch(error=>{});

IMPORTANT: 由于 axios遗留问题,实例的自定义配置项无法通过 axios 全局默认值配置。故以 http.defaults.directResponse = true 形式来预设 配置项 是无效的,请自行封装方法。

查看更多配置项,见 配置项说明

查看更完整的用法,见 demos

API

NOTE: http 库本质上是 axios 的实例

request<T>(config: HttpRequestConfig): Promise<T>

get<T>(url: string, config?: HttpRequestConfig): Promise<T>
post<T>(url: string, data?: any, config?: HttpRequestConfig): Promise<T>
put<T>(url: string, data?: any, config?: HttpRequestConfig): Promise<T>
delete<T>(url: string, config?: HttpRequestConfig): Promise<T>

配置项说明

interface HttpRequestConfig {
    //axios 原有配置项见 https://github.com/axios/axios
    //..., 
    
    // `directResponse` 可以禁用自动登录
    // 若 directResponse 为 false, 则接口 由于 node session 过期 返回失败时
    // 组件会自动调用 login 方法唤起登录
    directResponse: boolean, //default: false
        
    // `notParseResponse` 可以禁用 axios 返回值结构解析
    // 若 notParseResponse 为 false,组件的返回值为 response.data
    // 如果需要获取完整的 axios 返回值对象,配置 notParseResponse 为 true
    notParseResponse: boolean, //defalut: false
        
    // `beginLoginBackUrl` 用于设定 login 方法的 backUrl 参数
    beginLoginBackUrl: string, //default: location.href
    
    // `LoginAction` 用于设定 自定义 login 方法
    // default: beginLogin (import from '@sinolink/login')
    // hint1: 默认的login方法包含节流优化,配置自定义login时需要考虑节流处理
    // hint2: login方式应当返回 Promise 对象,该promise会堵塞当前请求。
    //        在promise被resolve或reject之前,请求不会返回内容。
    LoginAction: (loginType: LoginTypeEnum) => Promise<boolean | void>,

}

type LoginTypeEnum = '1'|'2'|'p'|'u';

Maintainers

@guxiaojun

Resources

Small note: 编辑README时,请遵循 standard-readme 参考规范。

License

MIT © 2020 guxiaojun