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

@xiaohuohumax/x-fetch-endpoint

v0.1.0

Published

x-fetch endpoint package

Downloads

24

Readme

x-fetch-endpoint

Version License GitHub Actions Workflow Status

一个简单、轻量级 HTTP 请求参数处理工具。

[!NOTE] 此项目由 octokit - endpoint.js [MIT] 项目修改而来。

🚀 快速开始

安装:

npm install @xiaohuohumax/x-fetch-endpoint

使用:

// ESM / Typescript
import { endpoint } from "@xiaohuohumax/x-fetch-endpoint"
// or

// CommonJS
const { endpoint } = require("@xiaohuohumax/x-fetch-endpoint")

📖 基本用法

import { endpoint } from "@xiaohuohumax/x-fetch-endpoint"

const e = endpoint({
  baseUrl: "https://example.com/api",
  method: "POST",
  url: "/users",
  headers: {
    "user-agent": "x-fetch"
  },
  body: {
    name: "xiaohuohumax"
  }
})

console.log(e)
// {
//   method: 'POST',
//   url: 'https://example.com/api/users',
//   headers: { 'user-agent': 'x-fetch' },
//   body: { name: 'xiaohuohumax' }
// }

const e2 = endpoint("GET /users", {
  baseUrl: "https://example.com/api",
  headers: {
    "user-agent": "x-fetch"
  },
  params: {
    name: "xiaohuohumax"
  }
})

console.log(e2)
// {
//   method: 'GET',
//   url: 'https://example.com/api/users?name=xiaohuohumax',
//   headers: { 'user-agent': 'x-fetch' }
// }

🔗 URI模板

[!NOTE] URI 模板规则参考 RFC 6570 规则。

import { endpoint } from "@xiaohuohumax/x-fetch-endpoint"

// 用户设置模板规则
// owner, repo => {owner}, {repo}
// page, per_page => {?page,per_page}
const e1 = endpoint("GET /repos/{owner}/{repo}/issues{?page,per_page}", {
  params: {
    owner: "octokit",
    repo: "request.js",
    page: 1,
    per_page: 1,
  }
})
console.log(e1.url) // /repos/octokit/request.js/issues?page=1&per_page=1

// 用户未设置模板规则,那么额外的参数会使用默认规则
// 规则如下:
// 1. 数组:hobby => {hobby*} => hobby=1&hobby=2&hobby=3
// 2. 非数组:name => {name} => name=value
const e2 = endpoint("GET /users", {
  params: {
    hobby: [1, 2, 3],
  }
})
console.log(e2.url) // /users?hobby=1&hobby=2&hobby=3

// 其他扩展规则
// 自动将路径中的 :id 格式转换为 {id}格式
const e3 = endpoint("GET /users/:id", {
  params: {
    id: 123,
  }
})
console.log(e3.url) // /users/123

📚 带有默认值的 endpoint 实例

import { endpoint } from "@xiaohuohumax/x-fetch-endpoint"

const newEndpoint = endpoint.defaults({
  baseUrl: "https://example.com",
  headers: {
    "user-agent": "x-fetch"
  },
})

const e = newEndpoint({
  url: "/api/test",
  method: "GET",
})

console.log(e)
// {
//   method: 'GET',
//   url: 'https://example.com/api/test',
//   headers: { 'user-agent': 'x-fetch' }
// }

📄 License

MIT

最后:玩的开心 🎉🎉🎉🎉