@star-insure/sdk 4.3.1 → 4.3.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.
@@ -0,0 +1,20 @@
1
+ declare type FetchOptions = {
2
+ url: string;
3
+ method?: 'GET' | 'POST' | 'PUT' | 'DELETE';
4
+ params?: Record<string, any>;
5
+ headers?: Record<string, string>;
6
+ };
7
+ declare type FetcherFunction<T> = () => Promise<{
8
+ ok: false;
9
+ message: string | undefined;
10
+ data?: undefined;
11
+ } | {
12
+ ok: true;
13
+ data?: T;
14
+ message: string | undefined;
15
+ }>;
16
+ /**
17
+ * A helper to return a fetch function including default headers and authorization.
18
+ */
19
+ export declare function useFetch<T = any>({ url: baseUrl, method, params, headers, }: FetchOptions): FetcherFunction<T>;
20
+ export {};
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@star-insure/sdk",
3
3
  "description": "The SDK for Star Insure client apps with shared helper functions and TypeScript definitions.",
4
4
  "author": "alexclark_nz",
5
- "version": "4.3.1",
5
+ "version": "4.3.2",
6
6
  "license": "MIT",
7
7
  "repository": {
8
8
  "type": "git",
@@ -0,0 +1,81 @@
1
+ import React from 'react';
2
+ import { usePage } from './page';
3
+
4
+ type FetchOptions = {
5
+ url: string;
6
+ method?: 'GET' | 'POST' | 'PUT' | 'DELETE';
7
+ params?: Record<string, any>;
8
+ headers?: Record<string, string>;
9
+ };
10
+
11
+ type FetcherFunction<T> = () => Promise<
12
+ | {
13
+ ok: false;
14
+ message: string | undefined;
15
+ data?: undefined;
16
+ }
17
+ | {
18
+ ok: true;
19
+ data?: T;
20
+ message: string | undefined;
21
+ }
22
+ >;
23
+
24
+ /**
25
+ * A helper to return a fetch function including default headers and authorization.
26
+ */
27
+ export function useFetch<T = any>({
28
+ url: baseUrl,
29
+ method = 'GET',
30
+ params = {},
31
+ headers = {},
32
+ }: FetchOptions): FetcherFunction<T> {
33
+ const { access_token } = usePage().props;
34
+
35
+ let url = baseUrl;
36
+
37
+ if (method === 'GET') {
38
+ const queryParams = new URLSearchParams(params);
39
+ url = `${url}?${queryParams.toString()}`;
40
+ }
41
+
42
+ return React.useMemo(
43
+ () => async () => {
44
+ const res = await fetch(url, {
45
+ method,
46
+ headers: {
47
+ 'Content-Type': 'application/json',
48
+ Accept: 'application/json',
49
+ Authorization: access_token ? `Bearer ${access_token}` : '',
50
+ ...headers,
51
+ },
52
+ body: Object.keys(params).length > 0 && method !== 'GET' ? JSON.stringify(params) : null,
53
+ });
54
+
55
+ const { ok } = res;
56
+ const body = await res.text();
57
+
58
+ let parsedBody: T | string = body;
59
+
60
+ try {
61
+ parsedBody = JSON.parse(body) as T;
62
+ } catch (error) {
63
+ // Response wasn't JSON, just use the text version.
64
+ }
65
+
66
+ const message = typeof parsedBody === 'string' ? parsedBody : undefined;
67
+
68
+ if (!ok) {
69
+ return {
70
+ ok,
71
+ message,
72
+ };
73
+ }
74
+
75
+ const data = typeof parsedBody !== 'string' ? parsedBody : undefined;
76
+
77
+ return { ok, data, message };
78
+ },
79
+ [access_token, url, params, headers, method],
80
+ );
81
+ }