Home > Enterprise >  how can I export all my files from different folders
how can I export all my files from different folders

Time:11-05

I have three folders:

controllers
 --shopify
 ---webhook.ts
 ---autht.ts
 ---products.ts
--index.ts

I want one index file where all shopify files are in index.ts exported so I can use them.

I dont want this

import { Webhook } from 'controllers/shopify/Webhook.ts';
import { Auth } from 'controllers/shopify/Auth.ts';
import { Product } from 'controllers/shopify/Product.ts';

I want this

import { Webhook, Auth, Product } from 'controllers/shopify';

How can I do it ? example of webhook.ts

import { Request, Response } from "express";
import * as Sentry from '@sentry/node';
import { shopify } from "src/server";

// Process webhooks
export const ShopifyWebhook = async (req: Request, res: Response) => {
  try {
    // Note: the express.text() given above is an Express middleware that will read
    // in the body as a string, and make it available at req.body, for this path only.
    await shopify.webhooks.process({
      rawBody: req.body, // is a string
      rawRequest: req,
      rawResponse: res,
    });

    console.log('PROCESS');
  } catch (e) {
    Sentry.captureException(e);
  }
};

index.ts now like this?

export { Webhook } from 'controllers/shopify/webhook'; and the others too ?

CodePudding user response:

yes, controllers/shopify/index.ts would be like this:

export { Webhook } from 'controllers/shopify/Webhook.ts';
export { Auth } from 'controllers/shopify/Auth.ts';
export { Product } from 'controllers/shopify/Product.ts';

then you can do this:

import { Webhook, Auth, Product } from 'controllers/shopify';

this is called a barrel file (which has its pros and cons)

  • Related