algolia-codegen 0.1.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 ADDED
@@ -0,0 +1,129 @@
1
+ # Algolia Type Generator
2
+
3
+ This script automatically generates TypeScript types from your Algolia index by fetching a sample record and analyzing its structure.
4
+
5
+ **Repository**: [https://github.com/nightlightmare/algolia-codegen](https://github.com/nightlightmare/algolia-codegen)
6
+
7
+ ## Prerequisites
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.
16
+
17
+ ## Installation
18
+
19
+ Install the package globally or locally:
20
+
21
+ ```bash
22
+ # Global installation
23
+ npm install -g algolia-codegen
24
+
25
+ # Or local installation
26
+ npm install algolia-codegen
27
+ # or
28
+ pnpm add algolia-codegen
29
+ # or
30
+ yarn add algolia-codegen
31
+ ```
32
+
33
+ ## Usage
34
+
35
+ ### CLI Usage
36
+
37
+ After installation, you can use the CLI command:
38
+
39
+ ```bash
40
+ algolia-codegen
41
+ ```
42
+
43
+ Or if installed locally:
44
+
45
+ ```bash
46
+ npx algolia-codegen
47
+ ```
48
+
49
+ ### Programmatic Usage
50
+
51
+ You can also import and use the package programmatically:
52
+
53
+ ```typescript
54
+ import { main } from 'algolia-codegen';
55
+
56
+ main();
57
+ ```
58
+
59
+ ## How It Works
60
+
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
70
+
71
+ ## Generated Files
72
+
73
+ The script generates TypeScript interface files in the output directory (default: `src/shared/algolia/`).
74
+
75
+ Each type gets its own file (e.g., `AlgoliaCampground.ts`, `AlgoliaAddress.ts`), and an `index.ts` file exports all types.
76
+
77
+ ## Notes
78
+
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
83
+
84
+ ## Customization
85
+
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
91
+
92
+ For contributions and feature requests, please visit the [GitHub repository](https://github.com/nightlightmare/algolia-codegen).
93
+
94
+ ## Publishing
95
+
96
+ This package is automatically published to npm when changes are merged into the `main` branch via GitHub Actions.
97
+
98
+ ### Setting up NPM_TOKEN
99
+
100
+ To enable automatic publishing, you need to configure the `NPM_TOKEN` secret in your GitHub repository:
101
+
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**
109
+
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
114
+
115
+ ### Manual Publishing
116
+
117
+ To publish manually:
118
+
119
+ ```bash
120
+ pnpm build
121
+ pnpm publish
122
+ ```
123
+
124
+ ## Repository
125
+
126
+ - **GitHub**: [https://github.com/nightlightmare/algolia-codegen](https://github.com/nightlightmare/algolia-codegen)
127
+ - **Issues**: [https://github.com/nightlightmare/algolia-codegen/issues](https://github.com/nightlightmare/algolia-codegen/issues)
128
+ - **npm**: [https://www.npmjs.com/package/algolia-codegen](https://www.npmjs.com/package/algolia-codegen)
129
+
@@ -0,0 +1,8 @@
1
+ // src/index.ts
2
+ var main = () => {
3
+ console.log("Main!");
4
+ };
5
+
6
+ export {
7
+ main
8
+ };
package/dist/cli.cjs ADDED
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env node
2
+ "use strict";
3
+
4
+ // src/cli.ts
5
+ var import_commander = require("commander");
6
+
7
+ // src/index.ts
8
+ var main = () => {
9
+ console.log("Main!");
10
+ };
11
+
12
+ // src/cli.ts
13
+ var program = new import_commander.Command();
14
+ program.name("algolia-codegen").description("Generate TypeScript types from Algolia index").option("-c, --config <path>", "Config file path").action(() => {
15
+ main();
16
+ }).parse();
package/dist/cli.d.cts ADDED
@@ -0,0 +1 @@
1
+ #!/usr/bin/env node
package/dist/cli.d.ts ADDED
@@ -0,0 +1 @@
1
+ #!/usr/bin/env node
package/dist/cli.js ADDED
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env node
2
+ import {
3
+ main
4
+ } from "./chunk-U33HDFDI.js";
5
+
6
+ // src/cli.ts
7
+ import { Command } from "commander";
8
+ var program = new Command();
9
+ program.name("algolia-codegen").description("Generate TypeScript types from Algolia index").option("-c, --config <path>", "Config file path").action(() => {
10
+ main();
11
+ }).parse();
package/dist/index.cjs ADDED
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var src_exports = {};
22
+ __export(src_exports, {
23
+ main: () => main
24
+ });
25
+ module.exports = __toCommonJS(src_exports);
26
+ var main = () => {
27
+ console.log("Main!");
28
+ };
29
+ // Annotate the CommonJS export names for ESM import in node:
30
+ 0 && (module.exports = {
31
+ main
32
+ });
@@ -0,0 +1,3 @@
1
+ declare const main: () => void;
2
+
3
+ export { main };
@@ -0,0 +1,3 @@
1
+ declare const main: () => void;
2
+
3
+ export { main };
package/dist/index.js ADDED
@@ -0,0 +1,6 @@
1
+ import {
2
+ main
3
+ } from "./chunk-U33HDFDI.js";
4
+ export {
5
+ main
6
+ };
package/package.json ADDED
@@ -0,0 +1,58 @@
1
+ {
2
+ "name": "algolia-codegen",
3
+ "version": "0.1.0",
4
+ "description": "Generate TypeScript types from Algolia indices",
5
+ "license": "MIT",
6
+ "author": "nightlightmare",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "https://github.com/nightlightmare/algolia-codegen.git"
10
+ },
11
+ "homepage": "https://github.com/nightlightmare/algolia-codegen#readme",
12
+ "bugs": {
13
+ "url": "https://github.com/nightlightmare/algolia-codegen/issues"
14
+ },
15
+ "keywords": [
16
+ "algolia",
17
+ "typescript",
18
+ "codegen",
19
+ "type-generation",
20
+ "algolia-types",
21
+ "code-generation",
22
+ "typescript-generator"
23
+ ],
24
+ "type": "module",
25
+ "main": "./dist/index.js",
26
+ "bin": {
27
+ "algolia-codegen": "dist/cli.js"
28
+ },
29
+ "exports": {
30
+ ".": {
31
+ "types": "./dist/index.d.ts",
32
+ "import": "./dist/index.js",
33
+ "require": "./dist/index.cjs"
34
+ }
35
+ },
36
+ "files": [
37
+ "dist"
38
+ ],
39
+ "dependencies": {
40
+ "algoliasearch": "^4.24.0",
41
+ "commander": "^12.0.0"
42
+ },
43
+ "devDependencies": {
44
+ "tsup": "^8.0.0",
45
+ "typescript": "^5.4.0"
46
+ },
47
+ "engines": {
48
+ "node": ">=18"
49
+ },
50
+ "publishConfig": {
51
+ "access": "public"
52
+ },
53
+ "scripts": {
54
+ "build": "tsup",
55
+ "dev": "tsup --watch",
56
+ "clean": "rm -rf dist"
57
+ }
58
+ }