@accelbyte/codegen 4.1.6 → 4.2.0

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/README.md CHANGED
@@ -2,24 +2,28 @@
2
2
 
3
3
  AccelByte Code Generator is a CLI tool that facilitates creating an AccelByte TypeScript SDK from AccelByte OpenAPI definitions.
4
4
 
5
+ ## Requirements
6
+
7
+ - **Node.js >= 22.18.0** (LTS) — required for native TypeScript type stripping, used to load `abcodegen.config.ts` at runtime.
8
+
5
9
  ## CLI
6
10
  This codegen build a CLI called `accelbyte-codegen` that will be used to generate code from given config.
7
11
 
8
12
  ```
9
- Commands:
10
- accelbyte-codegen download-swaggers Download swaggers JSON files
11
- accelbyte-codegen generate-code Generate code based on downloaded swagger files
13
+ Commands:
14
+ accelbyte-codegen download-swaggers Download swagger JSON files
15
+ accelbyte-codegen generate-code Generate code from downloaded swagger files
12
16
 
13
17
  Options:
14
- --version Show version number [boolean]
15
- --config Path to the config file with backend service URLs. [string] [required]
16
- --swaggersOutput Directory to save the downloaded Swagger JSON files. [string] [required]
17
- --output Directory for the generated code. Required when using the generate-code command. [string]
18
- --help Show help [boolean]
19
- --skipReactQuery Disable React Query code generation. [boolean]
20
- --snippetOutput Generate only code snippets. [boolean]
21
- --snippetOnly Directory for the generated code snippets. Required when generating snippets. [boolean]
22
- --webSocketSchema Path to the WebSocket schema file (schema.json). [string]
18
+ --version Show version number [boolean]
19
+ --config Path to the config file with backend service URLs. [string] [required]
20
+ --swaggersOutput Directory to save the downloaded Swagger JSON files. [string] [required]
21
+ --output Directory for the generated code. Required for generate-code. [string]
22
+ --help Show help [boolean]
23
+ --skipReactQuery Disable React Query code generation. [boolean]
24
+ --snippetOnly Generate only code snippets. [boolean]
25
+ --snippetOutput Directory for the generated code snippets. [string]
26
+ --webSocketSchema Path to the WebSocket schema file (schema.json). [string]
23
27
  ```
24
28
 
25
29
  ### Config file
@@ -33,9 +37,11 @@ Provide swaggers url you wish to generate, store it in .json format file.
33
37
 
34
38
  ## How to Generate
35
39
 
36
- **Step 1: Set Up Your Node.js Environment** Make sure you have Node.js and npm (Node Package Manager) installed on your system. You can download and install them from the official website: https://nodejs.org/en/
40
+ **Step 1: Set Up Your Node.js Environment** Make sure you have **Node.js >= 22.18.0** (LTS) and npm (Node Package Manager) installed on your system. You can download and install them from the official website: https://nodejs.org/en/
41
+
42
+ This minimum version is required because the codegen loads `abcodegen.config.ts` at runtime using Node.js native TypeScript type stripping, which was unflagged in Node.js 22.18.0.
37
43
 
