@neonwilderness/ecjsonstorage 1.0.4 → 1.0.5

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.
Files changed (2) hide show
  1. package/README.md +171 -21
  2. package/package.json +2 -2
package/README.md CHANGED
@@ -1,18 +1,18 @@
1
1
  # About ECJsonStorage
2
2
  ECJsonStorage is a TS/JS wrapper to the [ExtendsClass JSON Storage API](https://extendsclass.com/json-storage.html) offering of Cyril Bois.
3
3
 
4
- For API usage, you need a (free) API-Key for which you can apply [here](https://extendsclass.com/create-account-form).
4
+ For API usage, you need a (free) API-Key for which you can apply on the [ExtendsClass site](https://extendsclass.com/create-account-form).
5
5
 
6
- ECJsonStorage enables you to create, update, patch and delete Bins, which store JSON data. There is a usage limit of
6
+ ECJsonStorage enables you to create, update, patch and delete "Bins" to store JSON data. There is a usage limit of
7
7
  - 10.000 calls per month
8
8
  - Bin size limit of 100kb
9
9
  - total account limit of 10mb.
10
10
 
11
11
  # Installation
12
12
 
13
- - `npm i @neonwilderness/ecjsonstorage -S` to install the module, which is then both available as CommonJs and ESM version.
13
+ - `npm i @neonwilderness/ecjsonstorage -S` to install the package (available as CommonJs and ESM).
14
14
 
15
- - Create an .env file in your project directory root and add the following keys:
15
+ - Copy the `.env.template` to your project folder and create a related `.env` file by adding at least the following keys:
16
16
 
17
17
  `JSONAPI=your ExtendsClass Api-key`
18
18
 
@@ -24,26 +24,176 @@
24
24
 
25
25
  # Usage
26
26
 
27
- ## Constructor
27
+ ## Constructor
28
28
 
29
- ```
30
- import { ECJsonStorage } from '@neonwilderness/ecjsonstorage';
31
- import { config } from 'dotenv-safe';
32
- config();
29
+ ### constructor(apiKey: string, securityKey?: string, url?: string)
33
30
 
34
- const json = new ECJsonStorage(process.env.JSONAPI, process.env.JSONSEC);
35
- ...
36
- ```
31
+ ```
32
+ import { ECJsonStorage } from '@neonwilderness/ecjsonstorage';
33
+ import { config } from 'dotenv-safe';
34
+ config();
37
35
 
38
- ## Create a new bin
36
+ const json = new ECJsonStorage(process.env.JSONAPI, process.env.JSONSEC);
37
+ ...
38
+ ```
39
39
 
40
- `createBin(payload: object, keepPrivate: boolean)`
40
+ > Param `url` will only be needed once the main API address changes. To protect your bins from unauthorized access, you should use a Security-key and pass it as the second parameter.
41
41
 
42
- ```
43
- const json = new ECJsonStorage(process.env.JSONAPI, process.env.JSONSEC);
44
- const payload = { name: 'Neon', profession: 'IT-Consultant' };
45
- const res = await json.createBin(payload, true);
46
- console.log(res);
47
- ```
42
+ ## Create a new bin
48
43
 
49
- Further Documentation/Usage details will follow swiftly; for now you may wanna consult the [TS types file](dist/index.d.ts).
44
+ ### createBin(payload: object, keepPrivate: boolean): Promise<resCreateBin | resError>
45
+
46
+ Param | Type | Text
47
+ --- | --- | ---
48
+ payload | object | JSON object
49
+ keepPrivate | boolean | TRUE=Bin is kept private | Security-key needed for access
50
+
51
+ resCreateBin Property | Type | Text
52
+ --- | --- | ---
53
+ status | string | 'ok'
54
+ uri | string | full URI of the created bin
55
+ bin | string | bin ID
56
+
57
+ <u>or</u>
58
+
59
+ resError Property | Type | Text
60
+ --- | --- | ---
61
+ status | string | 'error'
62
+ statusText | string | error text
63
+
64
+ > If an error occurs, the returned object is always `<resError>`.
65
+
66
+ > Best practice is to always check `res.status` first. If it's not 'ok' (lowercase), then the class function did not run successfully.
67
+
68
+ ```
69
+ const json = new ECJsonStorage(process.env.JSONAPI, process.env.JSONSEC);
70
+ const payload = { name: 'Neon', profession: 'IT-Consultant' };
71
+ const res = await json.createBin(payload, true);
72
+ console.log(res);
73
+ ```
74
+
75
+ ## Get all bin IDs
76
+
77
+ ### getBins(): Promise<resGetBins | resError>
78
+
79
+ This function returns all bins that have been created (and still exist) for the given Api-key. It also returns the current quota limit, i.e. number of calls made and number of ramaining calls (Limit is 10.000 calls per month).
80
+
81
+ resGetBins Property | Type | Text
82
+ --- | --- | ---
83
+ status | string | 'ok'
84
+ callsDone | number | number of API calls made this month
85
+ callsRemaining | number | number of API calls remaining before the limit kicks in
86
+ bins | string[] | array of strings (bin ID)
87
+
88
+ ```
89
+ const json = new ECJsonStorage(process.env.JSONAPI, process.env.JSONSEC);
90
+ const res = await json.getBins();
91
+ console.log(`I have ${res.bins.length} bins and the IDs are: ${res.bins}`);
92
+ ```
93
+
94
+ ## Get statistics (Quota limit)
95
+
96
+ ### getStats(): Promise<resGetStats | resError>
97
+
98
+ This function is a syntactic sugar function for getBins and only returns the current quota limit, i.e. number of calls made and number of ramaining calls.
99
+
100
+ resGetStats Property | Type | Text
101
+ --- | --- | ---
102
+ status | string | 'ok'
103
+ callsDone | number | number of API calls made this month
104
+ callsRemaining | number | number of API calls remaining before the limit kicks in
105
+
106
+ ```
107
+ const json = new ECJsonStorage(process.env.JSONAPI, process.env.JSONSEC);
108
+ const res = await json.getStats();
109
+ console.log(`Used calls: ${res.callsDone}, calls remaining: ${res.callsRemaining}`);
110
+ ```
111
+
112
+ ## Get a bin's content
113
+
114
+ ### getBin(bin: string): Promise<resGetBin | resError>
115
+
116
+ This function reads the bin and returns its content.
117
+
118
+ Param | Type | Text
119
+ --- | --- | ---
120
+ bin | string | Bin ID to read
121
+
122
+ resGetBin Property | Type | Text
123
+ --- | --- | ---
124
+ status | string | 'ok'
125
+ data | object | JSON object (Content of bin)
126
+
127
+ ```
128
+ const json = new ECJsonStorage(process.env.JSONAPI, process.env.JSONSEC);
129
+ const res = await json.getBin('c8ba8b1da99c');
130
+ console.log(res.data);
131
+ ```
132
+
133
+ ## Update a bin
134
+
135
+ ### updateBin(payload: object, bin: string): Promise<resUpdateBin | resError>
136
+
137
+ Param | Type | Text
138
+ --- | --- | ---
139
+ payload | object | JSON object
140
+ bin | string | Bin ID to be updated
141
+
142
+ resUpdateBin Property | Type | Text
143
+ --- | --- | ---
144
+ status | string | 'ok'
145
+ data | string | Stringified JSON object (Content of updated bin)
146
+
147
+ ```
148
+ const json = new ECJsonStorage(process.env.JSONAPI, process.env.JSONSEC);
149
+ const newPayload = { name: 'Wilderness', profession: 'Web Ninja' };
150
+ const res = await json.updateBin(newPayload, 'c8ba8b1da99c');
151
+ console.log(res);
152
+ ```
153
+
154
+ ## Patch a bin
155
+
156
+ ### patchBin(payload: object, bin: string): Promise<resPatchBin | resError>
157
+
158
+ Param | Type | Text
159
+ --- | --- | ---
160
+ payload | object | JSON object
161
+ bin | string | Bin ID to be patched
162
+
163
+ resUpdateBin Property | Type | Text
164
+ --- | --- | ---
165
+ status | string | 'ok'
166
+ data | string | Stringified JSON object (Content of patched bin)
167
+
168
+ ```
169
+ const json = new ECJsonStorage(process.env.JSONAPI, process.env.JSONSEC);
170
+ //current bin = { name: 'Wilderness', profession: 'Web Ninja' }
171
+ const addPayload = { song: 'In a NeonWilderness he was restless', state: 'Germany' };
172
+ const res = await json.patchBin(addPayload, 'c8ba8b1da99c');
173
+ console.log(res);
174
+ //patched bin = { name: 'Wilderness', profession: 'Web Ninja', song: 'In a NeonWilderness he was restless', state: 'Germany' }
175
+ ```
176
+
177
+ > For more information regarding PATCH, please refer to the [ExtendsClass site - "Partially update JSON"](https://extendsclass.com/json-storage.html#apiDocumentation).
178
+
179
+ ## Delete a bin
180
+
181
+ ### deleteBin(bin: string): Promise<resDeleteBin | resError>
182
+
183
+ Param | Type | Text
184
+ --- | --- | ---
185
+ bin | string | Bin ID to be deleted
186
+
187
+ resDeleteBin Property | Type | Text
188
+ --- | --- | ---
189
+ status | string | 'ok'
190
+
191
+ ```
192
+ const json = new ECJsonStorage(process.env.JSONAPI, process.env.JSONSEC);
193
+ const bin = 'c8ba8b1da99c';
194
+ const res = await json.deleteBin(bin);
195
+ if (res.status !== 'ok')
196
+ console.error(`Could not delete bin ${bin}, error=${<resError>statusText}.`);
197
+ ```
198
+
199
+ In addition, you also may wanna consult the [TS types file](dist/index.d.ts).
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@neonwilderness/ecjsonstorage",
3
- "version": "1.0.4",
3
+ "version": "1.0.5",
4
4
  "description": "",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.js",
@@ -39,7 +39,7 @@
39
39
  "type": "module",
40
40
  "devDependencies": {
41
41
  "@types/dotenv-safe": "^9.1.0",
42
- "@types/node": "^26.6.3",
42
+ "@types/node": "^26.6.4",
43
43
  "dotenv": "^18.0.5",
44
44
  "dotenv-safe": "^9.1.0",
45
45
  "tsup": "^8.5.1",