@openapi-qraft/cli 1.0.0-beta.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 +72 -0
- package/dist/bin.d.ts +2 -0
- package/dist/bin.js +47 -0
- package/dist/bin.js.map +1 -0
- package/dist/lib/fileHeader.d.ts +1 -0
- package/dist/lib/fileHeader.js +5 -0
- package/dist/lib/fileHeader.js.map +1 -0
- package/dist/lib/open-api/OpenAPISchemaType.d.ts +29 -0
- package/dist/lib/open-api/OpenAPISchemaType.js +2 -0
- package/dist/lib/open-api/OpenAPISchemaType.js.map +1 -0
- package/dist/lib/open-api/getContent.d.ts +5 -0
- package/dist/lib/open-api/getContent.js +23 -0
- package/dist/lib/open-api/getContent.js.map +1 -0
- package/dist/lib/open-api/getOperationName.d.ts +6 -0
- package/dist/lib/open-api/getOperationName.js +20 -0
- package/dist/lib/open-api/getOperationName.js.map +1 -0
- package/dist/lib/open-api/getServiceName.d.ts +5 -0
- package/dist/lib/open-api/getServiceName.js +13 -0
- package/dist/lib/open-api/getServiceName.js.map +1 -0
- package/dist/lib/open-api/getServices.d.ts +23 -0
- package/dist/lib/open-api/getServices.js +75 -0
- package/dist/lib/open-api/getServices.js.map +1 -0
- package/dist/lib/open-api/readSchema.d.ts +7 -0
- package/dist/lib/open-api/readSchema.js +15 -0
- package/dist/lib/open-api/readSchema.js.map +1 -0
- package/dist/lib/ts-factory/astToString.d.ts +8 -0
- package/dist/lib/ts-factory/astToString.js +14 -0
- package/dist/lib/ts-factory/astToString.js.map +1 -0
- package/dist/lib/ts-factory/getClientFactory.d.ts +7 -0
- package/dist/lib/ts-factory/getClientFactory.js +60 -0
- package/dist/lib/ts-factory/getClientFactory.js.map +1 -0
- package/dist/lib/ts-factory/getIndexFactory.d.ts +5 -0
- package/dist/lib/ts-factory/getIndexFactory.js +17 -0
- package/dist/lib/ts-factory/getIndexFactory.js.map +1 -0
- package/dist/lib/ts-factory/getServiceFactory.d.ts +10 -0
- package/dist/lib/ts-factory/getServiceFactory.js +119 -0
- package/dist/lib/ts-factory/getServiceFactory.js.map +1 -0
- package/dist/lib/ts-factory/getServiceIndexFactory.d.ts +7 -0
- package/dist/lib/ts-factory/getServiceIndexFactory.js +26 -0
- package/dist/lib/ts-factory/getServiceIndexFactory.js.map +1 -0
- package/dist/writeOpenAPIServices.d.ts +20 -0
- package/dist/writeOpenAPIServices.js +97 -0
- package/dist/writeOpenAPIServices.js.map +1 -0
- package/package.json +70 -0
package/README.md
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
# @openapi-qraft/cli
|
|
2
|
+
|
|
3
|
+
`@openapi-qraft/cli` is a powerful command-line utility designed to streamline the development process by generating
|
|
4
|
+
service declarations and typed React Query interfaces directly from an OpenAPI Schema. With `@openapi-qraft/cli`,
|
|
5
|
+
frontend developers can easily generate typed API clients, ensuring type safety and improving development efficiency
|
|
6
|
+
within React applications.
|
|
7
|
+
|
|
8
|
+
## Features
|
|
9
|
+
|
|
10
|
+
- **Typed React Query Interfaces:** Create typed React Query hooks for seamless and type-safe API requests.
|
|
11
|
+
- **Generate Typed Services:** Automatically generate service declarations from an OpenAPI Schema.
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
npm install -g @openapi-qraft/cli@beta
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Usage
|
|
20
|
+
|
|
21
|
+
```
|
|
22
|
+
Usage: openapi-qraft [options] [input]
|
|
23
|
+
|
|
24
|
+
Arguments:
|
|
25
|
+
input Input OpenAPI Schema file path, URL (json, yml) (default: null)
|
|
26
|
+
|
|
27
|
+
Options:
|
|
28
|
+
-o, --output-dir <path> Output directory for generated services
|
|
29
|
+
--openapi-types-import-path <path> Path to schema types file (.d.ts), eg: "../openapi.d.ts"
|
|
30
|
+
--operation-generics-import-path <path> Path to operation generics file (default: "@openapi-qraft/react")
|
|
31
|
+
--file-header <string> Header to be added to the generated file (eg: /* eslint-disable */)
|
|
32
|
+
-rm, --clean Clean output directory before generating services
|
|
33
|
+
--postfix-services <string> Postfix to be added to the generated service name (eg: Service)
|
|
34
|
+
--explicit-import-extensions All import statements will include explicit `.js` extensions. Ideal for projects using ECMAScript modules.
|
|
35
|
+
-h, --help display help for command
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
### Example
|
|
39
|
+
|
|
40
|
+
To generate services from an OpenAPI schema file:
|
|
41
|
+
|
|
42
|
+
```bash
|
|
43
|
+
openapi-qraft https://example.com/openapi.json --output-dir src/api --openapi-types-import-path ../openapi.d.ts
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
This command generates service declarations and React Query hooks types in the `src/api` directory based on
|
|
47
|
+
the `schema.json` file.
|
|
48
|
+
|
|
49
|
+
## Configuration
|
|
50
|
+
|
|
51
|
+
- **`-o <path>, --output-dir <path>`:** Specify where to output the generated services.
|
|
52
|
+
- **`--openapi-types-import-path <path>`:** Set the path to the schema types definition file to ensure consistent type
|
|
53
|
+
usage (assumed, you already have `openapi.d.ts` as a result of
|
|
54
|
+
the [`openapi-typescript`](https://github.com/drwpow/openapi-typescript) utility).
|
|
55
|
+
- **`--operation-generics-import-path <path>`:** Define the path to the operation generics file, allowing for custom
|
|
56
|
+
operation handling _(optional, default: `@openapi-qraft/react`)_.
|
|
57
|
+
- **`--file-header`:** Add a custom header to each generated file, useful for disabling linting rules or adding file
|
|
58
|
+
comments _(optional)_.
|
|
59
|
+
- **`-rm, --clean`:** Clean the specified output directory services before generating to remove stale files _(
|
|
60
|
+
optional)_.
|
|
61
|
+
- **`--postfix-services`:** Customize the generated service names with a specific postfix _(optional)_.
|
|
62
|
+
- **`--explicit-import-extensions`:** Include explicit `.js` extensions in all import statements. Ideal for projects
|
|
63
|
+
using ECMAScript modules when TypeScript's _--moduleResolution_ is `node16` or `nodenext` _(optional)_.
|
|
64
|
+
|
|
65
|
+
## Contributing
|
|
66
|
+
|
|
67
|
+
Contributions to `@openapi-qraft/cli` are welcome! Please feel free to submit issues, pull requests, or suggest features
|
|
68
|
+
to help improve the utility.
|
|
69
|
+
|
|
70
|
+
## License
|
|
71
|
+
|
|
72
|
+
This project is licensed under the [MIT License](https://opensource.org/license/mit/).
|
package/dist/bin.d.ts
ADDED
package/dist/bin.js
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import c from 'ansi-colors';
|
|
3
|
+
import { program } from 'commander';
|
|
4
|
+
import { fileHeader } from './lib/fileHeader.js';
|
|
5
|
+
import { writeOpenAPIServices } from './writeOpenAPIServices.js';
|
|
6
|
+
program
|
|
7
|
+
.description('Generate services declarations and Typed React Query Interfaces from OpenAPI Schema')
|
|
8
|
+
.argument('[input]', 'Input OpenAPI Schema file path, URL (json, yml)', null)
|
|
9
|
+
.requiredOption('-o, --output-dir <path>', 'Output directory for generated services')
|
|
10
|
+
.requiredOption('--openapi-types-import-path <path>', // todo::specify better param name to avoid confusion with real path
|
|
11
|
+
'Path to schema types file (.d.ts), eg: "../openapi.d.ts"')
|
|
12
|
+
.option('--operation-generics-import-path <path>', 'Path to operation generics file', '@openapi-qraft/react')
|
|
13
|
+
.option('--file-header <string>', 'Header to be added to the generated file (eg: /* eslint-disable */)')
|
|
14
|
+
.option('-rm, --clean', 'Clean output directory before generating services')
|
|
15
|
+
.option('--postfix-services <string>', 'Postfix to be added to the generated service name (eg: Service)')
|
|
16
|
+
.option('--explicit-import-extensions', 'All import statements will include explicit `.js` extensions. Ideal for projects using ECMAScript modules.')
|
|
17
|
+
.action(async (input, args) => {
|
|
18
|
+
const source = input
|
|
19
|
+
? new URL(input, new URL(`file://${process.cwd()}/`))
|
|
20
|
+
: process.stdin;
|
|
21
|
+
if (source === process.stdin && source.isTTY) {
|
|
22
|
+
console.error(c.red('Input file not found or stdin is empty. Please specify `--input` option or pipe OpenAPI Schema to stdin.'));
|
|
23
|
+
process.exit(1);
|
|
24
|
+
}
|
|
25
|
+
await writeOpenAPIServices({
|
|
26
|
+
source,
|
|
27
|
+
serviceImports: {
|
|
28
|
+
operationGenericsImportPath: args.operationGenericsImportPath,
|
|
29
|
+
openapiTypesImportPath: args.openapiTypesImportPath,
|
|
30
|
+
},
|
|
31
|
+
output: {
|
|
32
|
+
dir: args.outputDir,
|
|
33
|
+
clean: args.clean,
|
|
34
|
+
fileHeader: args.fileHeader ?? fileHeader,
|
|
35
|
+
postfixServices: args.postfixServices,
|
|
36
|
+
explicitImportExtensions: args.explicitImportExtensions,
|
|
37
|
+
servicesDirName: 'services',
|
|
38
|
+
},
|
|
39
|
+
}).catch((error) => {
|
|
40
|
+
if (error instanceof Error)
|
|
41
|
+
console.error(c.red(error.message), c.red(error.stack ?? ''));
|
|
42
|
+
console.error(error);
|
|
43
|
+
process.exit(1);
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
program.parse(process.argv);
|
|
47
|
+
//# sourceMappingURL=bin.js.map
|
package/dist/bin.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bin.js","sourceRoot":"","sources":["../src/bin.ts"],"names":[],"mappings":";AACA,OAAO,CAAC,MAAM,aAAa,CAAC;AAC5B,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAEpC,OAAO,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAC;AACjD,OAAO,EAAE,oBAAoB,EAAE,MAAM,2BAA2B,CAAC;AAEjE,OAAO;KACJ,WAAW,CACV,qFAAqF,CACtF;KACA,QAAQ,CAAC,SAAS,EAAE,iDAAiD,EAAE,IAAI,CAAC;KAC5E,cAAc,CACb,yBAAyB,EACzB,yCAAyC,CAC1C;KACA,cAAc,CACb,oCAAoC,EAAE,oEAAoE;AAC1G,0DAA0D,CAC3D;KACA,MAAM,CACL,yCAAyC,EACzC,iCAAiC,EACjC,sBAAsB,CACvB;KACA,MAAM,CACL,wBAAwB,EACxB,qEAAqE,CACtE;KACA,MAAM,CAAC,cAAc,EAAE,mDAAmD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C,CAAC;QACrB,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IAClB,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC;AAEL,OAAO,CAAC,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const fileHeader = "/* generated using @openapi-qraft/cli -- do no edit */\n/* istanbul ignore file */\n/* tslint:disable */\n/* eslint-disable */";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fileHeader.js","sourceRoot":"","sources":["../../src/lib/fileHeader.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,UAAU,GAAG;;;qBAGL,CAAC"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
export type OpenAPISchemaType = {
|
|
2
|
+
paths: {
|
|
3
|
+
[path: string]: {
|
|
4
|
+
[method: string]: {
|
|
5
|
+
description?: string;
|
|
6
|
+
operationId?: string;
|
|
7
|
+
parameters?: Record<string, never>;
|
|
8
|
+
deprecated?: boolean;
|
|
9
|
+
requestBody?: {
|
|
10
|
+
content: {
|
|
11
|
+
[contentType: string]: {
|
|
12
|
+
schema: never;
|
|
13
|
+
};
|
|
14
|
+
};
|
|
15
|
+
};
|
|
16
|
+
responses: {
|
|
17
|
+
[statusCode in number | 'default']: {
|
|
18
|
+
description: string;
|
|
19
|
+
content: {
|
|
20
|
+
[contentType: string]: {
|
|
21
|
+
schema: never;
|
|
22
|
+
};
|
|
23
|
+
};
|
|
24
|
+
};
|
|
25
|
+
};
|
|
26
|
+
};
|
|
27
|
+
};
|
|
28
|
+
};
|
|
29
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"OpenAPISchemaType.js","sourceRoot":"","sources":["../../../src/lib/open-api/OpenAPISchemaType.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
const BASIC_MEDIA_TYPES = [
|
|
2
|
+
"application/json-patch+json",
|
|
3
|
+
"application/json",
|
|
4
|
+
"application/x-www-form-urlencoded",
|
|
5
|
+
"text/json",
|
|
6
|
+
"text/plain",
|
|
7
|
+
"multipart/form-data",
|
|
8
|
+
"multipart/mixed",
|
|
9
|
+
"multipart/related",
|
|
10
|
+
"multipart/batch",
|
|
11
|
+
];
|
|
12
|
+
export const getContentMediaType = (content) => {
|
|
13
|
+
const basicMediaTypeWithSchema = Object.keys(content)
|
|
14
|
+
.filter((mediaType) => {
|
|
15
|
+
const cleanMediaType = mediaType.split(";")[0].trim();
|
|
16
|
+
return BASIC_MEDIA_TYPES.includes(cleanMediaType);
|
|
17
|
+
})
|
|
18
|
+
.find((mediaType) => content[mediaType]?.schema);
|
|
19
|
+
if (basicMediaTypeWithSchema)
|
|
20
|
+
return basicMediaTypeWithSchema;
|
|
21
|
+
return Object.keys(content).find((mediaType) => content[mediaType]?.schema);
|
|
22
|
+
};
|
|
23
|
+
//# sourceMappingURL=getContent.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getContent.js","sourceRoot":"","sources":["../../../src/lib/open-api/getContent.ts"],"names":[],"mappings":"AAAA,MAAM,iBAAiB,GAAG;IACxB,6BAA6B;IAC7B,kBAAkB;IAClB,mCAAmC;IACnC,WAAW;IACX,YAAY;IACZ,qBAAqB;IACrB,iBAAiB;IACjB,mBAAmB;IACnB,iBAAiB;CAClB,CAAC;AAEF,MAAM,CAAC,MAAM,mBAAmB,GAAG,CAAC,OAEnC,EAAE,EAAE;IACH,MAAM,wBAAwB,GAAG,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC;SAClD,MAAM,CAAC,CAAC,SAAS,EAAE,EAAE;QACpB,MAAM,cAAc,GAAG,SAAS,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC;QACtD,OAAO,iBAAiB,CAAC,QAAQ,CAAC,cAAc,CAAC,CAAC;IACpD,CAAC,CAAC;SACD,IAAI,CAAC,CAAC,SAAS,EAAE,EAAE,CAAC,OAAO,CAAC,SAAS,CAAC,EAAE,MAAM,CAAC,CAAC;IAEnD,IAAI,wBAAwB;QAAE,OAAO,wBAAwB,CAAC;IAE9D,OAAO,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,IAAI,CAAC,CAAC,SAAS,EAAE,EAAE,CAAC,OAAO,CAAC,SAAS,CAAC,EAAE,MAAM,CAAC,CAAC;AAC9E,CAAC,CAAC"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Convert the input value to a correct operation (method) classname.
|
|
3
|
+
* This will use the operation ID - if available - and otherwise fallback
|
|
4
|
+
* on a generated name from the URL
|
|
5
|
+
*/
|
|
6
|
+
export declare const getOperationName: (url: string, method: string, operationId?: string) => string;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import camelCase from "camelcase";
|
|
2
|
+
/**
|
|
3
|
+
* Convert the input value to a correct operation (method) classname.
|
|
4
|
+
* This will use the operation ID - if available - and otherwise fallback
|
|
5
|
+
* on a generated name from the URL
|
|
6
|
+
*/
|
|
7
|
+
export const getOperationName = (url, method, operationId) => {
|
|
8
|
+
if (operationId) {
|
|
9
|
+
return camelCase(operationId
|
|
10
|
+
.replace(/^[^a-zA-Z]+/g, "")
|
|
11
|
+
.replace(/[^\w\-]+/g, "-")
|
|
12
|
+
.trim());
|
|
13
|
+
}
|
|
14
|
+
const urlWithoutPlaceholders = url
|
|
15
|
+
.replace(/[^/]*?{api-version}.*?\//g, "")
|
|
16
|
+
.replace(/{(.*?)}/g, "")
|
|
17
|
+
.replace(/\//g, "-");
|
|
18
|
+
return camelCase(`${method}-${urlWithoutPlaceholders}`);
|
|
19
|
+
};
|
|
20
|
+
//# sourceMappingURL=getOperationName.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getOperationName.js","sourceRoot":"","sources":["../../../src/lib/open-api/getOperationName.ts"],"names":[],"mappings":"AAAA,OAAO,SAAS,MAAM,WAAW,CAAC;AAElC;;;;GAIG;AACH,MAAM,CAAC,MAAM,gBAAgB,GAAG,CAC9B,GAAW,EACX,MAAc,EACd,WAAoB,EACZ,EAAE;IACV,IAAI,WAAW,EAAE,CAAC;QAChB,OAAO,SAAS,CACd,WAAW;aACR,OAAO,CAAC,cAAc,EAAE,EAAE,CAAC;aAC3B,OAAO,CAAC,WAAW,EAAE,GAAG,CAAC;aACzB,IAAI,EAAE,CACV,CAAC;IACJ,CAAC;IAED,MAAM,sBAAsB,GAAG,GAAG;SAC/B,OAAO,CAAC,2BAA2B,EAAE,EAAE,CAAC;SACxC,OAAO,CAAC,UAAU,EAAE,EAAE,CAAC;SACvB,OAAO,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC;IAEvB,OAAO,SAAS,CAAC,GAAG,MAAM,IAAI,sBAAsB,EAAE,CAAC,CAAC;AAC1D,CAAC,CAAC"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import camelCase from 'camelcase';
|
|
2
|
+
/**
|
|
3
|
+
* Convert the input value to a correct service name. This converts
|
|
4
|
+
* the input string to PascalCase.
|
|
5
|
+
*/
|
|
6
|
+
export const getServiceName = (value) => {
|
|
7
|
+
const clean = value
|
|
8
|
+
.replace(/^[^a-zA-Z]+/g, '')
|
|
9
|
+
.replace(/[^\w-]+/g, '-')
|
|
10
|
+
.trim();
|
|
11
|
+
return camelCase(clean, { pascalCase: true });
|
|
12
|
+
};
|
|
13
|
+
//# sourceMappingURL=getServiceName.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getServiceName.js","sourceRoot":"","sources":["../../../src/lib/open-api/getServiceName.ts"],"names":[],"mappings":"AAAA,OAAO,SAAS,MAAM,WAAW,CAAC;AAElC;;;GAGG;AACH,MAAM,CAAC,MAAM,cAAc,GAAG,CAAC,KAAa,EAAU,EAAE;IACpD,MAAM,KAAK,GAAG,KAAK;SACd,OAAO,CAAC,cAAc,EAAE,EAAE,CAAC;SAC3B,OAAO,CAAC,UAAU,EAAE,GAAG,CAAC;SACxB,IAAI,EAAE,CAAC;IACZ,OAAO,SAAS,CAAC,KAAK,EAAE,EAAE,UAAU,EAAE,IAAI,EAAE,CAAC,CAAC;AAClD,CAAC,CAAC"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { OpenAPISchemaType } from './OpenAPISchemaType.ts';
|
|
2
|
+
export type Service = {
|
|
3
|
+
name: string;
|
|
4
|
+
variableName: string;
|
|
5
|
+
typeName: string;
|
|
6
|
+
fileBaseName: string;
|
|
7
|
+
operations: ServiceOperation[];
|
|
8
|
+
};
|
|
9
|
+
export type ServiceOperation = {
|
|
10
|
+
method: 'get' | 'put' | 'post' | 'patch' | 'delete' | 'options' | 'head' | 'trace';
|
|
11
|
+
path: string;
|
|
12
|
+
name: string;
|
|
13
|
+
description: string | undefined;
|
|
14
|
+
deprecated: boolean | undefined;
|
|
15
|
+
mediaType: string | undefined;
|
|
16
|
+
errors: Record<string, string | undefined>;
|
|
17
|
+
success: Record<string, string | undefined>;
|
|
18
|
+
parameters: Record<string, never> | undefined;
|
|
19
|
+
};
|
|
20
|
+
export declare const getServices: (openApiJson: OpenAPISchemaType, { postfixServices }?: {
|
|
21
|
+
postfixServices?: string | undefined;
|
|
22
|
+
}) => Service[];
|
|
23
|
+
export declare const supportedMethod: (method: unknown) => method is "get" | "put" | "post" | "patch" | "delete" | "options" | "head" | "trace";
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import camelCase from 'camelcase';
|
|
2
|
+
import * as console from 'console';
|
|
3
|
+
import { getContentMediaType } from './getContent.js';
|
|
4
|
+
import { getOperationName } from './getOperationName.js';
|
|
5
|
+
import { getServiceName } from './getServiceName.js';
|
|
6
|
+
export const getServices = (openApiJson, { postfixServices = 'Service' } = {}) => {
|
|
7
|
+
const paths = openApiJson.paths;
|
|
8
|
+
const services = new Map();
|
|
9
|
+
for (const path in paths) {
|
|
10
|
+
if (!paths.hasOwnProperty(path))
|
|
11
|
+
continue;
|
|
12
|
+
for (const method in paths[path]) {
|
|
13
|
+
if (!paths[path].hasOwnProperty(method))
|
|
14
|
+
continue;
|
|
15
|
+
if (!supportedMethod(method)) {
|
|
16
|
+
console.warn(`The path "${path}" HTTP method "${method}" is not supported`);
|
|
17
|
+
continue;
|
|
18
|
+
}
|
|
19
|
+
const methodOperation = paths[path][method];
|
|
20
|
+
const serviceName = getServiceName(path.split('/')[1]);
|
|
21
|
+
const { success, errors } = Object.entries(methodOperation.responses).reduce((acc, [statusCode, response]) => {
|
|
22
|
+
if (!response.content || typeof response.content !== 'object')
|
|
23
|
+
return acc;
|
|
24
|
+
const statusType = statusCode !== 'default' && // See "default" response https://swagger.io/docs/specification/describing-responses/#default
|
|
25
|
+
Number(statusCode) < 400
|
|
26
|
+
? 'success'
|
|
27
|
+
: 'errors';
|
|
28
|
+
return {
|
|
29
|
+
...acc,
|
|
30
|
+
[statusType]: {
|
|
31
|
+
...acc[statusType],
|
|
32
|
+
[statusCode]: getContentMediaType(response.content),
|
|
33
|
+
},
|
|
34
|
+
};
|
|
35
|
+
}, {});
|
|
36
|
+
if (!services.has(serviceName)) {
|
|
37
|
+
services.set(serviceName, {
|
|
38
|
+
name: camelCase(serviceName),
|
|
39
|
+
variableName: `${camelCase(serviceName, {
|
|
40
|
+
preserveConsecutiveUppercase: false,
|
|
41
|
+
})}${postfixServices}`,
|
|
42
|
+
typeName: `${serviceName}${postfixServices}`,
|
|
43
|
+
fileBaseName: `${serviceName}${postfixServices}`,
|
|
44
|
+
operations: [],
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
services.get(serviceName).operations.push({
|
|
48
|
+
method,
|
|
49
|
+
path,
|
|
50
|
+
errors,
|
|
51
|
+
name: getOperationName(path, method, methodOperation.operationId),
|
|
52
|
+
description: methodOperation.description,
|
|
53
|
+
deprecated: methodOperation.deprecated,
|
|
54
|
+
parameters: methodOperation.parameters,
|
|
55
|
+
mediaType: methodOperation.requestBody?.content
|
|
56
|
+
? getContentMediaType(methodOperation.requestBody.content)
|
|
57
|
+
: undefined,
|
|
58
|
+
success: success,
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return Array.from(services.values());
|
|
63
|
+
};
|
|
64
|
+
export const supportedMethod = (method) => supportedHTTPMethods.includes(method);
|
|
65
|
+
const supportedHTTPMethods = Object.values({
|
|
66
|
+
get: 'get',
|
|
67
|
+
put: 'put',
|
|
68
|
+
post: 'post',
|
|
69
|
+
patch: 'patch',
|
|
70
|
+
delete: 'delete',
|
|
71
|
+
options: 'options',
|
|
72
|
+
head: 'head',
|
|
73
|
+
trace: 'trace',
|
|
74
|
+
});
|
|
75
|
+
//# sourceMappingURL=getServices.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getServices.js","sourceRoot":"","sources":["../../../src/lib/open-api/getServices.ts"],"names":[],"mappings":"AAAA,OAAO,SAAS,MAAM,WAAW,CAAC;AAClC,OAAO,KAAK,OAAO,MAAM,SAAS,CAAC;AAEnC,OAAO,EAAE,mBAAmB,EAAE,MAAM,iBAAiB,CAAC;AACtD,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AACzD,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AA+BrD,MAAM,CAAC,MAAM,WAAW,GAAG,CACzB,WAA8B,EAC9B,EAAE,eAAe,GAAG,SAAS,KAAmC,EAAE,EAClE,EAAE;IACF,MAAM,KAAK,GAAG,WAAW,CAAC,KAAK,CAAC;IAEhC,MAAM,QAAQ,GAAG,IAAI,GAAG,EAAmB,CAAC;IAE5C,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;QACzB,IAAI,CAAC,KAAK,CAAC,cAAc,CAAC,IAAI,CAAC;YAAE,SAAS;QAE1C,KAAK,MAAM,MAAM,IAAI,KAAK,CAAC,IAAI,CAAC,EAAE,CAAC;YACjC,IAAI,CAAC,KAA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|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/// <reference types="node" resolution-mode="require"/>
|
|
2
|
+
/// <reference types="node" resolution-mode="require"/>
|
|
3
|
+
/// <reference types="node" resolution-mode="require"/>
|
|
4
|
+
import type { Readable } from 'node:stream';
|
|
5
|
+
import type { OpenAPI3 } from 'openapi-typescript';
|
|
6
|
+
import { OpenAPISchemaType } from './OpenAPISchemaType.js';
|
|
7
|
+
export declare const readSchema: (source: string | URL | OpenAPI3 | Readable | Buffer) => Promise<OpenAPISchemaType>;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { createConfig } from '@redocly/openapi-core';
|
|
2
|
+
import { validateAndBundle } from 'openapi-typescript/dist/lib/redoc.js';
|
|
3
|
+
export const readSchema = async (source) => {
|
|
4
|
+
const redoc = await createConfig({
|
|
5
|
+
rules: {
|
|
6
|
+
'operation-operationId-unique': { severity: 'error' }, // throw error on duplicate operationIDs
|
|
7
|
+
},
|
|
8
|
+
}, { extends: ['minimal'] });
|
|
9
|
+
const parsed = await validateAndBundle(source, {
|
|
10
|
+
redoc,
|
|
11
|
+
silent: false,
|
|
12
|
+
});
|
|
13
|
+
return parsed;
|
|
14
|
+
};
|
|
15
|
+
//# sourceMappingURL=readSchema.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"readSchema.js","sourceRoot":"","sources":["../../../src/lib/open-api/readSchema.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,uBAAuB,CAAC;AAIrD,OAAO,EAAE,iBAAiB,EAAE,MAAM,sCAAsC,CAAC;AAIzE,MAAM,CAAC,MAAM,UAAU,GAAG,KAAK,EAC7B,MAAmD,EACnD,EAAE;IACF,MAAM,KAAK,GAAG,MAAM,YAAY,CAC9B;QACE,KAAK,EAAE;YACL,8BAA8B,EAAE,EAAE,QAAQ,EAAE,OAAO,EAAE,EAAE,wCAAwC;SAChG;KACF,EACD,EAAE,OAAO,EAAE,CAAC,SAAS,CAAC,EAAE,CACzB,CAAC;IAEF,MAAM,MAAM,GAAG,MAAM,iBAAiB,CAAC,MAAM,EAAE;QAC7C,KAAK;QACL,MAAM,EAAE,KAAK;KACd,CAAC,CAAC;IAEH,OAAO,MAA2B,CAAC;AACrC,CAAC,CAAC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
export interface AstToStringOptions {
|
|
3
|
+
fileName?: string;
|
|
4
|
+
sourceText?: string;
|
|
5
|
+
formatOptions?: ts.PrinterOptions;
|
|
6
|
+
}
|
|
7
|
+
/** Convert TypeScript AST to string */
|
|
8
|
+
export declare function astToString(ast: ts.Node | ts.Node[] | ts.TypeElement | ts.TypeElement[], options?: AstToStringOptions): string;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
/** Convert TypeScript AST to string */
|
|
3
|
+
export function astToString(ast, options) {
|
|
4
|
+
const sourceFile = ts.createSourceFile(options?.fileName ?? "openapi-ts.ts", options?.sourceText ?? "", ts.ScriptTarget.ESNext, false, ts.ScriptKind.TS);
|
|
5
|
+
// @ts-expect-error it’s OK to overwrite statements once
|
|
6
|
+
sourceFile.statements = ts.factory.createNodeArray(Array.isArray(ast) ? ast : [ast]);
|
|
7
|
+
const printer = ts.createPrinter({
|
|
8
|
+
newLine: ts.NewLineKind.LineFeed,
|
|
9
|
+
removeComments: false,
|
|
10
|
+
...options?.formatOptions,
|
|
11
|
+
});
|
|
12
|
+
return printer.printFile(sourceFile);
|
|
13
|
+
}
|
|
14
|
+
//# sourceMappingURL=astToString.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"astToString.js","sourceRoot":"","sources":["../../../src/lib/ts-factory/astToString.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,YAAY,CAAC;AAQ5B,uCAAuC;AACvC,MAAM,UAAU,WAAW,CACzB,GAA4D,EAC5D,OAA4B;IAE5B,MAAM,UAAU,GAAG,EAAE,CAAC,gBAAgB,CACpC,OAAO,EAAE,QAAQ,IAAI,eAAe,EACpC,OAAO,EAAE,UAAU,IAAI,EAAE,EACzB,EAAE,CAAC,YAAY,CAAC,MAAM,EACtB,KAAK,EACL,EAAE,CAAC,UAAU,CAAC,EAAE,CACjB,CAAC;IAEF,wDAAwD;IACxD,UAAU,CAAC,UAAU,GAAG,EAAE,CAAC,OAAO,CAAC,eAAe,CAChD,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CACjC,CAAC;IAEF,MAAM,OAAO,GAAG,EAAE,CAAC,aAAa,CAAC;QAC/B,OAAO,EAAE,EAAE,CAAC,WAAW,CAAC,QAAQ;QAChC,cAAc,EAAE,KAAK;QACrB,GAAG,OAAO,EAAE,aAAa;KAC1B,CAAC,CAAC;IAEH,OAAO,OAAO,CAAC,SAAS,CAAC,UAAU,CAAC,CAAC;AACvC,CAAC"}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import ts from 'typescript';
|
|
2
|
+
export const getClientFactory = (options) => {
|
|
3
|
+
return [
|
|
4
|
+
...getClientImportsFactory(options),
|
|
5
|
+
getCallbacksVariableFactory(),
|
|
6
|
+
getCreateClientFunctionFactory(),
|
|
7
|
+
];
|
|
8
|
+
};
|
|
9
|
+
const getClientImportsFactory = ({ servicesDirName, explicitImportExtensions, }) => {
|
|
10
|
+
const factory = ts.factory;
|
|
11
|
+
return [
|
|
12
|
+
factory.createImportDeclaration(undefined, factory.createImportClause(false, undefined, factory.createNamedImports([
|
|
13
|
+
factory.createImportSpecifier(false, undefined, factory.createIdentifier('qraftAPIClient')),
|
|
14
|
+
factory.createImportSpecifier(false, undefined, factory.createIdentifier('QraftClientOptions')),
|
|
15
|
+
])), factory.createStringLiteral('@openapi-qraft/react'), undefined),
|
|
16
|
+
...serviceCallbacks.map((propertyName) => factory.createImportDeclaration(undefined, factory.createImportClause(false, undefined, factory.createNamedImports([
|
|
17
|
+
factory.createImportSpecifier(false, undefined, factory.createIdentifier(propertyName)),
|
|
18
|
+
])), factory.createStringLiteral(`@openapi-qraft/react/callbacks/${propertyName}`), undefined)),
|
|
19
|
+
factory.createImportDeclaration(undefined, factory.createImportClause(false, undefined, factory.createNamedImports([
|
|
20
|
+
factory.createImportSpecifier(false, undefined, factory.createIdentifier('services')),
|
|
21
|
+
factory.createImportSpecifier(false, undefined, factory.createIdentifier('Services')),
|
|
22
|
+
])), factory.createStringLiteral(`./${servicesDirName}/index${explicitImportExtensions ? '.js' : ''}`), undefined),
|
|
23
|
+
];
|
|
24
|
+
};
|
|
25
|
+
const getCallbacksVariableFactory = () => {
|
|
26
|
+
const factory = ts.factory;
|
|
27
|
+
return factory.createVariableStatement(undefined, factory.createVariableDeclarationList([
|
|
28
|
+
factory.createVariableDeclaration(factory.createIdentifier('callbacks'), undefined, undefined, factory.createObjectLiteralExpression(serviceCallbacks.map((propertyName) => factory.createShorthandPropertyAssignment(factory.createIdentifier(propertyName), undefined)), true)),
|
|
29
|
+
], ts.NodeFlags.Const));
|
|
30
|
+
};
|
|
31
|
+
const getCreateClientFunctionFactory = () => {
|
|
32
|
+
const factory = ts.factory;
|
|
33
|
+
return factory.createFunctionDeclaration([factory.createToken(ts.SyntaxKind.ExportKeyword)], undefined, factory.createIdentifier('createAPIClient'), undefined, [
|
|
34
|
+
factory.createParameterDeclaration(undefined, undefined, factory.createIdentifier('options'), factory.createToken(ts.SyntaxKind.QuestionToken), factory.createTypeReferenceNode(factory.createIdentifier('QraftClientOptions'), undefined), undefined),
|
|
35
|
+
], factory.createTypeReferenceNode(factory.createIdentifier('Services'), undefined), factory.createBlock([
|
|
36
|
+
factory.createReturnStatement(factory.createCallExpression(factory.createIdentifier('qraftAPIClient'), [
|
|
37
|
+
factory.createTypeReferenceNode(factory.createIdentifier('Services'), undefined),
|
|
38
|
+
factory.createTypeQueryNode(factory.createIdentifier('callbacks'), undefined),
|
|
39
|
+
], [
|
|
40
|
+
factory.createIdentifier('services'),
|
|
41
|
+
factory.createIdentifier('callbacks'),
|
|
42
|
+
factory.createIdentifier('options'),
|
|
43
|
+
])),
|
|
44
|
+
], true));
|
|
45
|
+
};
|
|
46
|
+
const serviceCallbacks = [
|
|
47
|
+
'getInfiniteQueryData',
|
|
48
|
+
'getInfiniteQueryKey',
|
|
49
|
+
'getMutationKey',
|
|
50
|
+
'getQueryData',
|
|
51
|
+
'getQueryKey',
|
|
52
|
+
'mutationFn',
|
|
53
|
+
'queryFn',
|
|
54
|
+
'setInfiniteQueryData',
|
|
55
|
+
'setQueryData',
|
|
56
|
+
'useInfiniteQuery',
|
|
57
|
+
'useMutation',
|
|
58
|
+
'useQuery',
|
|
59
|
+
];
|
|
60
|
+
//# sourceMappingURL=getClientFactory.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getClientFactory.js","sourceRoot":"","sources":["../../../src/lib/ts-factory/getClientFactory.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,YAAY,CAAC;AAO5B,MAAM,CAAC,MAAM,gBAAgB,GAAG,CAAC,OAAgB,EAAE,EAAE;IACnD,OAAO;QACL,GAAG,uBAAuB,CAAC,OAAO,CAAC;QACnC,2BAA2B,EAAE;QAC7B,8BAA8B,EAAE;KACjC,CAAC;AACJ,CAAC,CAAC;AAEF,MAAM,uBAAuB,GAAG,CAAC,EAC/B,eAAe,EACf,wBAAwB,GAChB,EAAE,EAAE;IACZ,MAAM,OAAO,GAAG,EAAE,CAAC,OAAO,CAAC;IAE3B,OAAO;QACL,OAAO,CAAC,uBAAuB,CAC7B,SAAS,EACT,OAAO,CAAC,kBAAkB,CACxB,KAAK,EACL,SAAS,EACT,OAAO,CAAC,kBAAkB,CAAC;YACzB,OAAO,CAAC,qBAAqB,CAC3B,KAAK,EACL,SAAS,EACT,OAAO,CAAC,gBAAgB,CAAC,gBAAgB,CAAC,CAC3C;YACD,OAAO,CAAC,qBAAqB,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AC,WAAW,CAAC,EACrC,SAAS,CACV;SACF,EACD;YACE,OAAO,CAAC,gBAAgB,CAAC,UAAU,CAAC;YACpC,OAAO,CAAC,gBAAgB,CAAC,WAAW,CAAC;YACrC,OAAO,CAAC,gBAAgB,CAAC,SAAS,CAAC;SACpC,CACF,CACF;KACF,EACD,IAAI,CACL,CACF,CAAC;AACJ,CAAC,CAAC;AAEF,MAAM,gBAAgB,GAAG;IACvB,sBAAsB;IACtB,qBAAqB;IACrB,gBAAgB;IAChB,cAAc;IACd,aAAa;IACb,YAAY;IACZ,SAAS;IACT,sBAAsB;IACtB,cAAc;IACd,kBAAkB;IAClB,aAAa;IACb,UAAU;CACF,CAAC"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import ts from 'typescript';
|
|
2
|
+
export const getIndexFactory = ({ servicesDirName, explicitImportExtensions, }) => {
|
|
3
|
+
const factory = ts.factory;
|
|
4
|
+
const importExtension = explicitImportExtensions ? '.js' : '';
|
|
5
|
+
return [
|
|
6
|
+
factory.createExportDeclaration(undefined, false, factory.createNamedExports([
|
|
7
|
+
factory.createExportSpecifier(false, undefined, factory.createIdentifier('services')),
|
|
8
|
+
]), factory.createStringLiteral(`./${servicesDirName}/index${importExtension}`), undefined),
|
|
9
|
+
factory.createExportDeclaration(undefined, true, factory.createNamedExports([
|
|
10
|
+
factory.createExportSpecifier(false, undefined, factory.createIdentifier('Services')),
|
|
11
|
+
]), factory.createStringLiteral(`./${servicesDirName}/index${importExtension}`), undefined),
|
|
12
|
+
factory.createExportDeclaration(undefined, false, factory.createNamedExports([
|
|
13
|
+
factory.createExportSpecifier(false, undefined, factory.createIdentifier('createAPIClient')),
|
|
14
|
+
]), factory.createStringLiteral(`./create-api-client${importExtension}`), undefined),
|
|
15
|
+
];
|
|
16
|
+
};
|
|
17
|
+
//# sourceMappingURL=getIndexFactory.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getIndexFactory.js","sourceRoot":"","sources":["../../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import { ServiceOperation } from '../open-api/getServices.js';
|
|
3
|
+
export type ServiceImportsFactoryOptions = {
|
|
4
|
+
openapiTypesImportPath: string;
|
|
5
|
+
operationGenericsImportPath: string;
|
|
6
|
+
};
|
|
7
|
+
export declare const getServiceFactory: (service: {
|
|
8
|
+
typeName: string;
|
|
9
|
+
variableName: string;
|
|
10
|
+
}, operations: ServiceOperation[], { openapiTypesImportPath, operationGenericsImportPath, }: ServiceImportsFactoryOptions) => (ts.ImportDeclaration | ts.VariableStatement | ts.InterfaceDeclaration)[];
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import ts from 'typescript';
|
|
2
|
+
const SERVICE_OPERATION_QUERY = 'ServiceOperationQuery';
|
|
3
|
+
const SERVICE_OPERATION_MUTATION = 'ServiceOperationMutation';
|
|
4
|
+
const factory = ts.factory;
|
|
5
|
+
export const getServiceFactory = (service, operations, { openapiTypesImportPath, operationGenericsImportPath, }) => {
|
|
6
|
+
return [
|
|
7
|
+
getOpenAPISchemaImportsFactory(openapiTypesImportPath),
|
|
8
|
+
getServiceOperationGenericsPathImportsFactory(operationGenericsImportPath, operations),
|
|
9
|
+
getServiceInterfaceFactory(service, operations),
|
|
10
|
+
getServiceVariableFactory(service, operations),
|
|
11
|
+
];
|
|
12
|
+
};
|
|
13
|
+
const getOpenAPISchemaImportsFactory = (schemaTypesPath) => {
|
|
14
|
+
const factory = ts.factory;
|
|
15
|
+
return factory.createImportDeclaration(undefined, factory.createImportClause(true, undefined, factory.createNamedImports([
|
|
16
|
+
factory.createImportSpecifier(false, undefined, factory.createIdentifier('paths')),
|
|
17
|
+
])), factory.createStringLiteral(schemaTypesPath));
|
|
18
|
+
};
|
|
19
|
+
const getServiceOperationGenericsPathImportsFactory = (operationGenericsPath, operations) => {
|
|
20
|
+
const factory = ts.factory;
|
|
21
|
+
const queryMethods = ['get', 'head', 'options'];
|
|
22
|
+
return factory.createImportDeclaration(undefined, factory.createImportClause(true, undefined, factory.createNamedImports([
|
|
23
|
+
operations.some((operation) => queryMethods.some((method) => method === operation.method))
|
|
24
|
+
? factory.createImportSpecifier(false, undefined, factory.createIdentifier('ServiceOperationQuery'))
|
|
25
|
+
: null,
|
|
26
|
+
operations.some((operation) => !queryMethods.some((method) => method === operation.method))
|
|
27
|
+
? factory.createImportSpecifier(false, undefined, factory.createIdentifier('ServiceOperationMutation'))
|
|
28
|
+
: null,
|
|
29
|
+
].filter((node) => Boolean(node)))), factory.createStringLiteral(operationGenericsPath));
|
|
30
|
+
};
|
|
31
|
+
const getServiceInterfaceFactory = ({ typeName }, operations) => {
|
|
32
|
+
return factory.createInterfaceDeclaration([factory.createToken(ts.SyntaxKind.ExportKeyword)], factory.createIdentifier(typeName), undefined, undefined, operations.map(getServiceInterfaceOperationFactory));
|
|
33
|
+
};
|
|
34
|
+
const getServiceInterfaceOperationFactory = (operation) => {
|
|
35
|
+
const node = factory.createPropertySignature(undefined, factory.createIdentifier(operation.name), undefined, factory.createTypeReferenceNode(factory.createIdentifier(operation.method === 'get'
|
|
36
|
+
? SERVICE_OPERATION_QUERY
|
|
37
|
+
: SERVICE_OPERATION_MUTATION), [
|
|
38
|
+
getOperationSchemaFactory(operation),
|
|
39
|
+
operation.method !== 'get' ? getOperationBodyFactory(operation) : null,
|
|
40
|
+
getOperationResponseFactory(operation, 'success'),
|
|
41
|
+
getOperationParametersFactory(operation),
|
|
42
|
+
getOperationResponseFactory(operation, 'errors'),
|
|
43
|
+
].filter((node) => Boolean(node))));
|
|
44
|
+
const comment = createMultilineComment([
|
|
45
|
+
operation.deprecated ? '@deprecated' : null,
|
|
46
|
+
operation.description ? `@description ${operation.description}` : null,
|
|
47
|
+
].filter((comment) => Boolean(comment)));
|
|
48
|
+
if (comment)
|
|
49
|
+
ts.addSyntheticLeadingComment(node, ts.SyntaxKind.MultiLineCommentTrivia, comment, true);
|
|
50
|
+
return node;
|
|
51
|
+
};
|
|
52
|
+
const getOperationSchemaFactory = (operation) => {
|
|
53
|
+
return factory.createTypeLiteralNode([
|
|
54
|
+
factory.createPropertySignature(undefined, factory.createIdentifier('method'), undefined, factory.createLiteralTypeNode(factory.createStringLiteral(operation.method))),
|
|
55
|
+
factory.createPropertySignature(undefined, factory.createIdentifier('url'), undefined, factory.createLiteralTypeNode(factory.createStringLiteral(operation.path))),
|
|
56
|
+
operation.mediaType
|
|
57
|
+
? factory.createPropertySignature(undefined, factory.createIdentifier('mediaType'), undefined, factory.createLiteralTypeNode(factory.createStringLiteral(operation.mediaType)))
|
|
58
|
+
: null,
|
|
59
|
+
].filter((node) => Boolean(node)));
|
|
60
|
+
};
|
|
61
|
+
const getOperationResponseFactory = (operation, type) => {
|
|
62
|
+
if (!operation[type])
|
|
63
|
+
return factory.createKeywordTypeNode(ts.SyntaxKind.UnknownKeyword);
|
|
64
|
+
const responses = Object.entries(operation[type]);
|
|
65
|
+
if (!responses.length)
|
|
66
|
+
return factory.createKeywordTypeNode(ts.SyntaxKind.UnknownKeyword);
|
|
67
|
+
return factory.createUnionTypeNode(responses.map(([statusCode, mediaType]) => {
|
|
68
|
+
if (!mediaType)
|
|
69
|
+
return factory.createKeywordTypeNode(ts.SyntaxKind.UnknownKeyword);
|
|
70
|
+
return factory.createIndexedAccessTypeNode(factory.createIndexedAccessTypeNode(factory.createIndexedAccessTypeNode(factory.createIndexedAccessTypeNode(factory.createIndexedAccessTypeNode(factory.createIndexedAccessTypeNode(factory.createTypeReferenceNode(factory.createIdentifier('paths'), undefined), factory.createLiteralTypeNode(factory.createStringLiteral(operation.path))), factory.createLiteralTypeNode(factory.createStringLiteral(operation.method))), factory.createLiteralTypeNode(factory.createStringLiteral('responses'))), factory.createLiteralTypeNode(factory.createStringLiteral(statusCode))), factory.createLiteralTypeNode(factory.createStringLiteral('content'))), factory.createLiteralTypeNode(factory.createStringLiteral(mediaType)));
|
|
71
|
+
}));
|
|
72
|
+
};
|
|
73
|
+
const getOperationBodyFactory = (operation) => {
|
|
74
|
+
if (!operation.mediaType)
|
|
75
|
+
return factory.createKeywordTypeNode(ts.SyntaxKind.UndefinedKeyword);
|
|
76
|
+
return factory.createIndexedAccessTypeNode(factory.createIndexedAccessTypeNode(factory.createTypeReferenceNode(factory.createIdentifier('NonNullable'), [
|
|
77
|
+
factory.createIndexedAccessTypeNode(factory.createIndexedAccessTypeNode(factory.createIndexedAccessTypeNode(factory.createTypeReferenceNode(factory.createIdentifier('paths'), undefined), factory.createLiteralTypeNode(factory.createStringLiteral(operation.path))), factory.createLiteralTypeNode(factory.createStringLiteral(operation.method))), factory.createLiteralTypeNode(factory.createStringLiteral('requestBody'))),
|
|
78
|
+
]),
|
|
79
|
+
// todo::Add optional NonNullable inference, see `POST /entities/{entity_id}/documents`
|
|
80
|
+
factory.createLiteralTypeNode(factory.createStringLiteral('content'))), factory.createLiteralTypeNode(factory.createStringLiteral(operation.mediaType)));
|
|
81
|
+
};
|
|
82
|
+
const getOperationParametersFactory = (operation) => {
|
|
83
|
+
if (!operation.parameters)
|
|
84
|
+
return factory.createKeywordTypeNode(ts.SyntaxKind.UndefinedKeyword);
|
|
85
|
+
return factory.createIndexedAccessTypeNode(factory.createIndexedAccessTypeNode(factory.createIndexedAccessTypeNode(factory.createTypeReferenceNode(factory.createIdentifier('paths'), undefined), factory.createLiteralTypeNode(factory.createStringLiteral(operation.path))), factory.createLiteralTypeNode(factory.createStringLiteral(operation.method))), factory.createLiteralTypeNode(factory.createStringLiteral('parameters')));
|
|
86
|
+
};
|
|
87
|
+
const getServiceVariableFactory = ({ typeName, variableName }, operations) => {
|
|
88
|
+
return factory.createVariableStatement([factory.createToken(ts.SyntaxKind.ExportKeyword)], factory.createVariableDeclarationList([
|
|
89
|
+
factory.createVariableDeclaration(factory.createIdentifier(variableName), undefined, getServiceVariableTypeFactory({ typeName }), factory.createAsExpression(factory.createObjectLiteralExpression(operations.map(getServiceVariablePropertyFactory), true), factory.createTypeReferenceNode(factory.createIdentifier('const'), undefined))),
|
|
90
|
+
], ts.NodeFlags.Const));
|
|
91
|
+
};
|
|
92
|
+
const getServiceVariableTypeFactory = ({ typeName }) => {
|
|
93
|
+
return factory.createMappedTypeNode(undefined, factory.createTypeParameterDeclaration(undefined, factory.createIdentifier('key'), factory.createTypeOperatorNode(ts.SyntaxKind.KeyOfKeyword, factory.createTypeReferenceNode(factory.createIdentifier(typeName), undefined)), undefined), undefined, undefined, factory.createTypeReferenceNode(factory.createIdentifier('Pick'), [
|
|
94
|
+
factory.createIndexedAccessTypeNode(factory.createTypeReferenceNode(factory.createIdentifier(typeName), undefined), factory.createTypeReferenceNode(factory.createIdentifier('key'), undefined)),
|
|
95
|
+
factory.createLiteralTypeNode(factory.createStringLiteral('schema')),
|
|
96
|
+
]), undefined);
|
|
97
|
+
};
|
|
98
|
+
const getServiceVariablePropertyFactory = (operation) => {
|
|
99
|
+
return factory.createPropertyAssignment(factory.createIdentifier(operation.name), factory.createObjectLiteralExpression([
|
|
100
|
+
factory.createPropertyAssignment(factory.createIdentifier('schema'), factory.createObjectLiteralExpression([
|
|
101
|
+
factory.createPropertyAssignment(factory.createIdentifier('method'), factory.createStringLiteral(operation.method)),
|
|
102
|
+
factory.createPropertyAssignment(factory.createIdentifier('url'), factory.createStringLiteral(operation.path)),
|
|
103
|
+
operation.mediaType
|
|
104
|
+
? factory.createPropertyAssignment(factory.createIdentifier('mediaType'), factory.createStringLiteral(operation.mediaType))
|
|
105
|
+
: null,
|
|
106
|
+
].filter((node) => Boolean(node)), true)),
|
|
107
|
+
], true));
|
|
108
|
+
};
|
|
109
|
+
const createMultilineComment = (comment) => {
|
|
110
|
+
const output = comment.flatMap((line) => line.includes('\n') ? line.split('\n') : line);
|
|
111
|
+
if (output.length) {
|
|
112
|
+
const text = output.length === 1
|
|
113
|
+
? `* ${output.join('\n')} `
|
|
114
|
+
: `*\n * ${output.join('\n * ')}\n `;
|
|
115
|
+
return text.replace(/\*\//g, '*\\/'); // prevent inner comments from leaking
|
|
116
|
+
}
|
|
117
|
+
return '';
|
|
118
|
+
};
|
|
119
|
+
//# sourceMappingURL=getServiceFactory.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getServiceFactory.js","sourceRoot":"","sources":["../../../src/lib/ts-factory/getServiceFactory.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,YAAY,CAAC;AAI5B,MAAM,uBAAuB,GAAG,uBAAuB,CAAC;AACxD,MAAM,0BAA0B,GAAG,0BAA0B,CAAC;AAE9D,MAAM,OAAO,GAAG,EAAE,CAAC,OAAO,CAAC;AAO3B,MAAM,CAAC,MAAM,iBAAiB,GAAG,CAC/B,OAAmD,EACnD,UAA8B,EAC9B,EACE,sBAAsB,EACtB,2BAA2B,GACE,EAC/B,EAAE;IACF,OAAO;QACL,8BAA8B,CAAC,sBAAsB,CAAC;QACtD,6CAA6C,CAC3C,2BAA2B,EAC3B,UAAU,CACX;QACD,0BAA0B,CAAC,OAAO,EAAE,UAAU,CAAC;QAC/C,yBAAyB,CAAC,OAAO,EAAE,UAAU,CAAC;KAC/C,CAAC;AACJ,CAAC,CAAC;AAEF,MAAM,8BAA8B,GAAG,CAAC,eAAuB,EAAE,EAAE;IACjE,MAAM,OAAO,GAAG,EAAE,CAAC,OAAO,CAAC;IAE3B,OAAO,OAAO,CAAC,uBAAuB,CACpC,SAAS,EACT,OAAO,CAAC,kBAAkB,CACxB,IAAI,EACJ,SAAS,EACT,OAAO,CAAC,kBAAkB,CAAC;QACzB,OAAO,CAAC,qBAAqB,CAC3B,KAAK,EACL,SAAS,EACT,OAAO,CAAC,gBAAgB,CAAC,OAAO,CAAC,CAClC;KACF,CAAC,CACH,EACD,OAAO,CAAC,mBAAmB,CAAC,eAAe,CAAC,CAC7C,CAAC;AACJ,CAAC,CAAC;AAEF,MAAM,6CAA6C,GAAG,CACpD,qBAA6B,EAC7B,UAA8B,EAC9B,EAAE;IACF,MAAM,OAAO,GAAG,EAAE,CAAC,OAAO,CAAC;IAE3B,MAAM,YAAY,GAAG,CAAC,KAAK,EAAE,MAAM,EAAE,SAAS,CAA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AC,CAAC;AAEF,MAAM,sBAAsB,GAAG,CAAC,OAAiB,EAAE,EAAE;IACnD,MAAM,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC,IAAI,EAAE,EAAE,CACtC,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,CAC9C,CAAC;IAEF,IAAI,MAAM,CAAC,MAAM,EAAE,CAAC;QAClB,MAAM,IAAI,GACR,MAAM,CAAC,MAAM,KAAK,CAAC;YACjB,CAAC,CAAC,KAAK,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG;YAC3B,CAAC,CAAC,SAAS,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC;QAEzC,OAAO,IAAI,CAAC,OAAO,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC,CAAC,sCAAsC;IAC9E,CAAC;IAED,OAAO,EAAE,CAAC;AACZ,CAAC,CAAC"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import ts from 'typescript';
|
|
2
|
+
import { Service } from '../open-api/getServices.js';
|
|
3
|
+
type Options = {
|
|
4
|
+
explicitImportExtensions: boolean;
|
|
5
|
+
};
|
|
6
|
+
export declare const getServiceIndexFactory: (services: Service[], options: Options) => (ts.ImportDeclaration | ts.VariableStatement | ts.TypeAliasDeclaration)[];
|
|
7
|
+
export {};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import ts from 'typescript';
|
|
2
|
+
export const getServiceIndexFactory = (services, options) => {
|
|
3
|
+
return [
|
|
4
|
+
...getServicesImportsFactory(services, options),
|
|
5
|
+
getServiceIndexInterfaceFactory(services),
|
|
6
|
+
getServiceIndexVariableFactory(services),
|
|
7
|
+
];
|
|
8
|
+
};
|
|
9
|
+
const getServiceIndexInterfaceFactory = (services) => {
|
|
10
|
+
const factory = ts.factory;
|
|
11
|
+
return factory.createTypeAliasDeclaration([factory.createToken(ts.SyntaxKind.ExportKeyword)], factory.createIdentifier('Services'), undefined, factory.createTypeLiteralNode(services.map(({ name, typeName }) => factory.createPropertySignature(undefined, factory.createIdentifier(name), undefined, factory.createTypeReferenceNode(factory.createIdentifier(typeName), undefined)))));
|
|
12
|
+
};
|
|
13
|
+
const getServiceIndexVariableFactory = (services) => {
|
|
14
|
+
const factory = ts.factory;
|
|
15
|
+
return factory.createVariableStatement([factory.createToken(ts.SyntaxKind.ExportKeyword)], factory.createVariableDeclarationList([
|
|
16
|
+
factory.createVariableDeclaration(factory.createIdentifier('services'), undefined, undefined, factory.createAsExpression(factory.createObjectLiteralExpression(services.map(({ name, variableName }) => factory.createPropertyAssignment(factory.createIdentifier(name), factory.createIdentifier(variableName))), true), factory.createTypeReferenceNode(factory.createIdentifier('const'), undefined))),
|
|
17
|
+
], ts.NodeFlags.Const));
|
|
18
|
+
};
|
|
19
|
+
const getServicesImportsFactory = (services, { explicitImportExtensions }) => {
|
|
20
|
+
const factory = ts.factory;
|
|
21
|
+
return services.map(({ variableName, typeName, fileBaseName }) => factory.createImportDeclaration(undefined, factory.createImportClause(false, undefined, factory.createNamedImports([
|
|
22
|
+
factory.createImportSpecifier(false, undefined, factory.createIdentifier(typeName)),
|
|
23
|
+
factory.createImportSpecifier(false, undefined, factory.createIdentifier(variableName)),
|
|
24
|
+
])), factory.createStringLiteral(`./${fileBaseName}${explicitImportExtensions ? '.js' : ''}`)));
|
|
25
|
+
};
|
|
26
|
+
//# sourceMappingURL=getServiceIndexFactory.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getServiceIndexFactory.js","sourceRoot":"","sources":["../../../src/lib/ts-factory/getServiceIndexFactory.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,YAAY,CAAC;AAM5B,MAAM,CAAC,MAAM,sBAAsB,GAAG,CACpC,QAAmB,EACnB,OAAgB,EAChB,EAAE;IACF,OAAO;QACL,GAAG,yBAAyB,CAAC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B,OAAO,CAAC,gBAAgB,CAAC,IAAI,CAAC,EAC9B,OAAO,CAAC,gBAAgB,CAAC,YAAY,CAAC,CACvC,CACF,EACD,IAAI,CACL,EACD,OAAO,CAAC,uBAAuB,CAC7B,OAAO,CAAC,gBAAgB,CAAC,OAAO,CAAC,EACjC,SAAS,CACV,CACF,CACF;KACF,EACD,EAAE,CAAC,SAAS,CAAC,KAAK,CACnB,CACF,CAAC;AACJ,CAAC,CAAC;AAEF,MAAM,yBAAyB,GAAG,CAChC,QAAmB,EACnB,EAAE,wBAAwB,EAAW,EACrC,EAAE;IACF,MAAM,OAAO,GAAG,EAAE,CAAC,OAAO,CAAC;IAE3B,OAAO,QAAQ,CAAC,GAAG,CAAC,CAAC,EAAE,YAAY,EAAE,QAAQ,EAAE,YAAY,EAAE,EAAE,EAAE,CAC/D,OAAO,CAAC,uBAAuB,CAC7B,SAAS,EACT,OAAO,CAAC,kBAAkB,CACxB,KAAK,EACL,SAAS,EACT,OAAO,CAAC,kBAAkB,CAAC;QACzB,OAAO,CAAC,qBAAqB,CAC3B,KAAK,EACL,SAAS,EACT,OAAO,CAAC,gBAAgB,CAAC,QAAQ,CAAC,CACnC;QACD,OAAO,CAAC,qBAAqB,CAC3B,KAAK,EACL,SAAS,EACT,OAAO,CAAC,gBAAgB,CAAC,YAAY,CAAC,CACvC;KACF,CAAC,CACH,EACD,OAAO,CAAC,mBAAmB,CACzB,KAAK,YAAY,GAAG,wBAAwB,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,EAAE,CAC5D,CACF,CACF,CAAC;AACJ,CAAC,CAAC"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/// <reference types="node" resolution-mode="require"/>
|
|
2
|
+
/// <reference types="node" resolution-mode="require"/>
|
|
3
|
+
/// <reference types="node" resolution-mode="require"/>
|
|
4
|
+
import { Readable } from 'node:stream';
|
|
5
|
+
import { OpenAPI3 } from 'openapi-typescript';
|
|
6
|
+
import { ServiceImportsFactoryOptions } from './lib/ts-factory/getServiceFactory.js';
|
|
7
|
+
type OutputOptions = {
|
|
8
|
+
fileHeader?: string;
|
|
9
|
+
dir: string;
|
|
10
|
+
clean: boolean;
|
|
11
|
+
postfixServices?: string;
|
|
12
|
+
explicitImportExtensions?: boolean;
|
|
13
|
+
servicesDirName: string;
|
|
14
|
+
};
|
|
15
|
+
export declare const writeOpenAPIServices: ({ source, serviceImports, output, }: {
|
|
16
|
+
source: string | URL | OpenAPI3 | Readable | Buffer;
|
|
17
|
+
serviceImports: ServiceImportsFactoryOptions;
|
|
18
|
+
output: OutputOptions;
|
|
19
|
+
}) => Promise<void>;
|
|
20
|
+
export {};
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import c from 'ansi-colors';
|
|
2
|
+
import fs from 'node:fs';
|
|
3
|
+
import { resolve } from 'node:path';
|
|
4
|
+
import ora from 'ora';
|
|
5
|
+
import { getServices } from './lib/open-api/getServices.js';
|
|
6
|
+
import { readSchema } from './lib/open-api/readSchema.js';
|
|
7
|
+
import { astToString } from './lib/ts-factory/astToString.js';
|
|
8
|
+
import { getClientFactory } from './lib/ts-factory/getClientFactory.js';
|
|
9
|
+
import { getIndexFactory } from './lib/ts-factory/getIndexFactory.js';
|
|
10
|
+
import { getServiceFactory, } from './lib/ts-factory/getServiceFactory.js';
|
|
11
|
+
import { getServiceIndexFactory } from './lib/ts-factory/getServiceIndexFactory.js';
|
|
12
|
+
export const writeOpenAPIServices = async ({ source, serviceImports, output, }) => {
|
|
13
|
+
const schema = await readSchema(source);
|
|
14
|
+
const services = getServices(schema, output);
|
|
15
|
+
await writeServices(services, serviceImports, output);
|
|
16
|
+
await writeServiceIndex(services, output);
|
|
17
|
+
await writeClient(output);
|
|
18
|
+
await writeIndex(output);
|
|
19
|
+
};
|
|
20
|
+
const writeServices = async (services, serviceImports, output) => {
|
|
21
|
+
const servicesDir = resolve(output.dir, output.servicesDirName);
|
|
22
|
+
if (output.clean)
|
|
23
|
+
await fs.promises.rm(servicesDir, {
|
|
24
|
+
recursive: true,
|
|
25
|
+
force: true,
|
|
26
|
+
});
|
|
27
|
+
await fs.promises.mkdir(servicesDir, { recursive: true });
|
|
28
|
+
const spinner = ora(`Generating services`).start();
|
|
29
|
+
for (const service of services) {
|
|
30
|
+
const { operations, name, typeName, variableName, fileBaseName } = service;
|
|
31
|
+
spinner.text = `Generating ${c.magenta(name)} service`;
|
|
32
|
+
try {
|
|
33
|
+
const code = getFileHeader(output) +
|
|
34
|
+
astToString(getServiceFactory({
|
|
35
|
+
typeName,
|
|
36
|
+
variableName,
|
|
37
|
+
}, operations, serviceImports));
|
|
38
|
+
await fs.promises.writeFile(resolve(servicesDir, `${fileBaseName}.ts`), code);
|
|
39
|
+
}
|
|
40
|
+
catch (error) {
|
|
41
|
+
spinner.fail(c.redBright(`Error occurred during ${c.magenta(name)} service generation`));
|
|
42
|
+
throw error;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
spinner.succeed(c.green('Services has been generated'));
|
|
46
|
+
};
|
|
47
|
+
const writeServiceIndex = async (services, output) => {
|
|
48
|
+
const spinner = ora('Generating services index').start();
|
|
49
|
+
try {
|
|
50
|
+
const code = getFileHeader(output) +
|
|
51
|
+
astToString(getServiceIndexFactory(services, {
|
|
52
|
+
explicitImportExtensions: Boolean(output.explicitImportExtensions),
|
|
53
|
+
}));
|
|
54
|
+
await fs.promises.writeFile(resolve(output.dir, output.servicesDirName, 'index.ts'), code);
|
|
55
|
+
}
|
|
56
|
+
catch (error) {
|
|
57
|
+
spinner.fail(c.redBright('Error occurred during services index generation'));
|
|
58
|
+
throw error;
|
|
59
|
+
}
|
|
60
|
+
spinner.succeed(c.green('Services index has been generated'));
|
|
61
|
+
};
|
|
62
|
+
const writeClient = async (output) => {
|
|
63
|
+
const spinner = ora('Generating client').start();
|
|
64
|
+
try {
|
|
65
|
+
const code = getFileHeader(output) +
|
|
66
|
+
astToString(getClientFactory({
|
|
67
|
+
servicesDirName: output.servicesDirName,
|
|
68
|
+
explicitImportExtensions: Boolean(output.explicitImportExtensions),
|
|
69
|
+
}));
|
|
70
|
+
await fs.promises.writeFile(resolve(output.dir, 'create-api-client.ts'), code);
|
|
71
|
+
}
|
|
72
|
+
catch (error) {
|
|
73
|
+
spinner.fail(c.redBright('Error occurred during client generation'));
|
|
74
|
+
throw error;
|
|
75
|
+
}
|
|
76
|
+
spinner.succeed(c.green('Client has been generated'));
|
|
77
|
+
};
|
|
78
|
+
const writeIndex = async (output) => {
|
|
79
|
+
const spinner = ora('Generating index').start();
|
|
80
|
+
try {
|
|
81
|
+
const code = getFileHeader(output) +
|
|
82
|
+
astToString(getIndexFactory({
|
|
83
|
+
servicesDirName: output.servicesDirName,
|
|
84
|
+
explicitImportExtensions: Boolean(output.explicitImportExtensions),
|
|
85
|
+
}));
|
|
86
|
+
await fs.promises.writeFile(resolve(output.dir, 'index.ts'), code);
|
|
87
|
+
}
|
|
88
|
+
catch (error) {
|
|
89
|
+
spinner.fail(c.redBright('Error occurred during index generation'));
|
|
90
|
+
throw error;
|
|
91
|
+
}
|
|
92
|
+
spinner.succeed(c.green('Index has been generated'));
|
|
93
|
+
};
|
|
94
|
+
const getFileHeader = ({ fileHeader }) => {
|
|
95
|
+
return fileHeader && `${fileHeader}\n`;
|
|
96
|
+
};
|
|
97
|
+
//# sourceMappingURL=writeOpenAPIServices.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"writeOpenAPIServices.js","sourceRoot":"","sources":["../src/writeOpenAPIServices.ts"],"names":[],"mappings":"AAAA,OAAO,CAAC,MAAM,aAAa,CAAC;AAC5B,OAAO,EAAE,MAAM,SAAS,CAAC;AACzB,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAGpC,OAAO,GAAG,MAAM,KAAK,CAAC;AAEtB,OAAO,EAAE,WAAW,EAAW,MAAM,+BAA+B,CAAC;AACrE,OAAO,EAAE,UAAU,EAAE,MAAM,8BAA8B,CAAC;AAC1D,OAA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,MAAM,CAAC;YACrB,WAAW,CACT,eAAe,CAAC;gBACd,eAAe,EAAE,MAAM,CAAC,eAAe;gBACvC,wBAAwB,EAAE,OAAO,CAAC,MAAM,CAAC,wBAAwB,CAAC;aACnE,CAAC,CACH,CAAC;QAEJ,MAAM,EAAE,CAAC,QAAQ,CAAC,SAAS,CAAC,OAAO,CAAC,MAAM,CAAC,GAAG,EAAE,UAAU,CAAC,EAAE,IAAI,CAAC,CAAC;IACrE,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,SAAS,CAAC,wCAAwC,CAAC,CAAC,CAAC;QAEpE,MAAM,KAAK,CAAC;IACd,CAAC;IAED,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC,KAAK,CAAC,0BAA0B,CAAC,CAAC,CAAC;AACvD,CAAC,CAAC;AAEF,MAAM,aAAa,GAAG,CAAC,EAAE,UAAU,EAAqC,EAAE,EAAE;IAC1E,OAAO,UAAU,IAAI,GAAG,UAAU,IAAI,CAAC;AACzC,CAAC,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@openapi-qraft/cli",
|
|
3
|
+
"version": "1.0.0-beta.0",
|
|
4
|
+
"description": "CLI for generating typed Tanstack Query React Hooks and services from OpenAPI Schemas, improving type safety in React apps",
|
|
5
|
+
"scripts": {
|
|
6
|
+
"build": "rimraf dist/ && tsc --project tsconfig.build.json",
|
|
7
|
+
"dev": "yarn build --watch",
|
|
8
|
+
"test": "vitest run"
|
|
9
|
+
},
|
|
10
|
+
"type": "module",
|
|
11
|
+
"bin": {
|
|
12
|
+
"openapi-qraft": "./dist/bin.js"
|
|
13
|
+
},
|
|
14
|
+
"dependencies": {
|
|
15
|
+
"ansi-colors": "^4.1.3",
|
|
16
|
+
"camelcase": "^8.0.0",
|
|
17
|
+
"commander": "^11.1.0",
|
|
18
|
+
"openapi-typescript": "7.0.0-next.7",
|
|
19
|
+
"ora": "^8.0.1",
|
|
20
|
+
"typescript": "^5.3.3",
|
|
21
|
+
"yaml": "^2.3.4"
|
|
22
|
+
},
|
|
23
|
+
"devDependencies": {
|
|
24
|
+
"@types/node": "^18.19.4",
|
|
25
|
+
"rimraf": "^5.0.5",
|
|
26
|
+
"vitest": "^1.1.3"
|
|
27
|
+
},
|
|
28
|
+
"files": [
|
|
29
|
+
"dist"
|
|
30
|
+
],
|
|
31
|
+
"exports": {
|
|
32
|
+
"./package.json": "./package.json",
|
|
33
|
+
"./*": "./dist/*.js"
|
|
34
|
+
},
|
|
35
|
+
"typesVersions": {
|
|
36
|
+
"*": {
|
|
37
|
+
"*": [
|
|
38
|
+
"dist/*"
|
|
39
|
+
]
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
"repository": {
|
|
43
|
+
"type": "git",
|
|
44
|
+
"url": "git+https://github.com/OpenAPI-Qraft/openapi-qraft.git",
|
|
45
|
+
"directory": "packages/cli"
|
|
46
|
+
},
|
|
47
|
+
"bugs": {
|
|
48
|
+
"url": "https://github.com/OpenAPI-Qraft/openapi-qraft/issues"
|
|
49
|
+
},
|
|
50
|
+
"keywords": [
|
|
51
|
+
"openapi",
|
|
52
|
+
"swagger",
|
|
53
|
+
"rest",
|
|
54
|
+
"api",
|
|
55
|
+
"oapi_3",
|
|
56
|
+
"oapi_3_1",
|
|
57
|
+
"typescript",
|
|
58
|
+
"ts",
|
|
59
|
+
"dts",
|
|
60
|
+
"codegen",
|
|
61
|
+
"generation",
|
|
62
|
+
"fetch",
|
|
63
|
+
"react",
|
|
64
|
+
"hooks",
|
|
65
|
+
"TanStack Query"
|
|
66
|
+
],
|
|
67
|
+
"publishConfig": {
|
|
68
|
+
"access": "public"
|
|
69
|
+
}
|
|
70
|
+
}
|