2.8.7 • Published 17 days ago

keq v2.8.7

Weekly downloads
83
License
MIT
Repository
github
Last release
17 days ago

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");
ShorthandMime Type
json, xmlapplication/json, application/xml
formapplication/x-www-form-urlencoded
html, csstext/html, text/css
form-datamultipart/form-data
jpeg, bmp, apng, gif, x-icon, png, webp, tiffimage/jpeg, image/bmp, image/apng, image/gif, image/x-icon, image/png, image/webp, image/tiff
svgimage/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

methoddescription
.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

2.8.5

22 days ago

2.8.7

17 days ago

2.8.6

22 days ago

2.8.3

27 days ago

2.8.4

27 days ago

2.8.1

2 months ago

2.8.0

2 months ago

2.7.5

2 months ago

2.8.2

1 month ago

2.6.1

6 months ago

2.6.0

6 months ago

2.6.3

6 months ago

2.6.2

6 months ago

2.6.10

5 months ago

2.7.4

3 months ago

2.7.3

3 months ago

2.5.5

6 months ago

2.5.0

6 months ago

2.7.0

4 months ago

2.5.2

6 months ago

2.5.1

6 months ago

2.7.2

3 months ago

2.5.4

6 months ago

2.7.1

4 months ago

2.5.3

6 months ago

2.6.5

6 months ago

2.6.4

6 months ago

2.6.7

6 months ago

2.6.6

6 months ago

2.6.9

5 months ago

2.6.8

5 months ago

2.4.1

6 months ago

2.4.0

6 months ago

2.3.4

6 months ago

2.3.3

6 months ago

2.3.2

7 months ago

2.3.1

8 months ago

2.3.0

9 months ago

2.2.0

10 months ago

2.1.2

10 months ago

2.1.1

10 months ago

2.1.0

11 months ago

2.0.8

11 months ago

2.0.7

1 year ago

2.0.3

1 year ago

2.0.2

1 year ago

2.0.1

1 year ago

2.0.0

1 year ago

1.10.1

2 years ago

1.9.0

2 years ago

1.10.0

2 years ago

1.8.9

2 years ago

1.8.8

2 years ago

1.8.7

2 years ago

1.8.6

2 years ago

1.8.2

3 years ago

1.8.1

3 years ago

1.8.5

3 years ago

1.8.4

3 years ago

1.8.3

3 years ago

1.7.3

3 years ago

1.8.0

3 years ago

1.7.2

3 years ago

1.7.1

3 years ago

1.6.3

3 years ago

1.6.2

3 years ago

1.7.0

3 years ago

1.6.6

3 years ago

1.6.5

3 years ago

1.6.1

3 years ago

1.6.0

3 years ago

1.5.0

3 years ago

1.4.0

3 years ago

1.3.1

3 years ago

1.3.0

3 years ago

1.2.2

4 years ago

1.2.1

4 years ago

1.2.0

4 years ago

1.1.4

4 years ago

1.1.3

4 years ago

1.1.1

4 years ago

1.1.2

4 years ago

1.1.0

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago

0.0.10

4 years ago

0.0.11

4 years ago

0.0.9

4 years ago

0.0.8

4 years ago

0.0.7

4 years ago

0.0.6

4 years ago

0.0.5

4 years ago

0.0.4

4 years ago

0.0.3

4 years ago

0.0.2

5 years ago

0.0.1

5 years ago