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

feat: don't inject generated files into hook command #1181

Merged
merged 2 commits into from
Jan 31, 2024
Merged

feat: don't inject generated files into hook command #1181

merged 2 commits into from
Jan 31, 2024

Conversation

Arkanii
Copy link
Contributor

@Arkanii Arkanii commented Jan 26, 2024

Status

WIP OR READY IF NO TEST NEEDED

Description

afterAllFilesWrite hook inject generated files automatically for each command.

In my case I use :

import { defineConfig } from "orval";

export default defineConfig({
  api: {
    input: {
      target: "http://php/api/docs.jsonopenapi",
      validation: false,
    },
    output: {
      mode: "tags-split",
      client: "react-query",
      baseUrl: "/api",
      target: "./api/endpoints.ts",
      schemas: "./types/models",
      override: {
        useDates: true,
        useNativeEnums: true,
      },
    },
    hooks: {
      afterAllFilesWrite: async () => {
        const { execa } = await import("execa");

        await execa("pnpm", ["run", "lint:orval"]);
      },
    },
  },
});

My afterAllFilesWrite function is a bit ugly. 😅

I've added here the possibility to use afterAllFilesWrite as an object with two fields :

  • command: the command to run
  • injectGeneratedDirsAndFiles: a boolean that indicates whether the generated files and folders should be injected into the command or not

I've keep the previous implementation to not create a BC.

Todos

  • Tests
  • Documentation
  • Changelog Entry (unreleased)

Steps to Test or Reproduce

orval.config.ts

import { faker } from '@faker-js/faker';
import { defineConfig } from 'orval';

export default defineConfig({
  petstore: {
    hooks: {
      afterAllFilesWrite: {
        command: 'prettier --write .',
        injectGeneratedDirsAndFiles: false,
      },
    },
    output: {
      mode: 'split',
      target: 'src/api/endpoints/petstoreFromFileSpecWithTransformer.ts',
      schemas: 'src/api/model',
      client: 'react-query',
      mock: true,
      override: {
        mutator: {
          path: './src/api/mutator/custom-instance.ts',
          name: 'customInstance',
        },
        operations: {
          listPets: {
            mock: {
              properties: () => ({
                '[].id': () => faker.number.int({ min: 1, max: 99999 }),
              }),
            },
            query: {
              useQuery: true,
              useSuspenseQuery: true,
              useSuspenseInfiniteQuery: true,
              useInfinite: true,
              useInfiniteQueryParam: 'limit',
            },
          },
          showPetById: {
            mock: {
              data: () => ({
                id: faker.number.int({ min: 1, max: 99 }),
                name: faker.person.firstName(),
                tag: faker.helpers.arrayElement([
                  faker.word.sample(),
                  undefined,
                ]),
              }),
            },
          },
        },
        mock: {
          properties: {
            '/tag|name/': () => faker.person.lastName(),
          },
        },
      },
    },
    input: {
      target: './petstore.yaml',
      override: {
        transformer: './src/api/transformer/add-version.js',
      },
    },
  },
});

I'm new to open source so every comment or help is welcome.

Thanks !

@melloware
Copy link
Collaborator

I like where you are going but I assigned @anymaniax as he is the man in charge of this repo and I want to make sure this change aligns with what he typically expects.

@melloware melloware added the enhancement New feature or request label Jan 26, 2024
anymaniax
anymaniax previously approved these changes Jan 31, 2024
@melloware melloware merged commit 2a77a04 into orval-labs:master Jan 31, 2024
2 checks passed
@melloware melloware added this to the 6.24.0 milestone Jan 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants