@gambalabs/apollo 6.0.0-alpha.19 → 6.0.0-alpha.20

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/dist/config.d.mts CHANGED
@@ -1,4 +1,4 @@
1
- import { C as ClientConfig } from './shared/apollo.32d05cc9.mjs';
1
+ import { C as ClientConfig } from './shared/apollo.d410709a.mjs';
2
2
  import 'graphql-ws';
3
3
  import '@apollo/client';
4
4
  import 'nuxt/app';
package/dist/config.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { C as ClientConfig } from './shared/apollo.32d05cc9.js';
1
+ import { C as ClientConfig } from './shared/apollo.d410709a.js';
2
2
  import 'graphql-ws';
3
3
  import '@apollo/client';
4
4
  import 'nuxt/app';
package/dist/module.d.mts CHANGED
@@ -1,5 +1,5 @@
1
1
  import * as _nuxt_schema from '@nuxt/schema';
2
- import { N as NuxtApolloConfig, C as ClientConfig } from './shared/apollo.32d05cc9.mjs';
2
+ import { N as NuxtApolloConfig, C as ClientConfig } from './shared/apollo.d410709a.mjs';
3
3
  export { ErrorResponse } from '@apollo/client/link/error';
4
4
  import 'graphql-ws';
5
5
  import '@apollo/client';
package/dist/module.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import * as _nuxt_schema from '@nuxt/schema';
2
- import { N as NuxtApolloConfig, C as ClientConfig } from './shared/apollo.32d05cc9.js';
2
+ import { N as NuxtApolloConfig, C as ClientConfig } from './shared/apollo.d410709a.js';
3
3
  export { ErrorResponse } from '@apollo/client/link/error';
4
4
  import 'graphql-ws';
5
5
  import '@apollo/client';
package/dist/module.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gambalabs/apollo",
3
- "version": "6.0.0-alpha.19",
3
+ "version": "6.0.0-alpha.20",
4
4
  "configKey": "apollo",
