algolia-codegen 0.1.0 → 0.1.2
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 +194 -60
- package/dist/chunk-R66ECGLW.js +583 -0
- package/dist/cli.cjs +605 -4
- package/dist/cli.js +10 -3
- package/dist/index.cjs +594 -4
- package/dist/index.d.cts +36 -2
- package/dist/index.d.ts +36 -2
- package/dist/index.js +9 -3
- package/package.json +9 -4
- package/dist/chunk-U33HDFDI.js +0 -8
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
|
|
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
|
-
|
|
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
|
-
##
|
|
28
|
+
## Quick Start
|
|
34
29
|
|
|
35
|
-
|
|
30
|
+
1. Create a configuration file named `algolia-codegen.ts` (or `.js`) in your project root:
|
|
36
31
|
|
|
37
|
-
|
|
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,196 @@ Or if installed locally:
|
|
|
46
60
|
npx algolia-codegen
|
|
47
61
|
```
|
|
48
62
|
|
|
49
|
-
|
|
63
|
+
## Configuration
|
|
50
64
|
|
|
51
|
-
|
|
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 {
|
|
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
|
-
|
|
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
|
-
|
|
108
|
+
### Array of Configurations
|
|
109
|
+
|
|
110
|
+
You can also use an array of configurations:
|
|
60
111
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
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
|
-
##
|
|
134
|
+
## Usage
|
|
72
135
|
|
|
73
|
-
|
|
136
|
+
### CLI Usage
|
|
74
137
|
|
|
75
|
-
|
|
138
|
+
After installation, you can use the CLI command:
|
|
76
139
|
|
|
77
|
-
|
|
140
|
+
```bash
|
|
141
|
+
algolia-codegen
|
|
142
|
+
```
|
|
78
143
|
|
|
79
|
-
|
|
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
|
-
|
|
146
|
+
```bash
|
|
147
|
+
algolia-codegen --config path/to/config.ts
|
|
148
|
+
# or
|
|
149
|
+
algolia-codegen -c path/to/config.ts
|
|
150
|
+
```
|
|
85
151
|
|
|
86
|
-
|
|
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
|
-
|
|
154
|
+
```bash
|
|
155
|
+
npx algolia-codegen
|
|
156
|
+
```
|
|
93
157
|
|
|
94
|
-
##
|
|
158
|
+
## Configuration Schema
|
|
95
159
|
|
|
96
|
-
|
|
160
|
+
### `AlgoliaCodegenConfig`
|
|
97
161
|
|
|
98
|
-
|
|
162
|
+
```typescript
|
|
163
|
+
type AlgoliaCodegenConfig = {
|
|
164
|
+
overwrite: boolean;
|
|
165
|
+
generates: InstanceOrArray<UrlSchema>;
|
|
166
|
+
};
|
|
167
|
+
```
|
|
99
168
|
|
|
100
|
-
|
|
169
|
+
### `AlgoliaCodegenGeneratorConfig`
|
|
101
170
|
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
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
|
-
|
|
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
|
-
|
|
183
|
+
1. **Loads Environment Variables**: Automatically loads `.env` file if present in the current directory
|
|
184
|
+
2. **Loads Configuration**: Reads the `algolia-codegen.ts` config file (or custom path), supporting both TypeScript and JavaScript
|
|
185
|
+
3. **Processes Each Path**: For each file path specified in the config:
|
|
186
|
+
- Connects to Algolia using the provided credentials
|
|
187
|
+
- Fetches a sample record from the specified index
|
|
188
|
+
- Analyzes the record structure and generates TypeScript types
|
|
189
|
+
- Creates a single TypeScript file containing all types found in the index
|
|
190
|
+
3. **Type Generation**: The generator automatically:
|
|
191
|
+
- Infers types from the sample record structure
|
|
192
|
+
- Handles nested objects, arrays, and complex types
|
|
193
|
+
- Detects and generates generic `IdValue<T>` types for Algolia's id-value pattern arrays
|
|
194
|
+
- Generates proper TypeScript interfaces with JSDoc comments
|
|
195
|
+
- Sorts types by dependencies for correct ordering
|
|
196
|
+
4. **Error Handling**: Continues processing other files even if one fails, with detailed error messages
|
|
197
|
+
|
|
198
|
+
Each generated file contains all types found in the index, including nested types, properly organized and sorted by dependencies.
|
|
116
199
|
|
|
117
|
-
|
|
200
|
+
## Notes
|
|
118
201
|
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
202
|
+
- Each index must have at least one record for the script to work
|
|
203
|
+
- The script processes files sequentially and continues even if one fails
|
|
204
|
+
- Make sure your config file exports a default object
|
|
205
|
+
- TypeScript config files (`.ts`) are automatically supported - no compilation needed
|
|
206
|
+
- Environment variables from `.env` file are automatically loaded if present in the current directory
|
|
207
|
+
- Each generated file contains all types found in the index (including nested types) in a single file
|
|
208
|
+
- Types are automatically sorted by dependencies to ensure correct ordering
|
|
209
|
+
- The generator handles arrays, nested objects, optional fields, and null values
|
|
210
|
+
- Automatically detects Algolia's `IdValue` pattern (arrays of objects with `id` and `value` properties) and generates generic types
|
|
211
|
+
- The project uses ES Modules - all local imports use `.js` extensions
|
|
212
|
+
- The library is compiled to both ESM and CommonJS formats for maximum compatibility
|
|
213
|
+
|
|
214
|
+
## Examples
|
|
215
|
+
|
|
216
|
+
### Generated Type Example
|
|
217
|
+
|
|
218
|
+
Given an Algolia record like:
|
|
219
|
+
|
|
220
|
+
```json
|
|
221
|
+
{
|
|
222
|
+
"objectID": "123",
|
|
223
|
+
"name": "Product Name",
|
|
224
|
+
"price": 99.99,
|
|
225
|
+
"tags": ["tag1", "tag2"],
|
|
226
|
+
"metadata": {
|
|
227
|
+
"category": "electronics",
|
|
228
|
+
"rating": 4.5
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
The generator will create TypeScript types:
|
|
234
|
+
|
|
235
|
+
```typescript
|
|
236
|
+
/**
|
|
237
|
+
* Generated TypeScript types for Algolia index: products
|
|
238
|
+
* This file is auto-generated. Do not edit manually.
|
|
239
|
+
*/
|
|
240
|
+
|
|
241
|
+
export interface AlgoliaHitType {
|
|
242
|
+
metadata: AlgoliaMetadataType;
|
|
243
|
+
name: string;
|
|
244
|
+
objectID: string;
|
|
245
|
+
price: number;
|
|
246
|
+
tags: string[];
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
export interface AlgoliaMetadataType {
|
|
250
|
+
category: string;
|
|
251
|
+
rating: number;
|
|
252
|
+
}
|
|
122
253
|
```
|
|
123
254
|
|
|
124
255
|
## Repository
|
|
@@ -127,3 +258,6 @@ pnpm publish
|
|
|
127
258
|
- **Issues**: [https://github.com/nightlightmare/algolia-codegen/issues](https://github.com/nightlightmare/algolia-codegen/issues)
|
|
128
259
|
- **npm**: [https://www.npmjs.com/package/algolia-codegen](https://www.npmjs.com/package/algolia-codegen)
|
|
129
260
|
|
|
261
|
+
## License
|
|
262
|
+
|
|
263
|
+
MIT
|