@ludo.ninja/api 1.0.14 → 1.0.16

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.
@@ -0,0 +1,2 @@
1
+ import { ApolloClient, NormalizedCacheObject } from "@apollo/client";
2
+ export declare function initializeApollo(initialState?: null, domain?: string): ApolloClient<NormalizedCacheObject>;
@@ -0,0 +1,125 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.initializeApollo = void 0;
7
+ const client_1 = require("@apollo/client");
8
+ const hosts_1 = require("../hosts");
9
+ const schema_1 = require("../graphql_tools/__generated__/schema");
10
+ const error_1 = require("@apollo/client/link/error");
11
+ const context_1 = require("@apollo/client/link/context");
12
+ const apollo_upload_client_1 = require("apollo-upload-client");
13
+ const deepmerge_1 = __importDefault(require("deepmerge"));
14
+ const lodash_isequal_1 = __importDefault(require("lodash.isequal"));
15
+ const cookies_1 = require("../cookies");
16
+ const typePolicies_1 = require("./typePolicies");
17
+ let isRefreshing = false;
18
+ let pendingRequests = [];
19
+ let mainDomain = "";
20
+ const resolvePendingRequests = () => {
21
+ pendingRequests.map((callback) => callback());
22
+ pendingRequests = [];
23
+ };
24
+ let apolloClient;
25
+ const errorLink = (0, error_1.onError)(({ graphQLErrors, networkError, operation, forward }) => {
26
+ const { authToken, refreshToken } = (0, cookies_1.getCookies)();
27
+ if (graphQLErrors) {
28
+ graphQLErrors.forEach(({ message, locations, path }) => console.warn(`[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`));
29
+ }
30
+ if (networkError) {
31
+ console.warn(`[Network error]: ${networkError}`);
32
+ if (networkError.message.includes("401") && authToken) {
33
+ let forward$;
34
+ const client = apolloClient ?? createApolloClient();
35
+ if (!isRefreshing) {
36
+ isRefreshing = true;
37
+ forward$ = (0, client_1.fromPromise)(client
38
+ .mutate({
39
+ context: { uri: hosts_1.authHost },
40
+ variables: { refreshToken },
41
+ mutation: schema_1.RefreshTokenDocument,
42
+ })
43
+ .then(({ data: { refreshToken: { tokenAuth, tokenRefresh }, }, }) => {
44
+ (0, cookies_1.refreshCookies)(tokenAuth, tokenRefresh, mainDomain);
45
+ resolvePendingRequests();
46
+ return true;
47
+ })
48
+ .catch(async () => {
49
+ await apolloClient?.mutate({
50
+ context: { uri: hosts_1.authHost },
51
+ mutation: schema_1.RevokeTokenDocument,
52
+ });
53
+ (0, cookies_1.destroyCookies)(mainDomain);
54
+ pendingRequests = [];
55
+ window.location.reload();
56
+ return false;
57
+ })
58
+ .finally(() => {
59
+ isRefreshing = false;
60
+ })).filter((value) => Boolean(value));
61
+ }
62
+ else {
63
+ forward$ = (0, client_1.fromPromise)(new Promise((resolve) => {
64
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
65
+ // @ts-ignore
66
+ pendingRequests.push(() => resolve());
67
+ }));
68
+ }
69
+ return forward$.flatMap(() => forward(operation));
70
+ }
71
+ }
72
+ });
73
+ const authLink = (0, context_1.setContext)(async (operation, { headers }) => {
74
+ const { authToken } = (0, cookies_1.getCookies)();
75
+ const header = authToken
76
+ ? {
77
+ headers: { ...headers, "x-client-authorization": `${authToken}` },
78
+ }
79
+ : { headers: { ...headers } };
80
+ return {
81
+ headers: header.headers,
82
+ };
83
+ });
84
+ const httpLink = (0, apollo_upload_client_1.createUploadLink)({
85
+ uri: hosts_1.authHost,
86
+ });
87
+ function createApolloClient() {
88
+ return new client_1.ApolloClient({
89
+ ssrMode: typeof window === "undefined",
90
+ link: (0, client_1.from)([errorLink, authLink, httpLink]),
91
+ cache: new client_1.InMemoryCache({
92
+ typePolicies: typePolicies_1.typePoliciesPortal,
93
+ addTypename: true,
94
+ resultCaching: true,
95
+ }),
96
+ });
97
+ }
98
+ function initializeApollo(initialState = null, domain = "ludo.ninja") {
99
+ const _apolloClient = apolloClient ?? createApolloClient();
100
+ mainDomain = domain;
101
+ // If your page has Next.js data fetching methods that use Apollo Client, the initial state
102
+ // gets hydrated here
103
+ if (initialState) {
104
+ // Get existing cache, loaded during client side data fetching
105
+ const existingCache = _apolloClient.extract();
106
+ // Merge the initialState from getStaticProps/getServerSideProps in the existing cache
107
+ const data = (0, deepmerge_1.default)(existingCache, initialState, {
108
+ // combine arrays using object equality (like in sets)
109
+ arrayMerge: (destinationArray, sourceArray) => [
110
+ ...sourceArray,
111
+ ...destinationArray.filter((d) => sourceArray.every((s) => !(0, lodash_isequal_1.default)(d, s))),
112
+ ],
113
+ });
114
+ // Restore the cache with the merged data
115
+ _apolloClient.cache.restore(data);
116
+ }
117
+ // For SSG and SSR always create a new Apollo Client
118
+ if (typeof window === "undefined")
119
+ return _apolloClient;
120
+ // Create the Apollo Client once in the client
121
+ if (!apolloClient)
122
+ apolloClient = _apolloClient;
123
+ return _apolloClient;
124
+ }
125
+ exports.initializeApollo = initializeApollo;
@@ -0,0 +1,2 @@
1
+ import { TypePolicies } from "@apollo/client";
2
+ export declare const typePoliciesPortal: TypePolicies;
@@ -0,0 +1,57 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.typePoliciesPortal = void 0;
4
+ exports.typePoliciesPortal = {
5
+ Query: {
6
+ fields: {
7
+ fetchAsset: {
8
+ keyArgs: ["rank", "likes", "assetId"],
9
+ merge: true,
10
+ },
11
+ findUserCreations: {
12
+ keyArgs: ["itemId"],
13
+ merge: true,
14
+ },
15
+ fetchUserGalleries: {
16
+ keyArgs: ["galleryId"],
17
+ merge: true,
18
+ },
19
+ fetchUserFavorites: {
20
+ keyArgs: ["galleryId"],
21
+ merge: true,
22
+ },
23
+ fetchUserFavoritesV2: {
24
+ keyArgs: ["galleryId"],
25
+ merge: true,
26
+ },
27
+ fetchMyFavoritesV2: {
28
+ keyArgs: ["galleryId"],
29
+ merge: true,
30
+ },
31
+ fetchMyGalleriesV2: {
32
+ keyArgs: ["galleryId"],
33
+ merge: true,
34
+ },
35
+ fetchUserGalleriesV2: {
36
+ keyArgs: ["galleryId"],
37
+ merge: true,
38
+ },
39
+ fetchCreations: {
40
+ keyArgs: ["itemId"],
41
+ merge: true,
42
+ },
43
+ CollectionPage: {
44
+ keyArgs: ["collectionId"],
45
+ merge: true,
46
+ },
47
+ CreationsPage: {
48
+ keyArgs: ["itemId"],
49
+ merge: true,
50
+ },
51
+ fetchCreationsByItemType: {
52
+ keyArgs: ["nextPageToken"],
53
+ merge: true,
54
+ },
55
+ },
56
+ },
57
+ };
@@ -0,0 +1,17 @@
1
+ declare const assignCookies: (userId: string, wallets: Array<string>, authToken: string, refreshToken: string, newUser: string, domain?: string) => void;
2
+ declare const refreshCookies: (authToken: string, refreshToken: string, domain?: string) => void;
3
+ declare const getCookies: () => {
4
+ authToken: string;
5
+ refreshToken: string;
6
+ userId: string;
7
+ wallets: string;
8
+ newUser: string;
9
+ } | {
10
+ authToken: null;
11
+ refreshToken: null;
12
+ userId: null;
13
+ wallets: null;
14
+ newUser: null;
15
+ };
16
+ declare const destroyCookies: (domain?: string) => void;
17
+ export { assignCookies, refreshCookies, destroyCookies, getCookies };
@@ -0,0 +1,76 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getCookies = exports.destroyCookies = exports.refreshCookies = exports.assignCookies = void 0;
4
+ const nookies_1 = require("nookies");
5
+ const assignCookies = (userId, wallets, authToken, refreshToken, newUser, domain = "ludo.ninja") => {
6
+ if (userId)
7
+ (0, nookies_1.setCookie)(null, "userId", userId, { maxAge: 24 * 60 * 60, path: "/", domain });
8
+ if (wallets)
9
+ (0, nookies_1.setCookie)(null, "wallets", wallets.join(), {
10
+ maxAge: 24 * 60 * 60,
11
+ path: "/",
12
+ domain,
13
+ });
14
+ if (authToken)
15
+ (0, nookies_1.setCookie)(null, "authToken", authToken, {
16
+ maxAge: 24 * 60 * 60,
17
+ path: "/",
18
+ domain,
19
+ });
20
+ if (refreshToken)
21
+ (0, nookies_1.setCookie)(null, "refreshToken", refreshToken, {
22
+ maxAge: 24 * 60 * 60,
23
+ path: "/",
24
+ domain,
25
+ });
26
+ if (newUser)
27
+ (0, nookies_1.setCookie)(null, "newUser", newUser, {
28
+ maxAge: 24 * 60 * 60,
29
+ path: "/",
30
+ domain,
31
+ });
32
+ };
33
+ exports.assignCookies = assignCookies;
34
+ const refreshCookies = (authToken, refreshToken, domain = "ludo.ninja") => {
35
+ if (authToken)
36
+ (0, nookies_1.setCookie)(null, "authToken", authToken, {
37
+ maxAge: 24 * 60 * 60,
38
+ path: "/",
39
+ domain,
40
+ });
41
+ if (refreshToken)
42
+ (0, nookies_1.setCookie)(null, "refreshToken", refreshToken, {
43
+ maxAge: 24 * 60 * 60,
44
+ path: "/",
45
+ domain,
46
+ });
47
+ };
48
+ exports.refreshCookies = refreshCookies;
49
+ const getCookies = () => {
50
+ const { authToken, refreshToken, userId, wallets, newUser } = (0, nookies_1.parseCookies)();
51
+ if (authToken && refreshToken && userId && wallets && newUser) {
52
+ return {
53
+ authToken,
54
+ refreshToken,
55
+ userId,
56
+ wallets,
57
+ newUser,
58
+ };
59
+ }
60
+ return {
61
+ authToken: null,
62
+ refreshToken: null,
63
+ userId: null,
64
+ wallets: null,
65
+ newUser: null,
66
+ };
67
+ };
68
+ exports.getCookies = getCookies;
69
+ const destroyCookies = (domain = "ludo.ninja") => {
70
+ (0, nookies_1.destroyCookie)(null, "userId", { path: "/", domain });
71
+ (0, nookies_1.destroyCookie)(null, "wallets", { path: "/", domain });
72
+ (0, nookies_1.destroyCookie)(null, "authToken", { path: "/", domain });
73
+ (0, nookies_1.destroyCookie)(null, "refreshToken", { path: "/", domain });
74
+ (0, nookies_1.destroyCookie)(null, "newUser", { path: "/", domain });
75
+ };
76
+ exports.destroyCookies = destroyCookies;