@labeg/tfetch 0.7.0 → 0.7.2

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 +77 -4
  2. package/package.json +12 -12
package/README.md CHANGED
@@ -1,22 +1,95 @@
1
1
  Typescript Serializable Fetch
2
2
  =====
3
3
 
4
- A small library for sending serialized data and receiving deserialized data with strict data type checking.
4
+ A small library for sending serialized data and receiving deserialized data with strict data type checking. This library is built on top of the Fetch API and provides additional features like caching, error handling, and support for serializable classes.
5
5
 
6
6
  Installation
7
7
  ------
8
8
 
9
9
  You can use the following command to install this package:
10
10
 
11
- ``` bash
11
+ ```bash
12
12
  npm install ts-fetch
13
13
  ```
14
14
 
15
15
  Usage
16
16
  ------
17
17
 
18
- todo...
18
+ ### Basic Usage
19
19
 
20
20
  ```typescript
21
- todo...
21
+ import { tfetch } from "ts-fetch";
22
+
23
+ // Example with primitive types
24
+ const fetchNumber = async () => {
25
+ const result: number = await tfetch({
26
+ url: "https://example.com/number",
27
+ returnType: 0
28
+ });
29
+ console.log(result); // Logs the number fetched from the API
30
+ };
31
+
32
+ fetchNumber();
22
33
  ```
34
+
35
+ ### Working with Serializable Classes
36
+
37
+ ```typescript
38
+ import { tfetch } from "ts-fetch";
39
+ import { TestClass } from "./fixtures/TestClass";
40
+
41
+ const fetchClass = async () => {
42
+ const result: TestClass = await tfetch({
43
+ url: "https://example.com/class",
44
+ returnType: TestClass
45
+ });
46
+ console.log(result instanceof TestClass); // true
47
+ };
48
+
49
+ fetchClass();
50
+ ```
51
+
52
+ ### CRUD Operations with `CrudHttpRepository`
53
+
54
+ ```typescript
55
+ import { CrudHttpRepository } from "ts-fetch";
56
+ import { TestClass } from "./fixtures/TestClass";
57
+
58
+ class TestRepository extends CrudHttpRepository<TestClass> {
59
+ protected apiRoot = "https://example.com/api";
60
+ protected modelConstructor = TestClass;
61
+ }
62
+
63
+ const repository = new TestRepository();
64
+
65
+ const fetchData = async () => {
66
+ const item = await repository.getById(1);
67
+ console.log(item);
68
+ };
69
+
70
+ fetchData();
71
+ ```
72
+
73
+ ### Error Handling
74
+
75
+ The library provides custom error classes for handling network and backend errors:
76
+
77
+ ```typescript
78
+ import { tfetch } from "ts-fetch";
79
+
80
+ const fetchWithErrorHandling = async () => {
81
+ try {
82
+ await tfetch({
83
+ url: "https://example.com/error"
84
+ });
85
+ } catch (error) {
86
+ console.error(error);
87
+ }
88
+ };
89
+
90
+ fetchWithErrorHandling();
91
+ ```
92
+
93
+ ### Caching
94
+
95
+ GET and HEAD requests are cached automatically to improve performance. The cache is cleared when an error occurs or when the request completes successfully.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@labeg/tfetch",
3
- "version": "0.7.0",
3
+ "version": "0.7.2",
4
4
  "author": "Eugene Labutin",
5
5
  "license": "MIT",
6
6
  "homepage": "https://github.com/LabEG/ts-fetch#readme",
@@ -35,20 +35,20 @@
35
35
  "ts-serializable": ">=3.0.0"
36
36
  },
37
37
  "devDependencies": {
38
- "@commitlint/cli": "^19.6.1",
39
- "@commitlint/config-conventional": "^19.6.0",
38
+ "@commitlint/cli": "^19.8.1",
39
+ "@commitlint/config-conventional": "^19.8.1",
40
40
  "@favware/cliff-jumper": "^6.0.0",
41
- "@labeg/code-style": "^6.0.0",
42
- "@swc-node/register": "^1.10.9",
43
- "@types/chai": "^5.0.1",
44
- "chai": "^5.1.2",
45
- "fastify": "^5.2.2",
41
+ "@labeg/code-style": "^6.5.0",
42
+ "@swc-node/register": "^1.10.10",
43
+ "@types/chai": "^5.2.2",
44
+ "chai": "^5.2.1",
45
+ "fastify": "^5.4.0",
46
46
  "husky": "^9.1.7",
47
- "lint-staged": "^15.3.0",
48
- "npm-check-updates": "^17.1.13",
47
+ "lint-staged": "^16.1.2",
48
+ "npm-check-updates": "^18.0.1",
49
49
  "rimraf": "^6.0.1",
50
- "ts-serializable": "^3.7.3",
51
- "typescript": "^5.7.2"
50
+ "ts-serializable": "^4.2.1",
51
+ "typescript": "^5.8.3"
52
52
  },
53
53
  "keywords": [
54
54
  "serialization",