## What Alter generated types base on `x-expanded-relations` OAS extension declared on schemaObjects. ## Why Often, API endpoints will automatically expand a model relations by default. They can also decorate a model with calculated totals. In order to more accurately represent the API, we wish to alter the generated types based on the expanded relations information. ## How - Follow the relation declaration signature as the backend controllers and the `expand` query param, i.e.: `items.variant.product`. - Introduce a custom `x-expended-relations` OAS extension. - Allow for organizing declared relations to help their maintenance. - Use traversal algorithms in codegen to support deeply nested relationships. - Use [type-fest](https://www.npmjs.com/package/type-fest)'s `Merge` and `SetRequired` to efficiently alter the types while enabling great intellisense for IDEs. Extra scope: * Added convenience yarn script to interact with the `medisa-oas` CLI within the monorepo. ## Test Include in the PR are two implementations of the x-expanded-relations on OAS schema, a simple and a complex one. ### Step 1 * Run `yarn install` * Run `yarn build` * Run `yarn medusa-oas oas --type combined --out-dir ~/tmp/oas` * Run `yarn medusa-oas client --type combined --component types --src-file ~/tmp/oas/combined.osa.json --out-dir ~/tmp/types` * Open `~/tmp/types/models/StoreRegionsRes` * Expect relations to be declared as required ### Step 2 * Open `~/tmp/types/models/StoreCartsRes` * Expect relations to be declared as required * Expect nested relations to have relations as required. ### Step 3 (optional) * Open `~/tmp/types` in an intellisense capable IDE * Within the `index.ts` file, attempt to declare a `const storeRegionRes: StoreRegionRes = {}` * Expect IDE to highlight that `countries` is a required field of `StoreRegionRes`
49 lines
1.1 KiB
JSON
49 lines
1.1 KiB
JSON
{
|
|
"name": "@medusajs/medusa-oas-cli",
|
|
"version": "0.1.0",
|
|
"description": "OAS CLI",
|
|
"main": "dist/index.js",
|
|
"bin": {
|
|
"medusa-oas": "./dist/index.js"
|
|
},
|
|
"files": [
|
|
"dist",
|
|
"oas"
|
|
],
|
|
"repository": {
|
|
"type": "git",
|
|
"url": "https://github.com/medusajs/medusa",
|
|
"directory": "packages/oas/medusa-oas-cli"
|
|
},
|
|
"publishConfig": {
|
|
"access": "public"
|
|
},
|
|
"author": "Medusa",
|
|
"license": "MIT",
|
|
"devDependencies": {
|
|
"jest": "^25.5.4",
|
|
"js-yaml": "^4.1.0",
|
|
"ts-jest": "^25.5.1",
|
|
"ts-node": "^10.9.1",
|
|
"typescript": "4.9.5",
|
|
"uuid": "^9.0.0"
|
|
},
|
|
"scripts": {
|
|
"prepare": "cross-env NODE_ENV=production yarn run build",
|
|
"build": "tsc --build",
|
|
"medusa-oas": "ts-node src/index.ts",
|
|
"test": "jest src",
|
|
"test:unit": "jest src"
|
|
},
|
|
"dependencies": {
|
|
"@medusajs/medusa": "*",
|
|
"@medusajs/openapi-typescript-codegen": "*",
|
|
"@readme/openapi-parser": "^2.4.0",
|
|
"@types/lodash": "^4.14.191",
|
|
"commander": "^10.0.0",
|
|
"lodash": "^4.17.21",
|
|
"openapi3-ts": "^3.1.2",
|
|
"swagger-inline": "^6.1.0"
|
|
}
|
|
}
|