graycoreio/daffodil

View on GitHub
libs/auth/driver/magento/src/auth.service.ts

Summary

Maintainability
A
0 mins
Test Coverage
import { Injectable } from '@angular/core';
import { Apollo } from 'apollo-angular';
import {
  Observable,
  throwError,
} from 'rxjs';
import {
  catchError,
  map,
} from 'rxjs/operators';

import { DaffAuthServiceInterface } from '@daffodil/auth/driver';

import { transformMagentoAuthError } from './errors/transform';
import {
  checkTokenQuery,
  MagentoCheckTokenResponse,
} from './queries/public_api';
import { validateCheckTokenResponse } from './validators/public_api';

/**
 * @inheritdoc
 */
@Injectable({
  providedIn: 'root',
})
export class DaffMagentoAuthService implements DaffAuthServiceInterface {
  constructor(private apollo: Apollo) {}

  check(): Observable<void> {
    return this.apollo.query<MagentoCheckTokenResponse>({
      query: checkTokenQuery,
      fetchPolicy: 'no-cache',
    }).pipe(
      map(validateCheckTokenResponse),
      map(() => undefined),
      catchError(err => throwError(() => transformMagentoAuthError(err))),
    );
  }
}