## 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 ```
18 lines
507 B
TypeScript
18 lines
507 B
TypeScript
import Module from "module"
|
|
import path from "path"
|
|
|
|
const fallback = (filename: string) => {
|
|
const mod = new Module(filename)
|
|
|
|
mod.filename = filename
|
|
mod.paths = (Module as any)._nodeModulePaths(path.dirname(filename))
|
|
;(mod as any)._compile(`module.exports = require;`, filename)
|
|
|
|
return mod.exports
|
|
}
|
|
|
|
// Polyfill Node's `Module.createRequireFromPath` if not present (added in Node v10.12.0)
|
|
const createRequireFromPath = Module.createRequire || fallback
|
|
|
|
export default createRequireFromPath
|