## What Allow `medusa-oas` CLI to generate API client code from an OAS json file. ## Why Manually maintaining API clients is time consuming and error prone. We wish to automate the process by using code generation strategies. We also wish to eliminate direct import from `@medusajs/medusa` in clients which can lead to unwanted side effects like importing unnecessary dependencies. ## How Fork and customize an OAS code generator library that is TypeScript friendly. Attempt to match the interface and signature of our current client packages: `@medusajs/medusa-js`, `medusa-react` Add a new `client` command to the `medusa-oas` CLI as the main interface to interact with the code generation tooling. ## Test ### Prerequisites * From the root of the monorepo: * `yarn install` * `yarn build` ### Case - all in one build * From the root of the monorepo: * Run `yarn medusa-oas oas --out-dir ~/tmp/oas --type store` * Run `yarn medusa-oas client --src-file ~/tmp/oas/store.oas.json --out-dir ~/tmp/client-store --type store` * Expect `~/tmp/client-store` to contain the following files and directories: ``` core/ hooks/ models/ services/ index.ts MedusaStore.ts useMedusaStore.tsx ``` ### Case - types only * From the root of the monorepo: * Run `yarn medusa-oas oas --out-dir ~/tmp/oas --type store` * Run `yarn medusa-oas client --src-file ~/tmp/oas/store.oas.json --out-dir ~/tmp/client-types --type store --component types` * Expect `~/tmp/client-types` to contain the following files and directories: ``` models/ index.ts ``` ### Case - client only * From the root of the monorepo: * Run `yarn medusa-oas oas --out-dir ~/tmp/oas --type store` * Run `yarn medusa-oas client --src-file ~/tmp/oas/store.oas.json --out-dir ~/tmp/client-only --type store --component client --types-package @medusajs/client-types` * Expect `~/tmp/client-only` to contain the following files and directories: ``` core/ services/ index.ts MedusaStore.ts ```
medusa-oas-cli - 0.1.0 - experimental
A command-line tool for all OpenAPI Specifications (OAS) related tooling.
Install
yarn add --dev @medusajs/medusa-oas-cli
Install in the global namespace is not yet supported.
npm install -g @medusajs/medusa-oas-cli
Configuration / First time setup
N/A
How to use
yarn medusa-oas <command>
Command - oas
This command will scan the @medusajs/medusa package in order to extract JSDoc OAS into a json file.
By default, the command will output the two files admin.oas.json and store.oas.json in the same directory that the
command was run.
Invalid OAS with throw an error and will prevent the files from being outputted.
--type <string>
Specify which API OAS to create. Accepts all, admin, store.
Defaults to all.
yarn medusa-oas oas --type admin
--out-dir <path>
Specify in which directory should the files be outputted. It accepts a relative or absolute path.
If the directory doesn't exist, it will be created. Defaults to ./.
yarm medusa-oas oas --out-dir
--paths <paths...>
Allows passing additional directory paths to crawl for JSDoc OAS and include in the generated OAS. It accepts multiple entries.
yarn medusa-oas oas --paths ~/medusa-server/src
--dry-run
Will package the OAS but will not output file. Useful for validating OAS.
yarn medusa-oas oas --dry-run
--force
Ignore OAS errors and attempt to output generated OAS files.
yarn medusa-oas oas --force
Command - client
Will generate API client files from a given OAS file.
--src-file <path>
Specify the path to the OAS JSON file.
yarm medusa-oas client --src-file ./store.oas.json
--name <name>
Namespace for the generated client. Usually admin or store.
yarm medusa-oas client --name admin
--out-dir <path>
Specify in which directory should the files be outputted. Accepts relative and absolute path. It the directory doesn't
exist, it will be created. Defaults to ./.
yarm medusa-oas client --out-dir ./client
--type <type>
Client component types to generate. Accepts all, types, client, hooks.
Defaults to all.
yarn medusa-oas client --type types
--types-packages <name>
Replace relative import statements by types package name. Mandatory when using --type client or --type hooks.
--client-packages <name>
Replace relative import statements by client package name. Mandatory when using --type hooks.
yarn medusa-oas client --type types
--clean
Delete destination directory content before generating client.
yarn medusa-oas --clean