@labeg/tfetch 0.8.0 → 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.
Files changed (2) hide show
  1. package/README.md +161 -13
  2. package/package.json +5 -5
package/README.md CHANGED
@@ -1,10 +1,14 @@
1
- Typescript Serializable Fetch
2
- =====
1
+ # Typescript Serializable Fetch
2
+
3
+ ![npm version](https://img.shields.io/npm/v/@labeg/tfetch.svg)
4
+ ![npm downloads](https://img.shields.io/npm/dm/@labeg/tfetch.svg)
5
+ ![GitHub](https://img.shields.io/github/license/LabEG/ts-fetch.svg)
6
+ ![build status](https://github.com/LabEG/ts-fetch/workflows/Test%20Pull%20Request/badge.svg)
7
+ [![CodeQL](https://github.com/LabEG/ts-fetch/workflows/CodeQL%20Advanced/badge.svg)](https://github.com/LabEG/ts-fetch/security/code-scanning)
3
8
 
4
9
  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
10
 
6
- Installation
7
- ------
11
+ ## Installation
8
12
 
9
13
  You can use the following command to install this package:
10
14
 
@@ -12,8 +16,7 @@ You can use the following command to install this package:
12
16
  npm install @labeg/tfetch
13
17
  ```
14
18
 
15
- Usage
16
- ------
19
+ ## Usage
17
20
 
18
21
  ### Basic Usage
19
22
 
@@ -34,19 +37,164 @@ fetchNumber();
34
37
 
35
38
  ### Working with Serializable Classes
36
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
+
42
+ ```typescript
43
+ import { tfetch } from "@labeg/tfetch";
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
+ }
56
+
57
+ const fetchUser = async () => {
58
+ const result: User = await tfetch({
59
+ url: "https://example.com/api/user/1",
60
+ returnType: User
61
+ });
62
+ console.log(result instanceof User); // true
63
+ console.log(result.name); // Properly deserialized
64
+ };
65
+
66
+ fetchUser();
67
+ ```
68
+
69
+ ### POST Request with Body
70
+
37
71
  ```typescript
38
72
  import { tfetch } from "@labeg/tfetch";
39
- import { TestClass } from "./fixtures/TestClass";
40
73
 
41
- const fetchClass = async () => {
42
- const result: TestClass = await tfetch({
43
- url: "https://example.com/class",
44
- returnType: TestClass
74
+ const createUser = async () => {
75
+ const result = await tfetch({
76
+ method: "POST",
77
+ url: "https://example.com/api/users",
78
+ body: {
79
+ name: "John Doe",
80
+ email: "john@example.com"
81
+ },
82
+ returnType: Object
45
83
  });
46
- console.log(result instanceof TestClass); // true
84
+ console.log(result);
47
85
  };
48
86
 
49
- fetchClass();
87
+ createUser();
88
+ ```
89
+
90
+ ### Custom Headers
91
+
92
+ ```typescript
93
+ import { tfetch } from "@labeg/tfetch";
94
+
95
+ const fetchWithHeaders = async () => {
96
+ const result = await tfetch({
97
+ method: "GET",
98
+ url: "https://example.com/api/data",
99
+ headers: {
100
+ "Authorization": "Bearer your-token-here",
101
+ "X-Custom-Header": "custom-value"
102
+ },
103
+ returnType: Object
104
+ });
105
+ console.log(result);
106
+ };
107
+
108
+ fetchWithHeaders();
109
+ ```
110
+
111
+ ### All HTTP Methods
112
+
113
+ ```typescript
114
+ import { tfetch } from "@labeg/tfetch";
115
+
116
+ // GET request
117
+ const getData = async () => {
118
+ return await tfetch({
119
+ method: "GET",
120
+ url: "https://example.com/api/resource",
121
+ returnType: Object
122
+ });
123
+ };
124
+
125
+ // POST request
126
+ const postData = async () => {
127
+ return await tfetch({
128
+ method: "POST",
129
+ url: "https://example.com/api/resource",
130
+ body: { data: "value" },
131
+ returnType: Object
132
+ });
133
+ };
134
+
135
+ // PUT request
136
+ const updateData = async () => {
137
+ return await tfetch({
138
+ method: "PUT",
139
+ url: "https://example.com/api/resource/1",
140
+ body: { data: "updated value" }
141
+ });
142
+ };
143
+
144
+ // DELETE request
145
+ const deleteData = async () => {
146
+ return await tfetch({
147
+ method: "DELETE",
148
+ url: "https://example.com/api/resource/1"
149
+ });
150
+ };
151
+ ```
152
+
153
+ ### Advanced Fetch Options
154
+
155
+ You can pass any standard Fetch API options:
156
+
157
+ ```typescript
158
+ import { tfetch } from "@labeg/tfetch";
159
+
160
+ const advancedRequest = async () => {
161
+ const result = await tfetch({
162
+ method: "POST",
163
+ url: "https://example.com/api/data",
164
+ body: { key: "value" },
165
+ returnType: Object,
166
+ // Standard Fetch API options
167
+ cache: "no-cache",
168
+ credentials: "include",
169
+ mode: "cors",
170
+ redirect: "follow",
171
+ referrerPolicy: "no-referrer",
172
+ signal: AbortSignal.timeout(5000), // 5 second timeout
173
+ });
174
+ console.log(result);
175
+ };
176
+
177
+ advancedRequest();
178
+ ```
179
+
180
+ ### Working with FormData
181
+
182
+ ```typescript
183
+ import { tfetch } from "@labeg/tfetch";
184
+
185
+ const uploadFile = async (file: File) => {
186
+ const formData = new FormData();
187
+ formData.append("file", file);
188
+ formData.append("description", "My file");
189
+
190
+ const result = await tfetch({
191
+ method: "POST",
192
+ url: "https://example.com/api/upload",
193
+ body: formData,
194
+ returnType: Object
195
+ });
196
+ console.log(result);
197
+ };
50
198
  ```
51
199
 
52
200
  ### CRUD Operations with `CrudHttpRepository`
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@labeg/tfetch",
3
- "version": "0.8.0",
3
+ "version": "0.8.2",
4
4
  "author": "Eugene Labutin",
5
5
  "license": "MIT",
6
6
  "homepage": "https://github.com/LabEG/ts-fetch#readme",
@@ -35,17 +35,17 @@
35
35
  "ts-serializable": ">=3.0.0"
36
36
  },
37
37
  "devDependencies": {
38
- "@commitlint/cli": "^19.8.1",
39
- "@commitlint/config-conventional": "^19.8.1",
38
+ "@commitlint/cli": "^20.2.0",
39
+ "@commitlint/config-conventional": "^20.2.0",
40
40
  "@favware/cliff-jumper": "^6.0.0",
41
41
  "@labeg/code-style": "^6.5.0",
42
42
  "@swc-node/register": "^1.10.10",
43
43
  "@types/chai": "^5.2.2",
44
- "chai": "^5.2.1",
44
+ "chai": "^6.2.1",
45
45
  "fastify": "^5.4.0",
46
46
  "husky": "^9.1.7",
47
47
  "lint-staged": "^16.1.2",
48
- "npm-check-updates": "^18.0.1",
48
+ "npm-check-updates": "^19.2.0",
49
49
  "rimraf": "^6.0.1",
50
50
  "ts-serializable": "^4.2.1",
51
51
  "typescript": "^5.8.3"