UNPKG

3.28 kBMarkdownView Raw
1# @helperdiscord/centra
2
3A cool library to use for rest requests in nodejs :) <a href="https://helperdiscord.github.io/centra/">docs</a>
4
5## Examples
6
7Basic GET Request
8
9```js
10const req = require('@helperdiscord/centra');
11
12const res = await req('https://discord.com/api/gateway').json()
13
14console.log(res);
15
16// { url: 'wss://gateway.discord.gg' }
17
18```
19
20Simple POST Request with payload
21
22```js
23const req = require('@helperdiscord/centra');
24
25const res = await req('https://discord.com/api/channels/418933739540840451/messages', 'POST').body({ content: 'hi' }).header({ 'Authorization': 'Bot youryoken' }).json()
26
27console.log(res);
28
29/* {
30 id: '805925621561163806',
31 type: 0,
32 content: 'hi',
33 channel_id: '418933739540840451',
34 author: [Object],
35 attachments: [],
36 embeds: [],
37 mentions: [],
38 mention_roles: [],
39 pinned: false,
40 mention_everyone: false,
41 tts: false,
42 timestamp: '2021-02-01T22:20:41.554000+00:00',
43 edited_timestamp: null,
44 flags: 0,
45 referenced_message: null
46} */
47```
48
49Type casting json responses
50
51```ts
52import req from '@helperdiscord/centra';
53
54type Gateway {
55 url: string
56}
57
58const res = await req('https://discord.com/api/gateway').json<Gateway>();
59```
60
61## @helperdiscord/centra vs other http libraries
62
63#### Here's a size comparison table:
64
65Here you can see how efficient @helperdiscord/centra is compared in bundle size to other libraries
66
67Package | Size
68--- | ---
69request | [![request package size](https://packagephobia.now.sh/badge?p=request)](https://packagephobia.now.sh/result?p=request)
70superagent | [![superagent package size](https://packagephobia.now.sh/badge?p=superagent)](https://packagephobia.now.sh/result?p=superagent)
71got | [![got package size](https://packagephobia.now.sh/badge?p=got)](https://packagephobia.now.sh/result?p=got)
72axios | [![axios package size](https://packagephobia.now.sh/badge?p=axios)](https://packagephobia.now.sh/result?p=axios)
73isomorphic-fetch | [![isomorphic-fetch package size](https://packagephobia.now.sh/badge?p=isomorphic-fetch)](https://packagephobia.now.sh/result?p=isomorphic-fetch)
74r2 | [![r2 package size](https://packagephobia.now.sh/badge?p=r2)](https://packagephobia.now.sh/result?p=r2)
75node-fetch | [![node-fetch package size](https://packagephobia.now.sh/badge?p=node-fetch)](https://packagephobia.now.sh/result?p=node-fetch)
76phin | [![phin package size](https://packagephobia.now.sh/badge?p=phin)](https://packagephobia.now.sh/result?p=phin)
77@helperdiscord/centra | [![@helperdiscord/centra package size](https://packagephobia.now.sh/badge?p=@helperdiscord/centra)](https://packagephobia.now.sh/result?p=@helperdiscord/centra)
78
79---
80
81#### Here's a performance comparison of @helperdiscord/centra vs other http libraries
82
83These results were procured using [this](https://github.com/helperdiscord/http-benchmarks)
84
85
86```
87Results (i7-7700k, CPU governor: performance):
88
89got - promise x 2,770 ops/sec ±6.67% (71 runs sampled)
90request - callback x 4,888 ops/sec ±7.46% (74 runs sampled)
91node-fetch - promise x 5,457 ops/sec ±3.33% (74 runs sampled)
92centra - promise x 8,263 ops/sec ±2.10% (84 runs sampled)
93https - stream x 7,453 ops/sec ±3.19% (78 runs sampled)
94@helperdiscord/centra - promise x 8,202 ops/sec ±1.30% (82 runs sampled)
95Fastest is @helperdiscord/centra - promise
96```
\No newline at end of file