Détail du package

keq

keq-request21.3kMIT2.8.11

Request API write by Typescript for flexibility, readability, and a low learning curve.

request, superagent, fetch, node

readme

logo

KEQ

version downloads dependencies license Codecov

Keq is a request API write by Typescript for flexibility, readability, and a low learning curve. It also works with Node.js! Keq wraps the Fetch APIs, adding chain calls and middleware functions.

Document | 中文文档

Simple Usage

Send Request

A request can be initiated by invoking the appropriate method on the request object, then calling .then() (or .end() or await) to send the request. For example a simple GET request:

import { request } from "keq";

const body = await request
  .get("/search")
  .set("X-Origin-Host", "https://example.com")
  .query("key1", "value1");

Request can be initiated by:

import { request } from "keq";

const body = await request({
  url: "/search",
  method: "get",
});

Absolute URLs can be used. In web browsers absolute URLs work only if the server implements CORS.

import { request } from "keq";

const body = await request.get("https://example.com/search");

DELETE, HEAD, PATCH, POST, and PUT requests can also be used, simply change the method name:

import { request } from "keq";

await request.head("https://example.com/search");
await request.patch("https://example.com/search");
await request.post("https://example.com/search");
await request.put("https://example.com/search");
await request.delete("https://example.com/search");
await request.del("https://example.com/search");

.del() is the alias of .delete().

Keq will parse body according to the Content-Type of Response and return undefined if Content-Type not found. Add invoke .resolveWith('response') to get the origin Response Object.

import { request } from "keq";

const response = await request
  .get("http://test.com")
  .resolve('response')

const body = await response.json();

We will introduce resolveWith in more detail later.

Keq won't auto parse body, if response.status is 204. The HTTP 204 No Content success status response code indicates that server has fulfilled the request but does not need to return an entity-body, and might want to return updated meta information

Setting header fields

Setting header fields is simple, invoke .set() with a field name and value:

import { request } from "keq";

await request
  .get("/search")
  .set("X-Origin-Host", "https://example.com")
  .set("Accept", "application/json");

You may also pass an object or Headers to set several fields in a single call:

import { request } from "keq";

await request
  .get("/search")
  .set({
    "X-Origin-Host": "https://example.com",
    Accept: "application/json",
  });

Request query

The .query() method accepts objects, which when used with the GET method will form a query-string. The following will produce the path /search?query=Manny&range=1..5&order=desc.

import { request } from "keq";

await request
  .get("/search")
  .query({ query: "Manny" })
  .query({ range: "1..5" })
  .query("order", "desc");

Or as a single object:

import { request } from "keq";

await request
  .get("/search")
  .query({ query: "Manny", range: "1..5", order: "desc" });

Request routing parameters

The .params() method accepts key and value, which when used for the request with routing parameters.

import { request } from "keq";

await request
  // request to /users/jack/books/kafka
  .get("/users/:userName/books/{bookName}")
  .params("userName", 'jack');
  .params("bookName", "kafka");
  // or invoke with an object
  .params({
    "userName": "jack",
    "bookName": "kafka"
  })

JSON Request

A typical JSON POST request might look a little like the following, where we set the Content-Type header field appropriately:

import { request } from "keq";

await request
  .post("/user")
  .set("Content-Type", "application/json")
  .send({ name: "tj", pet: "tobi" });

When passed an object to .send(), it will auto set Content-Type to application/json

x-www-form-urlencoded Request

A typical Form POST request might look a little like the following:

import { request } from "keq";

await request
  .post("/user")
  .type("form")
  .send({ name: "tj", pet: "tobi" })
  .send("pet=tobi");

To send the data as application/x-www-form-urlencoded simply invoke .type() with "form". When passed an string to .send(), it will auto set Content-Type to application/x-www-form-urlencoded.

When calling .send () multiple times, the value of Content-Type will only be set when the first calling .send ().

Form-Data Request

A typical Form POST request might look a little like the following:

import { request } from "keq";

const form = new FormData();
form.append("name", "tj");
form.append("pet", "tobi");

// prettier-ignore
await request
  .post("/user")
  .type("form-data")
  .send(form)

When passed an FormData object to .send(), it will auto set Content-Type to multipart/form-data.

You can append field by invoke .field() and .attach()

import { request } from "keq";

await request
  .post("/user")
  .field("name", "tj")
  .field("pet", "tobi")
  .attach("file", new Blob(["I am tj"]));

Setting the Content-Type

The obvious solution is to use the .set() method:

import { request } from "keq";

// prettier-ignore
await request
  .post("/user")
  .set("Content-Type", "application/json")

