@intrig/react 1.0.10 → 1.0.11

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@intrig/react",
3
- "version": "1.0.10",
3
+ "version": "1.0.11",
4
4
  "main": "./index.js",
5
5
  "types": "./index.d.ts",
6
6
  "private": false,
@@ -13,10 +13,10 @@ import { useIntrigContext } from '@intrig/react/intrig-context';
13
13
  * 2. A function to execute the provided asynchronous operation.
14
14
  * 3. A function to reset the network state back to the initial state.
15
15
  */
16
- export function useAsNetworkState<T, F extends ((...args: any) => Promise<T>)>(fn: F, key = 'default'): [NetworkState<T>, (...params: Parameters<F>) => void, () => void] {
17
- const id = useId();
16
+ export function useAsNetworkState<T, F extends ((...args: any) => Promise<T>)>(fn: F, key: string = 'default'): [NetworkState<T>, (...params: Parameters<F>) => void, () => void] {
17
+ let id = useId();
18
18
 
19
- const context = useIntrigContext();
19
+ let context = useIntrigContext();
20
20
 
21
21
  const networkState = useMemo(() => {
22
22
  return context.state?.[`promiseState:${id}:${key}}`] ?? init()
@@ -82,7 +82,7 @@ export function useAsPromise<P, B, T, E>(
82
82
  const resolveRef = useRef<(value: T) => void>();
83
83
  const rejectRef = useRef<(reason?: any) => void>();
84
84
 
85
- const [state, dispatch, clear] = hook(options as any); // Casting to `any` to match all overloads
85
+ let [state, dispatch, clear] = hook(options as any); // Casting to `any` to match all overloads
86
86
 
87
87
  useEffect(() => {
88
88
  if (isSuccess(state)) {
@@ -99,7 +99,7 @@ export function useAsPromise<P, B, T, E>(
99
99
  resolveRef.current = resolve;
100
100
  rejectRef.current = reject;
101
101
 
102
- const dispatchState = (dispatch as any)(...args);
102
+ let dispatchState = (dispatch as any)(...args);
103
103
  if (isValidationError(dispatchState)) {
104
104
  reject(dispatchState.error);
105
105
  }
@@ -73,7 +73,7 @@ export function useResolvedCachedValue<P, B, T, E>(hook: BinaryFunctionHook<P, B
73
73
  export function useResolvedCachedValue<P, B, T, E>(hook: IntrigHook<P, B, T, E>, options: IntrigHookOptions<P, B>): T | undefined {
74
74
  const [cachedValue, setCachedValue] = useState<T | undefined>();
75
75
 
76
- const [state] = hook(options as any); // Ensure compatibility with different hook types
76
+ let [state] = hook(options as any); // Ensure compatibility with different hook types
77
77
 
78
78
  useEffect(() => {
79
79
  if (isSuccess(state)) {
@@ -68,7 +68,7 @@ export function useResolvedValue<P, B, T, E>(hook: BinaryFunctionHook<P, B, T, E
68
68
  export function useResolvedValue<P, B, T, E>(hook: IntrigHook<P, B, T, E>, options: IntrigHookOptions<P, B>): T | undefined {
69
69
  const [value, setValue] = useState<T | undefined>();
70
70
 
71
- const [state] = hook(options as any); // Ensure compatibility with different hook types
71
+ let [state] = hook(options as any); // Ensure compatibility with different hook types
72
72
 
73
73
  useEffect(() => {
74
74
  if (isSuccess(state)) {
@@ -43,15 +43,13 @@ export interface ContextType {
43
43
  *
44
44
  * @type {ContextType}
45
45
  */
46
- const Context = createContext<ContextType>({
46
+ let Context = createContext<ContextType>({
47
47
  state: {},
48
48
  filteredState: {},
49
- dispatch() {
50
- //noop
51
- },
49
+ dispatch() {},
52
50
  configs: {},
53
51
  async execute() {
54
- //noop
52
+
55
53
  }
56
54
  });
57
55
 
@@ -83,11 +83,11 @@ export function IntrigProvider({
83
83
  async function execute<T, E = unknown>(request: RequestType, dispatch: (state: NetworkState<T, E>) => void, schema: ZodSchema<T> | undefined, errorSchema: ZodSchema<E> | undefined) {
84
84
  try {
85
85
  dispatch(pending());
86
- const response = await axiosInstances[request.source].request(request);
86
+ let response = await axiosInstances[request.source].request(request);
87
87
 
88
88
  if (response.status >= 200 && response.status < 300) {
89
89
  if (schema) {
90
- const data = schema.safeParse(response.data);
90
+ let data = schema.safeParse(response.data);
91
91
  if (!data.success) {
92
92
  dispatch(
93
93
  error(data.error.issues, response.status, request)
@@ -99,7 +99,7 @@ export function IntrigProvider({
99
99
  dispatch(success(response.data));
100
100
  }
101
101
  } else {
102
- const { data, error: validationError } = errorSchema?.safeParse(response.data ?? {}) ?? {};
102
+ let { data, error: validationError } = errorSchema?.safeParse(response.data ?? {}) ?? {};
103
103
  //todo: handle error validation error.
104
104
  dispatch(
105
105
  error(data ?? response.data ?? response.statusText, response.status)
@@ -107,7 +107,7 @@ export function IntrigProvider({
107
107
  }
108
108
  } catch (e: any) {
109
109
  if (isAxiosError(e)) {
110
- const { data, error: validationError } = errorSchema?.safeParse(e.response?.data ?? {}) ?? {};
110
+ let { data, error: validationError } = errorSchema?.safeParse(e.response?.data ?? {}) ?? {};
111
111
  dispatch(error(data ?? e.response?.data, e.response?.status, request));
112
112
  } else {
113
113
  dispatch(error(e));
@@ -145,7 +145,7 @@ export function IntrigProviderStub({ children, configs = {}, stubs = () => {} }:
145
145
  const [state, dispatch] = useReducer(requestReducer, {} as GlobalState);
146
146
 
147
147
  const collectedStubs = useMemo(() => {
148
- const fns: Record<string, (params: any, body: any, dispatch: (state: NetworkState<any>) => void) => Promise<void>> = {};
148
+ let fns: Record<string, (params: any, body: any, dispatch: (state: NetworkState<any>) => void) => Promise<void>> = {};
149
149
  function stub<P, B, T>(hook: IntrigHook<P, B, T>, fn: (params: P, body: B, dispatch: (state: NetworkState<T>) => void) => Promise<void>) {
150
150
  fns[hook.key] = fn;
151
151
  }
@@ -156,9 +156,9 @@ export function IntrigProviderStub({ children, configs = {}, stubs = () => {} }:
156
156
  const contextValue = useMemo(() => {
157
157
 
158
158
  async function execute<T>(request: RequestType, dispatch: (state: NetworkState<T>) => void, schema: ZodSchema<T> | undefined) {
159
- const stub = collectedStubs[request.key];
159
+ let stub = collectedStubs[request.key];
160
160
 
161
- if (stub) {
161
+ if (!!stub) {
162
162
  try {
163
163
  await stub(request.params, request.data, dispatch);
164
164
  } catch (e) {
@@ -329,10 +329,10 @@ export function useNetworkState<T, E = unknown>({
329
329
  logger.info(`Executing request ${key} ${operation} ${source}`);
330
330
  logger.debug(`=>`, request)
331
331
 
332
- const abortController = new AbortController();
332
+ let abortController = new AbortController();
333
333
  setAbortController(abortController);
334
334
 
335
- const requestConfig: RequestType = {
335
+ let requestConfig: RequestType = {
336
336
  ...request,
337
337
  onUploadProgress(event: AxiosProgressEvent) {
338
338
  dispatch(
@@ -405,7 +405,7 @@ export function useCentralError() {
405
405
  return Object.entries(ctx.filteredState)
406
406
  .filter(([, state]) => isError(state))
407
407
  .map(([k, state]) => {
408
- const [source, operation, key] = k.split(':');
408
+ let [source, operation, key] = k.split(':');
409
409
  return {
410
410
  ...(state as ErrorState<unknown>),
411
411
  source,
@@ -426,12 +426,12 @@ export function useCentralPendingState() {
426
426
  const ctx = useContext(Context);
427
427
 
428
428
  const result: NetworkState = useMemo(() => {
429
- const pendingStates = Object.values(ctx.filteredState).filter(isPending);
429
+ let pendingStates = Object.values(ctx.filteredState).filter(isPending);
430
430
  if (!pendingStates.length) {
431
431
  return init();
432
432
  }
433
433
 
434
- const progress = pendingStates
434
+ let progress = pendingStates
435
435
  .filter((a) => a.progress)
436
436
  .reduce(
437
437
  (progress, current) => {
@@ -442,7 +442,7 @@ export function useCentralPendingState() {
442
442
  },
443
443
  { total: 0, loaded: 0 } satisfies Progress
444
444
  );
445
- return pending(progress.total ? progress : undefined);
445
+ return pending(!!progress.total ? progress : undefined);
446
446
  }, [ctx.filteredState]);
447
447
 
448
448
  return result;
package/src/logger.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import log from 'loglevel';
2
2
 
3
3
  // Set the default logging level (can be overridden via environment variables)
4
- log.setLevel(process.env.LOG_LEVEL as log.LogLevelDesc || 'error');
4
+ log.setLevel(process.env.LOG_LEVEL as log.LogLevelDesc || 'info');
5
5
 
6
6
  const logWrapper = {
7
7
  info: (msg: string, meta?: object) => meta ? log.info(msg, meta) : log.info(msg),
@@ -46,8 +46,8 @@ function appendFormData(
46
46
 
47
47
 
48
48
  encoders['multipart/form-data'] = async (request, mediaType, schema) => {
49
- const _request = request as Record<string, any>;
50
- const formData = new FormData();
49
+ let _request = request as Record<string, any>;
50
+ let formData = new FormData();
51
51
  Object.keys(_request).forEach((key: string) => {
52
52
  appendFormData(formData, _request[key], key);
53
53
  });
@@ -59,8 +59,8 @@ encoders['application/octet-stream'] = async (request, mediaType, schema) => {
59
59
  }
60
60
 
61
61
  encoders['application/x-www-form-urlencoded'] = async (request, mediaType, schema) => {
62
- const formData = new FormData();
63
- for (const key in request) {
62
+ let formData = new FormData();
63
+ for (let key in request) {
64
64
  const value = request[key];
65
65
  formData.append(key, value instanceof Blob || typeof value === 'string' ? value : String(value));
66
66
  }
@@ -93,8 +93,8 @@ transformers['application/json'] = async (request, mediaType, schema) => {
93
93
  };
94
94
 
95
95
  transformers['multipart/form-data'] = async (request, mediaType, schema) => {
96
- const formData = await request.formData();
97
- const content: Record<string, any> = {};
96
+ let formData = await request.formData();
97
+ let content: Record<string, any> = {};
98
98
  formData.forEach((value, key) => {
99
99
  if (content[key]) {
100
100
  if (!(content[key] instanceof Array)) {
@@ -125,15 +125,15 @@ transformers['application/x-www-form-urlencoded'] = async (
125
125
  mediaType,
126
126
  schema
127
127
  ) => {
128
- const formData = await request.formData();
129
- const content: Record<string, any> = {};
128
+ let formData = await request.formData();
129
+ let content: Record<string, any> = {};
130
130
  formData.forEach((value, key) => (content[key] = value));
131
131
  return schema.parse(content);
132
132
  };
133
133
 
134
134
  transformers['application/xml'] = async (request, mediaType, schema) => {
135
- const xmlParser = new XMLParser();
136
- const content = await xmlParser.parse(await request.text());
135
+ let xmlParser = new XMLParser();
136
+ let content = await xmlParser.parse(await request.text());
137
137
  return schema.parse(await content);
138
138
  };
139
139
 
@@ -168,7 +168,7 @@ responseTransformers['application/json'] = async (data, mediaType, schema) => {
168
168
  };
169
169
 
170
170
  responseTransformers['application/xml'] = async (data, mediaType, schema) => {
171
- const parsed = new XMLParser().parse(data);
171
+ let parsed = new XMLParser().parse(data);
172
172
  return schema.parse(parsed);
173
173
  }
174
174