5
5
  "compatibility": {
6
6
  "nuxt": "^3.0.0-rc.9"
package/dist/module.mjs CHANGED
@@ -5,7 +5,7 @@ import { useLogger, defineNuxtModule, createResolver, addTemplate, addPlugin, ad
5
5
  import GraphQLPlugin from '@rollup/plugin-graphql';
6
6
 
7
7
  const name = "@gambalabs/apollo";
8
- const version = "6.0.0-alpha.19";
8
+ const version = "6.0.0-alpha.20";
9
9
 
10
10
  const serializeConfig = (obj) => {
11
11
  if (typeof obj === "function") {
@@ -61,9 +61,9 @@ export default defineNuxtPlugin((nuxtApp) => {
61
61
  }
62
62
  };
63
63
  });
64
- const getCsrfToken = async () => {
64
+ const getCsrfToken = async (forceUpdate = false) => {
65
65
  const token = ref();
66
- await nuxtApp.callHook("apollo:csrf", { token, client: key });
66
+ await nuxtApp.callHook("apollo:csrf", { token, client: key, forceUpdate });
67
67
  return token.value;
68
68
  };
69
69
  const csrfLink = setContext(async (_, { headers }) => {
@@ -92,10 +92,24 @@ export default defineNuxtPlugin((nuxtApp) => {
92
92
  if (clientConfig.persistedQueries) {
93
93
  persistedLink = createPersistedQueryLink({ sha256, useGETForHashedQueries: true });
94
94
  }
95
+ const customFetch = async (uri, options, blocked1stCall = false) => {
96
+ const response = await fetch(uri, options);
97
+ if (response.status === 419) {
98
+ const token = await getCsrfToken(true);
99
+ if (token && !blocked1stCall) {
100
+ return customFetch(uri, options, !blocked1stCall);
101
+ } else {
102
+ nuxtApp.callHook("apollo:error", { networkError: { bodyText: "Session Expired", statusCode: 419 } });
103
+ }
104
+ }
105
+ return response;
106
+ };
95
107
  const httpEndLink = createUploadLink({
96
108
  ...clientConfig?.httpLinkOptions && clientConfig.httpLinkOptions,
97
109
  uri: process.client && clientConfig.browserHttpEndpoint || clientConfig.httpEndpoint,
98
- headers: { ...clientConfig?.httpLinkOptions?.headers || {} }
110
+ headers: { ...clientConfig?.httpLinkOptions?.headers || {} },
111
+ fetch: customFetch
112
+ // use custom fetch instead of default fetch to handle status code
99
113
  });
100
114
  const httpLink = baseLink.concat(httpEndLink);
101
115
  let wsLink = null;
@@ -121,23 +135,34 @@ export default defineNuxtPlugin((nuxtApp) => {
121
135
  }
122
136
  let pusherLink = null;
123
137
  if (process.client && clientConfig.pusher) {
124
- pusherLink = new PusherLink({
125
- pusher: new Pusher(clientConfig.pusher.pusherAppKey, {
126
- wsHost: clientConfig.pusher.wsHost,
127
- wsPort: clientConfig.pusher.wsPort,
128
- forceTLS: clientConfig.pusher.forceTLS,
129
- disableStats: true,
130
- enabledTransports: ["ws", "wss"],
131
- cluster: clientConfig.pusher.cluster,
132
- channelAuthorization: {
133
- endpoint: clientConfig.pusher.channelEndpoint,
134
- headersProvider() {
135
- const { token: csrfToken } = nuxtApp.$csrfToken();
136
- const { token: authToken } = nuxtApp.$authToken();
137
- return { "X-CSRF-Token": csrfToken.value, authorization: `Bearer ${authToken.value}` };
138
- }
138
+ const pusherObj = new Pusher(clientConfig.pusher.pusherAppKey, {
139
+ wsHost: clientConfig.pusher.wsHost,
140
+ wsPort: clientConfig.pusher.wsPort,
141
+ forceTLS: clientConfig.pusher.forceTLS,
142
+ disableStats: true,
143
+ enabledTransports: ["ws", "wss"],
144
+ cluster: clientConfig.pusher.cluster,
145
+ activityTimeout: clientConfig.pusher.activityTimeout,
146
+ reconnect: {
147
+ auto: true
148
+ },
149
+ channelAuthorization: {
150
+ endpoint: clientConfig.pusher.channelEndpoint,
151
+ headersProvider() {
152
+ const { token: csrfToken } = nuxtApp.$csrfToken();
153
+ const { token: authToken } = nuxtApp.$authToken();
154
+ return { "X-CSRF-Token": csrfToken.value, authorization: `Bearer ${authToken.value}` };
139
155
  }
140
- })
156
+ }
157
+ });
158
+ const handleVisibilityChange = () => {
159
+ if (document.visibilityState === "visible" && pusherObj.connection.state !== "connected") {
160
+ pusherObj.connect();
161
+ }
162
+ };
163
+ document.addEventListener("visibilitychange", handleVisibilityChange);
164
+ pusherLink = new PusherLink({
165
+ pusher: pusherObj
141
166
  });
142
167
  }
143
168
  const errorLink = onError((err) => {
@@ -11,6 +11,7 @@ type Pusher = {
11
11
  forceTLS: boolean;
12
12
  channelEndpoint: string;
13
13
  pusherAppKey: string;
14
+ activityTimeout: number;
14
15
  }
15
16
 
16
17
  type ClientConfig = {
@@ -11,6 +11,7 @@ type Pusher = {
11
11
  forceTLS: boolean;
12
12
  channelEndpoint: string;
13
13
  pusherAppKey: string;
14
+ activityTimeout: number;
14
15
  }
15
16
 
16
17
  type ClientConfig = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gambalabs/apollo",
3
- "version": "6.0.0-alpha.19",
3
+ "version": "6.0.0-alpha.20",
4
4
  "license": "MIT",
5
5
  "repository": "https://github.com/GambaLabs/apollo",
6
6
  "homepage": "https://apollo.nuxtjs.org",