All files / src context.ts

100% Statements 45/45
96.88% Branches 31/32
100% Functions 3/3
100% Lines 45/45

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 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125  22x 22x 22x 22x 22x 22x 22x 22x 22x 22x                             249x 39x     210x     249x 249x 2x   249x 2x   249x 2x   249x 2x   249x 5x   244x 244x 244x 244x 244x 244x 244x 244x   244x 243x   244x 2x   244x 2x   244x 1x         86x 2x       27x 2x       22x                                                                                    
import path from 'path';
 
import * as ShopifyErrors from './error';
import {SessionStorage} from './auth/session/session_storage';
import {SQLiteSessionStorage} from './auth/session/storage/sqlite';
import {ApiVersion, ContextParams} from './base-types';
import {AuthScopes} from './auth/scopes';
 
interface ContextInterface extends ContextParams {
  HOST_SCHEME: string;
  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 dbFile = path.join(
  require.main ? path.dirname(require.main.filename) : process.cwd(),
  'database.sqlite',
);
 
const Context: ContextInterface = {
  API_KEY: '',
  API_SECRET_KEY: '',
  SCOPES: new AuthScopes([]),
  HOST_NAME: '',
  HOST_SCHEME: 'https',
  API_VERSION: ApiVersion.Unstable,
  IS_EMBEDDED_APP: true,
  IS_PRIVATE_APP: false,
  // TS hack as SESSION_STORAGE is guaranteed to be set
  // to a correct value in `initialize()`.
  SESSION_STORAGE: null as unknown as SessionStorage,
 
  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;
    this.SESSION_STORAGE =
      params.SESSION_STORAGE ?? new SQLiteSessionStorage(dbFile);
 
    if (params.HOST_SCHEME) {
      this.HOST_SCHEME = params.HOST_SCHEME;
    }
 
    if (params.USER_AGENT_PREFIX) {
      this.USER_AGENT_PREFIX = params.USER_AGENT_PREFIX;
    }
 
    if (params.LOG_FILE) {
      this.LOG_FILE = params.LOG_FILE;
    }
 
    if (params.PRIVATE_APP_STOREFRONT_ACCESS_TOKEN) {
      this.PRIVATE_APP_STOREFRONT_ACCESS_TOKEN =
        params.PRIVATE_APP_STOREFRONT_ACCESS_TOKEN;
    }
  },
 
  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};