@accelbyte/codegen 2.0.0-beta.5 → 2.0.0-beta.6
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/dist/accelbyte-codegen.js +333 -299
- package/dist/accelbyte-codegen.js.map +1 -1
- package/dist/accelbyte-codegen.mjs +333 -299
- package/dist/accelbyte-codegen.mjs.map +1 -1
- package/package.json +1 -21
- package/scripts/generate-test-resources.mjs +105 -0
package/package.json
CHANGED
|
@@ -1,18 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@accelbyte/codegen",
|
|
3
|
-
"version": "2.0.0-beta.
|
|
3
|
+
"version": "2.0.0-beta.6",
|
|
4
4
|
"author": "AccelByte Inc",
|
|
5
5
|
"license": "SEE LICENSE IN LICENSE",
|
|
6
6
|
"main": "dist/accelbyte-codegen.js",
|
|
7
7
|
"module": "dist/accelbyte-codegen.mjs",
|
|
8
|
-
"scripts": {
|
|
9
|
-
"prebuild": "rimraf dist",
|
|
10
|
-
"build": "rollup -c rollup.config.mjs && (cd dist && chmod 700 *.*)",
|
|
11
|
-
"prettier": "prettier --write sdk/.",
|
|
12
|
-
"test": "vitest run",
|
|
13
|
-
"test:watch": "vitest",
|
|
14
|
-
"clean": "npx rimraf dist && npx rimraf node_modules"
|
|
15
|
-
},
|
|
16
8
|
"bin": {
|
|
17
9
|
"accelbyte-codegen": "dist/accelbyte-codegen.js"
|
|
18
10
|
},
|
|
@@ -24,17 +16,5 @@
|
|
|
24
16
|
"swagger-parser": "10.0.3",
|
|
25
17
|
"yargs": "17.6.2",
|
|
26
18
|
"zod": "3.17.3"
|
|
27
|
-
},
|
|
28
|
-
"devDependencies": {
|
|
29
|
-
"@rollup/plugin-commonjs": "20.0.0",
|
|
30
|
-
"@rollup/plugin-node-resolve": "13.0.4",
|
|
31
|
-
"@types/node": "16.4.14",
|
|
32
|
-
"@types/yargs": "17.0.14",
|
|
33
|
-
"esbuild": "0.14.10",
|
|
34
|
-
"rollup": "3.8.1",
|
|
35
|
-
"rollup-plugin-add-shebang": "0.3.1",
|
|
36
|
-
"rollup-plugin-esbuild": "5.0.0",
|
|
37
|
-
"typescript": "4.6.4",
|
|
38
|
-
"vitest": "0.31.1"
|
|
39
19
|
}
|
|
40
20
|
}
|
|
@@ -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
|
+
}
|