@labeg/tfetch 0.8.1 → 0.8.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.
- package/README.md +21 -7
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -37,19 +37,33 @@ fetchNumber();
|
|
|
37
37
|
|
|
38
38
|
### Working with Serializable Classes
|
|
39
39
|
|
|
40
|
+
To use automatic deserialization with classes, you need to use the [ts-serializable](https://github.com/LabEG/Serializable) library to define your models:
|
|
41
|
+
|
|
40
42
|
```typescript
|
|
41
43
|
import { tfetch } from "@labeg/tfetch";
|
|
42
|
-
import {
|
|
44
|
+
import { Serializable, jsonProperty } from "ts-serializable";
|
|
45
|
+
|
|
46
|
+
class User extends Serializable {
|
|
47
|
+
@jsonProperty(String)
|
|
48
|
+
public name: string = "";
|
|
49
|
+
|
|
50
|
+
@jsonProperty(String)
|
|
51
|
+
public email: string = "";
|
|
52
|
+
|
|
53
|
+
@jsonProperty(Number)
|
|
54
|
+
public age: number = 0;
|
|
55
|
+
}
|
|
43
56
|
|
|
44
|
-
const
|
|
45
|
-
const result:
|
|
46
|
-
url: "https://example.com/
|
|
47
|
-
returnType:
|
|
57
|
+
const fetchUser = async () => {
|
|
58
|
+
const result: User = await tfetch({
|
|
59
|
+
url: "https://example.com/api/user/1",
|
|
60
|
+
returnType: User
|
|
48
61
|
});
|
|
49
|
-
console.log(result instanceof
|
|
62
|
+
console.log(result instanceof User); // true
|
|
63
|
+
console.log(result.name); // Properly deserialized
|
|
50
64
|
};
|
|
51
65
|
|
|
52
|
-
|
|
66
|
+
fetchUser();
|
|
53
67
|
```
|
|
54
68
|
|
|
55
69
|
### POST Request with Body
|