1 | # @helperdiscord/centra
|
2 |
|
3 | A cool library to use for rest requests in nodejs :) <a href="https://helperdiscord.github.io/centra/">docs</a>
|
4 |
|
5 | ## Examples
|
6 |
|
7 | Basic GET Request
|
8 |
|
9 | ```js
|
10 | const req = require('@helperdiscord/centra');
|
11 |
|
12 | const res = await req('https://discord.com/api/gateway').json()
|
13 |
|
14 | console.log(res);
|
15 |
|
16 | // { url: 'wss://gateway.discord.gg' }
|
17 |
|
18 | ```
|
19 |
|
20 | Simple POST Request with payload
|
21 |
|
22 | ```js
|
23 | const req = require('@helperdiscord/centra');
|
24 |
|
25 | const res = await req('https://discord.com/api/channels/418933739540840451/messages', 'POST').body({ content: 'hi' }).header({ 'Authorization': 'Bot youryoken' }).json()
|
26 |
|
27 | console.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 |
|
49 | Type casting json responses
|
50 |
|
51 | ```ts
|
52 | import req from '@helperdiscord/centra';
|
53 |
|
54 | type Gateway {
|
55 | url: string
|
56 | }
|
57 |
|
58 | const 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 |
|
65 | Here you can see how efficient @helperdiscord/centra is compared in bundle size to other libraries
|
66 |
|
67 | Package | Size
|
68 | --- | ---
|
69 | request | [![request package size](https://packagephobia.now.sh/badge?p=request)](https://packagephobia.now.sh/result?p=request)
|
70 | superagent | [![superagent package size](https://packagephobia.now.sh/badge?p=superagent)](https://packagephobia.now.sh/result?p=superagent)
|
71 | got | [![got package size](https://packagephobia.now.sh/badge?p=got)](https://packagephobia.now.sh/result?p=got)
|
72 | axios | [![axios package size](https://packagephobia.now.sh/badge?p=axios)](https://packagephobia.now.sh/result?p=axios)
|
73 | isomorphic-fetch | [![isomorphic-fetch package size](https://packagephobia.now.sh/badge?p=isomorphic-fetch)](https://packagephobia.now.sh/result?p=isomorphic-fetch)
|
74 | r2 | [![r2 package size](https://packagephobia.now.sh/badge?p=r2)](https://packagephobia.now.sh/result?p=r2)
|
75 | node-fetch | [![node-fetch package size](https://packagephobia.now.sh/badge?p=node-fetch)](https://packagephobia.now.sh/result?p=node-fetch)
|
76 | phin | [![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 |
|
83 | These results were procured using [this](https://github.com/helperdiscord/http-benchmarks)
|
84 |
|
85 |
|
86 | ```
|
87 | Results (i7-7700k, CPU governor: performance):
|
88 |
|
89 | got - promise x 2,770 ops/sec ±6.67% (71 runs sampled)
|
90 | request - callback x 4,888 ops/sec ±7.46% (74 runs sampled)
|
91 | node-fetch - promise x 5,457 ops/sec ±3.33% (74 runs sampled)
|
92 | centra - promise x 8,263 ops/sec ±2.10% (84 runs sampled)
|
93 | https - 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)
|
95 | Fastest is @helperdiscord/centra - promise
|
96 | ``` |
\ | No newline at end of file |