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

@flyriselink/pai-auth-api

v2.0.1

Published

Pai auth api tools.

Downloads

287

Readme

pai-auth-api 工具使用指南

此工具兼容了独立运行、工作台微应用运行两种运行方式。

安装依赖

pai-auth-api

# 建议先锁定npm安装版本
# npm config set save-prefix=''
npm install @flyriselink/pai-auth-api

工具初始化

import AuthApi from '@flyriselink/pai-auth-api'

const Auth = new AuthApi({
  // 源地址(协议+域名+端口),主要用于跳转登录
  origin: process.env.Origin, 
  // 接口前置地址,主要用于满足生产环境的接口请求的代理
  baseURL: process.env.BaseURL, 
})

export default Auth

主要API

// 注意:均以异步方法执行

// 登录
login(opts?: ConfirmOptions): Promise<boolean>
// 登出(比login多一步:清缓存、token失效)
logout(opts?: LogoutOptions): Promise<boolean>
// 获取token
getToken(opts?: GetTokenOptions): Promise<string>
// 获取用户信息
getUserInfo(opts?: GetUserInfoOptions): Promise<UserInfo | null | void>
// 处理请求
requestHandler(req: AxiosRequestConfig | AuthRequestConfig): Promise<void>
// 处理响应
responseHandler(res: AxiosResponse | AuthResponse): Promise<ResponseStatusTypes>
  • 登录/登出

一般情况下,使用登出(logout)接口即可。

let options = {
    // 参数配置
}
await Auth.logout(options)

配置说明

/**
 * 操作完成后,是否跳转登录
 * 默认为false,即操作完成后马上跳转至授权中心
 */
stay?: boolean
/**
 * 指定回调地址
 * 默认为当前操作url
 */
redirectUrl?:string 
/**
 * 是否启用确认框,操作之前需要确认操作
 * 默认为false
 */
confirm?: boolean
/**
 * 确认框回调
 * 当confirm设为true,此配置才会生效
 * 为一个返回Promise<boolean>的异步方法
 */
confirmCallback(): Promise<boolean>

示例:确认框回调

await this.$auth.logout({
  confirm: true,
  confirmCallback: () => { 
    // $confirm为ElementUI的确认框
    return this.$confirm('确定要登出工作台吗?', '提示', {
      type: 'warning',
      confirmButtonText: '确定',
      cancelButtonText: '取消',
    })
  },
})
  • 获取token
let options = {
    // 参数配置
}
await Auth.getToken(options)

配置说明

/**
 * 获取失败(包括token为空),是否跳转登录
 * 默认为false
 */
stay?: boolean
  • 获取用户信息
let options = {
    // 参数配置
}
await Auth.getUserInfo(options)

配置说明

/**
 * 获取失败(包括token为空),是否跳转登录
 * 默认为false
 */
stay?: boolean
  • 处理请求

目前该API会为Axios请求的headers中注入Authorization信息

示例:在拦截器中使用(request)

// 请求成功
(config) => {
  // 是否启用认证校验
  if (process.env.isAuth) {
    try {
      // 填充config.headers.Authorization的值(暂时只有这个处理逻辑)
      await Auth.requestHandler(config)
      //
      // ...自行实现其它判断逻辑
    } catch (e) {
      console.error(e)
      return config
    }
  }
  return config
}
  • 处理响应

示例:在拦截器中使用(response)

import { ResponseStatusTypes } from '@flyriselink/pai-auth-api'

// 请求响应成功
(res) => {
  // 是否启用认证校验
  if (process.env.isAuth) {
    try {
      // 排除了401(可能会处理更多<1000的错误码)的情况
      const result = await Auth.responseHandler(res, {
        // 开启跳转登录前确认
        confirm: true,
        confirmCallback: () => {
          // 跳转登录前确认,返回结果决定是否跳转(Promise<Boolean>)
          return MessageBox.confirm(I18n.t('common.reloginTips'), I18n.t('common.systemTips'), {
            type: 'warning',
            confirmButtonText: I18n.t('common.relogin'),
            showCancelButton: false,
            showClose: false,
          })
        },
      })
      if (result !== ResponseStatusTypes.Pass) {
        // 认证失败
        return Promise.reject(Error)
      }
      // 判断返回结果是否有效
      if (res?.data?.code?.toString() !== '200') {
        Notification.error({
          title: I18n.t('common.error'),
          message: res.data.msg || res.data.message || res.data.code || I18n.t('common.error'),
          duration: 2000,
        })
        return Promise.reject(Error)
      } else {
        //
        // ...自行实现其它判断逻辑
        return res?.data
      }
      // ...自行实现其它判断逻辑
    } catch (e) {
      console.error(e)
      return res
    }
  }
  return res
}