38
- __*It is recommended__ to use [nvm](https://github.com/nvm-sh/nvm) or [fnm](https://github.com/Schniz/fnm) to install Node.js so you can switch between Node versions more easily
44
+ __*It is recommended__ to use [nvm](https://github.com/nvm-sh/nvm) or [fnm](https://github.com/Schniz/fnm) to install Node.js so you can switch between Node versions more easily
39
45
 
40
46
  **Step 2: Create a New Node.js Project (if needed)** If you are starting a new project, create a new directory and initialize it as a Node.js project. You can do this using the following commands:
41
47
  ```
@@ -110,4 +116,95 @@ after installing, execute prettier as below
110
116
  npm exec prettier --write swaggers/*.json && prettier --write sdk/**/*
111
117
  # Or, with yarn:
112
118
  yarn prettier --write swaggers/*.json && prettier --write sdk/**/*
113
- ```
119
+ ```
120
+
121
+ ## Configuration
122
+
123
+ Place an `abcodegen.config.ts` file next to your `swaggers.json` to customize codegen behavior. All options are optional — if the file is absent, defaults apply.
124
+
125
+ ```typescript
126
+ // abcodegen.config.ts
127
+ import { type CodegenConfigOptions } from '@accelbyte/codegen'
128
+
129
+ export default {
130
+ // Override the base path prepended to all API routes.
131
+ // Default: uses basePath from the swagger spec.
132
+ basePath: '/custom',
133
+
134
+ // Generate barrel index files (index.ts, all-imports.ts, all-query-imports.ts).
135
+ // Default: true
136
+ shouldProduceIndexFiles: false,
137
+
138
+ // Force specific definitions to emit `z.any()` instead of a full schema.
139
+ // Keyed by definition file name. Value can be `true` or a function receiving the schema.
140
+ overrideAsAny: {
141
+ ProtobufAny: true,
142
+ SomeOther: (schema) => schema.properties?.['@type'] !== undefined
143
+ },
144
+
145
+ // Output each swagger set into a subfolder named after its service name.
146
+ // Default: false
147
+ unstable_splitOutputByServiceName: true
148
+ } satisfies CodegenConfigOptions
149
+ ```
150
+
151
+ ### `unstable_splitOutputByServiceName`
152
+
153
+ By default, all swagger sets in `swaggers.json` are generated into the same `--output` directory. When this option is enabled, each set is placed in a subfolder named after its service name (the first element of each inner array).
154
+
155
+ Given a config like:
156
+
157
+ ```json
158
+ [
159
+ ["iam", "iam", "iam.json", "https://example.com/iam/apidocs/api.json"],
160
+ ["platform", "platform", "platform.json", "https://example.com/platform/swagger.json"]
161
+ ]
162
+ ```
163
+
164
+ The output becomes:
165
+
166
+ ```
167
+ sdk/
168
+ iam/
169
+ generated-admin/
170
+ generated-public/
171
+ generated-definitions/
172
+ Iam.ts
173
+ platform/
174
+ generated-admin/
175
+ generated-public/
176
+ generated-definitions/
177
+ Platform.ts
178
+ all-imports.ts ← re-exports from all subfolders
179
+ all-query-imports.ts
180
+ ```
181
+
182
+ Enable via `abcodegen.config.ts`:
183
+
184
+ ```typescript
185
+ export default {
186
+ unstable_splitOutputByServiceName: true
187
+ } satisfies CodegenConfigOptions
188
+ ```
189
+
190
+ ## Design Decisions
191
+
192
+ ### Zod definitions use `interface`, not `type`
193
+
194
+ Generated definitions use `interface Foo extends z.TypeOf<typeof Foo> {}` rather than a `type` alias. This makes editors display the object shape directly on hover instead of showing the `z.TypeOf<...>` indirection.
195
+
196
+ This triggers `@typescript-eslint/no-empty-object-type`. That rule guards against the `{}` gotcha in hand-written code, which does not apply to generated output. Consumers can disable it for generated files:
197
+
198
+ ```js
199
+ // eslint.config.js
200
+ {
201
+ files: ['src/generated-definitions/**'],
202
+ rules: { '@typescript-eslint/no-empty-object-type': 'off' }
203
+ }
204
+ ```
205
+
206
+ ## Troubleshooting
207
+
208
+ ### Type or lint errors in generated files
209
+
210
+ The generated code aims to use neutral, unopinionated style defaults. However, some generated syntax may conflict with your tsconfig or linter settings. In that case, you can ignore specific lint rules in the codegen folder, or create a `tsconfig.json` inside it to override your top-level configuration.
@@ -0,0 +1,31 @@
1
+ interface CodegenConfigOptions {
2
+ /**
3
+ * Force specific definitions to emit `z.any()` instead of a full schema.
4
+ * Keyed by definition file name. Value can be `true` or a function receiving the schema.
5
+ *
6
+ * @example
7
+ * { ProtobufAny: true, SomeOther: (schema) => schema.properties?.['@type'] !== undefined }
8
+ */
9
+ overrideAsAny?: Record<string, boolean | ((schema: Record<string, any>) => boolean)>;
10
+ /**
11
+ * Generate barrel index files (index.ts, all-imports.ts, all-query-imports.ts).
12
+ * @default true
13
+ */
14
+ shouldProduceIndexFiles?: boolean;
15
+ /**
16
+ * Override the base path prepended to all API routes.
17
+ * @default `basePath` from the swagger spec
18
+ */
19
+ basePath?: string;
20
+ /**
21
+ * When enabled, each swagger set is generated into a subfolder named after its service name
22
+ * inside the output directory, instead of all sets sharing the same root output folder.
23
+ *
24
+ * If swaggers.json contains [["a", "b", "c", "d"]], then the service name is the "a".
25
+ *
26
+ * @default false
27
+ */
28
+ unstable_splitOutputByServiceName?: boolean;
29
+ }
30
+
31
+ export type { CodegenConfigOptions };
@@ -0,0 +1,31 @@
1
+ interface CodegenConfigOptions {
2
+ /**
3
+ * Force specific definitions to emit `z.any()` instead of a full schema.
4
+ * Keyed by definition file name. Value can be `true` or a function receiving the schema.
5
+ *
6
+ * @example
7
+ * { ProtobufAny: true, SomeOther: (schema) => schema.properties?.['@type'] !== undefined }
8
+ */
9
+ overrideAsAny?: Record<string, boolean | ((schema: Record<string, any>) => boolean)>;
10
+ /**
11
+ * Generate barrel index files (index.ts, all-imports.ts, all-query-imports.ts).
12
+ * @default true
13
+ */
14
+ shouldProduceIndexFiles?: boolean;
15
+ /**
16
+ * Override the base path prepended to all API routes.
17
+ * @default `basePath` from the swagger spec
18
+ */
19
+ basePath?: string;
20
+ /**
21
+ * When enabled, each swagger set is generated into a subfolder named after its service name
22
+ * inside the output directory, instead of all sets sharing the same root output folder.
23
+ *
24
+ * If swaggers.json contains [["a", "b", "c", "d"]], then the service name is the "a".
25
+ *
26
+ * @default false
27
+ */
28
+ unstable_splitOutputByServiceName?: boolean;
29
+ }
30
+
31
+ export type { CodegenConfigOptions };