Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MSW: export controllers to a separate index file. #347

Open
stijnvanhulle opened this issue Apr 2, 2022 · 3 comments
Open

MSW: export controllers to a separate index file. #347

stijnvanhulle opened this issue Apr 2, 2022 · 3 comments
Labels
enhancement New feature or request msw MSW related issues

Comments

@stijnvanhulle
Copy link

Hi, At this moment we are using Orval 🍻 to generate our own API client which is already working really great. We can import our Axios controllers and React-query controllers from the index.ts(api-client/lib/gen/clients/axios/index.ts) file. But we also need the MSW controllers to mock our API calls.

At this moment we have a couple of them but those can become really fast 100 imports that we need to specify with the correct path.

import { petControllerMSW } from 'api-client/lib/gen/clients/axios/pet-controller/pet-controller.msw'
import { jupilerControllerMSW } from 'api-client/lib/gen/clients/axios/jupiler-controller/jupiler-controller.msw'

It would be nice to have something like this: just one import that contains all the different MSW controllers.

import { petControllerMSW, jupilerControllerMSW } from 'api-client/lib/gen/clients/msw'

I have tried playing around with workspace and the different modes but all modes have the same thing: MSW is excluded in the exports.

Idea

src/core/writers/specs.ts
I tried something out by adding a new file msw.ts that contains all the controllers but not sure if this would be the best way to solve this issue, any clues on what can be possible here? Maybe a new client just for the MSW?

  if (output.workspace) {
    const workspacePath = output.workspace;
    let imports = implementationPaths
      .filter((path) => !path.endsWith('.msw.ts'))
      .map((path) =>
        relativeSafe(workspacePath, getFileInfo(path).pathWithoutExtension),
      );

    if (output.schemas) {
      imports.push(
        relativeSafe(workspacePath, getFileInfo(output.schemas).dirname),
      );
    }

    const indexFile = join(workspacePath, '/index.ts');

    if (await pathExists(indexFile)) {
      const data = await readFile(indexFile, 'utf8');
      const importsNotDeclared = imports.filter((imp) => !data.includes(imp));
      await appendFile(
        indexFile,
        uniq(importsNotDeclared)
          .map((imp) => `export * from '${imp}';`)
          .join('\n') + '\n',
      );
    } else {
      await outputFile(
        indexFile,
        uniq(imports)
          .map((imp) => `export * from '${imp}';`)
          .join('\n') + '\n',
      );
    }

    // indexFile for MSW => this creates a msw file with all exports for the MSW controllers
    const indexMSWFile = join(workspacePath, '/msw.ts');

    let importsMSW = implementationPaths
      .filter((path) => !!path.endsWith('.msw.ts'))
      .map((path) =>
        relativeSafe(workspacePath, getFileInfo(path).pathWithoutExtension),
      );

    if (await pathExists(indexMSWFile)) {
      const data = await readFile(indexMSWFile, 'utf8');
      const importsNotDeclared = importsMSW.filter(
        (imp) => !data.includes(imp),
      );
      await appendFile(
        indexMSWFile,
        uniq(importsNotDeclared)
          .map((imp) => `export * from '${imp}';`)
          .join('\n') + '\n',
      );
    } else {
      await outputFile(
        indexMSWFile,
        uniq(importsMSW)
          .map((imp) => `export * from '${imp}';`)
          .join('\n') + '\n',
      );
    }

    implementationPaths = [indexFile, ...implementationPaths];
  }

Thanks 🙂

@stijnvanhulle stijnvanhulle changed the title Export MSW controllers to a separate index file. Feat: export MSW controllers to a separate index file. Apr 9, 2022
@yekver
Copy link
Contributor

yekver commented Apr 26, 2022

I've thought about almost the same. Current approach is good but do not fit well in some obvious situations like for example: I need to have all my MSW generators exported as array.

Right now I have to manually do this like:

import { getSomeSchema1MSW } from 'Src/generated/schema1.msw'
import { getSomeSchema2MSW } from 'Src/generated/schema2.msw'
import { getSomeSchema3MSW } from 'Src/generated/schema3.msw'

export default [
  ...getSomeSchema1MSW(),
  ...getSomeSchema2MSW(),
  ...getSomeSchema3MSW()
]

@Kangarused
Copy link

+1 - This would be an absolute life saver for large projects

@anymaniax anymaniax added the enhancement New feature or request label Jun 13, 2022
@melloware melloware changed the title Feat: export MSW controllers to a separate index file. MSW: export controllers to a separate index file. Nov 13, 2023
@melloware melloware added the msw MSW related issues label Jun 26, 2024
@HecFlo
Copy link

HecFlo commented Aug 7, 2024

+1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request msw MSW related issues
Projects
None yet
Development

No branches or pull requests

6 participants