UNPKG

@relewise/create-relewise-learning-example

Version:

CLI tool to scaffold new Relewise learning projects with TypeScript, examples, and AI instructions

340 lines (266 loc) โ€ข 11.1 kB
# Create Relewise Learning Example A **CLI tool** for quick scaffolding of Relewise learning projects with a preconfigured Node.js + TypeScript setup. ## โœจ Features - TypeScript (with strict settings) - ESLint + Prettier preconfigured (with custom formatting rules) - Relewise SDK (`@relewise/client`, `@relewise/integrations`) - `.vscode` launch configuration with MCP server integration - Comprehensive `examples/` folder with search, recommendations, import, historical data import, and tracking - Configuration management with `src/config/relewiseConfig.ts` - Type definitions and utilities for external data integration - Example `docs/` folder for internal documentation - Ready-to-use `src/` folder with utilities and starter code - **QUICKSTART.md** - Complete step-by-step tutorial - Git initialization (optional) - Automatic dependency installation --- ## ๐Ÿš€ Usage ### Option 1: Run with npx (Recommended) ```bash npx @relewise/create-relewise-learning-example ``` ### Option 2: Run directly from GitHub ```bash npx github:Relewise/adhoc-relewise-examples ``` ## ๐Ÿ“‹ Requirements - Node.js 16 or higher - npm or yarn ## ๐Ÿ› ๏ธ CLI Options ```bash # Show help npx @relewise/create-relewise-learning-example --help # Show version npx @relewise/create-relewise-learning-example --version ``` You will be prompted for: 1. **Project name** โ€“ The folder name for the new project. 2. **Description** โ€“ Used in `package.json`. 3. **Git initialization** โ€“ Whether to automatically run `git init`. ### ๐Ÿ“‹ Required Relewise Credentials Before running examples, you'll need: - **Dataset ID** - Your Relewise dataset identifier - **API Key** - Must have **Read & Write permissions** for: - Product search and recommendations - User tracking and behavioral data - Product import/export operations - **Server URL** - Use sandbox for testing: `https://sandbox-api.relewise.com` > ๐Ÿ’ก **Tip**: Start with the sandbox environment for development. You can find your credentials in the Relewise dashboard under Settings > API Keys. For complete setup instructions, see the **[QUICKSTART.md](./template/QUICKSTART.md)** guide included with every generated project. --- ## ๐Ÿ“‚ Project Structure When generated, a new project will look like this: ``` MY-RELEWISE-EXAMPLES/ โ”œโ”€โ”€ .github/ โ”‚ โ””โ”€โ”€ copilot-instructions.md โ”œโ”€โ”€ .vscode/ โ”‚ โ”œโ”€โ”€ launch.json โ”‚ โ”œโ”€โ”€ mcp.json โ”‚ โ””โ”€โ”€ settings.json โ”œโ”€โ”€ docs/ โ”‚ โ””โ”€โ”€ relewise-response-patterns.md โ”œโ”€โ”€ product_data/ โ”‚ โ””โ”€โ”€ product_data_example.json โ”œโ”€โ”€ src/ โ”‚ โ”œโ”€โ”€ config/ โ”‚ โ”‚ โ””โ”€โ”€ relewiseConfig.ts โ”‚ โ”œโ”€โ”€ examples/ โ”‚ โ”‚ โ”œโ”€โ”€ import/ โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ productImportExample.ts โ”‚ โ”‚ โ”‚ โ””โ”€โ”€ historicalDataImportExample.ts โ”‚ โ”‚ โ”œโ”€โ”€ recommendations/ โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ popularProductsExample.ts โ”‚ โ”‚ โ”‚ โ””โ”€โ”€ purchasedWithProductExample.ts โ”‚ โ”‚ โ”œโ”€โ”€ search/ โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ categoryBasedSearchExample.ts โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ cdpPersonalizedSearchExample.ts โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ searchCategoryExample.ts โ”‚ โ”‚ โ”‚ โ””โ”€โ”€ termBasedSearchExample.ts โ”‚ โ”‚ โ”œโ”€โ”€ tracking/ โ”‚ โ”‚ โ”‚ โ””โ”€โ”€ basicTrackingExample.ts โ”‚ โ”‚ โ””โ”€โ”€ README.md โ”‚ โ”œโ”€โ”€ types/ โ”‚ โ”‚ โ”œโ”€โ”€ externalData.ts โ”‚ โ”‚ โ””โ”€โ”€ interfaces.ts โ”‚ โ”œโ”€โ”€ utils/ โ”‚ โ”‚ โ”œโ”€โ”€ externalDataServices.ts โ”‚ โ”‚ โ””โ”€โ”€ relewiseTypeGuards.ts โ”‚ โ””โ”€โ”€ index.ts โ”œโ”€โ”€ .env.local โ”œโ”€โ”€ .gitignore โ”œโ”€โ”€ .prettierrc โ”œโ”€โ”€ eslint.config.js โ”œโ”€โ”€ package-lock.json โ”œโ”€โ”€ package.json โ”œโ”€โ”€ QUICKSTART.md โ”œโ”€โ”€ README.md โ””โ”€โ”€ tsconfig.json ``` ## ๐Ÿ› ๏ธ How It Works The CLI (`bin/index.js`): 1. **Prompts for project details** using `inquirer`. 2. **Copies** the `template/` directory to the target folder. 3. **Removes any `.git` folder** from the template (to start fresh). 4. **Updates `package.json`** with the project name and description. 5. **Runs `npm install`** to install all dependencies. 6. **Initializes Git** if requested. --- ## โœจ Example ```bash $ npx @relewise/create-relewise-learning-example ๐Ÿš€ Welcome to the Relewise examples and AI instructions setup! ? Project name: my-relewise-learning ? Project description: My Relewise learning and experimentation project ? Initialize a new Git repository? (Y/n) Y ๐Ÿ“‚ Creating project in /path/to/my-relewise-learning ๐Ÿงน Removed template .git folder ๐Ÿ“ฆ Installing dependencies (this may take a minute)... ๐Ÿ”ง Initializing Git repository... โœ… Project scaffolded successfully! Next steps: cd my-relewise-learning # 1. Configure your environment variables # Edit .env with your Relewise credentials (already created from template) nano .env # 2. Follow the quickstart tutorial cat QUICKSTART.md # 3. Run examples npm run dev termBasedSearchExample "headphones" ``` --- ## ๐Ÿงฉ Template Project The template includes: - **TypeScript** (`tsconfig.json` with strict settings and ES modules) - **Linting & Formatting** (`eslint`, `prettier` with custom rules) - **Relewise SDK** preinstalled with latest versions - **VS Code Integration**: - Debug configurations (`.vscode/launch.json`) - MCP server for Relewise documentation (`.vscode/mcp.json`) - Workspace settings (`.vscode/settings.json`) - **Comprehensive Examples**: - Search (basic, category, personalized with CDP) - Recommendations (popular products, purchased-with-product) - Product import with multilingual support - Historical data import with proper timeline preservation - User tracking implementation - **Configuration Management**: - Centralized Relewise configuration (`src/config/relewiseConfig.ts`) - Environment variable validation - Pre-configured clients (searcher, recommender, integrator) - **Type Safety**: - Custom type definitions (`src/types/`) - Type guards for external data (`src/utils/relewiseTypeGuards.ts`) - **Documentation**: - Complete quickstart tutorial (`QUICKSTART.md`) - Copilot instructions for AI assistance (`.github/copilot-instructions.md`) - Example documentation patterns (`docs/`) - **Real Product Data**: Sample dataset for immediate testing (`product_data/`) --- ## ๐Ÿ›ก Requirements - **Node.js** 18+ - **npm** 9+ (or `npx` support) --- ## โš™๏ธ Development (for the CLI itself) If you are modifying this CLI: ```bash git clone git@github.com:Relewise/adhoc-relewise-examples.git cd adhoc-relewise-examples npm install npm link # to test locally create-relewise-learning-example my-local-test ``` --- ## ๐ŸŽฏ Available Examples The generated project includes comprehensive examples that you can run immediately: ### Search Examples ```bash npm run dev termBasedSearchExample "headphones" npm run dev categoryBasedSearchExample "5" # Hi-Fi category from sample data npm run dev searchCategoryExample "5" # Hi-Fi category from sample data npm run dev cdpPersonalizedSearchExample user_1 "headphones" ``` ### Recommendation Examples ```bash npm run dev popularProductsExample npm run dev purchasedWithProductExample "00198c54-6c62-4e08-be40-a539963985d0" ``` > **Note**: The `purchasedWithProductExample` returns recommendations with `rank` scores. Lower rank values (e.g., 1, 2, 3) indicate stronger relevance - products ranked 1 are most commonly purchased together with the specified product. Results are sorted in ascending order by rank for optimal relevance. ### Import & Tracking Examples ```bash npm run dev productImportExample npm run dev historicalDataImportExample npm run dev basicTrackingExample ``` Each example is fully documented and uses real product data from the included `product_data_example.json` file. --- ## ๐Ÿ“š Getting Started Guide The generated project includes a comprehensive `QUICKSTART.md` that provides: - **Step-by-step setup instructions** with environment configuration - **Implementation walkthrough** following official Relewise documentation - **Hands-on examples** you can run immediately - **Best practices** for production implementation - **Troubleshooting guide** for common issues ### Key Features: - Environment setup with `.env.local` template - SDK verification process for reliable integrations - Real product data examples with multilingual support - Historical data import with timeline preservation for personalization - CDP integration patterns for personalized experiences ### VS Code Integration: - **MCP Server**: Direct access to Relewise documentation within VS Code - **Debug Configurations**: Pre-configured launch settings for all examples - **AI Assistance**: Copilot instructions for accurate Relewise SDK usage --- ## ๐Ÿ“„ Product Data Structure The `product_data_example.json` file contains an array of products to be used for the Product import example. Each product is structured to support **multi-language**, **multi-market**, and **multi-channel** contexts. ### High-Level Structure - **Identification & Metadata** - `unique_id`: Unique product identifier. - `brand`: Object with `id` and `displayName`. - `channels`: Sales channels (e.g., `B2C`, `B2B`). - `markets`: Markets where the product is available (e.g., `dk`, `gb`, `se`). - **Product Information** - `name` and `description`: Arrays with localized values for multiple languages. - `image`: URL to the product image. - **Category Information** - `mainCategory`: The main category object: - `id`: Category ID. - `name`: Localized category names. - `subCategory`: Nested object with: - `id`: Subcategory ID. - `name`: Localized subcategory names. - **Pricing & Availability** - `list_price` and `sales_price`: Arrays of price objects (amount and currency). - `OnSale`, `soldOut`, `lowStock`: Arrays with localized boolean values. - `availability`: Localized stock availability. - `margin`: Profit margin classification (e.g., `Low`, `Medium`, `High`). - **Promotion & Campaigns** - `promoted`: Optional flags for promotions (localized). - `campaignIds`: Linked campaign identifiers if applicable. - **Additional Attributes** - `daysAvailable`: Number of days available (nullable). - `salesStatus`: Current sales state (nullable). This structure provides everything required for building product search, recommendations, and merchandising features. ### Example ```json { "unique_id": "12345", "brand": { "id": "bose", "displayName": "Bose" }, "channels": ["B2C"], "markets": ["en"], "name": [{ "language": "en-gb", "value": "Noise cancelling headphones" }], "mainCategory": { "id": "audio", "name": [{ "language": "en-gb", "value": "Audio" }], "subCategory": { "id": "headphones", "name": [{ "language": "en-gb", "value": "Headphones" }] } }, "sales_price": [{ "amount": 1999, "currency": "GBP" }], "availability": [{ "language": "en-gb", "value": 12 }], "margin": "High" } ```