@openapi-qraft/react 1.0.0-beta.2 → 1.0.0-beta.3

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 (3) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/README.md +98 -56
  3. package/package.json +1 -1
package/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # @openapi-qraft/react
2
2
 
3
+ ## 1.0.0-beta.3
4
+
5
+ ### Patch Changes
6
+
7
+ - ab8d154: Update README
8
+
3
9
  ## 1.0.0-beta.2
4
10
 
5
11
  ### Patch Changes
package/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  `@openapi-qraft/react` is a modular TypeScript client designed to facilitate type-safe API requests in React
4
4
  applications,
5
- leveraging the power of Tanstack Query. It utilizes a Proxy-based architecture to dynamically generate Tanstack Query
5
+ leveraging the power of **Tanstack Query v5**. It utilizes a Proxy-based architecture to dynamically generate
6
6
  hooks with typed parameters, ensuring that your API requests are both type-safe and efficient.
7
7
 
8
8
  ## Features
@@ -11,17 +11,26 @@ hooks with typed parameters, ensuring that your API requests are both type-safe
11
11
  developer experience.
12
12
  - **Modular Design:** Customize the utility with a set of callbacks to handle API calls according to your project's
13
13
  needs.
14
- - **Integration with [Tanstack Query](https://tanstack.com/query/v5):** Seamlessly integrate with _Tanstack Query_ for
15
- handling server state, caching, and data synchronization.
14
+ - **Integration with [Tanstack Query v5](https://tanstack.com/query/v5):** Seamlessly integrate with _Tanstack Query_
15
+ for handling server state, caching, and data synchronization.
16
16
  - **Dynamic Proxy-Based Hooks:** Automatically generate React Query hooks for your API endpoints without manual
17
17
  boilerplate.
18
18
 
19
19
  ## Installation
20
20
 
21
+ First, install the core package for your project:
22
+
21
23
  ```bash
22
24
  npm install @openapi-qraft/react@beta
23
25
  ```
24
26
 
27
+ If your project doesn't already include `@tanstack/react-query`, you'll also need to install it. This package is
28
+ essential for handling server state in React applications:
29
+
30
+ ```bash
31
+ npm install @tanstack/react-query
32
+ ```
33
+
25
34
  ## Getting Started
26
35
 
27
36
  To get started with `@openapi-qraft/react`, you'll need to set up your client by passing in your API services and a set
@@ -47,8 +56,8 @@ npx openapi-typescript https://api.dev.monite.com/openapi.json?version=2023-09-0
47
56
 
48
57
  #### Generating Qraft Services
49
58
 
50
- Next, use `@openapi-qraft/cli` to generate the services and typed Tanstack Query React Hooks. Ensure to specify the path to the
51
- TypeScript definitions generated in the previous step.
59
+ Next, use `@openapi-qraft/cli` to generate the services and typed Tanstack Query React Hooks. Ensure to specify the path
60
+ to the TypeScript definitions generated in the previous step.
52
61
 
53
62
  ```bash
54
63
  npx @openapi-qraft/cli@beta https://api.dev.monite.com/openapi.json?version=2023-09-01 --output-dir src/api --openapi-types-import-path '../openapi.d.ts'
@@ -122,7 +131,11 @@ function Providers({ children }: { children: React.ReactNode }) {
122
131
 
123
132
  function Example() {
124
133
  const { isPending, error, data } =
125
- qract.approvalPolicies.getApprovalPoliciesId.useQuery({
134
+ qraft.approvalPolicies.getApprovalPoliciesId.useQuery({
135
+ header: {
136
+ 'x-monite-version': '2023-09-01',
137
+ 'x-monite-entity-id': '3e3e-3e3e-3e3e',
138
+ },
126
139
  path: {
127
140
  approval_policy_id: '1',
128
141
  },
@@ -156,25 +169,19 @@ and more.
156
169
 
157
170
  ```ts
158
171
  /**
159
- * `qraft.approvalPolicies.getApprovalPoliciesId.useQuery(...)` will execute:
172
+ * Will execute the request:
160
173
  *
161
- * GET /approval_policies/1?items_order=asc
162
- * x-monite-version: '2023-09-01'
163
- *
164
- * Used Open API: GET /approval_policies/{approval_policy_id}
174
+ * GET /entities?sort=asc
175
+ * x-monite-version: 2023-09-01
165
176
  **/
166
- const { data, error, isPending } =
167
- qraft.approvalPolicies.getApprovalPoliciesId.useQuery({
168
- header: {
169
- 'x-monite-version': '2023-09-01',
170
- },
171
- path: {
172
- approval_policy_id: '1',
173
- },
174
- query: {
175
- items_order: 'asc',
176
- },
177
- });
177
+ const { data, error, isPending } = qraft.entities.getEntities.useQuery({
178
+ header: {
179
+ 'x-monite-version': '2023-09-01',
180
+ },
181
+ query: {
182
+ sort: 'updated_at',
183
+ },
184
+ });
178
185
  ```
179
186
 
180
187
  ### [useMutation 🔗](https://tanstack.com/query/latest/docs/framework/react/reference/useMutation)
@@ -187,21 +194,23 @@ only `body` when calling `mutate()` function:
187
194
  ```ts
188
195
  const mutation = qraft.entities.postEntitiesIdDocuments.useMutation({
189
196
  path: {
190
- entity_id: '1',
197
+ entity_id: '3e3e-3e3e-3e3e',
198
+ },
199
+ header: {
200
+ 'x-monite-version': '2023-09-01',
191
201
  },
192
202
  });
193
203
 
194
204
  /**
195
- * `mutation.mutate(...)` will execute:
205
+ * Will execute the request when call `mutation.mutate(...)`:
196
206
  *
197
- * POST /entities/1/documents
207
+ * POST /entities/3e3e-3e3e-3e3e/documents
198
208
  * Content-Type: application/json
199
- * {"document_title": "Agreement of Intent"}
200
- *
201
- * Used Open API: POST /entities/{entity_id}/documents
209
+ * x-monite-version: 2023-09-01
210
+ * {"company_tax_id_verification": ["verification-id"]}
202
211
  **/
203
212
  mutation.mutate({
204
- document_title: 'Agreement of Intent',
213
+ company_tax_id_verification: ['verification-id'],
205
214
  });
206
215
  ```
207
216
 
@@ -215,65 +224,98 @@ you will need to pass the _parameters and the body_ of the request when you call
215
224
  const mutation = qraft.entities.postEntitiesIdDocuments.useMutation();
216
225
 
217
226
  /**
218
- * `mutation.mutate(...)` will execute:
227
+ * Will execute the request when call `mutation.mutate(...)`:
219
228
  *
220
- * POST /entities/1/documents
229
+ * POST /entities/3e3e-3e3e-3e3e/documents
221
230
  * Content-Type: application/json
222
- * {"document_title": "Agreement of Intent"}
223
- *
224
- * Used Open API: POST /entities/{entity_id}/documents
231
+ * {"company_tax_id_verification": ["verification-id"]}
225
232
  **/
226
-
227
233
  mutation.mutate({
228
234
  path: {
229
- entity_id: '1',
235
+ entity_id: '3e3e-3e3e-3e3e',
236
+ },
237
+ header: {
238
+ 'x-monite-version': '2023-09-01',
230
239
  },
231
240
  body: {
232
- document_title: 'Agreement of Intent',
241
+ company_tax_id_verification: ['verification-id'],
233
242
  },
234
243
  });
235
244
  ```
236
245
 
237
246
  ### [useInfiniteQuery 🔗](https://tanstack.com/query/latest/docs/framework/react/reference/useInfiniteQuery)
238
247
 
239
- `todo::Add Description`
248
+ #### _Offset-based_
240
249
 
241
250
  ```ts
242
251
  /**
243
- * `qraft.files.getFiles.useInfiniteQuery(...)` will execute:
252
+ * Will execute the initial request:
244
253
  *
245
- * GET /files?search=Agreements&limit=10&page=1
254
+ * GET /posts?search=limit=10&page=1
246
255
  * Content-Type: application/json
247
- *
248
- * Used Open API: GET /files
249
256
  **/
250
- const infiniteQuery = qraft.files.getFiles.useInfiniteQuery(
251
- {
252
- query: {
253
- search: 'Agreements',
254
- limit: 10,
255
- },
256
- },
257
+ const infiniteQuery = qraft.posts.getPosts.useInfiniteQuery(
258
+ { query: { limit: 10 } },
257
259
  {
258
260
  getNextPageParam: (lastPage, allPages, lastPageParams) => {
259
- if (lastPage.length < 10) return;
261
+ if (lastPage.length < 10) return; // if less than 10 items, there are no more pages
260
262
  return {
261
263
  query: {
262
- page: lastPageParams.query?.page
263
- ? lastPageParams.query.page + 1
264
- : undefined,
264
+ page: Number(lastPageParams.query?.page) + 1,
265
265
  },
266
266
  };
267
267
  },
268
268
  initialPageParam: {
269
269
  query: {
270
- page: 1,
270
+ page: 1, // will be used in initial request
271
271
  },
272
272
  },
273
273
  }
274
274
  );
275
275
 
276
- infiniteQuery.fetchNextPage(); // will execute GET /files?search=Agreements&limit=10&page=2
276
+ // ⬇︎ will execute GET /posts?limit=10&page=2
277
+ infiniteQuery.fetchNextPage();
278
+ ```
279
+
280
+ #### _Pagination token_ based
281
+
282
+ If your API uses pagination tokens, the Infinite Queries implementation becomes quite laconically:
283
+
284
+ ```ts
285
+ /**
286
+ * Will execute the initial request:
287
+ *
288
+ * GET /data_exports
289
+ * Content-Type: application/json
290
+ **/
291
+ const infiniteQuery = qraft.dataExports.getDataExports.useInfiniteQuery(
292
+ {
293
+ header: {
294
+ 'x-monite-version': '2023-09-01',
295
+ 'x-monite-entity-id': '3e3e-3e3e-3e3e',
296
+ },
297
+ },
298
+ {
299
+ // * required by Tanstack Query
300
+ initialPageParam: {
301
+ query: { pagination_token: undefined }, // will be used in initial request
302
+ },
303
+ // * required by Tanstack Query
304
+ getNextPageParam: (lastPage, allPages, lastPageParam) => ({
305
+ query: { pagination_token: lastPage.next_pagination_token },
306
+ }),
307
+ // * optional
308
+ getPreviousPageParam: (firstPage, allPages, firstPageParam) => ({
309
+ query: { pagination_token: firstPage.prev_pagination_token },
310
+ }),
311
+ }
312
+ );
313
+
314
+ // ⬇︎ will execute GET /data_exports?pagination_token=...NEXT_PAGE_TOKEN....
315
+ infiniteQuery.fetchNextPage();
316
+
317
+ // ⬇︎ will execute GET /data_exports?pagination_token=...PREV_PAGE_TOKEN....
318
+ infiniteQuery.fetchPreviousPage();
277
319
  ```
278
320
 
279
321
  ## Contributing
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openapi-qraft/react",
3
- "version": "1.0.0-beta.2",
3
+ "version": "1.0.0-beta.3",
4
4
  "description": "API client for React, providing type-safe requests and dynamic Tanstack Query React Hooks via a modular, Proxy-based architecture.",
5
5
  "scripts": {
6
6
  "build": "rimraf dist/ && tsc --project tsconfig.build.json",