@7nohe/openapi-react-query-codegen 0.1.4 → 0.2.1
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 +19 -8
- package/dist/package.json +8 -9
- package/dist/src/cli.js +25 -0
- package/dist/src/constants.js +6 -0
- package/dist/src/createUseQuery.js +37 -32
- package/dist/src/generate.js +18 -8
- package/dist/src/print.js +8 -4
- package/package.json +8 -9
- package/dist/bin/cli.js +0 -20
package/README.md
CHANGED
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
## Features
|
|
6
6
|
|
|
7
7
|
- Supports generation of custom react hooks that use React Query's `useQuery` and `useMutation` hooks
|
|
8
|
+
- Supports generation of query keys for query caching
|
|
8
9
|
- Supports the option to use pure TypeScript clients generated by [OpenAPI Typescript Codegen](https://github.com/ferdikoomen/openapi-typescript-codegen)
|
|
9
10
|
|
|
10
11
|
## Install
|
|
@@ -18,15 +19,20 @@ $ npm install -D @7nohe/openapi-react-query-codegen
|
|
|
18
19
|
```
|
|
19
20
|
$ openapi-rq --help
|
|
20
21
|
|
|
21
|
-
|
|
22
|
+
Usage: openapi-rq [options]
|
|
22
23
|
|
|
23
|
-
|
|
24
|
+
Generate React Query code based on OpenAPI
|
|
24
25
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
26
|
+
Options:
|
|
27
|
+
-V, --version output the version number
|
|
28
|
+
-i, --input <value> OpenAPI specification, can be a path, url or string content (required)
|
|
29
|
+
-o, --output <value> Output directory (default: "openapi")
|
|
30
|
+
-c, --client <value> HTTP client to generate [fetch, xhr, node, axios, angular] (default: "fetch")
|
|
31
|
+
--exportSchemas <value> Write schemas to disk (default: false)
|
|
32
|
+
--indent <value> Indentation options [4, 2, tabs] (default: "4")
|
|
33
|
+
--postfix <value> Service name postfix (default: "Service")
|
|
34
|
+
--request <value> Path to custom request file
|
|
35
|
+
-h, --help display help for command
|
|
30
36
|
```
|
|
31
37
|
|
|
32
38
|
## Example Usage
|
|
@@ -76,8 +82,13 @@ You can also use pure TS clients.
|
|
|
76
82
|
```tsx
|
|
77
83
|
import { useQuery } from "@tanstack/react-query";
|
|
78
84
|
import { PetService } from '../openapi/requests/services/PetService';
|
|
85
|
+
import {
|
|
86
|
+
usePetServiceFindPetsByStatusKey,
|
|
87
|
+
} from "../openapi/queries";
|
|
88
|
+
|
|
79
89
|
function App() {
|
|
80
|
-
|
|
90
|
+
// You can still use the auto-generated query key
|
|
91
|
+
const { data } = useQuery([usePetServiceFindPetsByStatusKey], () => {
|
|
81
92
|
// Do something here
|
|
82
93
|
|
|
83
94
|
return PetService.findPetsByStatus(['available']);
|
package/dist/package.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@7nohe/openapi-react-query-codegen",
|
|
3
|
-
"version": "0.1
|
|
3
|
+
"version": "0.2.1",
|
|
4
4
|
"description": "OpenAPI React Query Codegen",
|
|
5
5
|
"bin": {
|
|
6
|
-
"openapi-rq": "dist/
|
|
6
|
+
"openapi-rq": "dist/src/cli.js"
|
|
7
7
|
},
|
|
8
8
|
"scripts": {
|
|
9
9
|
"build": "tsc -p tsconfig.json",
|
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
},
|
|
14
14
|
"repository": {
|
|
15
15
|
"type": "git",
|
|
16
|
-
"url": "https://github.com/7nohe/openapi-react-query-codegen.git"
|
|
16
|
+
"url": "git+https://github.com/7nohe/openapi-react-query-codegen.git"
|
|
17
17
|
},
|
|
18
18
|
"homepage": "https://github.com/7nohe/openapi-react-query-codegen",
|
|
19
19
|
"bugs": "https://github.com/7nohe/openapi-react-query-codegen/issues",
|
|
@@ -32,14 +32,13 @@
|
|
|
32
32
|
"author": "Daiki Urata (@7nohe)",
|
|
33
33
|
"license": "MIT",
|
|
34
34
|
"dependencies": {
|
|
35
|
-
"commander": "^9.4.0"
|
|
36
|
-
},
|
|
37
|
-
"devDependencies": {
|
|
38
|
-
"@types/glob": "^7.2.0",
|
|
39
|
-
"@types/node": "^18.0.6",
|
|
35
|
+
"commander": "^9.4.0",
|
|
40
36
|
"glob": "^8.0.3",
|
|
41
|
-
"openapi-types": "^12.0.0",
|
|
42
37
|
"openapi-typescript-codegen": "^0.23.0",
|
|
43
38
|
"typescript": "^4.7.4"
|
|
39
|
+
},
|
|
40
|
+
"devDependencies": {
|
|
41
|
+
"@types/glob": "^7.2.0",
|
|
42
|
+
"@types/node": "^18.0.6"
|
|
44
43
|
}
|
|
45
44
|
}
|
package/dist/src/cli.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
"use strict";
|
|
3
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
4
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
5
|
+
};
|
|
6
|
+
exports.__esModule = true;
|
|
7
|
+
var generate_1 = require("./generate");
|
|
8
|
+
var commander_1 = require("commander");
|
|
9
|
+
var package_json_1 = __importDefault(require("../package.json"));
|
|
10
|
+
var program = new commander_1.Command();
|
|
11
|
+
program
|
|
12
|
+
.name("openapi-rq")
|
|
13
|
+
.version(package_json_1["default"].version)
|
|
14
|
+
.description("Generate React Query code based on OpenAPI")
|
|
15
|
+
.requiredOption("-i, --input <value>", "OpenAPI specification, can be a path, url or string content (required)")
|
|
16
|
+
.option("-o, --output <value>", "Output directory", "openapi")
|
|
17
|
+
.option("-c, --client <value>", "HTTP client to generate [fetch, xhr, node, axios, angular]", "fetch")
|
|
18
|
+
.option("--exportSchemas <value>", "Write schemas to disk", false)
|
|
19
|
+
.option("--indent <value>", "Indentation options [4, 2, tabs]", "4")
|
|
20
|
+
.option("--postfix <value>", "Service name postfix", "Service")
|
|
21
|
+
.option("--request <value>", "Path to custom request file")
|
|
22
|
+
.parse();
|
|
23
|
+
var options = program.opts();
|
|
24
|
+
console.log("Generating React Query code using OpenApi file ".concat(options.output));
|
|
25
|
+
(0, generate_1.generate)(options);
|
|
@@ -42,40 +42,45 @@ var createUseQuery = function (node, className, method) {
|
|
|
42
42
|
return typescript_1["default"].factory.createPropertySignature(undefined, typescript_1["default"].factory.createIdentifier(param.name.getText(node)), undefined, param.type);
|
|
43
43
|
})), undefined));
|
|
44
44
|
}
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
45
|
+
var customHookName = "use".concat(className).concat((0, common_1.capitalizeFirstLetter)(methodName));
|
|
46
|
+
var queryKey = "".concat(customHookName, "Key");
|
|
47
|
+
return [
|
|
48
|
+
typescript_1["default"].factory.createVariableStatement([typescript_1["default"].factory.createModifier(typescript_1["default"].SyntaxKind.ExportKeyword)], typescript_1["default"].factory.createVariableDeclarationList([typescript_1["default"].factory.createVariableDeclaration(typescript_1["default"].factory.createIdentifier(queryKey), undefined, undefined, typescript_1["default"].factory.createStringLiteral("".concat(className).concat((0, common_1.capitalizeFirstLetter)(methodName))))], typescript_1["default"].NodeFlags.Const)),
|
|
49
|
+
typescript_1["default"].factory.createVariableStatement([typescript_1["default"].factory.createModifier(typescript_1["default"].SyntaxKind.ExportKeyword)], typescript_1["default"].factory.createVariableDeclarationList([
|
|
50
|
+
typescript_1["default"].factory.createVariableDeclaration(typescript_1["default"].factory.createIdentifier(customHookName), undefined, undefined, typescript_1["default"].factory.createArrowFunction(undefined, undefined, __spreadArray(__spreadArray([], __read(requestParam), false), [
|
|
51
|
+
typescript_1["default"].factory.createParameterDeclaration(undefined, undefined, undefined, typescript_1["default"].factory.createIdentifier("queryKey"), undefined, typescript_1["default"].factory.createArrayTypeNode(typescript_1["default"].factory.createKeywordTypeNode(typescript_1["default"].SyntaxKind.StringKeyword)), typescript_1["default"].factory.createArrayLiteralExpression([], false)),
|
|
52
|
+
typescript_1["default"].factory.createParameterDeclaration(undefined, undefined, undefined, typescript_1["default"].factory.createIdentifier("options"), typescript_1["default"].factory.createToken(typescript_1["default"].SyntaxKind.QuestionToken), typescript_1["default"].factory.createTypeReferenceNode(typescript_1["default"].factory.createIdentifier("Omit"), [
|
|
53
|
+
typescript_1["default"].factory.createTypeReferenceNode(typescript_1["default"].factory.createIdentifier("UseQueryOptions"), [
|
|
54
|
+
typescript_1["default"].factory.createTypeReferenceNode(typescript_1["default"].factory.createIdentifier("Awaited"), [
|
|
55
|
+
typescript_1["default"].factory.createTypeReferenceNode(typescript_1["default"].factory.createIdentifier("ReturnType"), [
|
|
56
|
+
typescript_1["default"].factory.createTypeQueryNode(typescript_1["default"].factory.createQualifiedName(typescript_1["default"].factory.createIdentifier(className), typescript_1["default"].factory.createIdentifier(methodName)), undefined),
|
|
57
|
+
]),
|
|
53
58
|
]),
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
+
typescript_1["default"].factory.createKeywordTypeNode(typescript_1["default"].SyntaxKind.UnknownKeyword),
|
|
60
|
+
typescript_1["default"].factory.createTypeReferenceNode(typescript_1["default"].factory.createIdentifier("Awaited"), [
|
|
61
|
+
typescript_1["default"].factory.createTypeReferenceNode(typescript_1["default"].factory.createIdentifier("ReturnType"), [
|
|
62
|
+
typescript_1["default"].factory.createTypeQueryNode(typescript_1["default"].factory.createQualifiedName(typescript_1["default"].factory.createIdentifier(className), typescript_1["default"].factory.createIdentifier(methodName)), undefined),
|
|
63
|
+
]),
|
|
59
64
|
]),
|
|
65
|
+
typescript_1["default"].factory.createArrayTypeNode(typescript_1["default"].factory.createKeywordTypeNode(typescript_1["default"].SyntaxKind.StringKeyword)),
|
|
66
|
+
]),
|
|
67
|
+
typescript_1["default"].factory.createUnionTypeNode([
|
|
68
|
+
typescript_1["default"].factory.createLiteralTypeNode(typescript_1["default"].factory.createStringLiteral("queryKey")),
|
|
69
|
+
typescript_1["default"].factory.createLiteralTypeNode(typescript_1["default"].factory.createStringLiteral("queryFn")),
|
|
70
|
+
typescript_1["default"].factory.createLiteralTypeNode(typescript_1["default"].factory.createStringLiteral("initialData")),
|
|
60
71
|
]),
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
typescript_1["default"].factory.
|
|
64
|
-
typescript_1["default"].factory.
|
|
65
|
-
typescript_1["default"].factory.
|
|
66
|
-
|
|
67
|
-
]),
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
typescript_1["default"].factory.createArrowFunction(undefined, undefined, [], undefined, typescript_1["default"].factory.createToken(typescript_1["default"].SyntaxKind.EqualsGreaterThanToken), typescript_1["default"].factory.createCallExpression(typescript_1["default"].factory.createPropertyAccessExpression(typescript_1["default"].factory.createIdentifier(className), typescript_1["default"].factory.createIdentifier(methodName)), undefined, method.parameters.map(function (param) {
|
|
75
|
-
return typescript_1["default"].factory.createIdentifier(param.name.getText(node));
|
|
76
|
-
}))),
|
|
77
|
-
typescript_1["default"].factory.createIdentifier("options"),
|
|
78
|
-
]))),
|
|
79
|
-
], typescript_1["default"].NodeFlags.Const));
|
|
72
|
+
]), undefined),
|
|
73
|
+
], false), undefined, typescript_1["default"].factory.createToken(typescript_1["default"].SyntaxKind.EqualsGreaterThanToken), typescript_1["default"].factory.createCallExpression(typescript_1["default"].factory.createIdentifier("useQuery"), undefined, [
|
|
74
|
+
typescript_1["default"].factory.createArrayLiteralExpression([
|
|
75
|
+
typescript_1["default"].factory.createIdentifier(queryKey),
|
|
76
|
+
typescript_1["default"].factory.createSpreadElement(typescript_1["default"].factory.createIdentifier("queryKey"))
|
|
77
|
+
], false),
|
|
78
|
+
typescript_1["default"].factory.createArrowFunction(undefined, undefined, [], undefined, typescript_1["default"].factory.createToken(typescript_1["default"].SyntaxKind.EqualsGreaterThanToken), typescript_1["default"].factory.createCallExpression(typescript_1["default"].factory.createPropertyAccessExpression(typescript_1["default"].factory.createIdentifier(className), typescript_1["default"].factory.createIdentifier(methodName)), undefined, method.parameters.map(function (param) {
|
|
79
|
+
return typescript_1["default"].factory.createIdentifier(param.name.getText(node));
|
|
80
|
+
}))),
|
|
81
|
+
typescript_1["default"].factory.createIdentifier("options"),
|
|
82
|
+
]))),
|
|
83
|
+
], typescript_1["default"].NodeFlags.Const))
|
|
84
|
+
];
|
|
80
85
|
};
|
|
81
86
|
exports.createUseQuery = createUseQuery;
|
package/dist/src/generate.js
CHANGED
|
@@ -1,4 +1,15 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __assign = (this && this.__assign) || function () {
|
|
3
|
+
__assign = Object.assign || function(t) {
|
|
4
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
+
s = arguments[i];
|
|
6
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
+
t[p] = s[p];
|
|
8
|
+
}
|
|
9
|
+
return t;
|
|
10
|
+
};
|
|
11
|
+
return __assign.apply(this, arguments);
|
|
12
|
+
};
|
|
2
13
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
14
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
15
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
@@ -44,19 +55,18 @@ var openapi_typescript_codegen_1 = require("openapi-typescript-codegen");
|
|
|
44
55
|
var print_1 = require("./print");
|
|
45
56
|
var path_1 = __importDefault(require("path"));
|
|
46
57
|
var createSource_1 = require("./createSource");
|
|
58
|
+
var constants_1 = require("./constants");
|
|
47
59
|
function generate(options) {
|
|
60
|
+
var _a;
|
|
48
61
|
return __awaiter(this, void 0, void 0, function () {
|
|
49
62
|
var openApiOutputPath, source;
|
|
50
|
-
return __generator(this, function (
|
|
51
|
-
switch (
|
|
63
|
+
return __generator(this, function (_b) {
|
|
64
|
+
switch (_b.label) {
|
|
52
65
|
case 0:
|
|
53
|
-
openApiOutputPath = path_1["default"].join(options.
|
|
54
|
-
return [4 /*yield*/, (0, openapi_typescript_codegen_1.generate)({
|
|
55
|
-
input: options.path,
|
|
56
|
-
output: openApiOutputPath
|
|
57
|
-
})];
|
|
66
|
+
openApiOutputPath = path_1["default"].join((_a = options.output) !== null && _a !== void 0 ? _a : constants_1.defaultOutputPath, constants_1.requestsOutputPath);
|
|
67
|
+
return [4 /*yield*/, (0, openapi_typescript_codegen_1.generate)(__assign(__assign({}, options), { httpClient: options.client, output: openApiOutputPath }))];
|
|
58
68
|
case 1:
|
|
59
|
-
|
|
69
|
+
_b.sent();
|
|
60
70
|
source = (0, createSource_1.createSource)(openApiOutputPath);
|
|
61
71
|
(0, print_1.print)(source, options);
|
|
62
72
|
return [2 /*return*/];
|
package/dist/src/print.js
CHANGED
|
@@ -6,16 +6,20 @@ exports.__esModule = true;
|
|
|
6
6
|
exports.print = void 0;
|
|
7
7
|
var fs_1 = __importDefault(require("fs"));
|
|
8
8
|
var path_1 = __importDefault(require("path"));
|
|
9
|
+
var constants_1 = require("./constants");
|
|
9
10
|
function printGeneratedTS(result, options) {
|
|
10
|
-
var
|
|
11
|
+
var _a;
|
|
12
|
+
var dir = path_1["default"].join((_a = options.output) !== null && _a !== void 0 ? _a : constants_1.defaultOutputPath, constants_1.queriesOutputPath);
|
|
11
13
|
if (!fs_1["default"].existsSync(dir)) {
|
|
12
14
|
fs_1["default"].mkdirSync(dir, { recursive: true });
|
|
13
15
|
}
|
|
14
|
-
fs_1["default"].writeFileSync(path_1["default"].join(dir,
|
|
16
|
+
fs_1["default"].writeFileSync(path_1["default"].join(dir, "index.ts"), result);
|
|
15
17
|
}
|
|
16
18
|
function print(result, options) {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
+
var _a;
|
|
20
|
+
var outputPath = (_a = options.output) !== null && _a !== void 0 ? _a : constants_1.defaultOutputPath;
|
|
21
|
+
if (!fs_1["default"].existsSync(outputPath)) {
|
|
22
|
+
fs_1["default"].mkdirSync(outputPath);
|
|
19
23
|
}
|
|
20
24
|
printGeneratedTS(result, options);
|
|
21
25
|
}
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@7nohe/openapi-react-query-codegen",
|
|
3
|
-
"version": "0.1
|
|
3
|
+
"version": "0.2.1",
|
|
4
4
|
"description": "OpenAPI React Query Codegen",
|
|
5
5
|
"bin": {
|
|
6
|
-
"openapi-rq": "dist/
|
|
6
|
+
"openapi-rq": "dist/src/cli.js"
|
|
7
7
|
},
|
|
8
8
|
"repository": {
|
|
9
9
|
"type": "git",
|
|
10
|
-
"url": "https://github.com/7nohe/openapi-react-query-codegen.git"
|
|
10
|
+
"url": "git+https://github.com/7nohe/openapi-react-query-codegen.git"
|
|
11
11
|
},
|
|
12
12
|
"homepage": "https://github.com/7nohe/openapi-react-query-codegen",
|
|
13
13
|
"bugs": "https://github.com/7nohe/openapi-react-query-codegen/issues",
|
|
@@ -26,16 +26,15 @@
|
|
|
26
26
|
"author": "Daiki Urata (@7nohe)",
|
|
27
27
|
"license": "MIT",
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"commander": "^9.4.0"
|
|
30
|
-
},
|
|
31
|
-
"devDependencies": {
|
|
32
|
-
"@types/glob": "^7.2.0",
|
|
33
|
-
"@types/node": "^18.0.6",
|
|
29
|
+
"commander": "^9.4.0",
|
|
34
30
|
"glob": "^8.0.3",
|
|
35
|
-
"openapi-types": "^12.0.0",
|
|
36
31
|
"openapi-typescript-codegen": "^0.23.0",
|
|
37
32
|
"typescript": "^4.7.4"
|
|
38
33
|
},
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"@types/glob": "^7.2.0",
|
|
36
|
+
"@types/node": "^18.0.6"
|
|
37
|
+
},
|
|
39
38
|
"scripts": {
|
|
40
39
|
"build": "tsc -p tsconfig.json",
|
|
41
40
|
"preview": "npm run build && npm -C examples/react-app run generate:api",
|
package/dist/bin/cli.js
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
"use strict";
|
|
3
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
4
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
5
|
-
};
|
|
6
|
-
exports.__esModule = true;
|
|
7
|
-
var generate_1 = require("../src/generate");
|
|
8
|
-
var commander_1 = require("commander");
|
|
9
|
-
var package_json_1 = __importDefault(require("../package.json"));
|
|
10
|
-
var program = new commander_1.Command();
|
|
11
|
-
program
|
|
12
|
-
.name('openapi-rq')
|
|
13
|
-
.version(package_json_1["default"].version)
|
|
14
|
-
.description("Generate React Query code based on OpenAPI")
|
|
15
|
-
.requiredOption("-p, --path <path>", "Path to OpenAPI file")
|
|
16
|
-
.option("-o, --output-dir [directory]", "Directory to output the generated package", "openapi")
|
|
17
|
-
.parse();
|
|
18
|
-
var options = program.opts();
|
|
19
|
-
console.log("Generating React Query code using OpenApi file ".concat(options.path));
|
|
20
|
-
(0, generate_1.generate)(options);
|