**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
26 lines
476 B
TypeScript
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()
|