# Migration to TypeScript

This document describes the recommended migration path for converting `slack-notifier` from plain JavaScript to TypeScript while preserving support for both Node.js and browser usage.

## Goals

- Keep the same package name and repository
- Preserve server-side (`require('slack-notifier')`) support
- Preserve browser-side usage via a bundled global
- Keep behavior compatible with existing `configure()` / `send()` API
- Add type safety and build output

## Recommended Approach

1. Create a new `src/` folder
   - For example: `src/index.ts`

2. Rewrite `slack-notifier.js` in TypeScript
   - Define strong types for `configure()` options
   - Use a discriminated runtime check for server vs browser
   - Keep the same environment detection:
     - `typeof module !== 'undefined' && module.exports`
   - Keep the same browser global fallback:
     - `window.slackNotifier = ...`

3. Keep runtime dependencies
   - Node: use built-in `http` / `https`
   - Browser: either keep `jQuery` or replace with a small `fetch`/XHR wrapper
   - Prefer a browser-safe wrapper if you want to reduce external client dependency

4. Add build tooling
   - Install `typescript`
   - Add `tsconfig.json`
   - Add a bundler for browser output, such as `rollup` or `esbuild`
   - Build output should go into `dist/`

5. Update package metadata
   - `main`: point to `dist/slack-notifier.js` or similar
   - `types`: point to `dist/index.d.ts`
   - Optionally add `module` / `browser` fields if publishing ES/browser bundles
   - Keep the same package name so existing npm users continue to work

6. Keep examples up to date
   - Update `example/server.js` or add `example/server.ts`
   - Update `example/client.html` if the browser bundle path changes

## Example Migration Plan

- `src/index.ts`
- `tsconfig.json`
- `rollup.config.js` or `esbuild` script
- `package.json`
  - add `scripts`:
    - `build`
    - `build:browser`
    - `prepare`
- `dist/`
  - generated JS and declaration files
- `README.md`
  - document TypeScript support and browser bundle usage

## Notes

- This repo is the right place to migrate, because it preserves package continuity and upgrades the existing package rather than fragmenting it into a new repo.
- If you want a separate experimental package, create a new repo only when you need a different npm name or a clean legacy branch.

## Suggested `MIGRATE.md` Contents

Use this guide as the migration checklist and keep it in the repository root once the TypeScript rewrite is complete.
