@go-avro/avro-js 0.0.2-beta.134 → 0.0.2-beta.135

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.
@@ -17,8 +17,11 @@ export class LocalStorage {
17
17
  this.key = 'cache_data';
18
18
  }
19
19
  async get(key) {
20
- const item = localStorage.getItem(this.key);
21
- return item ? JSON.parse(item) : null;
20
+ const item = JSON.parse(localStorage.getItem(this.key) ?? 'null');
21
+ if (typeof item !== 'object' || item === null) {
22
+ return null;
23
+ }
24
+ return item ? key ? item[key] ?? null : item : null;
22
25
  }
23
26
  async set(data) {
24
27
  const current = await this.get() || {};
@@ -0,0 +1,14 @@
1
+ import React, { ReactNode } from "react";
2
+ import { QueryClient } from "@tanstack/react-query";
3
+ import { AvroQueryClient, AvroQueryClientConfig } from "./QueryClient";
4
+ import { AuthManager } from "../auth/AuthManager";
5
+ export interface AvroQueryClientProviderProps {
6
+ baseUrl: string;
7
+ authManager: AuthManager;
8
+ queryClient?: QueryClient;
9
+ configOverrides?: Partial<AvroQueryClientConfig>;
10
+ children: ReactNode;
11
+ }
12
+ export declare const AvroQueryClientProvider: ({ baseUrl, authManager, configOverrides, children, }: AvroQueryClientProviderProps) => React.JSX.Element;
13
+ export declare const useAvroQueryClient: () => AvroQueryClient;
14
+ export default AvroQueryClientProvider;
@@ -0,0 +1,32 @@
1
+ import React, { createContext, useContext, useMemo, useEffect } from "react";
2
+ import { AvroQueryClient } from "./QueryClient";
3
+ const AvroQueryClientContext = createContext(null);
4
+ export const AvroQueryClientProvider = ({ baseUrl, authManager, configOverrides, children, }) => {
5
+ const client = useMemo(() => {
6
+ const cfg = {
7
+ baseUrl,
8
+ authManager,
9
+ ...(configOverrides || {}),
10
+ };
11
+ return new AvroQueryClient(cfg);
12
+ }, [baseUrl, authManager, configOverrides]);
13
+ useEffect(() => {
14
+ return () => {
15
+ try {
16
+ client.socket?.disconnect();
17
+ }
18
+ catch (e) {
19
+ // ignore
20
+ }
21
+ };
22
+ }, [client]);
23
+ return (React.createElement(AvroQueryClientContext.Provider, { value: client }, children));
24
+ };
25
+ export const useAvroQueryClient = () => {
26
+ const ctx = useContext(AvroQueryClientContext);
27
+ if (!ctx) {
28
+ throw new Error("useAvroQueryClient must be used within <AvroQueryClientProvider>");
29
+ }
30
+ return ctx;
31
+ };
32
+ export default AvroQueryClientProvider;
@@ -9,7 +9,6 @@ import { CacheData } from '../types/cache';
9
9
  export interface AvroQueryClientConfig {
10
10
  baseUrl: string;
11
11
  authManager: AuthManager;
12
- queryClient: QueryClient;
13
12
  maxRetries?: number;
14
13
  retryStrategy?: RetryStrategy;
15
14
  timeout?: number;
@@ -1,5 +1,5 @@
1
1
  import io from 'socket.io-client';
2
- import { useMutation } from '@tanstack/react-query';
2
+ import { useMutation, useQueryClient } from '@tanstack/react-query';
3
3
  import { LoginResponse } from '../types/api';
4
4
  import { StandardError } from '../types/error';
5
5
  export class AvroQueryClient {
@@ -12,7 +12,6 @@ export class AvroQueryClient {
12
12
  this.config = {
13
13
  baseUrl: config.baseUrl,
14
14
  authManager: config.authManager,
15
- queryClient: config.queryClient,
16
15
  maxRetries: config.maxRetries ?? 3,
17
16
  retryStrategy: config.retryStrategy ?? 'fixed',
18
17
  timeout: config.timeout ?? 0,
@@ -233,7 +232,7 @@ export class AvroQueryClient {
233
232
  return this.config.authManager.isAuthenticated();
234
233
  }
235
234
  getQueryClient() {
236
- return this.config.queryClient;
235
+ return useQueryClient();
237
236
  }
238
237
  useLogout() {
239
238
  const queryClient = this.getQueryClient();
package/dist/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export { AvroQueryClientConfig, AvroQueryClient } from './client/QueryClient';
2
+ export { AvroQueryClientProvider } from './client/AvroQueryClientProvider';
2
3
  export { AuthManager } from './auth/AuthManager';
3
4
  export { MemoryStorage, LocalStorage } from './auth/storage';
4
5
  import './client/core/xhr';
package/dist/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  export { AvroQueryClient } from './client/QueryClient';
2
+ export { AvroQueryClientProvider } from './client/AvroQueryClientProvider';
2
3
  export { AuthManager } from './auth/AuthManager';
3
4
  export { MemoryStorage, LocalStorage } from './auth/storage';
4
5
  import './client/core/xhr';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@go-avro/avro-js",
3
- "version": "0.0.2-beta.134",
3
+ "version": "0.0.2-beta.135",
4
4
  "description": "JS client for Avro backend integration.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",