Files
medusa-store/packages/medusa-js/src/key-manager.ts
Frane Polić 9eafde07fe feat(medusa-js): Register PublishableApiKey (#2616)
**What**
- register a publishable API key through `medusa-js` i.e. define the key that will be sent with each request

**How**
- introduce KeyManager class which is used to share keys between medusa-js objects.

**Usage**
1. Set the key through the `Medusa` config
2. Set the key through `KeyManager` dynamically:
```ts
import { KeyManager } from "medusa-js"

KeyManager.registerPublishableApiKey("pk_123")
```

---

RESOLVES CORE-794
2022-11-17 12:19:18 +00:00

26 lines
476 B
TypeScript

/**
* `KeyManager` holds API keys in state.
*/
class KeyManager {
private publishableApiKey: string | null = null
/**
* Set a publishable api key to be sent with each request.
*/
public registerPublishableApiKey(key: string) {
this.publishableApiKey = key
}
/**
* Retrieve the publishable api key.
*/
public getPublishableApiKey() {
return this.publishableApiKey
}
}
/**
* Export singleton instance.
*/
export default new KeyManager()