@go-avro/avro-js 0.0.93 → 0.0.94

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.
@@ -754,6 +754,13 @@ export declare class AvroQueryClient {
754
754
  construct?: (raw: any) => any;
755
755
  matchesQuery?: (queryKey: readonly unknown[], item: any) => ListQueryMatch;
756
756
  }): Promise<any>;
757
+ /**
758
+ * Drop all local auth state without a server round-trip: stored
759
+ * tokens, cached company, query cache, socket, and auth state.
760
+ * The single teardown path for logout, refresh failure, and a
761
+ * server that no longer recognizes our identity.
762
+ */
763
+ forceLogout(): Promise<void>;
757
764
  useLogout(): ReturnType<typeof useMutation<void, StandardError, CancelToken | undefined>>;
758
765
  fetchJobs(body?: {
759
766
  amt?: number;
@@ -440,11 +440,7 @@ export class AvroQueryClient {
440
440
  }
441
441
  });
442
442
  config.authManager.onTokenRefreshFailed(() => {
443
- this.clearCache();
444
- this.setAuthState(AuthState.UNAUTHENTICATED);
445
- if (this.socket && this.socket.connected) {
446
- this.socket.disconnect();
447
- }
443
+ void this.forceLogout();
448
444
  });
449
445
  }
450
446
  emit(eventName, data) {
@@ -886,26 +882,34 @@ export class AvroQueryClient {
886
882
  return undefined;
887
883
  }
888
884
  }
885
+ /**
886
+ * Drop all local auth state without a server round-trip: stored
887
+ * tokens, cached company, query cache, socket, and auth state.
888
+ * The single teardown path for logout, refresh failure, and a
889
+ * server that no longer recognizes our identity.
890
+ */
891
+ async forceLogout() {
892
+ await this.clearCache();
893
+ this.setAuthState(AuthState.UNAUTHENTICATED);
894
+ if (this.socket && this.socket.connected) {
895
+ this.socket.disconnect();
896
+ }
897
+ this.getQueryClient().removeQueries();
898
+ }
889
899
  useLogout() {
890
- const queryClient = this.getQueryClient();
891
900
  return useMutation({
892
901
  mutationFn: async (cancelToken) => {
893
- await this.post({
894
- path: '/logout',
895
- cancelToken,
896
- });
897
- await this.config.authManager.clearCache();
898
- if (this.socket && this.socket.connected) {
899
- this.socket.disconnect();
902
+ try {
903
+ await this.post({
904
+ path: '/logout',
905
+ cancelToken,
906
+ });
907
+ }
908
+ finally {
909
+ await this.forceLogout();
900
910
  }
901
- },
902
- onSettled: () => {
903
- this.clearCache();
904
- this.setAuthState(AuthState.UNAUTHENTICATED);
905
- queryClient.removeQueries();
906
911
  },
907
912
  onError: (err) => {
908
- this.clearCache();
909
913
  console.error('Logout failed:', err);
910
914
  throw new StandardError(500, 'Logout failed');
911
915
  },
@@ -1,5 +1,6 @@
1
1
  import { useMutation, useQuery } from '@tanstack/react-query';
2
2
  import { AvroQueryClient } from '../../client/QueryClient';
3
+ import { StandardError } from '../../types/error';
3
4
  import { AuthState } from '../../types/auth';
4
5
  AvroQueryClient.prototype.useGetCompanies = function (options = {}) {
5
6
  return useQuery({
@@ -47,9 +48,17 @@ AvroQueryClient.prototype.useGetCurrentCompany = function () {
47
48
  throw new Error('No company ID set and no companies available');
48
49
  }
49
50
  }
50
- this.company = await this.get({
51
- path: `/company/${this.companyId}`,
52
- });
51
+ try {
52
+ this.company = await this.get({
53
+ path: `/company/${this.companyId}`,
54
+ });
55
+ }
56
+ catch (err) {
57
+ if (err instanceof StandardError && err.status === 404) {
58
+ await this.setCompanyId('');
59
+ }
60
+ throw err;
61
+ }
53
62
  return this.company;
54
63
  },
55
64
  enabled: this.getAuthState() === AuthState.AUTHENTICATED,
@@ -1,5 +1,6 @@
1
1
  import { useMutation, useQuery } from '@tanstack/react-query';
2
2
  import { AvroQueryClient } from '../../client/QueryClient';
3
+ import { StandardError } from '../../types/error';
3
4
  import { AuthState } from '../../types/auth';
4
5
  AvroQueryClient.prototype.useGetUser = function (userId) {
5
6
  return useQuery({
@@ -22,7 +23,17 @@ AvroQueryClient.prototype.useSearchUsers = function (searchUsername) {
22
23
  AvroQueryClient.prototype.useGetSelf = function () {
23
24
  return useQuery({
24
25
  queryKey: ['user'],
25
- queryFn: () => this.get({ path: `/user` }),
26
+ queryFn: async () => {
27
+ try {
28
+ return await this.get({ path: `/user` });
29
+ }
30
+ catch (err) {
31
+ if (err instanceof StandardError && err.status === 404) {
32
+ await this.forceLogout();
33
+ }
34
+ throw err;
35
+ }
36
+ },
26
37
  enabled: this.getAuthState() === AuthState.AUTHENTICATED,
27
38
  });
28
39
  };
package/dist/version.d.ts CHANGED
@@ -1 +1 @@
1
- export declare const AVRO_JS_VERSION = "0.0.93";
1
+ export declare const AVRO_JS_VERSION = "0.0.94";
package/dist/version.js CHANGED
@@ -1,3 +1,3 @@
1
1
  // AUTO-GENERATED by scripts/gen-version.js — do not edit by hand.
2
2
  // Regenerated from package.json by the `prebuild` npm hook.
3
- export const AVRO_JS_VERSION = '0.0.93';
3
+ export const AVRO_JS_VERSION = '0.0.94';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@go-avro/avro-js",
3
- "version": "0.0.93",
3
+ "version": "0.0.94",
4
4
  "description": "JS client for Avro backend integration.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",