Source: api/product/get.js

import { File } from 'cpb-storage';
import app, { cleanupData } from '../index.js';

/**
 * Get Custom Product Configuration Data
 * @method module:cpb-api/product.get
 * @async
 * @param {number|string} shop_id
 * @param {number|string} id
 * @param {?string} [bucket=App.get('bucket')=process.env.BUCKET] - gce storage bucket
 * @param {?boolean} [fetch=false] - fetch new data
 * @param  {?boolean} [files=false] - include files
 * @param  {?boolean} [versions=false]  - include previous versions
 * @param {string} [path=`${shop_id}/${id}.json`]  - custom product config file path
 * @return {Promise<CustomProduct>}
 */
export default async function get({ shop_id, id, bucket = app.get('bucket'), fetch = false, files = false, versions = false, path = `${shop_id}/${id}.json` }) {
  if (!id) throw new TypeError('!id');
  if (!shop_id) throw new TypeError('!shop_id');

  if (!fetch) {
    try {
      const data = await File.read({ bucket, name: path });
      return cleanupData(data, { files, versions });
    } catch (e) {
      console.error('ERROR.StoreGet.CACHED', e);
    }
  }
}

function GetProductPublicDownloadUrl({ shop_id, id, bucket = app.get('bucket') }) {
  if (!id) throw new TypeError('!id');
  if (!shop_id) throw new TypeError('!shop_id');
  const productUrl = `https://storage.googleapis.com/${bucket}/{${shop_id}/${id}.json`;

  return productUrl;
}