UNPKG

@infosel-sdk/core

Version:

Core SDK for Infosel financial services platform. Provides essential infrastructure for authentication, HTTP/GraphQL communication, storage management, and error handling.

115 lines 5.26 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const client_1 = require("@apollo/client"); const context_1 = require("@apollo/client/link/context"); const error_1 = require("@apollo/client/link/error"); const sdk_error_1 = tslib_1.__importStar(require("../../errors/sdk_error")); class InfoselGraphQlClient { constructor(apolloClient) { this.apolloClient = apolloClient; } static newInstance(baseURL, sdkManager) { const httpLink = (0, client_1.createHttpLink)({ uri: baseURL }); const errorLink = (0, error_1.onError)( // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore ({ graphQLErrors, networkError, operation, forward }) => { const graphOAuthErrors = (graphQLErrors === null || graphQLErrors === void 0 ? void 0 : graphQLErrors.some(error => { var _a; return error.message === 'Unauthenticated' || ((_a = error.extensions) === null || _a === void 0 ? void 0 : _a['code']) === 'UNAUTHENTICATED'; })) || false; const networkOAuthErrors = networkError && // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore (networkError.statusCode === 401 || networkError.statusCode === 403); const refreshTokenRequest = () => tslib_1.__awaiter(this, void 0, void 0, function* () { const localToken = yield sdkManager.storage.getObject('oauth_token'); /* For graphQl the url that needs to generate the token its optional because its always the same. In this case url only works like an identifier for the auth provider. */ const accessToken = yield sdkManager.authProvider.refreshToken(baseURL, localToken); return yield sdkManager.storage.saveObject('oauth_token', accessToken); }); if (graphOAuthErrors || networkOAuthErrors) { return (0, client_1.fromPromise)(refreshTokenRequest()).flatMap(token => { const context = operation.getContext(); operation.setContext({ headers: Object.assign(Object.assign({}, context['headers']), { Authorization: `Bearer ${token === null || token === void 0 ? void 0 : token.accessToken}` }), }); return forward(operation); }); } }); const authMiddleware = (0, context_1.setContext)((_, { headers = {} }) => tslib_1.__awaiter(this, void 0, void 0, function* () { try { const localToken = yield sdkManager.storage.getObject('oauth_token'); return { headers: Object.assign(Object.assign({}, headers), { Authorization: `Bearer ${localToken.accessToken}` }), }; } catch (error) { const shouldRefresh = yield sdkManager.authProvider.shouldRefreshTokenOnError(error); if (!shouldRefresh) { return { headers, }; } /* For graphQl the url that needs to generate the token its optional because its always the same. In this case url only works like an identifier for the auth provider. */ const token = yield sdkManager.authProvider.generateToken(baseURL); const authenticationToken = yield sdkManager.storage.saveObject('oauth_token', token); return { headers, Authorization: `Bearer ${authenticationToken.accessToken}`, }; } })); const client = new client_1.ApolloClient({ defaultOptions: { watchQuery: { fetchPolicy: 'no-cache', }, query: { fetchPolicy: 'no-cache', }, }, cache: new client_1.InMemoryCache(), link: (0, client_1.from)([errorLink, authMiddleware, httpLink]), }); return new InfoselGraphQlClient(client); } query(query, variables) { return tslib_1.__awaiter(this, void 0, void 0, function* () { const response = yield this.apolloClient.query({ query: (0, client_1.gql) ` ${query} `, variables, }); return response.data; }); } mutate(query, variables) { return tslib_1.__awaiter(this, void 0, void 0, function* () { const { data } = yield this.apolloClient.mutate({ mutation: (0, client_1.gql) ` ${query} `, variables, }); if (data) { return data; } throw new sdk_error_1.default(sdk_error_1.SdkErrorType.INVALID_GRAPH_QL_DATA_RESPONSE); }); } } exports.default = InfoselGraphQlClient; //# sourceMappingURL=infosel_graph_ql_client.js.map