@brianbuie/node-kit 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +91 -8
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
# Node Kit
|
|
1
|
+
# Node Kit • 
|
|
2
2
|
|
|
3
3
|
Basic tools for quick node.js projects
|
|
4
4
|
|
|
5
|
-
##
|
|
5
|
+
## Installing
|
|
6
6
|
|
|
7
7
|
```
|
|
8
8
|
npm add @brianbuie/node-kit
|
|
@@ -12,17 +12,100 @@ npm add @brianbuie/node-kit
|
|
|
12
12
|
import { thing } from '@brianbuie/node-kit';
|
|
13
13
|
```
|
|
14
14
|
|
|
15
|
+
## Features
|
|
16
|
+
|
|
17
|
+
### Fetcher
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
import { Fetcher } from '@brianbuie/node-kit';
|
|
21
|
+
|
|
22
|
+
// All requests will include Authorization header
|
|
23
|
+
const api = new Fetcher({
|
|
24
|
+
base: 'https://www.example.com',
|
|
25
|
+
headers: {
|
|
26
|
+
Authorization: `Bearer ${process.env.EXAMPLE_SECRET}`,
|
|
27
|
+
},
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
// GET https://www.example.com/route
|
|
31
|
+
// returns [Response, Request]
|
|
32
|
+
const [res] = await api.fetch('/route');
|
|
33
|
+
|
|
34
|
+
// GET https://www.example.com/other-route
|
|
35
|
+
// returns [string, Response, Request]
|
|
36
|
+
const [text] = await api.fetchText('/other-route');
|
|
37
|
+
|
|
38
|
+
// GET https://www.example.com/thing?page=1
|
|
39
|
+
// returns [Thing, Response, Request]
|
|
40
|
+
const [data] = await api.fetchJson<Thing>('/thing', { query: { page: 1 } });
|
|
41
|
+
|
|
42
|
+
// POST https://www.example.com/thing (data is sent as JSON in body)
|
|
43
|
+
// returns [Thing, Response, Request]
|
|
44
|
+
const [result] = await api.fetchJson<Thing>('/thing', { data: { example: 1 } });
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
### Jwt
|
|
48
|
+
|
|
49
|
+
Save a JSON Web Token in memory and reuse it throughout the process.
|
|
50
|
+
|
|
51
|
+
```js
|
|
52
|
+
import { Jwt, Fetcher } from '@brianbuie/node-kit';
|
|
53
|
+
|
|
54
|
+
const apiJwt = new Jwt({
|
|
55
|
+
payload: {
|
|
56
|
+
example: 'value',
|
|
57
|
+
},
|
|
58
|
+
options: {
|
|
59
|
+
algorithm: 'HS256',
|
|
60
|
+
},
|
|
61
|
+
seconds: 60,
|
|
62
|
+
key: process.env.JWT_KEY,
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
const api = new Fetcher({
|
|
66
|
+
base: 'https://example.com',
|
|
67
|
+
headers: {
|
|
68
|
+
Authorization: `Bearer ${apiJwt.token}`,
|
|
69
|
+
},
|
|
70
|
+
});
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
> TODO: expiration is not checked again when provided in a header
|
|
74
|
+
|
|
75
|
+
### Log
|
|
76
|
+
|
|
77
|
+
Chalk output in development, structured JSON when running in gcloud
|
|
78
|
+
|
|
79
|
+
```js
|
|
80
|
+
import { Log } from '@brianbuie/node-kit';
|
|
81
|
+
|
|
82
|
+
Log.info('message', { other: 'details' });
|
|
83
|
+
|
|
84
|
+
// Print in development, or if process.env.DEBUG or --debug argument is present
|
|
85
|
+
Log.debug('message', Response);
|
|
86
|
+
|
|
87
|
+
// Log details and throw
|
|
88
|
+
Log.error('Something happened', details, moreDetails);
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
### snapshot
|
|
92
|
+
|
|
93
|
+
Gets all enumerable and non-enumerable properties, so they can be included in JSON.stringify. Helpful for built-in objects, like Error, Request, Response, Headers, Map, etc.
|
|
94
|
+
|
|
95
|
+
```js
|
|
96
|
+
fs.writeFileSync('result.json', JSON.stringify(snapshot(response), null, 2));
|
|
97
|
+
```
|
|
98
|
+
|
|
15
99
|
## Publishing changes to this package
|
|
16
100
|
|
|
17
|
-
Commit all changes, then run
|
|
101
|
+
Commit all changes, then run:
|
|
18
102
|
|
|
19
103
|
```
|
|
20
104
|
npm version [patch|minor|major] [-m "custom commit message"]
|
|
21
105
|
```
|
|
22
106
|
|
|
23
107
|
- Bumps version in `package.json`
|
|
24
|
-
- Runs tests
|
|
25
|
-
-
|
|
26
|
-
-
|
|
27
|
-
-
|
|
28
|
-
- The new tag will trigger github action to publish to npm
|
|
108
|
+
- Runs tests (`"preversion"` script in `package.json`)
|
|
109
|
+
- Creates new commit, tagged with version
|
|
110
|
+
- Pushes commit and tags to github (`"postversion"` script in `package.json`)
|
|
111
|
+
- The new tag will trigger github action to publish to npm (`.github/actions/publish.yml`)
|