rastack 0.0.16 → 0.0.19
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/CHANGELOG.md +43874 -43013
- package/dist/.prettierrc +7 -7
- package/dist/compile/analyze.js +19 -11
- package/dist/compile/index.js +26 -17
- package/dist/compile/manifest.js +2 -3
- package/dist/compile/openapi.js +5 -8
- package/dist/compile/program.js +19 -10
- package/dist/csv-schema.js +5 -6
- package/dist/define/index.js +2 -2
- package/dist/entity-generation/delete-method.js +8 -10
- package/dist/entity-generation/form.js +80 -82
- package/dist/entity-generation/get-method.js +35 -36
- package/dist/entity-generation/imports.js +24 -20
- package/dist/entity-generation/sync-method.d.ts +1 -1
- package/dist/entity-generation/sync-method.js +45 -46
- package/dist/entity-generation/update-method.js +9 -10
- package/dist/rad-wasm-build.d.ts +1 -1
- package/dist/rad-wasm-build.js +15 -6
- package/dist/rad.js +9 -2
- package/dist/schema/camel-to-pastel.js +1 -2
- package/dist/schema/capitalise-first-letter.js +1 -2
- package/dist/schema/extract-response.js +3 -4
- package/dist/schema/fetch-schema.js +4 -16
- package/dist/schema/remove-non-model-paths.js +1 -2
- package/dist/schema/to-camel-case.js +1 -2
- package/dist/schema/to-pastel-case.js +1 -2
- package/dist/schema-convert.js +22 -33
- package/dist/schema-entities.js +177 -189
- package/dist/schema-fetch.js +46 -63
- package/dist/schema-full.js +23 -36
- package/dist/schema-index.js +16 -27
- package/dist/schema-params.js +11 -22
- package/dist/seed.js +95 -97
- package/hooks/form/form.ts +207 -207
- package/hooks/form/index.ts +8 -8
- package/hooks/form/interfaces.ts +217 -217
- package/hooks/form/structure.ts +39 -39
- package/hooks/form/validate-schema.ts +49 -49
- package/hooks/index.ts +3 -3
- package/hooks/query/api.ts +42 -42
- package/hooks/query/delete.ts +45 -45
- package/hooks/query/fetch.ts +48 -48
- package/hooks/query/index.ts +21 -21
- package/hooks/query/interfaces.ts +111 -111
- package/hooks/query/list.ts +286 -286
- package/hooks/query/update.ts +88 -88
- package/hooks/query/url.ts +31 -31
- package/hooks/real-time/index.ts +1 -1
- package/hooks/real-time/pusher.ts +43 -43
- package/jest.config.cjs +6 -6
- package/package.json +57 -57
- package/provider/index.ts +13 -13
- package/provider/provider.tsx +187 -187
- package/provider/types.ts +114 -114
- package/provider/warehouse.ts +167 -167
- package/provider/wasm.ts +124 -124
- package/runtime.ts +3 -3
- package/src/.prettierrc +7 -7
- package/src/compile/analyze.ts +224 -224
- package/src/compile/index.ts +86 -86
- package/src/compile/manifest.ts +10 -10
- package/src/compile/model.ts +69 -69
- package/src/compile/openapi.ts +266 -266
- package/src/compile/program.ts +40 -40
- package/src/csv-schema.ts +187 -187
- package/src/define/index.ts +162 -162
- package/src/entity-generation/delete-method.ts +37 -37
- package/src/entity-generation/form.ts +169 -169
- package/src/entity-generation/get-method.ts +80 -80
- package/src/entity-generation/imports.ts +110 -110
- package/src/entity-generation/sync-method.ts +222 -222
- package/src/entity-generation/update-method.ts +38 -38
- package/src/index.ts +77 -77
- package/src/rad-compile.ts +88 -88
- package/src/rad-wasm-build.ts +102 -102
- package/src/rad.ts +101 -101
- package/src/scan.ts +89 -89
- package/src/schema/camel-to-pastel.ts +3 -3
- package/src/schema/capitalise-first-letter.ts +3 -3
- package/src/schema/extract-response.ts +38 -38
- package/src/schema/fetch-schema.ts +12 -12
- package/src/schema/remove-non-model-paths.ts +13 -13
- package/src/schema/to-camel-case.ts +7 -7
- package/src/schema/to-pastel-case.ts +6 -6
- package/src/schema-convert.ts +61 -61
- package/src/schema-entities.ts +401 -401
- package/src/schema-fetch.ts +82 -82
- package/src/schema-full.ts +38 -38
- package/src/schema-index.ts +29 -29
- package/src/schema-params.ts +116 -116
- package/src/seed.ts +297 -297
- package/sync/engine.ts +392 -392
- package/sync/hooks.ts +450 -450
- package/sync/index.ts +8 -8
- package/sync/persistence.ts +237 -237
- package/sync/provider.tsx +91 -91
- package/sync/registry.ts +36 -36
- package/sync/store.ts +126 -126
- package/sync/transactions.ts +300 -300
- package/sync/types.ts +94 -94
- package/test/compile.spec.ts +192 -192
- package/test/csv-schema.spec.ts +143 -143
- package/test/schema-entities.spec.ts +688 -688
- package/tsconfig.json +24 -24
- package/types.ts +18 -18
package/src/schema-fetch.ts
CHANGED
|
@@ -1,82 +1,82 @@
|
|
|
1
|
-
import { readdirSync, readFileSync, writeFileSync } from "fs";
|
|
2
|
-
import { join } from "path";
|
|
3
|
-
import axios, { AxiosResponse } from "axios";
|
|
4
|
-
|
|
5
|
-
interface OpenAPISchema {
|
|
6
|
-
components: { schemas: Record<string, any> };
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
async function fetchSchema(
|
|
10
|
-
url: string,
|
|
11
|
-
schemaPath: string,
|
|
12
|
-
): Promise<OpenAPISchema> {
|
|
13
|
-
const response: AxiosResponse<OpenAPISchema> = await axios.get(url);
|
|
14
|
-
writeFileSync(schemaPath, JSON.stringify(response.data, null, 2));
|
|
15
|
-
return response.data;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
async function generateFiles(
|
|
19
|
-
basePath: string,
|
|
20
|
-
components: { schemas: Record<string, any> },
|
|
21
|
-
): Promise<void> {
|
|
22
|
-
const schemas = components.schemas;
|
|
23
|
-
Object.keys(schemas).forEach((key) => {
|
|
24
|
-
const schema = schemas[key];
|
|
25
|
-
let schemaKey = key;
|
|
26
|
-
if (key.endsWith("Enum")) {
|
|
27
|
-
schemaKey = key.replace("Enum", "");
|
|
28
|
-
schemaKey = `E${schemaKey}`;
|
|
29
|
-
} else {
|
|
30
|
-
schemaKey = `I${schemaKey}`;
|
|
31
|
-
}
|
|
32
|
-
const schemaFolderPath = join(basePath, "api-schema");
|
|
33
|
-
const fileName = `${schemaFolderPath}/${schemaKey}.json`;
|
|
34
|
-
writeFileSync(fileName, JSON.stringify(schema, null, 2));
|
|
35
|
-
});
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
async function replaceReferences(basePath: string): Promise<void> {
|
|
39
|
-
const schemaFolderPath = join(basePath, "api-schema");
|
|
40
|
-
const schemaFiles = readdirSync(schemaFolderPath);
|
|
41
|
-
|
|
42
|
-
schemaFiles.forEach((file) => {
|
|
43
|
-
const filePath = join(schemaFolderPath, file);
|
|
44
|
-
const fileContent = JSON.parse(readFileSync(filePath, "utf8"));
|
|
45
|
-
let convertedSchema = JSON.stringify(fileContent);
|
|
46
|
-
|
|
47
|
-
const searchPattern = /"#\/components\/schemas\/([A-Za-z0-9_-]+)"/g;
|
|
48
|
-
|
|
49
|
-
convertedSchema = convertedSchema.replace(
|
|
50
|
-
searchPattern,
|
|
51
|
-
(_, matchedSchemaName) => {
|
|
52
|
-
let updatedSchemaName = matchedSchemaName;
|
|
53
|
-
|
|
54
|
-
if (updatedSchemaName.endsWith("Enum")) {
|
|
55
|
-
updatedSchemaName = updatedSchemaName.replace("Enum", "");
|
|
56
|
-
updatedSchemaName = `E${updatedSchemaName}`;
|
|
57
|
-
} else {
|
|
58
|
-
updatedSchemaName = `I${updatedSchemaName}`;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
return `"${updatedSchemaName}.json"`;
|
|
62
|
-
},
|
|
63
|
-
);
|
|
64
|
-
|
|
65
|
-
const writeSchema = JSON.parse(convertedSchema);
|
|
66
|
-
writeFileSync(filePath, JSON.stringify(writeSchema, null, 2));
|
|
67
|
-
});
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
async function main() {
|
|
71
|
-
console.log("Fetching Schema");
|
|
72
|
-
const args: string[] = process.argv.slice(2);
|
|
73
|
-
const url = args[0];
|
|
74
|
-
const basePath = args[1];
|
|
75
|
-
const schemaPath = join(basePath, "schema.json");
|
|
76
|
-
const { components } = await fetchSchema(url, schemaPath);
|
|
77
|
-
|
|
78
|
-
await generateFiles(basePath, components);
|
|
79
|
-
await replaceReferences(basePath);
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
main();
|
|
1
|
+
import { readdirSync, readFileSync, writeFileSync } from "fs";
|
|
2
|
+
import { join } from "path";
|
|
3
|
+
import axios, { AxiosResponse } from "axios";
|
|
4
|
+
|
|
5
|
+
interface OpenAPISchema {
|
|
6
|
+
components: { schemas: Record<string, any> };
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
async function fetchSchema(
|
|
10
|
+
url: string,
|
|
11
|
+
schemaPath: string,
|
|
12
|
+
): Promise<OpenAPISchema> {
|
|
13
|
+
const response: AxiosResponse<OpenAPISchema> = await axios.get(url);
|
|
14
|
+
writeFileSync(schemaPath, JSON.stringify(response.data, null, 2));
|
|
15
|
+
return response.data;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
async function generateFiles(
|
|
19
|
+
basePath: string,
|
|
20
|
+
components: { schemas: Record<string, any> },
|
|
21
|
+
): Promise<void> {
|
|
22
|
+
const schemas = components.schemas;
|
|
23
|
+
Object.keys(schemas).forEach((key) => {
|
|
24
|
+
const schema = schemas[key];
|
|
25
|
+
let schemaKey = key;
|
|
26
|
+
if (key.endsWith("Enum")) {
|
|
27
|
+
schemaKey = key.replace("Enum", "");
|
|
28
|
+
schemaKey = `E${schemaKey}`;
|
|
29
|
+
} else {
|
|
30
|
+
schemaKey = `I${schemaKey}`;
|
|
31
|
+
}
|
|
32
|
+
const schemaFolderPath = join(basePath, "api-schema");
|
|
33
|
+
const fileName = `${schemaFolderPath}/${schemaKey}.json`;
|
|
34
|
+
writeFileSync(fileName, JSON.stringify(schema, null, 2));
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
async function replaceReferences(basePath: string): Promise<void> {
|
|
39
|
+
const schemaFolderPath = join(basePath, "api-schema");
|
|
40
|
+
const schemaFiles = readdirSync(schemaFolderPath);
|
|
41
|
+
|
|
42
|
+
schemaFiles.forEach((file) => {
|
|
43
|
+
const filePath = join(schemaFolderPath, file);
|
|
44
|
+
const fileContent = JSON.parse(readFileSync(filePath, "utf8"));
|
|
45
|
+
let convertedSchema = JSON.stringify(fileContent);
|
|
46
|
+
|
|
47
|
+
const searchPattern = /"#\/components\/schemas\/([A-Za-z0-9_-]+)"/g;
|
|
48
|
+
|
|
49
|
+
convertedSchema = convertedSchema.replace(
|
|
50
|
+
searchPattern,
|
|
51
|
+
(_, matchedSchemaName) => {
|
|
52
|
+
let updatedSchemaName = matchedSchemaName;
|
|
53
|
+
|
|
54
|
+
if (updatedSchemaName.endsWith("Enum")) {
|
|
55
|
+
updatedSchemaName = updatedSchemaName.replace("Enum", "");
|
|
56
|
+
updatedSchemaName = `E${updatedSchemaName}`;
|
|
57
|
+
} else {
|
|
58
|
+
updatedSchemaName = `I${updatedSchemaName}`;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
return `"${updatedSchemaName}.json"`;
|
|
62
|
+
},
|
|
63
|
+
);
|
|
64
|
+
|
|
65
|
+
const writeSchema = JSON.parse(convertedSchema);
|
|
66
|
+
writeFileSync(filePath, JSON.stringify(writeSchema, null, 2));
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
async function main() {
|
|
71
|
+
console.log("Fetching Schema");
|
|
72
|
+
const args: string[] = process.argv.slice(2);
|
|
73
|
+
const url = args[0];
|
|
74
|
+
const basePath = args[1];
|
|
75
|
+
const schemaPath = join(basePath, "schema.json");
|
|
76
|
+
const { components } = await fetchSchema(url, schemaPath);
|
|
77
|
+
|
|
78
|
+
await generateFiles(basePath, components);
|
|
79
|
+
await replaceReferences(basePath);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
main();
|
package/src/schema-full.ts
CHANGED
|
@@ -1,38 +1,38 @@
|
|
|
1
|
-
import { readdirSync, readFileSync, writeFileSync } from "fs";
|
|
2
|
-
import { join } from "path";
|
|
3
|
-
|
|
4
|
-
const $RefParser = require("@apidevtools/json-schema-ref-parser");
|
|
5
|
-
|
|
6
|
-
async function generateDereferencedSchema(schemaPath: string): Promise<string> {
|
|
7
|
-
try {
|
|
8
|
-
const schemaContent = readFileSync(schemaPath, "utf8");
|
|
9
|
-
const schemaObject = JSON.parse(schemaContent);
|
|
10
|
-
|
|
11
|
-
const dereferencedSchema = await $RefParser.dereference(schemaObject);
|
|
12
|
-
return JSON.stringify(dereferencedSchema, null, 2);
|
|
13
|
-
} catch (error) {
|
|
14
|
-
return "";
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
async function main() {
|
|
19
|
-
console.log("Resolving Schemas");
|
|
20
|
-
const args: string[] = process.argv.slice(2);
|
|
21
|
-
const basePath = args[0];
|
|
22
|
-
const schemaFolderPath = join(basePath, "api-schema");
|
|
23
|
-
const outputFolder = join(basePath, "form-schema");
|
|
24
|
-
|
|
25
|
-
const schemaFiles = readdirSync(schemaFolderPath);
|
|
26
|
-
|
|
27
|
-
let compilePromises = schemaFiles.map(async (schemaFile) => {
|
|
28
|
-
const schemaFilePath = join(schemaFolderPath, schemaFile);
|
|
29
|
-
|
|
30
|
-
const generatedCode = await generateDereferencedSchema(schemaFilePath);
|
|
31
|
-
|
|
32
|
-
const outputFilePath = join(outputFolder, schemaFile);
|
|
33
|
-
writeFileSync(outputFilePath, generatedCode);
|
|
34
|
-
});
|
|
35
|
-
let results = await Promise.all(compilePromises);
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
main();
|
|
1
|
+
import { readdirSync, readFileSync, writeFileSync } from "fs";
|
|
2
|
+
import { join } from "path";
|
|
3
|
+
|
|
4
|
+
const $RefParser = require("@apidevtools/json-schema-ref-parser");
|
|
5
|
+
|
|
6
|
+
async function generateDereferencedSchema(schemaPath: string): Promise<string> {
|
|
7
|
+
try {
|
|
8
|
+
const schemaContent = readFileSync(schemaPath, "utf8");
|
|
9
|
+
const schemaObject = JSON.parse(schemaContent);
|
|
10
|
+
|
|
11
|
+
const dereferencedSchema = await $RefParser.dereference(schemaObject);
|
|
12
|
+
return JSON.stringify(dereferencedSchema, null, 2);
|
|
13
|
+
} catch (error) {
|
|
14
|
+
return "";
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
async function main() {
|
|
19
|
+
console.log("Resolving Schemas");
|
|
20
|
+
const args: string[] = process.argv.slice(2);
|
|
21
|
+
const basePath = args[0];
|
|
22
|
+
const schemaFolderPath = join(basePath, "api-schema");
|
|
23
|
+
const outputFolder = join(basePath, "form-schema");
|
|
24
|
+
|
|
25
|
+
const schemaFiles = readdirSync(schemaFolderPath);
|
|
26
|
+
|
|
27
|
+
let compilePromises = schemaFiles.map(async (schemaFile) => {
|
|
28
|
+
const schemaFilePath = join(schemaFolderPath, schemaFile);
|
|
29
|
+
|
|
30
|
+
const generatedCode = await generateDereferencedSchema(schemaFilePath);
|
|
31
|
+
|
|
32
|
+
const outputFilePath = join(outputFolder, schemaFile);
|
|
33
|
+
writeFileSync(outputFilePath, generatedCode);
|
|
34
|
+
});
|
|
35
|
+
let results = await Promise.all(compilePromises);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
main();
|
package/src/schema-index.ts
CHANGED
|
@@ -1,29 +1,29 @@
|
|
|
1
|
-
import { join } from "path";
|
|
2
|
-
import { readdirSync, writeFileSync } from "fs";
|
|
3
|
-
|
|
4
|
-
async function main() {
|
|
5
|
-
console.log("Generating Schema Index");
|
|
6
|
-
const args: string[] = process.argv.slice(2);
|
|
7
|
-
const basePath = args[0];
|
|
8
|
-
const schemaFolderPath = join(basePath, "form-schema");
|
|
9
|
-
|
|
10
|
-
let indexFileData =
|
|
11
|
-
'export * from "./interface";\nexport * from "./params";\n';
|
|
12
|
-
|
|
13
|
-
const schemaFiles = readdirSync(schemaFolderPath);
|
|
14
|
-
|
|
15
|
-
let compilePromises = schemaFiles.map((schemaFile) => {
|
|
16
|
-
const schemaFileName = schemaFile.replace(".json", "");
|
|
17
|
-
const formSchemaExport = `export { default as Deref${schemaFileName} } from "./form-schema/${schemaFile}";`;
|
|
18
|
-
const schemaExport = `export { default as Schema${schemaFileName} } from "./api-schema/${schemaFile}";`;
|
|
19
|
-
return `${formSchemaExport}\n${schemaExport}`;
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
let results = await Promise.all(compilePromises);
|
|
23
|
-
indexFileData = indexFileData + results.join("\n");
|
|
24
|
-
|
|
25
|
-
const outputFile = join(basePath, "index.ts");
|
|
26
|
-
writeFileSync(outputFile, indexFileData);
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
main();
|
|
1
|
+
import { join } from "path";
|
|
2
|
+
import { readdirSync, writeFileSync } from "fs";
|
|
3
|
+
|
|
4
|
+
async function main() {
|
|
5
|
+
console.log("Generating Schema Index");
|
|
6
|
+
const args: string[] = process.argv.slice(2);
|
|
7
|
+
const basePath = args[0];
|
|
8
|
+
const schemaFolderPath = join(basePath, "form-schema");
|
|
9
|
+
|
|
10
|
+
let indexFileData =
|
|
11
|
+
'export * from "./interface";\nexport * from "./params";\n';
|
|
12
|
+
|
|
13
|
+
const schemaFiles = readdirSync(schemaFolderPath);
|
|
14
|
+
|
|
15
|
+
let compilePromises = schemaFiles.map((schemaFile) => {
|
|
16
|
+
const schemaFileName = schemaFile.replace(".json", "");
|
|
17
|
+
const formSchemaExport = `export { default as Deref${schemaFileName} } from "./form-schema/${schemaFile}";`;
|
|
18
|
+
const schemaExport = `export { default as Schema${schemaFileName} } from "./api-schema/${schemaFile}";`;
|
|
19
|
+
return `${formSchemaExport}\n${schemaExport}`;
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
let results = await Promise.all(compilePromises);
|
|
23
|
+
indexFileData = indexFileData + results.join("\n");
|
|
24
|
+
|
|
25
|
+
const outputFile = join(basePath, "index.ts");
|
|
26
|
+
writeFileSync(outputFile, indexFileData);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
main();
|
package/src/schema-params.ts
CHANGED
|
@@ -1,116 +1,116 @@
|
|
|
1
|
-
import { writeFileSync } from "fs";
|
|
2
|
-
import {
|
|
3
|
-
ComponentsObject,
|
|
4
|
-
ParameterObject,
|
|
5
|
-
PathsObject,
|
|
6
|
-
} from "@ts-stack/openapi-spec";
|
|
7
|
-
import { OperationObject } from "@ts-stack/openapi-spec/src/origin/operation-object";
|
|
8
|
-
import { join } from "path";
|
|
9
|
-
import { fetchSchema } from "./schema/fetch-schema";
|
|
10
|
-
|
|
11
|
-
interface OpenAPISchema {
|
|
12
|
-
paths: PathsObject;
|
|
13
|
-
components: ComponentsObject;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
interface PathParamItem {
|
|
17
|
-
name: string;
|
|
18
|
-
required: boolean;
|
|
19
|
-
schema: object | undefined;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
interface PathParam {
|
|
23
|
-
operationName: string;
|
|
24
|
-
paramsArray: PathParamItem[];
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
function generateParams(paths: PathsObject): PathParam[] {
|
|
28
|
-
const pathParam: PathParam[] = [];
|
|
29
|
-
Object.keys(paths).map((path) => {
|
|
30
|
-
const pathItem = paths[path];
|
|
31
|
-
console.log(path);
|
|
32
|
-
Object.keys(pathItem).forEach((method) => {
|
|
33
|
-
// @ts-ignore
|
|
34
|
-
const pathOperation: OperationObject = pathItem[method];
|
|
35
|
-
const pathParamData: PathParamItem[] = [];
|
|
36
|
-
if (pathOperation.parameters) {
|
|
37
|
-
pathOperation.parameters.forEach((param) => {
|
|
38
|
-
// @ts-ignore
|
|
39
|
-
const paramObject: ParameterObject = param;
|
|
40
|
-
pathParamData.push({
|
|
41
|
-
name: paramObject.name,
|
|
42
|
-
required: paramObject.required || false,
|
|
43
|
-
schema: paramObject.schema,
|
|
44
|
-
});
|
|
45
|
-
});
|
|
46
|
-
}
|
|
47
|
-
if (pathOperation.operationId) {
|
|
48
|
-
// Operation id to snake case to camelcase and remove the text v1
|
|
49
|
-
const operationId = pathOperation.operationId;
|
|
50
|
-
const camelCaseOperationId = operationId
|
|
51
|
-
.replace(/_v1/g, "")
|
|
52
|
-
.replace(/_([a-z])/g, function (g) {
|
|
53
|
-
return g[1].toUpperCase();
|
|
54
|
-
});
|
|
55
|
-
const capitalizedOperationId =
|
|
56
|
-
camelCaseOperationId.charAt(0).toUpperCase() +
|
|
57
|
-
camelCaseOperationId.slice(1);
|
|
58
|
-
|
|
59
|
-
pathParam.push({
|
|
60
|
-
operationName: capitalizedOperationId,
|
|
61
|
-
paramsArray: pathParamData,
|
|
62
|
-
});
|
|
63
|
-
}
|
|
64
|
-
});
|
|
65
|
-
});
|
|
66
|
-
return pathParam;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
function generateInterfacesFiles(pathParams: PathParam[], schemaPath: string) {
|
|
70
|
-
const interfaceFileValues = pathParams.map((pathParam) => {
|
|
71
|
-
const interfaceName = `I${pathParam.operationName}Params`;
|
|
72
|
-
const interfaceValues = pathParam.paramsArray.map((param) => {
|
|
73
|
-
let paramSchemaType = "string";
|
|
74
|
-
if (param.schema) {
|
|
75
|
-
// @ts-ignore
|
|
76
|
-
if (param.schema.type === "array") {
|
|
77
|
-
// @ts-ignore
|
|
78
|
-
if (param.schema.items.enum) {
|
|
79
|
-
// @ts-ignore
|
|
80
|
-
paramSchemaType = param.schema.items.enum
|
|
81
|
-
.map((value: string) => `"${value}"`)
|
|
82
|
-
.join(" | ");
|
|
83
|
-
paramSchemaType = `(${paramSchemaType})[]`;
|
|
84
|
-
}
|
|
85
|
-
} else {
|
|
86
|
-
// @ts-ignore
|
|
87
|
-
if (param.schema.type === "integer") {
|
|
88
|
-
paramSchemaType = "number";
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
return `${param.name}${param.required ? "" : "?"}: ${paramSchemaType};`;
|
|
94
|
-
});
|
|
95
|
-
return `export interface ${interfaceName} {
|
|
96
|
-
${interfaceValues.join("\n")}
|
|
97
|
-
}`;
|
|
98
|
-
});
|
|
99
|
-
const interfaceFileValuesString = interfaceFileValues.join("\n");
|
|
100
|
-
const interfaceFile = join(schemaPath, `params.ts`);
|
|
101
|
-
writeFileSync(interfaceFile, interfaceFileValuesString, { flag: "w" });
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
async function main() {
|
|
105
|
-
console.log("Generating Schema Parameter Definitions");
|
|
106
|
-
const args: string[] = process.argv.slice(2);
|
|
107
|
-
const url = args[0];
|
|
108
|
-
const schemaPath = args[1];
|
|
109
|
-
const data = await fetchSchema(url);
|
|
110
|
-
const paths = data.paths;
|
|
111
|
-
const pathParams = generateParams(paths);
|
|
112
|
-
|
|
113
|
-
generateInterfacesFiles(pathParams, schemaPath);
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
main();
|
|
1
|
+
import { writeFileSync } from "fs";
|
|
2
|
+
import {
|
|
3
|
+
ComponentsObject,
|
|
4
|
+
ParameterObject,
|
|
5
|
+
PathsObject,
|
|
6
|
+
} from "@ts-stack/openapi-spec";
|
|
7
|
+
import { OperationObject } from "@ts-stack/openapi-spec/src/origin/operation-object";
|
|
8
|
+
import { join } from "path";
|
|
9
|
+
import { fetchSchema } from "./schema/fetch-schema";
|
|
10
|
+
|
|
11
|
+
interface OpenAPISchema {
|
|
12
|
+
paths: PathsObject;
|
|
13
|
+
components: ComponentsObject;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
interface PathParamItem {
|
|
17
|
+
name: string;
|
|
18
|
+
required: boolean;
|
|
19
|
+
schema: object | undefined;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
interface PathParam {
|
|
23
|
+
operationName: string;
|
|
24
|
+
paramsArray: PathParamItem[];
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function generateParams(paths: PathsObject): PathParam[] {
|
|
28
|
+
const pathParam: PathParam[] = [];
|
|
29
|
+
Object.keys(paths).map((path) => {
|
|
30
|
+
const pathItem = paths[path];
|
|
31
|
+
console.log(path);
|
|
32
|
+
Object.keys(pathItem).forEach((method) => {
|
|
33
|
+
// @ts-ignore
|
|
34
|
+
const pathOperation: OperationObject = pathItem[method];
|
|
35
|
+
const pathParamData: PathParamItem[] = [];
|
|
36
|
+
if (pathOperation.parameters) {
|
|
37
|
+
pathOperation.parameters.forEach((param) => {
|
|
38
|
+
// @ts-ignore
|
|
39
|
+
const paramObject: ParameterObject = param;
|
|
40
|
+
pathParamData.push({
|
|
41
|
+
name: paramObject.name,
|
|
42
|
+
required: paramObject.required || false,
|
|
43
|
+
schema: paramObject.schema,
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
if (pathOperation.operationId) {
|
|
48
|
+
// Operation id to snake case to camelcase and remove the text v1
|
|
49
|
+
const operationId = pathOperation.operationId;
|
|
50
|
+
const camelCaseOperationId = operationId
|
|
51
|
+
.replace(/_v1/g, "")
|
|
52
|
+
.replace(/_([a-z])/g, function (g) {
|
|
53
|
+
return g[1].toUpperCase();
|
|
54
|
+
});
|
|
55
|
+
const capitalizedOperationId =
|
|
56
|
+
camelCaseOperationId.charAt(0).toUpperCase() +
|
|
57
|
+
camelCaseOperationId.slice(1);
|
|
58
|
+
|
|
59
|
+
pathParam.push({
|
|
60
|
+
operationName: capitalizedOperationId,
|
|
61
|
+
paramsArray: pathParamData,
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
return pathParam;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function generateInterfacesFiles(pathParams: PathParam[], schemaPath: string) {
|
|
70
|
+
const interfaceFileValues = pathParams.map((pathParam) => {
|
|
71
|
+
const interfaceName = `I${pathParam.operationName}Params`;
|
|
72
|
+
const interfaceValues = pathParam.paramsArray.map((param) => {
|
|
73
|
+
let paramSchemaType = "string";
|
|
74
|
+
if (param.schema) {
|
|
75
|
+
// @ts-ignore
|
|
76
|
+
if (param.schema.type === "array") {
|
|
77
|
+
// @ts-ignore
|
|
78
|
+
if (param.schema.items.enum) {
|
|
79
|
+
// @ts-ignore
|
|
80
|
+
paramSchemaType = param.schema.items.enum
|
|
81
|
+
.map((value: string) => `"${value}"`)
|
|
82
|
+
.join(" | ");
|
|
83
|
+
paramSchemaType = `(${paramSchemaType})[]`;
|
|
84
|
+
}
|
|
85
|
+
} else {
|
|
86
|
+
// @ts-ignore
|
|
87
|
+
if (param.schema.type === "integer") {
|
|
88
|
+
paramSchemaType = "number";
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
return `${param.name}${param.required ? "" : "?"}: ${paramSchemaType};`;
|
|
94
|
+
});
|
|
95
|
+
return `export interface ${interfaceName} {
|
|
96
|
+
${interfaceValues.join("\n")}
|
|
97
|
+
}`;
|
|
98
|
+
});
|
|
99
|
+
const interfaceFileValuesString = interfaceFileValues.join("\n");
|
|
100
|
+
const interfaceFile = join(schemaPath, `params.ts`);
|
|
101
|
+
writeFileSync(interfaceFile, interfaceFileValuesString, { flag: "w" });
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
async function main() {
|
|
105
|
+
console.log("Generating Schema Parameter Definitions");
|
|
106
|
+
const args: string[] = process.argv.slice(2);
|
|
107
|
+
const url = args[0];
|
|
108
|
+
const schemaPath = args[1];
|
|
109
|
+
const data = await fetchSchema(url);
|
|
110
|
+
const paths = data.paths;
|
|
111
|
+
const pathParams = generateParams(paths);
|
|
112
|
+
|
|
113
|
+
generateInterfacesFiles(pathParams, schemaPath);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
main();
|