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

@yyx219/uni-request

v1.0.0

Published

使用 UniRequest 进行网络请求的步骤如下: 在需要的地方引入 uniRequest.js 文件。 ```javascript import UniRequest from 'uniRequest.js' ``` 实例化 UniRequest,传入配置参数。 ```javascript const request = new UniRequest({ baseURL: 'http://api.example.com', header: { 'token': 'abc123' }

Downloads

2

Readme

使用 UniRequest 进行网络请求的步骤如下: 在需要的地方引入 uniRequest.js 文件。

import UniRequest from 'uniRequest.js'

实例化 UniRequest,传入配置参数。

const request = new UniRequest({
  baseURL: 'http://api.example.com',
  header: {
    'token': 'abc123'
  }
})

可以设置拦截器来处理请求和响应。

UniRequest.use({
  request: (config) => {
    // 请求拦截器
    // 可以在这里添加一些通用参数、headers 等等
    return Promise.resolve(config)
  },
  response: (res) => {
    // 响应拦截器
    // 可以在这里对响应数据进行处理、判断响应状态等等
    return Promise.resolve(res)
  }
})

使用请求方法来发送网络请求。

request.request({
  url: '/user',
  method: 'GET',
  data: {
    name: '张三',
    age: 20
  }
}).then(res => {
  console.log(res)
}).catch(error => {
  console.error(error)
})

使用上传文件方法来上传文件。

request.uploadFile({
  url: '/upload',
  filePath: 'xxx',
  name: 'file',
  formData: {
    type: 'image'
  }
}).then(res => {
  console.log(res)
}).catch(error => {
  console.error(error)
})

使用下载文件方法来下载文件。

request.downloadFile({
  url: '/download',
  filePath: 'xxx',
}).then(res => {
  console.log(res)
}).catch(error => {
  console.error(error)
})

使用 WebSocket 方法来创建 WebSocket 连接。

const socket = request.connectSocket({
  url: 'ws://localhost:8080'
})

监听 WebSocket 连接成功事件和 WebSocket 接收到消息事件。

request.onSocketOpen(() => {
  console.log('WebSocket 连接成功')
})

request.onSocketMessage(res => {
  console.log('WebSocket 接收到消息', res)
})

发送 WebSocket 消息。

request.sendSocketMessage({
  data: 'hello'
})

关闭 WebSocket 连接并监听 WebSocket 连接关闭事件。

request.closeSocket()

request.onSocketClose(() => {
  console.log('WebSocket 连接关闭')
})

完整示例代码:

import UniRequest from 'uniRequest.js'

const request = new UniRequest({
  baseURL: 'http://api.example.com',
  header: {
    'token': 'abc123'
  }
})

UniRequest.use({
  request: (config) => {
    // 请求拦截器
    // 可以在这里添加一些通用参数、headers 等等
    return Promise.resolve(config)
  },
  response: (res) => {
    // 响应拦截器
    // 可以在这里对响应数据进行处理、判断响应状态等等
    return Promise.resolve(res)
  }
})

request.request({
  url: '/user',
  method: 'GET',
  data: {
    name: '张三',
    age:
  }).then(res => {
      console.log(res)
}).catch(error => {
    console.error(error)
})

request.uploadFile({
    url: '/upload',
    filePath: 'xxx',
    name: 'file',
    formData: {
        type: 'image'
    }
}).then(res => {
    console.log(res)
}).catch(error => {
    console.error(error)
})

request.downloadFile({
    url: '/download',
    filePath: 'xxx',
}).then(res => {
    console.log(res)
}).catch(error => {
    console.error(error)
})

const socket = request.connectSocket({
    url: 'ws://localhost:8080'
})

request.onSocketOpen(() => {
    console.log('WebSocket 连接成功')
})

request.onSocketMessage(res => {
    console.log('WebSocket 接收到消息', res)
})

request.sendSocketMessage({
    data: 'hello'
})

request.closeSocket()

request.onSocketClose(() => {
    console.log('WebSocket 连接关闭')
})