@qoretechnologies/reqraft 0.2.2 → 0.2.4

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 (47) hide show
  1. package/dist/contexts/ReqraftContext.d.ts +0 -4
  2. package/dist/contexts/ReqraftContext.d.ts.map +1 -1
  3. package/dist/contexts/ReqraftContext.js +0 -3
  4. package/dist/contexts/ReqraftContext.js.map +1 -1
  5. package/dist/hooks/useReqraftProperty.d.ts +1 -1
  6. package/dist/hooks/useReqraftProperty.d.ts.map +1 -1
  7. package/dist/index.d.ts +2 -2
  8. package/dist/index.d.ts.map +1 -1
  9. package/dist/index.js +2 -1
  10. package/dist/index.js.map +1 -1
  11. package/dist/providers/FetchProvider.d.ts.map +1 -1
  12. package/dist/providers/FetchProvider.js +0 -18
  13. package/dist/providers/FetchProvider.js.map +1 -1
  14. package/dist/providers/ReqraftProvider.d.ts +9 -1
  15. package/dist/providers/ReqraftProvider.d.ts.map +1 -1
  16. package/dist/providers/ReqraftProvider.js +13 -3
  17. package/dist/providers/ReqraftProvider.js.map +1 -1
  18. package/dist/providers/StorageProvider.d.ts +3 -2
  19. package/dist/providers/StorageProvider.d.ts.map +1 -1
  20. package/dist/providers/StorageProvider.js +2 -2
  21. package/dist/providers/StorageProvider.js.map +1 -1
  22. package/dist/utils/fetch.d.ts +1 -0
  23. package/dist/utils/fetch.d.ts.map +1 -1
  24. package/dist/utils/fetch.js +9 -9
  25. package/dist/utils/fetch.js.map +1 -1
  26. package/package.json +1 -1
  27. package/src/components/form/fields/Field.tsx +0 -37
  28. package/src/components/form/fields/string/String.stories.tsx +0 -23
  29. package/src/components/form/fields/string/String.tsx +0 -68
  30. package/src/components/menu/Menu.stories.tsx +0 -73
  31. package/src/components/menu/Menu.tsx +0 -244
  32. package/src/contexts/FetchContext.tsx +0 -25
  33. package/src/contexts/ReqraftContext.tsx +0 -16
  34. package/src/contexts/StorageContext.tsx +0 -33
  35. package/src/hooks/useFetch/useFetch.stories.tsx +0 -123
  36. package/src/hooks/useFetch/useFetch.tsx +0 -71
  37. package/src/hooks/useReqraftProperty.ts +0 -16
  38. package/src/hooks/useStorage/useStorage.stories.tsx +0 -84
  39. package/src/hooks/useStorage/useStorage.ts +0 -30
  40. package/src/hooks/useValidation.ts +0 -9
  41. package/src/index.tsx +0 -12
  42. package/src/providers/FetchProvider.tsx +0 -62
  43. package/src/providers/ReqraftProvider.tsx +0 -33
  44. package/src/providers/StorageProvider.tsx +0 -80
  45. package/src/types/Form.ts +0 -57
  46. package/src/types.ts +0 -12
  47. package/src/utils/fetch.ts +0 -121
