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

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.21",
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.21";
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,27 @@ 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
+ } else if (response.status > 300) {
105
+ const errorText = await response.text();
106
+ nuxtApp.callHook("apollo:error", { networkError: { bodyText: errorText, statusCode: response.status } });
107
+ }
108
+ return response;
109
+ };
95
110
  const httpEndLink = createUploadLink({
96
111
  ...clientConfig?.httpLinkOptions && clientConfig.httpLinkOptions,
97
112
  uri: process.client && clientConfig.browserHttpEndpoint || clientConfig.httpEndpoint,
98
- headers: { ...clientConfig?.httpLinkOptions?.headers || {} }
113
+ headers: { ...clientConfig?.httpLinkOptions?.headers || {} },
114
+ fetch: customFetch
115
+ // use custom fetch instead of default fetch to handle status code
99
116
  });
100
117
  const httpLink = baseLink.concat(httpEndLink);
101
118
  let wsLink = null;
@@ -121,23 +138,34 @@ export default defineNuxtPlugin((nuxtApp) => {
121
138
  }
122
139
  let pusherLink = null;
123
140
  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
- }
141
+ const pusherObj = new Pusher(clientConfig.pusher.pusherAppKey, {
142
+ wsHost: clientConfig.pusher.wsHost,
143
+ wsPort: clientConfig.pusher.wsPort,
144
+ forceTLS: clientConfig.pusher.forceTLS,
145
+ disableStats: true,
146
+ enabledTransports: ["ws", "wss"],
147
+ cluster: clientConfig.pusher.cluster,
148
+ activityTimeout: clientConfig.pusher.activityTimeout,
149
+ reconnect: {
150
+ auto: true
151
+ },
152
+ channelAuthorization: {
153
+ endpoint: clientConfig.pusher.channelEndpoint,
154
+ headersProvider() {
155
+ const { token: csrfToken } = nuxtApp.$csrfToken();
156
+ const { token: authToken } = nuxtApp.$authToken();
157
+ return { "X-CSRF-Token": csrfToken.value, authorization: `Bearer ${authToken.value}` };
139
158
  }
140
- })
159
+ }
160
+ });
161
+ const handleVisibilityChange = () => {
162
+ if (document.visibilityState === "visible" && pusherObj.connection.state !== "connected") {
163
+ pusherObj.connect();
164
+ }
165
+ };
166
+ document.addEventListener("visibilitychange", handleVisibilityChange);
167
+ pusherLink = new PusherLink({
168
+ pusher: pusherObj
141
169
  });
142
170
  }
143
171
  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.21",
4
4
  "license": "MIT",
5
5
  "repository": "https://github.com/GambaLabs/apollo",
6
6
  "homepage": "https://apollo.nuxtjs.org",