@xiaohuohumax/x-fetch-endpoint
TypeScript icon, indicating that this package has built-in type declarations

0.1.0 • Public • Published

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

最后:玩的开心 🎉🎉🎉🎉

Package Sidebar

Install

npm i @xiaohuohumax/x-fetch-endpoint

Weekly Downloads

6

Version

0.1.0

License

MIT

Unpacked Size

23.2 kB

Total Files

6

Last publish

Collaborators

  • xiaohuohumax