@@ -1,62 +0,0 @@
1
- import { useQueryClient } from '@tanstack/react-query';
2
- import { useState } from 'react';
3
- import { useEffectOnce } from 'react-use';
4
- import { FetchContext, TReqraftContextQueryConfig } from '../contexts/FetchContext';
5
- import { useReqraftProperty } from '../hooks/useReqraftProperty';
6
- import { query, setupFetch } from '../utils/fetch';
7
-
8
- export interface IReqraftFetchProviderProps {
9
- children: React.ReactNode;
10
- }
11
-
12
- export const ReqraftFetchProvider = ({ children }: IReqraftFetchProviderProps) => {
13
- const queryClient = useQueryClient();
14
- const instance = useReqraftProperty('instance');
15
- const instanceToken = useReqraftProperty('instanceToken');
16
- const instanceUnauthorizedRedirect = useReqraftProperty('instanceUnauthorizedRedirect');
17
-
18
- const [ready, setReady] = useState(false);
19
-
20
- useEffectOnce(() => {
21
- setupFetch({
22
- instance,
23
- instanceToken,
24
- unauthorizedRedirect: instanceUnauthorizedRedirect,
25
- });
26
-
27
- setReady(true);
28
- });
29
-
30
- if (!ready) {
31
- return null;
32
- }
33
-
34
- async function get<T>(config: TReqraftContextQueryConfig) {
35
- return query<T>({ queryClient, ...config, method: 'GET' });
36
- }
37
-
38
- async function post<T>(config: TReqraftContextQueryConfig) {
39
- return query<T>({ queryClient, ...config, method: 'POST' });
40
- }
41
-
42
- async function put<T>(config: TReqraftContextQueryConfig) {
43
- return query<T>({ queryClient, ...config, method: 'PUT' });
44
- }
45
-
46
- async function del<T>(config: TReqraftContextQueryConfig) {
47
- return query<T>({ queryClient, ...config, method: 'DELETE' });
48
- }
49
-
50
- return (
51
- <FetchContext.Provider
52
- value={{
53
- get,
54
- post,
55
- put,
56
- del,
57
- }}
58
- >
59
- {children}
60
- </FetchContext.Provider>
61
- );
62
- };
@@ -1,33 +0,0 @@
1
- import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
2
- import { ReactNode } from 'react';
3
- import { IReqraftContext, ReqraftContext } from '../contexts/ReqraftContext';
4
- import { ReqraftFetchProvider } from './FetchProvider';
5
- import { ReqraftStorageProvider } from './StorageProvider';
6
-
7
- export const ReqraftQueryClient = new QueryClient();
8
-
9
- export interface IReqraftProviderProps extends IReqraftContext {
10
- children: ReactNode;
11
- reactQueryClient?: QueryClient;
12
- }
13
-
14
- export const ReqraftProvider = ({
15
- appName,
16
- children,
17
- instance,
18
- instanceToken,
19
- instanceUnauthorizedRedirect,
20
- reactQueryClient,
21
- }: IReqraftProviderProps) => {
22
- return (
23
- <ReqraftContext.Provider
24
- value={{ appName, instanceToken, instance, instanceUnauthorizedRedirect }}
25
- >
26
- <QueryClientProvider client={reactQueryClient || ReqraftQueryClient}>
27
- <ReqraftFetchProvider>
28
- <ReqraftStorageProvider>{children}</ReqraftStorageProvider>
29
- </ReqraftFetchProvider>
30
- </QueryClientProvider>
31
- </ReqraftContext.Provider>
32
- );
33
- };
@@ -1,80 +0,0 @@
1
- import { cloneDeep, get, set } from 'lodash';
2
- import { ReactNode, useEffect, useState } from 'react';
3
- import type { Get } from 'type-fest';
4
- import { ReqraftStorageContext, TReqraftStorage } from '../contexts/StorageContext';
5
- import { useFetch } from '../hooks/useFetch/useFetch';
6
- import { useReqraftProperty } from '../hooks/useReqraftProperty';
7
- import { TReqraftStorageValue } from '../hooks/useStorage/useStorage';
8
-
9
- export interface IReqraftStorageProviderProps {
10
- children: ReactNode;
11
- }
12
-
13
- export const ReqraftStorageProvider = ({ children }: IReqraftStorageProviderProps) => {
14
- const appName = useReqraftProperty('appName');
15
-
16
- const { data, loading } = useFetch({
17
- url: 'users/_current_/storage',
18
- cache: false,
19
- loadOnMount: true,
20
- });
21
-
22
- const { load } = useFetch({
23
- url: 'users/_current_/',
24
- method: 'PUT',
25
- cache: false,
26
- });
27
-
28
- const [storage, setStorage] = useState<TReqraftStorage>(data);
29
-
30
- useEffect(() => {
31
- if (data) {
32
- setStorage(data);
33
- }
34
- }, [data]);
35
-
36
- const getStorage = function <T extends TReqraftStorageValue>(
37
- path: string,
38
- defaultValue: T,
39
- includeAppPrefix: boolean = true
40
- ): Get<TReqraftStorage, string> {
41
- const _path = includeAppPrefix ? `${appName}.${path}` : path;
42
-
43
- return get(storage, _path) ?? defaultValue;
44
- };
45
-
46
- const updateStorage = function <T extends TReqraftStorageValue>(
47
- path: string,
48
- value: T,
49
- includeAppPrefix: boolean = true
50
- ) {
51
- const _path = includeAppPrefix ? `${appName}.${path}` : path;
52
- const updatedStorage = set(cloneDeep(storage), _path, value);
53
-
54
- setStorage(updatedStorage);
55
-
56
- load({ body: { storage: updatedStorage } });
57
- };
58
-
59
- const removeStorageValue = function (path: string, includeAppPrefix: boolean = true) {
60
- const _path = includeAppPrefix ? `${appName}.${path}` : path;
61
-
62
- const updatedStorage = set(cloneDeep(storage), _path, null);
63
-
64
- setStorage(updatedStorage);
65
-
66
- load({ body: { storage_path: _path } });
67
- };
68
-
69
- if (loading || !storage) {
70
- return null;
71
- }
72
-
73
- return (
74
- <ReqraftStorageContext.Provider
75
- value={{ storage, getStorage, updateStorage, removeStorageValue }}
76
- >
77
- {children}
78
- </ReqraftStorageContext.Provider>
79
- );
80
- };
package/src/types/Form.ts DELETED
@@ -1,57 +0,0 @@
1
- export type TFormFieldType =
2
- | 'string'
3
- | 'number'
4
- | 'boolean'
5
- | 'date'
6
- | 'time'
7
- | 'datetime'
8
- | 'select'
9
- | 'multiSelect'
10
- | 'radio'
11
- | 'checkbox'
12
- | 'file'
13
- | 'image'
14
- | 'color'
15
- | 'password'
16
- | 'email'
17
- | 'phone'
18
- | 'url'
19
- | 'markdown';
20
-
21
- export type TFormFieldValueType<T> = T extends 'string'
22
- ? string
23
- : T extends 'number'
24
- ? number
25
- : T extends 'boolean'
26
- ? boolean
27
- : T extends 'date'
28
- ? Date | string
29
- : T extends 'time'
30
- ? Date | string
31
- : T extends 'datetime'
32
- ? Date | string
33
- : T extends 'select'
34
- ? string
35
- : T extends 'multiSelect'
36
- ? string[]
37
- : T extends 'radio'
38
- ? string
39
- : T extends 'checkbox'
40
- ? boolean
41
- : T extends 'file'
42
- ? File
43
- : T extends 'image'
44
- ? string
45
- : T extends 'color'
46
- ? string
47
- : T extends 'password'
48
- ? string
49
- : T extends 'email'
50
- ? string
51
- : T extends 'phone'
52
- ? string
53
- : T extends 'url'
54
- ? string
55
- : T extends 'markdown'
56
- ? string
57
- : any;
package/src/types.ts DELETED
@@ -1,12 +0,0 @@
1
- import { IReqoreUIProviderProps } from '@qoretechnologies/reqore/dist/containers/UIProvider';
2
- import { Meta } from '@storybook/react';
3
-
4
- export type StoryMeta<
5
- Component extends keyof JSX.IntrinsicElements | React.JSXElementConstructor<any>,
6
- AdditionalArgs = Record<string, any>
7
- > = Meta<
8
- React.ComponentProps<Component> &
9
- AdditionalArgs & {
10
- reqoreOptions: IReqoreUIProviderProps['options'];
11
- }
12
- >;
@@ -1,121 +0,0 @@
1
- import { QueryClient } from '@tanstack/react-query';
2
- import { ReqraftQueryClient } from '../providers/ReqraftProvider';
3
-
4
- export interface IReqraftFetchConfig {
5
- instance: string;
6
- instanceToken: string;
7
- unauthorizedRedirect?: (pathname: string) => string;
8
- }
9
-
10
- export interface IReqraftFetchResponse<T> {
11
- data: T;
12
- ok: boolean;
13
- code?: number;
14
- error?: any;
15
- }
16
-
17
- const fetchConfig: IReqraftFetchConfig = {
18
- instance: window.location.origin + '/',
19
- instanceToken: '',
20
- unauthorizedRedirect: (pathname: string) => `/?next=${pathname}`,
21
- };
22
-
23
- const CACHE_EXPIRATION_TIME = 5 * 60 * 1000; // 5 minutes
24
-
25
- export const setupFetch = ({
26
- instance,
27
- instanceToken,
28
- unauthorizedRedirect,
29
- }: IReqraftFetchConfig) => {
30
- fetchConfig.instance = instance;
31
- fetchConfig.instanceToken = instanceToken;
32
-
33
- if (unauthorizedRedirect) {
34
- fetchConfig.unauthorizedRedirect = unauthorizedRedirect;
35
- }
36
- };
37
-
38
- async function doFetchData(
39
- url: string,
40
- method = 'GET',
41
- body?: { [key: string]: any }
42
- ): Promise<Response> {
43
- if (!fetchConfig.instanceToken) {
44
- return new Response(JSON.stringify({}), {
45
- status: 401,
46
- statusText: 'Unauthorized',
47
- });
48
- }
49
-
50
- return fetch(`${fetchConfig.instance}api/latest/${url}`, {
51
- method,
52
- headers: {
53
- 'Content-Type': 'application/json',
54
- Authorization: `Bearer ${fetchConfig.instanceToken}`,
55
- },
56
- body: JSON.stringify(body),
57
- }).catch((error) => {
58
- return new Response(JSON.stringify({}), {
59
- status: 500,
60
- statusText: `Request failed ${error.message}`,
61
- });
62
- });
63
- }
64
-
65
- export interface IReqraftQueryConfig {
66
- url: string;
67
- method?: 'GET' | 'POST' | 'PUT' | 'DELETE';
68
- body?: Record<string | number, any>;
69
- cache?: boolean;
70
- queryClient?: QueryClient;
71
- }
72
-
73
- export async function query<T>({
74
- url,
75
- method = 'GET',
76
- body,
77
- cache = true,
78
- queryClient = ReqraftQueryClient,
79
- }: IReqraftQueryConfig): Promise<IReqraftFetchResponse<T>> {
80
- const shouldCache = method === 'DELETE' || method === 'POST' ? false : cache;
81
- const cacheKey = `${url}:${method}:${JSON.stringify(body || {})}`;
82
-
83
- const requestData = await queryClient.fetchQuery({
84
- queryKey: [cacheKey],
85
- queryFn: async () => {
86
- const response = await doFetchData(url, method, body);
87
-
88
- const clone = response.clone();
89
- const json = await clone.json();
90
-
91
- if (response.status === 401) {
92
- window.location.href = fetchConfig.unauthorizedRedirect(window.location.pathname);
93
- }
94
-
95
- return {
96
- data: json,
97
- ok: response.ok,
98
- status: response.status,
99
- statusText: response.statusText,
100
- };
101
- },
102
- staleTime: shouldCache ? CACHE_EXPIRATION_TIME : 0,
103
- });
104
-
105
- if (!requestData.ok) {
106
- queryClient.invalidateQueries({ queryKey: [cacheKey] });
107
-
108
- return {
109
- data: null,
110
- ok: false,
111
- code: requestData.status,
112
- error: requestData.statusText,
113
- };
114
- }
115
-
116
- return {
117
- data: requestData.data,
118
- ok: true,
119
- code: requestData.status,
120
- };
121
- }