@medusajs/medusa-oas-cli 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,122 @@
1
+ # medusa-oas-cli - 0.1.0 - experimental
2
+
3
+ A command-line tool for all OpenAPI Specifications (OAS) related tooling.
4
+
5
+ ## Install
6
+
7
+ `yarn add --dev @medusajs/medusa-oas-cli`
8
+
9
+ Install in the global namespace is not yet supported.
10
+ ~~`npm install -g @medusajs/medusa-oas-cli`~~
11
+
12
+ ## Configuration / First time setup
13
+
14
+ N/A
15
+
16
+ ## How to use
17
+
18
+ ```bash
19
+ yarn medusa-oas <command>
20
+ ```
21
+
22
+ ---
23
+
24
+ ### Command - `oas`
25
+
26
+ This command will scan the `@medusajs/medusa` package in order to extract JSDoc OAS into a json file.
27
+
28
+ By default, the command will output the two files `admin.oas.json` and `store.oas.json` in the same directory that the
29
+ command was run.
30
+
31
+ Invalid OAS with throw an error and will prevent the files from being outputted.
32
+
33
+ #### `--type <string>`
34
+
35
+ Specify which API OAS to create. Accepts `all`, `admin`, `store`.
36
+ Defaults to `all`.
37
+
38
+ ```bash
39
+ yarn medusa-oas oas --type admin
40
+ ```
41
+
42
+ #### `--out-dir <path>`
43
+
44
+ Specify in which directory should the files be outputted. It accepts a relative or absolute path.
45
+ If the directory doesn't exist, it will be created. Defaults to `./`.
46
+
47
+ ```bash
48
+ yarm medusa-oas oas --out-dir
49
+ ```
50
+
51
+ #### `--paths <paths...>`
52
+
53
+ Allows passing additional directory paths to crawl for JSDoc OAS and include in the generated OAS.
54
+ It accepts multiple entries.
55
+
56
+ ```bash
57
+ yarn medusa-oas oas --paths ~/medusa-server/src
58
+ ```
59
+
60
+ #### `--dry-run`
61
+
62
+ Will package the OAS but will not output file. Useful for validating OAS.
63
+
64
+ ```bash
65
+ yarn medusa-oas oas --dry-run
66
+ ```
67
+
68
+ #### `--force`
69
+
70
+ Ignore OAS errors and attempt to output generated OAS files.
71
+
72
+ ```bash
73
+ yarn medusa-oas oas --force
74
+ ```
75
+
76
+ ---
77
+
78
+ ### Command - `client`
79
+
80
+ Will generate API client files from a given OAS file.
81
+
82
+ #### `--src-file <path>`
83
+
84
+ Specify the path to the OAS JSON file.
85
+
86
+ `yarm medusa-oas client --src-file ./store.oas.json`
87
+
88
+ #### `--name <name>`
89
+
90
+ Namespace for the generated client. Usually `admin` or `store`.
91
+
92
+ `yarm medusa-oas client --name admin`
93
+
94
+ #### `--out-dir <path>`
95
+
96
+ Specify in which directory should the files be outputted. Accepts relative and absolute path. It the directory doesn't
97
+ exist, it will be created. Defaults to `./`.
98
+
99
+ `yarm medusa-oas client --out-dir ./client`
100
+
101
+ #### `--type <type>`
102
+
103
+ Client component types to generate. Accepts `all`, `types`, `client`, `hooks`.
104
+ Defaults to `all`.
105
+
106
+ `yarn medusa-oas client --type types`
107
+
108
+ #### `--types-packages <name>`
109
+
110
+ Replace relative import statements by types package name. Mandatory when using `--type client` or `--type hooks`.
111
+
112
+ #### `--client-packages <name>`
113
+
114
+ Replace relative import statements by client package name. Mandatory when using `--type hooks`.
115
+
116
+ `yarn medusa-oas client --type types`
117
+
118
+ #### `--clean`
119
+
120
+ Delete destination directory content before generating client.
121
+
122
+ `yarn medusa-oas --clean`
@@ -0,0 +1,12 @@
1
+ import { Command, Option, OptionValues } from "commander";
2
+ /**
3
+ * CLI Command declaration
4
+ */
5
+ export declare const commandName = "client";
6
+ export declare const commandDescription = "Generate API clients from OAS.";
7
+ export declare const commandOptions: Option[];
8
+ export declare function getCommand(): Command;
9
+ /**
10
+ * Main
11
+ */
12
+ export declare function execute(cliParams: OptionValues): Promise<void>;
@@ -0,0 +1,148 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __importDefault = (this && this.__importDefault) || function (mod) {
26
+ return (mod && mod.__esModule) ? mod : { "default": mod };
27
+ };
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ exports.execute = exports.getCommand = exports.commandOptions = exports.commandDescription = exports.commandName = void 0;
30
+ const path_1 = __importDefault(require("path"));
31
+ const openapi_typescript_codegen_1 = require("@medusajs/openapi-typescript-codegen");
32
+ const lodash_1 = require("lodash");
33
+ const promises_1 = __importStar(require("fs/promises"));
34
+ const commander_1 = require("commander");
35
+ /**
36
+ * CLI Command declaration
37
+ */
38
+ exports.commandName = "client";
39
+ exports.commandDescription = "Generate API clients from OAS.";
40
+ exports.commandOptions = [
41
+ new commander_1.Option("-t, --type <type>", "Namespace for the generated client. Usually `admin` or `store`.").makeOptionMandatory(),
42
+ new commander_1.Option("-s, --src-file <srcFile>", "Path to source OAS JSON file.").makeOptionMandatory(),
43
+ new commander_1.Option("-o, --out-dir <outDir>", "Output directory for generated client files.").default(path_1.default.resolve(process.cwd(), "client")),
44
+ new commander_1.Option("-c, --component <component>", "Client component types to generate.")
45
+ .choices(["all", "types", "client", "hooks"])
46
+ .default("all"),
47
+ new commander_1.Option("--types-package <name>", "Replace relative import statements by types package name."),
48
+ new commander_1.Option("--client-package <name>", "Replace relative import statements by client package name."),
49
+ new commander_1.Option("--clean", "Delete destination directory content before generating client."),
50
+ ];
51
+ function getCommand() {
52
+ const command = new commander_1.Command(exports.commandName);
53
+ command.description(exports.commandDescription);
54
+ for (const opt of exports.commandOptions) {
55
+ command.addOption(opt);
56
+ }
57
+ command.action(async (options) => await execute(options));
58
+ return command;
59
+ }
60
+ exports.getCommand = getCommand;
61
+ /**
62
+ * Main
63
+ */
64
+ async function execute(cliParams) {
65
+ /**
66
+ * Process CLI options
67
+ */
68
+ if (["client", "hooks"].includes(cliParams.component) &&
69
+ !cliParams.typesPackage) {
70
+ throw new Error(`--types-package must be declared when using --component=${cliParams.component}`);
71
+ }
72
+ if (cliParams.component === "hooks" && !cliParams.clientPackage) {
73
+ throw new Error(`--client-package must be declared when using --component=${cliParams.component}`);
74
+ }
75
+ const shouldClean = !!cliParams.clean;
76
+ const srcFile = path_1.default.resolve(cliParams.srcFile);
77
+ const outDir = path_1.default.resolve(cliParams.outDir);
78
+ const apiName = cliParams.type;
79
+ const packageNames = {
80
+ models: cliParams.typesPackage,
81
+ client: cliParams.clientPackage,
82
+ };
83
+ const exportComponent = cliParams.component;
84
+ /**
85
+ * Command execution
86
+ */
87
+ console.log(`🟣 Generating client - ${apiName} - ${exportComponent}`);
88
+ if (shouldClean) {
89
+ console.log(`🟠 Cleaning output directory`);
90
+ await promises_1.default.rm(outDir, { recursive: true, force: true });
91
+ }
92
+ await (0, promises_1.mkdir)(outDir, { recursive: true });
93
+ const oas = await getOASFromFile(srcFile);
94
+ await generateClientSDK(oas, outDir, apiName, exportComponent, packageNames);
95
+ console.log(`🟢 Client generated - ${apiName} - ${exportComponent} - ${outDir}`);
96
+ }
97
+ exports.execute = execute;
98
+ /**
99
+ * Methods
100
+ */
101
+ const getOASFromFile = async (jsonFile) => {
102
+ const jsonString = await (0, promises_1.readFile)(jsonFile, "utf8");
103
+ return JSON.parse(jsonString);
104
+ };
105
+ const generateClientSDK = async (oas, targetDir, apiName, exportComponent, packageNames = {}) => {
106
+ const exports = {
107
+ exportCore: false,
108
+ exportServices: false,
109
+ exportModels: false,
110
+ exportHooks: false,
111
+ };
112
+ switch (exportComponent) {
113
+ case "types":
114
+ exports.exportModels = true;
115
+ break;
116
+ case "client":
117
+ exports.exportCore = true;
118
+ exports.exportServices = true;
119
+ break;
120
+ case "hooks":
121
+ exports.exportHooks = true;
122
+ break;
123
+ default:
124
+ exports.exportCore = true;
125
+ exports.exportServices = true;
126
+ exports.exportModels = true;
127
+ exports.exportHooks = true;
128
+ }
129
+ await (0, openapi_typescript_codegen_1.generate)({
130
+ input: oas,
131
+ output: targetDir,
132
+ httpClient: openapi_typescript_codegen_1.HttpClient.AXIOS,
133
+ useOptions: true,
134
+ useUnionTypes: true,
135
+ exportCore: exports.exportCore,
136
+ exportServices: exports.exportServices,
137
+ exportModels: exports.exportModels,
138
+ exportHooks: exports.exportHooks,
139
+ exportSchemas: false,
140
+ indent: openapi_typescript_codegen_1.Indent.SPACE_2,
141
+ postfixServices: "Service",
142
+ postfixModels: "",
143
+ clientName: `Medusa${(0, lodash_1.upperFirst)(apiName)}`,
144
+ request: undefined,
145
+ packageNames,
146
+ });
147
+ };
148
+ //# sourceMappingURL=command-client.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"command-client.js","sourceRoot":"","sources":["../src/command-client.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,gDAAuB;AACvB,qFAK6C;AAC7C,mCAAmC;AACnC,wDAAiD;AAEjD,yCAAyD;AAEzD;;GAEG;AACU,QAAA,WAAW,GAAG,QAAQ,CAAA;AACtB,QAAA,kBAAkB,GAAG,gCAAgC,CAAA;AACrD,QAAA,cAAc,GAAa;IACtC,IAAI,kBAAM,CACR,mBAAmB,EACnB,iEAAiE,CAClE,CAAC,mBAAmB,EAAE;IAEvB,IAAI,kBAAM,CACR,0BAA0B,EAC1B,+BAA+B,CAChC,CAAC,mBAAm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QACjB,UAAU,EAAE,SAAS,IAAA,mBAAU,EAAC,OAAO,CAAC,EAAE;QAC1C,OAAO,EAAE,SAAS;QAClB,YAAY;KACb,CAAC,CAAA;AACJ,CAAC,CAAA"}
@@ -0,0 +1,12 @@
1
+ import { Command, Option, OptionValues } from "commander";
2
+ /**
3
+ * CLI Command declaration
4
+ */
5
+ export declare const commandName = "oas";
6
+ export declare const commandDescription = "Compile full OAS from swagger-inline compliant JSDoc.";
7
+ export declare const commandOptions: Option[];
8
+ export declare function getCommand(): Command;
9
+ /**
10
+ * Main
11
+ */
12
+ export declare function execute(cliParams: OptionValues): Promise<void>;
@@ -0,0 +1,135 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __importDefault = (this && this.__importDefault) || function (mod) {
26
+ return (mod && mod.__esModule) ? mod : { "default": mod };
27
+ };
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ exports.execute = exports.getCommand = exports.commandOptions = exports.commandDescription = exports.commandName = void 0;
30
+ const path = __importStar(require("path"));
31
+ const promises_1 = require("fs/promises");
32
+ const swagger_inline_1 = __importDefault(require("swagger-inline"));
33
+ const openapi_parser_1 = __importDefault(require("@readme/openapi-parser"));
34
+ const commander_1 = require("commander");
35
+ /**
36
+ * Constants
37
+ */
38
+ // Medusa core package directory
39
+ const medusaPackagePath = path.dirname(require.resolve("@medusajs/medusa/package.json"));
40
+ /**
41
+ * CLI Command declaration
42
+ */
43
+ exports.commandName = "oas";
44
+ exports.commandDescription = "Compile full OAS from swagger-inline compliant JSDoc.";
45
+ exports.commandOptions = [
46
+ new commander_1.Option("-t, --type <type>", "API type to compile []")
47
+ .choices(["all", "admin", "store"])
48
+ .default("all"),
49
+ new commander_1.Option("-o, --out-dir <outDir>", "Destination directory to output generated OAS files.").default(process.cwd()),
50
+ new commander_1.Option("-D, --dry-run", "Do not output files."),
51
+ new commander_1.Option("-p, --paths <paths...>", "Additional paths to crawl for OAS JSDoc."),
52
+ new commander_1.Option("-F, --force", "Ignore OAS validation and output OAS files."),
53
+ ];
54
+ function getCommand() {
55
+ const command = new commander_1.Command(exports.commandName);
56
+ command.description(exports.commandDescription);
57
+ for (const opt of exports.commandOptions) {
58
+ command.addOption(opt);
59
+ }
60
+ command.action(async (options) => await execute(options));
61
+ return command;
62
+ }
63
+ exports.getCommand = getCommand;
64
+ /**
65
+ * Main
66
+ */
67
+ async function execute(cliParams) {
68
+ var _a;
69
+ /**
70
+ * Process CLI options
71
+ */
72
+ const dryRun = !!cliParams.dryRun;
73
+ const force = !!cliParams.force;
74
+ const apiTypesToExport = cliParams.type === "all" ? ["store", "admin"] : [cliParams.type];
75
+ const outDir = path.resolve(cliParams.outDir);
76
+ const additionalPaths = ((_a = cliParams.paths) !== null && _a !== void 0 ? _a : []).map((additionalPath) => path.resolve(additionalPath));
77
+ for (const additionalPath of additionalPaths) {
78
+ if (!(await isDirectory(additionalPath))) {
79
+ throw new Error(`--paths must be a directory - ${additionalPath}`);
80
+ }
81
+ }
82
+ /**
83
+ * Command execution
84
+ */
85
+ if (!dryRun) {
86
+ await (0, promises_1.mkdir)(outDir, { recursive: true });
87
+ }
88
+ for (const apiType of apiTypesToExport) {
89
+ console.log(`🟣 Generating OAS - ${apiType}`);
90
+ const oas = await getOASFromCodebase(apiType, additionalPaths);
91
+ await validateOAS(oas, apiType, force);
92
+ if (!dryRun) {
93
+ await exportOASToJSON(oas, apiType, outDir);
94
+ }
95
+ }
96
+ }
97
+ exports.execute = execute;
98
+ /**
99
+ * Methods
100
+ */
101
+ async function getOASFromCodebase(apiType, additionalPaths = []) {
102
+ const gen = await (0, swagger_inline_1.default)([
103
+ path.resolve(medusaPackagePath, "dist", "models"),
104
+ path.resolve(medusaPackagePath, "dist", "types"),
105
+ path.resolve(medusaPackagePath, "dist", "api/middlewares"),
106
+ path.resolve(medusaPackagePath, "dist", `api/routes/${apiType}`),
107
+ ...additionalPaths,
108
+ ], {
109
+ base: path.resolve(medusaPackagePath, "oas", `${apiType}-spec3-base.yaml`),
110
+ format: ".json",
111
+ });
112
+ return await openapi_parser_1.default.parse(JSON.parse(gen));
113
+ }
114
+ async function validateOAS(oas, apiType, force = false) {
115
+ try {
116
+ await openapi_parser_1.default.validate(JSON.parse(JSON.stringify(oas)));
117
+ console.log(`🟢 Valid OAS - ${apiType}`);
118
+ }
119
+ catch (err) {
120
+ console.error(`🔴 Invalid OAS - ${apiType}`, err);
121
+ if (!force) {
122
+ process.exit(1);
123
+ }
124
+ }
125
+ }
126
+ async function exportOASToJSON(oas, apiType, targetDir) {
127
+ const json = JSON.stringify(oas, null, 2);
128
+ const filePath = path.resolve(targetDir, `${apiType}.oas.json`);
129
+ await (0, promises_1.writeFile)(filePath, json);
130
+ console.log(`🔵 Exported OAS - ${apiType} - ${filePath}`);
131
+ }
132
+ async function isDirectory(dirPath) {
133
+ return (await (0, promises_1.lstat)(path.resolve(dirPath))).isDirectory();
134
+ }
135
+ //# sourceMappingURL=command-oas.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file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ChB,SAAiB;IAEjB,MAAM,IAAI,GAAG,IAAI,CAAC,SAAS,CAAC,GAAG,EAAE,IAAI,EAAE,CAAC,CAAC,CAAA;IACzC,MAAM,QAAQ,GAAG,IAAI,CAAC,OAAO,CAAC,SAAS,EAAE,GAAG,OAAO,WAAW,CAAC,CAAA;IAC/D,MAAM,IAAA,oBAAS,EAAC,QAAQ,EAAE,IAAI,CAAC,CAAA;IAC/B,OAAO,CAAC,GAAG,CAAC,qBAAqB,OAAO,MAAM,QAAQ,EAAE,CAAC,CAAA;AAC3D,CAAC;AAED,KAAK,UAAU,WAAW,CAAC,OAAe;IACxC,OAAO,CAAC,MAAM,IAAA,gBAAK,EAAC,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,WAAW,EAAE,CAAA;AAC3D,CAAC"}
@@ -0,0 +1,2 @@
1
+ #! /usr/bin/env node
2
+ export {};
package/dist/index.js ADDED
@@ -0,0 +1,25 @@
1
+ #! /usr/bin/env node
2
+ "use strict";
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ const commander_1 = require("commander");
5
+ const command_oas_1 = require("./command-oas");
6
+ const command_client_1 = require("./command-client");
7
+ const run = async () => {
8
+ const program = new commander_1.Command();
9
+ /**
10
+ * Alias to command-oas.ts
11
+ */
12
+ program.addCommand((0, command_oas_1.getCommand)());
13
+ /**
14
+ * Alias to command-client.ts
15
+ */
16
+ program.addCommand((0, command_client_1.getCommand)());
17
+ /**
18
+ * Run CLI
19
+ */
20
+ await program.parseAsync();
21
+ };
22
+ void (async () => {
23
+ await run();
24
+ })();
25
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAEA,yCAAmC;AACnC,+CAA2D;AAC3D,qDAAiE;AAEjE,MAAM,GAAG,GAAG,KAAK,IAAI,EAAE;IACrB,MAAM,OAAO,GAAG,IAAI,mBAAO,EAAE,CAAA;IAE7B;;OAEG;IACH,OAAO,CAAC,UAAU,CAAC,IAAA,wBAAa,GAAE,CAAC,CAAA;IAEnC;;OAEG;IACH,OAAO,CAAC,UAAU,CAAC,IAAA,2BAAgB,GAAE,CAAC,CAAA;IAEtC;;OAEG;IACH,MAAM,OAAO,CAAC,UAAU,EAAE,CAAA;AAC5B,CAAC,CAAA;AAED,KAAK,CAAC,KAAK,IAAI,EAAE;IACf,MAAM,GAAG,EAAE,CAAA;AACb,CAAC,CAAC,EAAE,CAAA"}
package/package.json ADDED
@@ -0,0 +1,43 @@
1
+ {
2
+ "name": "@medusajs/medusa-oas-cli",
3
+ "version": "0.1.0",
4
+ "description": "OAS CLI",
5
+ "main": "dist/index.js",
6
+ "bin": {
7
+ "medusa-oas": "./dist/index.js"
8
+ },
9
+ "files": [
10
+ "dist"
11
+ ],
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "https://github.com/medusajs/medusa",
15
+ "directory": "packages/oas/medusa-oas-cli"
16
+ },
17
+ "publishConfig": {
18
+ "access": "public"
19
+ },
20
+ "author": "Medusa",
21
+ "license": "MIT",
22
+ "devDependencies": {
23
+ "ts-node": "^10.9.1",
24
+ "typescript": "4.9.5"
25
+ },
26
+ "scripts": {
27
+ "prepare": "cross-env NODE_ENV=production yarn run build",
28
+ "build": "tsc --build",
29
+ "cli": "ts-node src/index.ts",
30
+ "test": "jest --passWithNoTests",
31
+ "test:unit": "jest --passWithNoTests"
32
+ },
33
+ "dependencies": {
34
+ "@medusajs/medusa": "*",
35
+ "@medusajs/openapi-typescript-codegen": "*",
36
+ "@readme/openapi-parser": "^2.4.0",
37
+ "@types/lodash": "^4.14.191",
38
+ "commander": "^10.0.0",
39
+ "lodash": "^4.17.21",
40
+ "openapi3-ts": "^3.1.2",
41
+ "swagger-inline": "^6.1.0"
42
+ }
43
+ }