Skip to content

Commit

Permalink
script generate-open-api-with-specific-endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
p-zielinski committed Nov 23, 2023
1 parent eb09fcb commit a55327f
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"readme-fix-reference-docs": "ts-node ./scripts/readme-fix-reference-docs.ts",
"remove-stoplight-tags-from-openapi": "ts-node ./scripts/remove-stoplight-tags-from-openapi.ts",
"remove-deprecated-from-openapi": "ts-node ./scripts/remove-deprecated-from-openapi.ts",
"generate-open-api-with-specific-endpoints": "ts-node ./scripts/generate-open-api-with-specific-endpoints.ts",
"manage-project": "ts-node ./scripts/build-md-tables-from-openapi.ts && ts-node ./scripts/manage-project.ts",
"readme-upload-missing-images": "ts-node ./scripts/readme-upload-missing-images.ts"
},
Expand Down
132 changes: 132 additions & 0 deletions scripts/generate-open-api-with-specific-endpoints.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
import fsPromises from "fs/promises";
import fs from "fs";
import path from "path";

function isObject(value) {
return typeof value === "object" && value !== null && !Array.isArray(value);
}

const removeStoplightTag = (node: object): object => {
delete node["x-stoplight"];
for (const attr in node) {
if (isObject(node[attr])) {
removeStoplightTag(node[attr]);
}
}
return node;
};

const grabList: { endpoint: string; methods: string[] | true }[] = [
{ endpoint: "/v1/qualifications", methods: true },
];

const main = async () => {
const openApiPath = path.join(__dirname, "../reference/OpenAPI.json");
const openAPIContent = JSON.parse(
(await fsPromises.readFile(openApiPath)).toString()
);
removeStoplightTag(openAPIContent);

// Removing deprecated paths
const paths = {};
const pathsKeys = Object.keys(openAPIContent.paths);
for (const pathKey of pathsKeys) {
const grab = grabList.find((grab) => grab.endpoint === pathKey);
const path = {};
const methods = Object.keys(openAPIContent.paths[pathKey]);
for (const method of methods) {
if (
grab?.methods === true ||
(grab?.methods && grab.methods.includes(method))
) {
path[method] = openAPIContent.paths[pathKey][method];
} else {
continue;
}
}
if (Object.keys(path).length > 0) {
paths[pathKey] = path;
}
}

const pathsStringify = JSON.stringify(paths);

// Removing not used parameters
const parametersNames =
pathsStringify
.match(/"#\/components\/parameters\/.*?"/g)
?.map((match) =>
match.replace('"#/components/parameters/', "").slice(0, -1)
)
.sort() || [];
const parameters = {};
for (const parameterName of parametersNames) {
if (!openAPIContent.components.parameters?.[parameterName]) {
console.log(`not found ${parameterName} in parameters`);
continue;
}
parameters[parameterName] =
openAPIContent.components.parameters[parameterName];
}

// Removing not used schemas
const schemas = {};
const schemasNames =
pathsStringify
.match(/"#\/components\/schemas\/.*?"/g)
?.map((match) => match.replace('"#/components/schemas/', "").slice(0, -1))
.sort() || [];
for (const schemaName of schemasNames) {
if (!openAPIContent.components.schemas?.[schemaName]) {
console.log(`not found ${schemaName} in schemas`);
continue;
}
schemas[schemaName] = openAPIContent.components.schemas[schemaName];
}

// Finding other schemas uses
let lastSchemaStringify = "";
while (true) {
const schemaStringify = JSON.stringify(schemas);
if (lastSchemaStringify === schemaStringify) {
break;
}
lastSchemaStringify = schemaStringify;
const schemasNames =
schemaStringify
.match(/"#\/components\/schemas\/.*?"/g)
?.map((match) =>
match.replace('"#/components/schemas/', "").slice(0, -1)
)
.sort() || [];
for (const schemaName of schemasNames) {
if (!openAPIContent.components.schemas?.[schemaName]) {
console.log(`not found ${schemaName} in schemas`);
continue;
}
schemas[schemaName] = openAPIContent.components.schemas[schemaName];
}
}

// Building all together
const newOpenApiFile = { ...openAPIContent };
newOpenApiFile.components.parameters = parameters;
newOpenApiFile.components.schemas = schemas;
newOpenApiFile.paths = paths;

//write the new OpenApiFile
const pathToTmp = path.join(__dirname, "../tmp");
if (!fs.existsSync(pathToTmp)) {
fs.mkdirSync(pathToTmp);
}
const pathToTmpReference = path.join(__dirname, "../tmp/referencePartial");
if (!fs.existsSync(pathToTmpReference)) {
fs.mkdirSync(pathToTmpReference);
}
await fsPromises.writeFile(
path.join(__dirname, "../tmp/referencePartial/OpenAPI.json"),
JSON.stringify(newOpenApiFile, null, 2)
);
};

main();

0 comments on commit a55327f

Please sign in to comment.