@go-avro/avro-js 0.0.2-beta.76 → 0.0.2-beta.77

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.
@@ -254,11 +254,12 @@ export declare class AvroQueryClient {
254
254
  post<T>(path: string, data: any, cancelToken?: CancelToken, headers?: Record<string, string>, progressUpdateCallback?: (loaded: number, total: number) => void): Promise<T>;
255
255
  put<T>(path: string, data: any, cancelToken?: CancelToken, headers?: Record<string, string>, progressUpdateCallback?: (loaded: number, total: number) => void): Promise<T>;
256
256
  delete<T>(path: string, cancelToken?: CancelToken, headers?: Record<string, string>, progressUpdateCallback?: (loaded: number, total: number) => void): Promise<T>;
257
- login(data: {
257
+ useLogin(): ReturnType<typeof useMutation<LoginResponse, StandardError, {
258
258
  username: string;
259
259
  password: string;
260
260
  code?: string;
261
- }, cancelToken?: CancelToken): Promise<LoginResponse>;
261
+ cancelToken?: CancelToken;
262
+ }>>;
262
263
  setTokens(tokens: Tokens): Promise<void>;
263
264
  clearTokens(): Promise<void>;
264
265
  useLogout(): ReturnType<typeof useMutation<void, StandardError, CancelToken | undefined>>;
@@ -61,26 +61,32 @@ export class AvroQueryClient {
61
61
  delete(path, cancelToken, headers = {}, progressUpdateCallback) {
62
62
  return this._xhr('DELETE', path, null, cancelToken, headers, false, this.config.maxRetries, progressUpdateCallback);
63
63
  }
64
- login(data, cancelToken) {
65
- return this._fetch('POST', '/login', data, cancelToken)
66
- .then(resp => {
67
- if (!resp || !('access_token' in resp)) {
68
- if (resp.msg === "TOTP email sent") {
69
- return LoginResponse.NEEDS_TOTP;
64
+ useLogin() {
65
+ const queryClient = useQueryClient();
66
+ return useMutation({
67
+ mutationFn: async ({ username, password, code, cancelToken }) => {
68
+ const resp = await this._fetch('POST', '/login', { username, password, code }, cancelToken);
69
+ if (!resp || !('access_token' in resp)) {
70
+ if (resp.msg === "TOTP email sent") {
71
+ return LoginResponse.NEEDS_TOTP;
72
+ }
73
+ throw new StandardError(401, 'Invalid login response');
70
74
  }
71
- throw new StandardError(401, 'Invalid login response');
72
- }
73
- this.socket.auth = { token: resp.access_token };
74
- if (!this.socket.connected) {
75
- this.socket.connect();
76
- }
77
- return this.config.authManager.setTokens({ access_token: resp.access_token, refresh_token: resp.refresh_token }).then(() => {
75
+ this.socket.auth = { token: resp.access_token };
76
+ if (!this.socket.connected) {
77
+ this.socket.connect();
78
+ }
79
+ await this.config.authManager.setTokens({ access_token: resp.access_token, refresh_token: resp.refresh_token });
78
80
  return LoginResponse.SUCCESS;
79
- });
80
- })
81
- .catch(err => {
82
- console.error('Login failed:', err);
83
- throw new StandardError(401, 'Login failed');
81
+ },
82
+ onSettled: () => {
83
+ queryClient.invalidateQueries();
84
+ },
85
+ onError: (err) => {
86
+ this.config.authManager.clearTokens();
87
+ console.error('Login failed:', err);
88
+ throw new StandardError(401, 'Login failed');
89
+ }
84
90
  });
85
91
  }
86
92
  setTokens(tokens) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@go-avro/avro-js",
3
- "version": "0.0.2-beta.76",
3
+ "version": "0.0.2-beta.77",
4
4
  "description": "JS client for Avro backend integration.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",