As a short-hand the .type() method is also available, accepting the canonicalized MIME type name complete with type/subtype, or simply the extension name such as "xml", "json", "png", etc:

import { request } from "keq";

await request
  .post("/user")
  .type("json");
Shorthand Mime Type
json, xml application/json, application/xml
form application/x-www-form-urlencoded
html, css text/html, text/css
form-data multipart/form-data
jpeg, bmp, apng, gif, x-icon, png, webp, tiff image/jpeg, image/bmp, image/apng, image/gif, image/x-icon, image/png, image/webp, image/tiff
svg image/svg+xml

Set Request Redirect mode

Follow redirect by default, invoke .redirect(mode) to set the redirect mode. Allow values are "error", "manual" and "follow".

import { request } from "keq";

await request
  .get("http://test.com")
  .redirect("manual");

Set Request Credentials And Mode

These two parameters are used to control cross-domain requests.

import { request } from "keq";

await request
  .get("http://test.com")
  .mode("cors")
  .credentials("include");

resolve responseBody

It was mentioned before that Keq will automatically parses the response body. And we can control the parsing behavior by calling .resolveWith(method). There are multiple parsing methods for us to choose from

method description
.resolveWith('intelligent') It is the default method of Keq. This will returned context.output first if it exists. Otherwise return undefined when the response status is 204. Or return parsed response body according to the Content-Type of Response.
.resolveWith('response') Return Response.
.resolveWith('text') Return response.text().
.resolveWith('json') Return response.json().
.resolveWith('form-data') Return response.formData().
.resolveWith('blob') Return response.blob().
.resolveWith('array-buffer') Return response.arrayBuffer()

See more usage in the Document

Contributing & Development

If there is any doubt, it is very welcome to discuss the issue together.

github-keq-request-keq

changelog

Changelog

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

2.8.11 (2025-06-05)

Bug Fixes

  • ensure proper cleanup of abort flow control function (fc5ad5e)

2.8.10 (2024-12-26)

Bug Fixes

  • cannot extend KeqOperations (908983d)

2.8.9 (2024-12-22)

Bug Fixes

  • wrong type of .query/.set/.params (b11b959)

Performance Improvements

  • support stict type checking for .send/.params/.set/.query (2c82b01)

2.8.8 (2024-12-10)

Bug Fixes

  • make some properties in KeqContext readonly to prevent modifications (b2321cf)

2.8.7 (2024-10-29)

Bug Fixes

  • .finally() cannot invoke at keq request (4315979)

2.8.6 (2024-10-25)

Bug Fixes

  • cannot send blob body (ba7a14d)
  • cannot send readableStream (1b16142)

2.8.5 (2024-10-25)

Performance Improvements

  • support send binary body (4f17198)

2.8.4 (2024-10-20)

Bug Fixes

2.8.3 (2024-10-20)

Bug Fixes

Performance Improvements

  • set context.retry to be deprecated (e8b76a4)

2.8.2 (2024-10-07)

Performance Improvements

  • export createResponseProxy function (261606e)

2.8.1 (2024-09-13)

Performance Improvements

  • allow middleware access context.metadata (4561592)
  • set identifier to be readonly (6f64f1b)

2.8.0 (2024-09-11)

Features

  • add the unique identifier of the request's location in the code (e9eb95f)

2.7.5 (2024-09-10)

Bug Fixes

2.7.4 (2024-08-19)

Performance Improvements

  • add options method support (bfc7ae4)

2.7.3 (2024-08-13)

Bug Fixes

  • .set(key, value) cannot set the number type value when adding a custom header (2085cc6)

2.7.2 (2024-08-13)

Performance Improvements

  • allow header to add number type fields (bacacaa)

2.7.1 (2024-07-20)

Performance Improvements

  • remove whatwg-url because mosts browsers had support (6877d79)

2.7.0 (2024-07-05)

Features

  • add ctx.request.url that return a url merged routeParams (896c029)

Bug Fixes

  • the default parameter of KeqRequest (d7939d2)

2.6.10 (2024-06-27)

Performance Improvements

  • optimize the error message of the wrong header (92f4aa6)

2.6.9 (2024-06-24)

Bug Fixes

  • cannot find buffer in the browser (49b57d8)

2.6.8 (2024-06-04)

Performance Improvements

  • remove clone and object.fromentries dependencies (7a3ef9b)

2.6.7 (2024-06-03)

Performance Improvements

  • rename clone to cloneBody (c008b50)

2.6.6 (2024-06-03)

Bug Fixes

  • unable to package correctly in nuxt3 (b5ce266)

