Skip to main content

Built and signed on GitHub Actions

Simple encapsulation based on web API fetch/基于 web fetch 的简单封装

This package works with Node.js, Deno, Bun, BrowsersIt is unknown whether this package works with Cloudflare Workers
It is unknown whether this package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
This package works with Bun
This package works with Browsers
JSR Score
100%
Published
3 days ago (1.0.7)
class Fetcher
extends SyncEventDispatcher<{ request: Parameters<RequestInterceptor>; response: Parameters<ResponseInterceptor>; }>

发送请求核心class对象

Examples

Example 1

const fetcher = createFetcher({
  baseURL:"https://examp.com",
  requestInterceptor(req) {
    req.headers={
      "Authorization": "Bearer bar"
    }
  }
  responseInterceptor(resp, requconfig) {
    const content = resp.text()
    console.log(requconfig.url, requconfig.method, content);
    return content
  }
})

fetcher.options('/api/login',{param:{q:"123"},body:{password:"123"}})

这样,fetcher会使用baseURL+url拼接 成为 pathname,同时自动将param自动拼接成为search参数 请求体会自动将body自动转换为json字符串

Constructors

new
Fetcher(
baseURL: string,
config: FetherConfig,
)

Properties

Methods

private
buildRequest(config: InterceptorConfig): Promise<Request>

构建请求参数

private
buildResponse<T = Response>(
resp: Response,
config: InterceptorConfig,
): Promise<T>

构建响应体

get<T = Response>(
url: string,
options?: Omit<FetherConfig, "body">,
): Promise<T>

发起 http methods=get的请求

private
getFormatedURL(pathname: string)
head<T = Response>(
url: string,
options?: Omit<FetherConfig, "body">,
): Promise<T>

method为 head的请求

options<T = Response>(
url: string,
options?: FetherConfig,
): Promise<T>

http method=options的请求

post<T = Response>(
url: string,
options?: FetherConfig,
): Promise<T>

发起http method=post的请求

request<T = Response>(
url: string,
options?: FetherConfig,
): Promise<T>

请求核心方法 其他所有具名方法均调用此方法

Static Methods

concatURL(
baseURL: string,
pathname: string,
): string

拼接基本路径和业务逻辑 主要就是/字符处理问题

Add Package

deno add jsr:@advanced/fetcher

Import symbol

import { Fetcher } from "@advanced/fetcher";

---- OR ----

Import directly with a jsr specifier

import { Fetcher } from "jsr:@advanced/fetcher";

Add Package

npx jsr add @advanced/fetcher

Import symbol

import { Fetcher } from "@advanced/fetcher";

Add Package

yarn dlx jsr add @advanced/fetcher

Import symbol

import { Fetcher } from "@advanced/fetcher";

Add Package

pnpm dlx jsr add @advanced/fetcher

Import symbol

import { Fetcher } from "@advanced/fetcher";

Add Package

bunx jsr add @advanced/fetcher

Import symbol

import { Fetcher } from "@advanced/fetcher";