6
6
copyFileSync ,
7
7
} from "node:fs" ;
8
8
import * as prettier from "prettier" ;
9
- import _ from "lodash" ;
10
9
11
10
/* if (!process.env.GITHUB_ACTIONS && !process.env.ANICCA_REPOSITORY_PATH) {
12
11
throw new Error("Please set ANICCA_REPOSITORY_PATH");
@@ -35,16 +34,21 @@ async function run() {
35
34
delete schema . components . responses ;
36
35
delete schema . components . parameters ;
37
36
delete schema . components . headers ;
38
- delete schema . components . examples ;
39
37
40
38
const tempSchema = { ...schema } ;
41
39
tempSchema . components = {
42
40
schemas : { } ,
41
+ examples : { }
43
42
} ;
44
43
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 ) => {
48
52
if ( typeof obj !== "object" || obj === null ) return obj ;
49
53
50
54
for ( let key of Object . keys ( obj ) ) {
@@ -54,10 +58,10 @@ async function run() {
54
58
if ( key === "$ref" && typeof obj [ key ] === "string" ) {
55
59
const ref = obj [ key ] . split ( "/" ) . at ( - 1 ) ;
56
60
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 ] ) ;
59
63
} else {
60
- obj [ key ] = handleRefs ( obj [ key ] ) ;
64
+ obj [ key ] = specialHandling ( obj [ key ] ) ;
61
65
}
62
66
}
63
67
return obj ;
@@ -68,6 +72,7 @@ async function run() {
68
72
const webhook = webhooks [ webhookId ] . post ;
69
73
const requestBody = webhook . requestBody ;
70
74
const ref = requestBody . content [ "application/json" ] . schema . $ref ;
75
+ const examples = requestBody . content [ "application/json" ] . examples ;
71
76
const refName = ref . split ( "/" ) . at ( - 1 ) ;
72
77
if (
73
78
typeof requestBody . content [ "application/x-www-form-urlencoded" ] !==
@@ -81,7 +86,13 @@ async function run() {
81
86
}
82
87
tempSchema . components . schemas [ refName ] =
83
88
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
+ }
85
96
}
86
97
87
98
writeFileSync (
0 commit comments