2.6.5 (2024-06-03)

Bug Fixes

  • wrong typescript defination (b1bf5c2)

2.6.4 (2024-06-02)

Bug Fixes

  • cannot find Buffer at browser (c165673)

2.6.3 (2024-05-30)

Performance Improvements

  • human warn when set undefined query (e9695ab)
  • interface type could be extends through generics (62797e3)

2.6.2 (2024-05-29)

Bug Fixes

  • retryDelay option setting is invaild (9bc07f8)

2.6.1 (2024-05-28)

Bug Fixes

  • body should not be set when undefined (0674870)

2.6.0 (2024-05-28)

Features

  • use .on(eventName, listener) add event listener (500ef14)

Bug Fixes

  • esm import syntax (c312146)
  • the body is undeifned when send plain/text request (ed24ba6)

2.5.5 (2024-05-26)

Bug Fixes

  • avoid conflicts between flowController and timeout middleware (a075602)
  • retry timeout and flowControll not triggered (846c884)

2.5.4 (2024-05-23)

Performance Improvements

  • retryOn and retryDelay support return Promise (398c262)

2.5.3 (2024-05-23)

Bug Fixes

  • retryTimes avoids throw errors caused by NaN (a5ece3f)

2.5.2 (2024-05-22)

Bug Fixes

  • composeMiddleware should not change parameters (131c1d4)

Performance Improvements

  • avoid duplication of compose middleware (c4e5cc1)
  • warn for incorrect invoke next() (f4b418f), closes #74

2.5.1 (2024-05-21)

Bug Fixes

  • throw error when retryOn is not set (cbf4594)

2.5.0 (2024-05-21)

Features

  • third-party middleware can extend option typescript declaration (a9e559a)

2.4.1 (2024-05-17)

Bug Fixes

  • response.body is not a function (8332fa2)

2.4.0 (2024-05-17)

Features

  • avoid time-consuming cloning (5f6b773)

2.3.4 (2024-05-14)

Bug Fixes

  • avoid repeatedly defining response causing errors to be throw (fc4d0ab)
  • missing typescript definition (fff9046)

Performance Improvements

2.3.3 (2024-05-10)

Bug Fixes

  • the error thrown when the request timeout is not DOMException (7c84d8f)

Performance Improvements

2.3.2 (2024-04-23)

Bug Fixes

  • cannot send form-data when invoke .attach() first (0195ee2)

2.3.1 (2024-03-22)

Bug Fixes

  • unabled send formDate request with buffer file (9db1508)

2.3.0 (2024-02-24)

Features

  • add .resolveWith method add deprecated resolveWithFullResponse (1e01e7f)

2.2.0 (2024-02-04)

Features

  • add .timeout(millisecond) (b99009b)

2.1.2 (2024-01-17)

Bug Fixes

  • unable send request when parentheses exit in pathname (24c81ff)

2.1.1 (2024-01-09)

Bug Fixes

  • should not throw error when query is undefined (515fa20)

2.1.0 (2024-01-05)

Features

  • add .route to Router (dc27057)
  • flow control function for sending multiple requests (5d24adf)

2.0.8 (2024-01-02)

Bug Fixes

  • fetch error not thrown correctly (cbc7778)

2.0.7 (2023-11-09)

Bug Fixes

  • wrong repo address in package.json (1101a79)

2.0.6 (2023-11-08)

Bug Fixes

2.0.5 (2023-11-08)

Bug Fixes

2.0.4 (2023-11-08)

Performance Improvements

2.0.3 (2023-09-27)

Bug Fixes

  • cannot find dist/umd/src/index.js (9264923)

2.0.2 (2023-09-27)

Bug Fixes

  • cannot import umd package (4eb05b3)

2.0.1 (2023-09-27)

Bug Fixes

  • cannot import createRequest (2acdc47)

2.0.0 (2023-09-25)

⚠ BREAKING CHANGES

  • Drop support for Node16.
  • Remove node-fetch and internal FormData support in nodejs.
  • content.url, content.query, content.headers, content.body has be removed, use content.request.* instead.
  • resolveWithOriginResponse has be removed. context.request.options has be removed, use context.request instead.
  • mount has be removed, use request.useRouter instead.
  • .serialize has be removed.

Code Refactoring

1.10.1 (2023-05-17)

Bug Fixes

  • keq crashes when the response body does not match content-type (f0bc15e)

1.10.0 (2022-11-22)

Features

  • add .mode() and .credentials() (73f1cc2)
  • add .redirect(mode) (094549b)

1.9.0 (2022-10-19)

Features

