@labeg/tfetch 0.7.0 → 0.7.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 +77 -4
- package/package.json +1 -1
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
|
-
```
|
|
11
|
+
```bash
|
|
12
12
|
npm install ts-fetch
|
|
13
13
|
```
|
|
14
14
|
|
|
15
15
|
Usage
|
|
16
16
|
------
|
|
17
17
|
|
|
18
|
-
|
|
18
|
+
### Basic Usage
|
|
19
19
|
|
|
20
20
|
```typescript
|
|
21
|
-
|
|
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.
|