algolia-codegen 0.1.0 → 0.1.1

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
@@ -1,18 +1,13 @@
1
1
  # Algolia Type Generator
2
2
 
3
- This script automatically generates TypeScript types from your Algolia index by fetching a sample record and analyzing its structure.
3
+ This script automatically generates TypeScript types from your Algolia indices by fetching sample records and analyzing their structure. It supports multiple indices and flexible configuration through a config file.
4
4
 
5
5
  **Repository**: [https://github.com/nightlightmare/algolia-codegen](https://github.com/nightlightmare/algolia-codegen)
6
6
 
7
7
  ## Prerequisites
8
8
 
9
- Make sure you have the following environment variables set in your `.env` file:
10
-
11
- - `ALGOLIA_APP_ID` - Your Algolia Application ID
12
- - `ALGOLIA_SEARCH_KEY` - Your Algolia Search API Key
13
- - `ALGOLIA_INDEX_NAME` - Your Algolia Index Name
14
-
15
- The script will automatically check for `.env`, `.env.local`, and `.env.development.local` files in the current directory.
9
+ - Node.js >= 18
10
+ - Algolia account with at least one index
16
11
 
17
12
  ## Installation
18
13
 
@@ -30,11 +25,30 @@ pnpm add algolia-codegen
30
25
  yarn add algolia-codegen
31
26
  ```
32
27
 
33
- ## Usage
28
+ ## Quick Start
34
29
 
35
- ### CLI Usage
30
+ 1. Create a configuration file named `algolia-codegen.ts` (or `.js`) in your project root:
36
31
 
37
- After installation, you can use the CLI command:
32
+ ```typescript
33
+ import type { AlgoliaCodegenConfig } from "algolia-codegen";
34
+
35
+ const config: AlgoliaCodegenConfig = {
36
+ overwrite: true,
37
+ generates: {
38
+ "src/algolia/types.ts": {
39
+ appId: "YOUR_APP_ID",
40
+ searchKey: "YOUR_SEARCH_API_KEY",
41
+ indexName: "products",
42
+ prefix: "Algolia", // Optional
43
+ postfix: "Type", // Optional
44
+ },
45
+ },
46
+ };
47
+
48
+ export default config;
49
+ ```
50
+
51
+ 2. Run the generator:
38
52
 
39
53
  ```bash
40
54
  algolia-codegen
@@ -46,79 +60,194 @@ Or if installed locally:
46
60
  npx algolia-codegen
47
61
  ```
48
62
 
49
- ### Programmatic Usage
63
+ ## Configuration
50
64
 
51
- You can also import and use the package programmatically:
65
+ Create a configuration file named `algolia-codegen.ts` (or `.js`) in your project root. The config file should export a default object with the following structure:
52
66
 
53
67
  ```typescript
54
- import { main } from 'algolia-codegen';
68
+ import type { AlgoliaCodegenConfig } from "algolia-codegen";
69
+
70
+ const config: AlgoliaCodegenConfig = {
71
+ overwrite: true,
72
+ generates: {
73
+ "src/algolia/types.ts": {
74
+ appId: "YOUR_APP_ID",
75
+ searchKey: "YOUR_SEARCH_API_KEY",
76
+ indexName: "products",
77
+ prefix: "Algolia", // Optional
78
+ postfix: "Type", // Optional
79
+ },
80
+ },
81
+ };
82
+
83
+ export default config;
84
+ ```
85
+
86
+ ### Multiple Indices
87
+
88
+ You can generate types for multiple indices:
55
89
 
56
- main();
90
+ ```typescript
91
+ const config: AlgoliaCodegenConfig = {
92
+ overwrite: true,
93
+ generates: {
94
+ "src/algolia/products.ts": {
95
+ appId: "YOUR_APP_ID",
96
+ searchKey: "YOUR_SEARCH_API_KEY",
97
+ indexName: "products",
98
+ },
99
+ "src/algolia/users.ts": {
100
+ appId: "YOUR_APP_ID",
101
+ searchKey: "YOUR_SEARCH_API_KEY",
102
+ indexName: "users",
103
+ },
104
+ },
105
+ };
57
106
  ```
58
107
 
59
- ## How It Works
108
+ ### Array of Configurations
109
+
110
+ You can also use an array of configurations:
60
111
 
61
- 1. **Connects to Algolia**: Uses your configured Algolia credentials to connect to your index
62
- 2. **Fetches Sample Record**: Retrieves one record from your Algolia index
63
- 3. **Analyzes Structure**: Recursively analyzes the JSON structure to infer TypeScript types
64
- 4. **Generates Types**: Creates TypeScript interface files in the specified output directory
65
- 5. **Handles Special Cases**:
66
- - Detects `AlgoliaIdValue` patterns (objects with `id` and `value` properties)
67
- - Handles nested objects and arrays
68
- - Preserves optional fields (null/undefined values)
69
- - Generates proper imports between types
112
+ ```typescript
113
+ const config: AlgoliaCodegenConfig = {
114
+ overwrite: true,
115
+ generates: [
116
+ {
117
+ "src/algolia/products.ts": {
118
+ appId: "YOUR_APP_ID",
119
+ searchKey: "YOUR_SEARCH_API_KEY",
120
+ indexName: "products",
121
+ },
122
+ },
123
+ {
124
+ "src/algolia/users.ts": {
125
+ appId: "YOUR_APP_ID",
126
+ searchKey: "YOUR_SEARCH_API_KEY",
127
+ indexName: "users",
128
+ },
129
+ },
130
+ ],
131
+ };
132
+ ```
70
133
 
71
- ## Generated Files
134
+ ## Usage
72
135
 
73
- The script generates TypeScript interface files in the output directory (default: `src/shared/algolia/`).
136
+ ### CLI Usage
74
137
 
75
- Each type gets its own file (e.g., `AlgoliaCampground.ts`, `AlgoliaAddress.ts`), and an `index.ts` file exports all types.
138
+ After installation, you can use the CLI command:
76
139
 
77
- ## Notes
140
+ ```bash
141
+ algolia-codegen
142
+ ```
78
143
 
79
- - The script analyzes a single sample record, so make sure your index has at least one record
80
- - If your data structure varies significantly between records, you may need to manually adjust some types
81
- - The script will overwrite existing type files, so make sure to commit your changes before running
82
- - Consider running this script as part of your CI/CD pipeline to keep types in sync with your Algolia index
144
+ Or specify a custom config file:
83
145
 
84
- ## Customization
146
+ ```bash
147
+ algolia-codegen --config path/to/config.ts
148
+ # or
149
+ algolia-codegen -c path/to/config.ts
150
+ ```
85
151
 
86
- You can modify `generate-types.ts` to:
87
- - Adjust type naming conventions
88
- - Add custom type inference logic
89
- - Change the output directory
90
- - Add additional type transformations
152
+ Or if installed locally:
91
153
 
92
- For contributions and feature requests, please visit the [GitHub repository](https://github.com/nightlightmare/algolia-codegen).
154
+ ```bash
155
+ npx algolia-codegen
156
+ ```
93
157
 
94
- ## Publishing
158
+ ## Configuration Schema
95
159
 
96
- This package is automatically published to npm when changes are merged into the `main` branch via GitHub Actions.
160
+ ### `AlgoliaCodegenConfig`
97
161
 
98
- ### Setting up NPM_TOKEN
162
+ ```typescript
163
+ type AlgoliaCodegenConfig = {
164
+ overwrite: boolean;
165
+ generates: InstanceOrArray<UrlSchema>;
166
+ };
167
+ ```
99
168
 
100
- To enable automatic publishing, you need to configure the `NPM_TOKEN` secret in your GitHub repository:
169
+ ### `AlgoliaCodegenGeneratorConfig`
101
170
 
102
- 1. Go to your GitHub repository
103
- 2. Navigate to **Settings** → **Secrets and variables** → **Actions**
104
- 3. Click **New repository secret**
105
- 4. Name: `NPM_TOKEN`
106
- 5. Value: Your npm access token (create one at https://www.npmjs.com/settings/YOUR_USERNAME/tokens)
107
- 6. Make sure the token has **Automation** or **Publish** permissions
108
- 7. Click **Add secret**
171
+ ```typescript
172
+ type AlgoliaCodegenGeneratorConfig = {
173
+ appId: string; // Required: Algolia Application ID
174
+ searchKey: string; // Required: Algolia Search API Key
175
+ indexName: string; // Required: Algolia Index Name
176
+ prefix?: string; // Optional: Prefix for generated type names
177
+ postfix?: string; // Optional: Postfix for generated type names
178
+ };
179
+ ```
109
180
 
110
- The workflow will automatically:
111
- - Build the package
112
- - Publish to npm when PRs are merged into `main`
113
- - Use provenance for enhanced security
181
+ ## How It Works
114
182
 
115
- ### Manual Publishing
183
+ 1. **Loads Configuration**: Reads the `algolia-codegen.ts` config file (or custom path)
184
+ 2. **Processes Each Path**: For each file path specified in the config:
185
+ - Connects to Algolia using the provided credentials
186
+ - Fetches a sample record from the specified index
187
+ - Analyzes the record structure and generates TypeScript types
188
+ - Creates a single TypeScript file containing all types found in the index
189
+ 3. **Type Generation**: The generator automatically:
190
+ - Infers types from the sample record structure
191
+ - Handles nested objects, arrays, and complex types
192
+ - Detects and generates generic `IdValue<T>` types for Algolia's id-value pattern arrays
193
+ - Generates proper TypeScript interfaces with JSDoc comments
194
+ - Sorts types by dependencies for correct ordering
195
+ 4. **Error Handling**: Continues processing other files even if one fails, with detailed error messages
196
+
197
+ Each generated file contains all types found in the index, including nested types, properly organized and sorted by dependencies.
116
198
 
117
- To publish manually:
199
+ ## Notes
118
200
 
119
- ```bash
120
- pnpm build
121
- pnpm publish
201
+ - Each index must have at least one record for the script to work
202
+ - The script processes files sequentially and continues even if one fails
203
+ - Make sure your config file exports a default object
204
+ - For TypeScript config files, you may need to use `tsx` or compile them first: `tsx algolia-codegen`
205
+ - Each generated file contains all types found in the index (including nested types) in a single file
206
+ - Types are automatically sorted by dependencies to ensure correct ordering
207
+ - The generator handles arrays, nested objects, optional fields, and null values
208
+ - Automatically detects Algolia's `IdValue` pattern (arrays of objects with `id` and `value` properties) and generates generic types
209
+ - The project uses ES Modules - all local imports use `.js` extensions
210
+ - The library is compiled to both ESM and CommonJS formats for maximum compatibility
211
+
212
+ ## Examples
213
+
214
+ ### Generated Type Example
215
+
216
+ Given an Algolia record like:
217
+
218
+ ```json
219
+ {
220
+ "objectID": "123",
221
+ "name": "Product Name",
222
+ "price": 99.99,
223
+ "tags": ["tag1", "tag2"],
224
+ "metadata": {
225
+ "category": "electronics",
226
+ "rating": 4.5
227
+ }
228
+ }
229
+ ```
230
+
231
+ The generator will create TypeScript types:
232
+
233
+ ```typescript
234
+ /**
235
+ * Generated TypeScript types for Algolia index: products
236
+ * This file is auto-generated. Do not edit manually.
237
+ */
238
+
239
+ export interface AlgoliaHitType {
240
+ metadata: AlgoliaMetadataType;
241
+ name: string;
242
+ objectID: string;
243
+ price: number;
244
+ tags: string[];
245
+ }
246
+
247
+ export interface AlgoliaMetadataType {
248
+ category: string;
249
+ rating: number;
250
+ }
122
251
  ```
123
252
 
124
253
  ## Repository
@@ -127,3 +256,6 @@ pnpm publish
127
256
  - **Issues**: [https://github.com/nightlightmare/algolia-codegen/issues](https://github.com/nightlightmare/algolia-codegen/issues)
128
257
  - **npm**: [https://www.npmjs.com/package/algolia-codegen](https://www.npmjs.com/package/algolia-codegen)
129
258
 
259
+ ## License
260
+
261
+ MIT