All files / src context.ts

100% Statements 37/37
100% Branches 24/24
100% Functions 3/3
100% Lines 37/37

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 10418x 18x 18x 18x                                               18x                       181x 35x   146x       181x 181x 2x   181x 2x   181x 2x   181x 2x     181x 5x         176x 176x 176x 176x 176x 176x 176x   176x 175x     176x 2x     176x 2x         79x 2x             20x 2x         18x  
import * as ShopifyErrors from './error';
import {SessionStorage, MemorySessionStorage} from './auth/session';
import {ApiVersion, ContextParams} from './base_types';
import {AuthScopes} from './auth/scopes';
 
interface ContextInterface extends ContextParams {
  SESSION_STORAGE: SessionStorage;
  SCOPES: AuthScopes;
 
  /**
   * Sets up the Shopify API Library to be able to integrate with Shopify and run authenticated commands.
   *
   * @param params Settings to update
   */
  initialize(params: ContextParams): void;
 
  /**
   * Throws error if context has not been initialized.
   */
  throwIfUninitialized(): void | never;
 
  /**
   * Throws error if the current app is private.
   */
  throwIfPrivateApp(message: string): void | never;
}
 
const Context: ContextInterface = {
  API_KEY: '',
  API_SECRET_KEY: '',
  SCOPES: new AuthScopes([]),
  HOST_NAME: '',
  API_VERSION: ApiVersion.Unstable,
  IS_EMBEDDED_APP: true,
  IS_PRIVATE_APP: false,
  SESSION_STORAGE: new MemorySessionStorage(),
 
  initialize(params: ContextParams): void {
    let scopes: AuthScopes;
    if (params.SCOPES instanceof AuthScopes) {
      scopes = params.SCOPES;
    } else {
      scopes = new AuthScopes(params.SCOPES);
    }
 
    // Make sure that the essential params actually have content in them
    const missing: string[] = [];
    if (!params.API_KEY.length) {
      missing.push('API_KEY');
    }
    if (!params.API_SECRET_KEY.length) {
      missing.push('API_SECRET_KEY');
    }
    if (!scopes.toArray().length) {
      missing.push('SCOPES');
    }
    if (!params.HOST_NAME.length) {
      missing.push('HOST_NAME');
    }
 
    if (missing.length) {
      throw new ShopifyErrors.ShopifyError(
        `Cannot initialize Shopify API Library. Missing values for: ${missing.join(', ')}`,
      );
    }
 
    this.API_KEY = params.API_KEY;
    this.API_SECRET_KEY = params.API_SECRET_KEY;
    this.SCOPES = scopes;
    this.HOST_NAME = params.HOST_NAME;
    this.API_VERSION = params.API_VERSION;
    this.IS_EMBEDDED_APP = params.IS_EMBEDDED_APP;
    this.IS_PRIVATE_APP = params.IS_PRIVATE_APP;
 
    if (params.SESSION_STORAGE) {
      this.SESSION_STORAGE = params.SESSION_STORAGE;
    }
 
    if (params.USER_AGENT_PREFIX) {
      this.USER_AGENT_PREFIX = params.USER_AGENT_PREFIX;
    }
 
    if (params.LOG_FILE) {
      this.LOG_FILE = params.LOG_FILE;
    }
  },
 
  throwIfUninitialized(): void {
    if (!this.API_KEY || this.API_KEY.length === 0) {
      throw new ShopifyErrors.UninitializedContextError(
        'Context has not been properly initialized. Please call the .initialize() method to setup your app context object.',
      );
    }
  },
 
  throwIfPrivateApp(message: string): void {
    if (Context.IS_PRIVATE_APP) {
      throw new ShopifyErrors.PrivateAppError(message);
    }
  },
};
 
export {Context, ContextInterface};