Skip to content

Commit 3f8537a

Browse files
committed
build: add examples to output, rename handleRef function
1 parent 2da52b6 commit 3f8537a

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed

scripts/build.js

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66
copyFileSync,
77
} from "node:fs";
88
import * as prettier from "prettier";
9-
import _ from "lodash";
109

1110
/* if (!process.env.GITHUB_ACTIONS && !process.env.ANICCA_REPOSITORY_PATH) {
1211
throw new Error("Please set ANICCA_REPOSITORY_PATH");
@@ -35,16 +34,21 @@ async function run() {
3534
delete schema.components.responses;
3635
delete schema.components.parameters;
3736
delete schema.components.headers;
38-
delete schema.components.examples;
3937

4038
const tempSchema = { ...schema };
4139
tempSchema.components = {
4240
schemas: {},
41+
examples: {}
4342
};
4443

45-
// Check all instances of `$ref` in the OpenAPI spec, and add them to the definitions
46-
// Remove all instances of `enterprise` in the OpenAPI spec for GitHub.com
47-
const handleRefs = (obj) => {
44+
/**
45+
* Function to handle special cases:
46+
* Check all instances of `$ref` in the OpenAPI spec, and add them to the definitions
47+
* Remove all instances of `enterprise` in the OpenAPI spec for GitHub.com
48+
* @param {object} obj - The object to check for special cases
49+
* @returns {object} - The object with any special cases handled
50+
*/
51+
const specialHandling = (obj) => {
4852
if (typeof obj !== "object" || obj === null) return obj;
4953

5054
for (let key of Object.keys(obj)) {
@@ -54,10 +58,10 @@ async function run() {
5458
if (key === "$ref" && typeof obj[key] === "string") {
5559
const ref = obj[key].split("/").at(-1);
5660
tempSchema.components.schemas[ref] = schema.components.schemas[ref];
57-
// Call the function with the new definition to handle any of it's $refs
58-
handleRefs(tempSchema.components.schemas[ref]);
61+
// Call the function with the new definition to handle any of it's `$ref`s, and `enterprise` keys
62+
specialHandling(tempSchema.components.schemas[ref]);
5963
} else {
60-
obj[key] = handleRefs(obj[key]);
64+
obj[key] = specialHandling(obj[key]);
6165
}
6266
}
6367
return obj;
@@ -68,6 +72,7 @@ async function run() {
6872
const webhook = webhooks[webhookId].post;
6973
const requestBody = webhook.requestBody;
7074
const ref = requestBody.content["application/json"].schema.$ref;
75+
const examples = requestBody.content["application/json"].examples;
7176
const refName = ref.split("/").at(-1);
7277
if (
7378
typeof requestBody.content["application/x-www-form-urlencoded"] !==
@@ -81,7 +86,13 @@ async function run() {
8186
}
8287
tempSchema.components.schemas[refName] =
8388
schema.components.schemas[refName];
84-
handleRefs(schema.components.schemas[refName]);
89+
specialHandling(schema.components.schemas[refName]);
90+
if (typeof examples !== "undefined") {
91+
for (let key of Object.keys(examples)) {
92+
const example$ref = examples[key].$ref.split("/").at(-1);
93+
tempSchema.components.examples[example$ref] = schema.components.examples[example$ref];
94+
}
95+
}
8596
}
8697

8798
writeFileSync(

0 commit comments

Comments
 (0)