README.md

# MapkitJS

Elixir helper library for generating [MapKit JS](https://developer.apple.com/documentation/mapkitjs) authorization tokens.

It is designed for simple server-side token issuance for browser clients that call `mapkit.init/1`.

## Installation

Add to your `mix.exs`:

```elixir
defp deps do
  [
    {:mapkit_js, "~> 0.1.0"}
  ]
end
```

## Configuration

```elixir
config :mapkit_js,
  team_id: System.get_env("APPLE_TEAM_ID"),
  key_id: System.get_env("MAPKIT_KEY_ID"),
  private_key_path: System.get_env("MAPKIT_PRIVATE_KEY_PATH"),
  origin: System.get_env("MAPKIT_ORIGIN")
```

Supported options:

- `team_id` - Apple Developer Team ID
- `key_id` - Maps key ID
- `private_key` - inline `.p8` contents
- `private_key_path` - path to the `.p8` file
- `origin` - optional exact browser origin to constrain the token
- `token_ttl_seconds` - defaults to `300`

## Quick Start

```elixir
# Generate a token
{:ok, token} = MapkitJS.token()

# Override origin per call
{:ok, token} = MapkitJS.generate_token(origin: "https://example.com")

# Build a payload for a browser-side authorization callback
{:ok, payload} = MapkitJS.authorization_callback_payload()
```

## Browser Usage

```javascript
mapkit.init({
  authorizationCallback: async function(done) {
    const response = await fetch("/token")
    const payload = await response.json()
    done(payload.token)
  }
})
```

## Local Demo

This project includes a local demo page and token server.

Run it with:

```bash
cd mapkit_js
source .env && elixir demo_server.exs
```

Then open:

```text
http://127.0.0.1:4002
```

## Live Smoke Test

```bash
cd mapkit_js
source .env && elixir test_live.exs
```

## Notes

- The `origin` claim must match the browser origin exactly when you use it.
- GitHub Pages works fine as an origin, for example `https://dl-alexandre.github.io`.
- Tokens are cached in `MapkitJS.TokenCache` with a 60-second refresh buffer.

## License

MIT