Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .github/workflows/javascript.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,22 @@ jobs:
npm install
npm run lint

typescript_test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Node
uses: actions/setup-node@v3
with:
node-version: 20
cache-dependency-path: ./package-lock.json
cache: 'npm'
- name: Run TypeScript example
working-directory: .
run: |
npm install
npm run test-typescript

integration_tests:
uses: optimizely/javascript-sdk/.github/workflows/integration_test.yml@master
secrets:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ lerna-debug.log

coverage/
dist/
.build/

# user-specific ignores ought to be defined in user's `core.excludesfile`
.idea/*
Expand Down
4 changes: 4 additions & 0 deletions examples/typescript/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules/
dist/
*.log
.DS_Store
66 changes: 66 additions & 0 deletions examples/typescript/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# TypeScript Example - Optimizely SDK

This example demonstrates all factory functions from the Optimizely SDK, including static and polling datafile managers.

## Files

- `src/index.ts` - Main example demonstrating all SDK factory functions
- `datafile.json` - Test datafile with Optimizely configuration
- `datafile-server.js` - Simple HTTP server for testing polling datafile manager

## Running the Example

### From Project Root (Recommended)

Run from the project root to automatically build the SDK, create a tarball, and run the example:
```bash
npm run test-typescript
```

This will:
1. Install SDK dependencies
2. Pack the SDK as a tarball (triggers build via `prepare` script)
3. Install the tarball in this example
4. Build and run the TypeScript example
5. Start the datafile server automatically
6. Clean up after completion

### Manual Setup

### 1. Start the Datafile Server (for polling manager test)

In a separate terminal, run:
```bash
node datafile-server.js
```

This starts an HTTP server at `http://localhost:8910` that serves the `datafile.json` file. The polling project config manager will fetch updates from this server every 10 seconds.

### 2. Run the Example

```bash
npm start
```

## What This Example Demonstrates

1. **createStaticProjectConfigManager** - Creates a config manager with a static datafile
2. **createInstance** - Creates an Optimizely client instance with the static config manager
3. **createPollingProjectConfigManager** - Creates a config manager that polls for datafile updates from localhost:8910
4. **getSendBeaconEventDispatcher** - Gets the SendBeacon event dispatcher (Node.js returns undefined)
5. **eventDispatcher** - Default event dispatcher
6. **createForwardingEventProcessor** - Creates an event processor that forwards events immediately
7. **createBatchEventProcessor** - Creates an event processor that batches events
8. **createOdpManager** - Creates an Optimizely Data Platform manager
9. **createVuidManager** - Creates a Visitor Unique ID manager

## Test Datafile

The `datafile.json` contains a complete Optimizely configuration with:
- 2 feature flags (`flag_1`, `flag_2`)
- 4 experiments (`exp_1`, `exp_2`, `exp_3`, `exp_4`)
- 7 typed audiences based on age conditions
- 2 rollouts with delivery rules
- 1 integer variable (`integer_variable`)

This datafile is used for testing SDK functionality with realistic configuration.
61 changes: 61 additions & 0 deletions examples/typescript/datafile-server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#!/usr/bin/env node

/**
* Simple HTTP server to serve the datafile for testing polling project config manager
* Runs at http://localhost:8910
Copy link

Copilot AI Jan 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment at the top of this file says it will be "running at http://localhost:8910" but actually the constant PORT is defined on line 17 as 8910. While this is consistent, if someone changes the PORT constant, they should also update the comment. Consider referencing the PORT constant in the comment or making it dynamically reflect the actual port in the startup message (which it already does on line 46).

Suggested change
* Runs at http://localhost:8910
* Runs at http://localhost on the port specified by the PORT constant below

Copilot uses AI. Check for mistakes.
*/

import http from 'http';
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import { dirname } from 'path';

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

const PORT = 8910;
const datafilePath = path.join(__dirname, 'datafile.json');

const server = http.createServer((req, res) => {
console.log(`[${new Date().toISOString()}] ${req.method} ${req.url}`);

// Read and serve the datafile
fs.readFile(datafilePath, 'utf8', (err, data) => {
if (err) {
console.error('Error reading datafile:', err);
res.writeHead(500, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: 'Failed to read datafile' }));
return;
}

// Set CORS headers for cross-origin requests
res.writeHead(200, {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type'
});

res.end(data);
});
});

server.listen(PORT, () => {
console.log(`\n=== Datafile Server Started ===`);
console.log(`Server running at http://localhost:${PORT}`);
console.log(`Serving datafile from: ${datafilePath}`);
console.log(`\nPress Ctrl+C to stop the server\n`);
});

// Handle graceful shutdown
const shutdown = () => {
console.log('\n\nShutting down datafile server...');
server.close(() => {
console.log('Server stopped');
process.exit(0);
});
};

process.on('SIGINT', shutdown);
process.on('SIGTERM', shutdown);
Loading
Loading