@accelbyte/codegen 1.0.5 → 2.0.0-beta.10

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@accelbyte/codegen",
3
- "version": "1.0.5",
3
+ "version": "2.0.0-beta.10",
4
4
  "author": "AccelByte Inc",
5
5
  "license": "SEE LICENSE IN LICENSE",
6
6
  "main": "dist/accelbyte-codegen.js",
@@ -9,10 +9,12 @@
9
9
  "accelbyte-codegen": "dist/accelbyte-codegen.js"
10
10
  },
11
11
  "dependencies": {
12
+ "@apidevtools/swagger-parser": "10.1.0",
12
13
  "axios": "1.3.6",
13
14
  "fast-json-patch": "3.1.1",
14
15
  "lodash": "4.17.21",
15
16
  "rimraf": "4.1.2",
17
+ "semver": "7.5.4",
16
18
  "swagger-parser": "10.0.3",
17
19
  "yargs": "17.6.2",
18
20
  "zod": "3.17.3"
@@ -0,0 +1,105 @@
1
+ // @ts-check
2
+ import fs from 'fs/promises'
3
+ import path from 'path'
4
+
5
+ // The objective of this script is so that we could trim down the IAM Swagger.
6
+ // We want to test our Swagger parser and generator, in some ways so that we could be sure
7
+ // that when we do something, nothing breaks.
8
+ //
9
+ // And having the entire IAM Swagger JSON might be overkill.
10
+ // Hence, wh at we're doing in the `INCLUDED_ENDPOINTS` is that, we want to make sure
11
+ // to cover the scenarios. For example, function naming, deprecated endpoints exclusion.
12
+ const PATH_TO_IAM_SAMPLE = 'src/helpers/test-resources/swagger-iam-sample.json'
13
+
14
+ const INCLUDED_ENDPOINTS = [
15
+ // These are deprecated endpoints, should be automatically excluded.
16
+ '/iam/bans',
17
+ '/iam/roles',
18
+ // These are the normal ones.
19
+ '/iam/v3/admin/bans',
20
+ '/iam/v3/admin/roles',
21
+ '/iam/v3/admin/users/me',
22
+ '/iam/v3/public/users/me',
23
+ '/iam/v3/admin/namespaces/{namespace}/clients/{clientId}/permissions/{resource}/{action}',
24
+ // For array definitions test.
25
+ '/iam/v3/admin/namespaces/{namespace}/users/{userId}/platforms/justice'
26
+ ]
27
+
28
+ async function main() {
29
+ const content = await fs.readFile(path.join(process.cwd(), PATH_TO_IAM_SAMPLE), 'utf-8')
30
+ const json = JSON.parse(content)
31
+
32
+ const newPaths = {}
33
+ const modelsSet = new Set()
34
+ const newDefinitions = {}
35
+
36
+ for (const key in json.paths) {
37
+ // Only include the endpoints set above.
38
+ if (INCLUDED_ENDPOINTS.includes(key)) {
39
+ newPaths[key] = json.paths[key]
40
+
41
+ for (const methodKey in json.paths[key]) {
42
+ const { parameters = [], responses = {} } = json.paths[key][methodKey]
43
+
44
+ // Get the request body schema, if any.
45
+ for (const parameter of parameters) {
46
+ const { schema } = parameter
47
+ if (!schema) continue
48
+
49
+ const ref = getEffectiveRef(schema)
50
+ if (ref) modelsSet.add(ref)
51
+ }
52
+
53
+ // Get the resposne body schema, if any.
54
+ for (const responseStatus in responses) {
55
+ const { schema } = responses[responseStatus]
56
+ if (!schema) continue
57
+
58
+ const ref = getEffectiveRef(schema)
59
+ if (ref) modelsSet.add(ref)
60
+ }
61
+ }
62
+ }
63
+ }
64
+
65
+ for (const model of modelsSet) {
66
+ // Recursively add definitions (including definitions inside definitions).
67
+ recursivelyAddToNewDefinitions(newDefinitions, json.definitions, model)
68
+ }
69
+
70
+ json.paths = newPaths
71
+ json.definitions = newDefinitions
72
+
73
+ // Re-write the old Swagger into the new Swagger.
74
+ await fs.writeFile(PATH_TO_IAM_SAMPLE, JSON.stringify(json, null, 2), 'utf-8')
75
+ }
76
+
77
+ main()
78
+
79
+ // Helper functions.
80
+ function getEffectiveRef(schema) {
81
+ let ref = schema['$ref']
82
+ if (!ref) {
83
+ ref = schema.items?.['$ref']
84
+ }
85
+
86
+ return ref?.slice('#/definitions/'.length)
87
+ }
88
+
89
+ function recursivelyAddToNewDefinitions(newDefinitions, definitions, modelName) {
90
+ // Store the definition in the new definitions.
91
+ const currentDefinition = definitions[modelName]
92
+ newDefinitions[modelName] = currentDefinition
93
+
94
+ // From the definition, check the properties.
95
+ // See if they have "nested" definitions, so we can traverse them recursively.
96
+ for (const propertyKey in currentDefinition.properties) {
97
+ const propertySchema = currentDefinition.properties[propertyKey]
98
+ const ref = getEffectiveRef(propertySchema)
99
+
100
+ if (ref) {
101
+ newDefinitions[ref] = definitions[ref]
102
+ recursivelyAddToNewDefinitions(newDefinitions, definitions, ref)
103
+ }
104
+ }
105
+ }