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
This commit is contained in:
Frane Polić
2022-11-17 12:19:18 +00:00
committed by GitHub
parent f60267a494
commit 9eafde07fe
5 changed files with 65 additions and 2 deletions
+15 -1
View File
@@ -20,16 +20,30 @@ interface MedusaProviderProps {
baseUrl: string
queryClientProviderProps: QueryClientProviderProps
children: React.ReactNode
/**
* Authentication token
*/
apiKey?: string
/**
* PublishableApiKey identifier that defines the scope of resources
* available within the request
*/
publishableApiKey?: string
}
export const MedusaProvider = ({
queryClientProviderProps,
baseUrl,
apiKey,
publishableApiKey,
children,
}: MedusaProviderProps) => {
const medusaClient = new Medusa({ baseUrl, maxRetries: 0, apiKey })
const medusaClient = new Medusa({
baseUrl,
maxRetries: 0,
apiKey,
publishableApiKey,
})
return (
<QueryClientProvider {...queryClientProviderProps}>
<MedusaContext.Provider