orval icon indicating copy to clipboard operation
orval copied to clipboard

Provided transformer does not work

Open Hypenate opened this issue 3 years ago • 1 comments

What are the steps to reproduce this issue?

I tried the example to add a version to the api (just as a test) but it fails. I've added as described

     override: {
       transformer: 'src/api/transformer/add-version.js',
     },

and used this file add-version.js

What happens?

The generation fails with: 🛑 api- Oups... 🍻. Path: C:/git/libs/core/api/transformers/add-version.js => Error: Cannot find module 'C:/git/libs/core/api/transfor mers/add-version.js' Require stack:

  • C:\git\node_modules\orval\dist\chunk-DKPBKVU7.js
  • C:\git\node_modules\orval\dist\bin\orval.js

What were you expecting to happen?

A working example

What versions are you using?

Operating System: Windows 10 Package Version: 6.6.4

Hypenate avatar Jun 14 '22 09:06 Hypenate

Hi @Hypenate, all the tests and examples on the project are working with it so can you give a bit more context about your project structure

anymaniax avatar Jun 18 '22 13:06 anymaniax

hmm, maybe the case is similar to mine. It's not "not working" per-say, but can definitely work better.

What works

I have a micro-services architecture and many micro-services are at different subpaths. I use a reverse proxy in front to access them at /api. The way I have it configured is as follows:

  • user-service.config.ts (snippet)
/**
 * Configuration for the `user-service` API client.
 *
 * **NOTE:** Expects `user-service` API documentation to be reachable at `http://localhost:3002/api-docs/json`.
 */
export default defineConfig({
  evo: {
    input: {
      target: "http://localhost:4000/api/user/api-docs/json",
      override: {
        transformer: "../src/api/transformer/add-user-subpath.js",
      },
    },
    output: {
      mode: "tags",
     ...
}

See how the transformer used is add-user-subpath.jswhich contains the following (full):

/**
 * Transformer function for orval.
 *
 * @param {OpenAPIObject} inputSchema
 * @return {OpenAPIObject}
 */
// eslint-disable-next-line no-undef
module.exports = (inputSchema) => ({
  ...inputSchema,
  paths: Object.entries(inputSchema.paths).reduce(
    (acc, [path, pathItem]) => ({
      ...acc,
      [`{subpath}${path}`]: Object.entries(pathItem).reduce(
        (pathItemAcc, [verb, operation]) => ({
          ...pathItemAcc,
          [verb]: {
            ...operation,
            parameters: [
              ...(operation.parameters || []),
              {
                name: "subpath",
                in: "path",
                required: true,
                schema: {
                  type: "string",
                  default: "user",
                },
              },
            ],
          },
        }),
        {}
      ),
    }),
    {}
  ),
});

For every micro-service, I have to create a new config and transformer

What does not work

Trying to use a CLI argument for the transformer will not work:

  • user-service.config.ts
transformer: "../src/api/transformer/add-subpath.js user",
  • add-subpath.js
/* eslint-disable no-undef */
/**
 * Transformer function for orval.
 *
 * @param {OpenAPIObject} inputSchema
 * @return {OpenAPIObject}
 */
module.exports = (inputSchema) => {
  const subpathDefault = process.argv[2];
  if (!subpathDefault) {
    throw new Error("You must provide a subpath as a first argument to this script.");
  }
  return {
    ...inputSchema,
    paths: Object.entries(inputSchema.paths).reduce(
      (acc, [path, pathItem]) => ({
        ...acc,
        [`{subpath}${path}`]: Object.entries(pathItem).reduce(
          (pathItemAcc, [verb, operation]) => ({
            ...pathItemAcc,
            [verb]: {
              ...operation,
              parameters: [
                ...(operation.parameters || []),
                {
                  name: "subpath",
                  in: "path",
                  required: true,
                  schema: {
                    type: "string",
                    default: subpathDefault,
                  },
                },
              ],
            },
          }),
          {},
        ),
      }),
      {},
    ),
  };
};

🛑 evo - Oups... 🍻. Path: /users/sina/client/src/api/transformer/add-subpath.js user => Error: Cannot find module '/users/sina/client/src/api/transformer/add-subpath.js user' Require stack:

  • /users/sina/client/node_modules/@orval/core/dist/index.js
  • /users/sina/client/node_modules/orval/dist/chunk-L4BGJ5OZ.js
  • /users/sina/client/node_modules/orval/dist/bin/orval.js

usersina avatar Dec 13 '23 14:12 usersina