1.2.6 • Published 2 years ago

dame v1.2.6

Weekly downloads
-
License
ISC
Repository
github
Last release
2 years ago

dame package size dame package size minzipped dame dependency count Coverage Status

dame minimalistic HTTP client for the browser and Node.js

  • 🚀 Lightweight
  • ⚪️ Zero dependencies.
  • 😀 Easy to use.
  • 🟢 Node (http & https) and 💻 browser (Fetch).
  • 👉 Promise API.
  • ⌛ Custom timeout.
  • 📄 Automatic transforms to JSON data.
  • ⏭ Follows redirects.

📃 Changelog

Table of contents

Import

const dame = require("dame");

Basic examples

GET

let {response} = dame.get("https://rickandmortyapi.com/api/location/1");

POST

let {response} = dame.post("https://your.api.com/login", {
	username: "Username",
	password: "****",
});

Response object

{
	isError: false,
	code: 200,
	status: "OK",
	response: {...},
	error: null,
	redirectCount: 3,
}
  • isError boolean: True if code is >= 200 and < 300 (this is configurable).
  • code number: Status code.
  • status string: Status.
  • response any: Response of the request.
  • error any: If there was any error during the request it will be here.
  • redirectCount number: How many redirects have been followed. Not present if there have been no redirects.

The response can be destructured like this:

let {isError, code, status, response} = dame.get("https://rickandmortyapi.com/api/location/1");

Methods

get

const {response} = dame.get(url, config);
  • url string: Full URL or path. - If you set a baseUrl, this url will be concatenated to it: baseUrl + url. - If url starts with "http://" or "https://" the baseUrl from config will be ignored and url will be treated like a full url.
  • config object: See Config.

post, put, delete, patch

const {response} = dame.post(url, body, config);
  • url string:See get.
  • body object: The request body.
  • config object: See Config.

Config

  • baseUrl string: Base URL that will be concatenated with the url of the requests.
  • headers object: Headers that will be attached to the request.
  • timeout number: Number of miliseconds that must pass before timeout the request.
  • checkIsError function<boolean>: Function that will receive the status code (number) and must return boolean. Default isError = !(code >= 200 && < 300).
  • Any option that fits on request or fetch.
  • maxRedirects number: Max redirects to follow. Default 20. Use 0 to disable redirects.
  • responseType "arraybuffer" | "stream" | "json" | "text": Browser only. Default "json". Type of the data that the server will respond with.

Configuring base instance

Syntax:

dame.<configKey> = <value>;
  • configKey: any key from Config.
  • value: any value that fits on the config key.

Examples:

dame.baseUrl = "http://localhost:3010";
dame.headers.Authorization = `Bearer abcd.1234`;
dame.timeout = 5000;

Then you'll be able to:

dame.get("/protectedRoute");
// url will be → http://localhost:3010/protectedRoute
// headers will be → {Authorization: "Bearer abcd.1234"}

Creating an instance

const dameInstance = dame.new(config, instanceName?);
  • config object: See Config.
  • instanceName string: (optional) If filled, this instance will be saved on dame.instances.<instanceName>.

Removing a saved instance:

delete dame.instances.<instanceNameToRemove>

Examples

Set base URL

const yourApi = dame.new({
	"baseUrl": "http://localhost:3000",
});

Set headers

const yourApi = dame.new({
	"headers": {
		Authorization: "Bearer abc.123"
	}
});

Editing an instance

yourApi.headers.Authorization: "Bearer new.token";

Special statuses

Timeout

{
	isError: true,
	code: 0,
	status: 'Timed out',
	response: null
}

No response

{
	isError: true,
	code: -1,
	status: "No response from server",
	response: null
}

Offline

{
	isError: true,
	code: -2,
	status: "No internet connection",
	response: null
}

dame vs. others

PackageBrowser + NodeDependenciesSize
dame0dame package size
phinnpm.iophin package size
node-fetchnpm.ionode-fetch package size
axiosnpm.ioaxios package size
gotnpm.iogot package size
superagentnpm.iosuperagent package size
requestnpm.iorequest package size

☝ Return to top

1.2.6

2 years ago

1.2.5

2 years ago

1.2.4

2 years ago

1.2.2

3 years ago

1.2.0

3 years ago

1.2.1

3 years ago

1.1.7

3 years ago

1.1.6

3 years ago

1.1.5

3 years ago

1.1.4

3 years ago

1.1.3

3 years ago

1.1.2

3 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

0.0.43

3 years ago

0.0.44

3 years ago

0.0.45

3 years ago

0.0.46

3 years ago

0.0.42

3 years ago

0.0.41

3 years ago

0.0.40

3 years ago

0.0.39

3 years ago

0.0.37

3 years ago

0.0.38

3 years ago

0.0.31

3 years ago

0.0.32

3 years ago

0.0.33

3 years ago

0.0.34

3 years ago

0.0.35

3 years ago

0.0.36

3 years ago

0.0.30

3 years ago

0.0.28

3 years ago

0.0.20

3 years ago

0.0.21

3 years ago

0.0.22

3 years ago

0.0.23

3 years ago

0.0.25

3 years ago

0.0.15

3 years ago

0.0.16

3 years ago

0.0.17

3 years ago

0.0.18

3 years ago

0.0.19

3 years ago

0.0.11

3 years ago

0.0.12

3 years ago

0.0.13

3 years ago

0.0.14

3 years ago

0.0.26

3 years ago

0.0.27

3 years ago

0.0.10

3 years ago

0.0.9

3 years ago

0.0.8

3 years ago

0.0.7

3 years ago

0.0.6

3 years ago

0.0.5

3 years ago

0.0.4

3 years ago

0.0.3

3 years ago

0.0.2

3 years ago

0.0.1

3 years ago