1.8.9 (2022-09-08)

Bug Fixes

  • unexpected querystring when invoke .query() with an object param that some value is undfined (372e2e8)

1.8.8 (2022-06-24)

Bug Fixes

  • not working when response include null (e98ea2b)

1.8.7 (2022-06-22)

Bug Fixes

  • wrong content-type when send request in browser (a100a5d)

1.8.6 (2022-05-26)

Bug Fixes

  • cannot send multiple files (ec70e66)

1.8.5 (2022-05-01)

Bug Fixes

1.8.4 (2022-05-01)

Bug Fixes

  • global middleware is polluted (1c00f0e)

1.8.3 (2022-04-26)

Bug Fixes

  • cannot find node:stream (3137cad)

1.8.2 (2022-04-26)

Bug Fixes

  • cannot find keq main file (41908fa)

1.8.1 (2022-04-25)

Bug Fixes

  • cannot compile by vite (2727867)
  • wrong return of response.blob() (ca5a5b5)

1.8.0 (2022-03-22)

Features

  • support custom request instance (2c05dec)

1.7.3 (2022-03-08)

Bug Fixes

  • unable to send formdate request with file (a4ed415)

1.7.2 (2022-02-25)

Bug Fixes

  • calling response.json() in middleware will cause garbled chinese characters (40ffe02)
  • throw error when not set url origin (9040396)

1.7.1 (2022-02-24)

Bug Fixes

  • cannot import URL from url package (b89721e)
  • esm parse failed (b95a97c)

1.7.0 (2022-01-17)

Features

Performance Improvements

  • ctx.request.url extends from whatwg url api (96dd049)

1.6.6 (2021-12-13)

Bug Fixes

  • response cannot call .text, .json, .formData and .blob together (fa1605a)

1.6.5 (2021-12-13)

Bug Fixes

  • iterator cannot be looped (022ca72)

1.6.4 (2021-12-13)

Bug Fixes

  • the default array formatting of query is nonstandard (229e08c)

1.6.3 (2021-12-07)

Bug Fixes

  • cannot invoke .blob() on the proxy response (4e21c90)

1.6.2 (2021-11-30)

Bug Fixes

  • cannot parse response.body when response.status is 204 (1b7e88d), closes #21

1.6.1 (2021-11-26)

Bug Fixes

  • retryCallback interface (aa25c99)

1.6.0 (2021-11-26)

Features

1.5.0 (2021-11-26)

Features

  • add intial retry time option (accb899)

1.4.0 (2021-10-16)

Features

1.3.1 (2021-10-16)

Bug Fixes

  • cannot get middleware matcher interface (e285b73)

1.3.0 (2021-08-29)

Features

  • add an error message that the routing parameters cannot be resolved (ea267cb)

1.2.2 (2021-05-09)

Bug Fixes

  • response.clone is not responding (ef1d66c)

1.2.1 (2021-05-06)

Bug Fixes

  • cannot find mount.module (2031351)

1.2.0 (2021-05-05)

Features

1.1.4 (2021-04-26)

Bug Fixes

  • type error when set option resolveWithFullResponse (b32dc20)

1.1.3 (2021-04-21)

1.1.2 (2021-04-06)

Bug Fixes

  • cannot run in browser (56c08b9)
  • throw undefined when response body is empty (1904392)

1.1.1 (2021-04-06)

Bug Fixes

1.1.0 (2021-04-04)

Features

  • middleware mount utils (fa35ff0), closes #5

1.0.1 (2021-02-24)

1.0.0 (2021-02-23)

Features

0.0.11 (2020-12-30)

Bug Fixes

0.0.10 (2020-12-30)

Bug Fixes

  • cannot send big file by form-data (d25218a)

0.0.9 (2020-12-29)

Features

  • add new option resolveWithOriginalResponse (158d32d)

0.0.8 (2020-11-20)

Features

Bug Fixes

  • middleware cannot modified resolveWithFullResponse (329b766)

0.0.7 (2020-11-20)

Bug Fixes

  • don't set content-type when no request body (291bb65)

0.0.6 (2020-11-13)

0.0.5 (2020-10-18)

Bug Fixes

  • cannot proxy polyfill response (74e26d2), closes #3

0.0.4 (2020-07-16)

Bug Fixes

  • x-ww-form-urlencoded not support array (4f88e22), closes #1

0.0.3 (2020-05-30)

Bug Fixes

  • cannot find fs (bf52f11)
  • dependence missing (73f3409)
  • throw error when set undefined to query value (5fb94a6)

0.0.2 (2020-04-17)

0.0.1 (2020-03-01)