Skip to content

Commit

Permalink
feat: [FFM-6507]: Add environment ID and account ID headers (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
knagurski authored Jan 20, 2023
1 parent ac6688f commit 6f98f3b
Show file tree
Hide file tree
Showing 11 changed files with 52 additions and 44 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,21 +120,21 @@ In case you want to import this library directly (without having to use npm or y

```html
<script type="module">
import { initialize, Event } from 'https://unpkg.com/@harnessio/ff-javascript-client-sdk@1.6.0/dist/sdk.client.js'
import { initialize, Event } from 'https://unpkg.com/@harnessio/ff-javascript-client-sdk@1.7.0/dist/sdk.client.js'
</script>
```

If you need to support old browsers which don't support ES Module:

```html
<script src="https://unpkg.com/@harnessio/ff-javascript-client-sdk@1.6.0/dist/sdk.client.js"></script>
<script src="https://unpkg.com/@harnessio/ff-javascript-client-sdk@1.7.0/dist/sdk.client.js"></script>
<script>
var initialize = HarnessFFSDK.initialize
var Event = HarnessFFSDK.Event
</script>
```

Remember to change the version `1.6.0` in the unpkg url accordingly.
Remember to change the version `1.7.0` in the unpkg url accordingly.

## License

Expand Down
12 changes: 6 additions & 6 deletions dist/sdk.cjs.js

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions dist/sdk.client-iife.js

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions dist/sdk.client.js

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions dist/sdk.esm.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion examples/preact/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/react-redux/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/react/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@harnessio/ff-javascript-client-sdk",
"version": "1.6.0",
"version": "1.7.0",
"author": "Harness",
"license": "Apache-2.0",
"main": "dist/sdk.cjs.js",
Expand Down
30 changes: 19 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import type {
import { Event } from './types'
import { defaultOptions, logError, MIN_EVENTS_SYNC_INTERVAL } from './utils'

const SDK_VERSION = '1.6.0'
const SDK_VERSION = '1.7.0'
const METRICS_VALID_COUNT_INTERVAL = 500
const fetch = globalThis.fetch
const EventSource = EventSourcePolyfill
Expand Down Expand Up @@ -54,6 +54,7 @@ const initialize = (apiKey: string, target: Target, options?: Options): Result =
let jwtToken: string
let metricsSchedulerId: number
let metricsCollectorEnabled = true
let standardHeaders: Record<string, string> = {}

const stopMetricsCollector = () => {
metricsCollectorEnabled = false
Expand Down Expand Up @@ -158,7 +159,7 @@ const initialize = (apiKey: string, target: Target, options?: Options): Result =

fetch(`${configurations.eventUrl}/metrics/${environment}?cluster=${clusterIdentifier}`, {
method: 'POST',
headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${jwtToken}` },
headers: { 'Content-Type': 'application/json', ...standardHeaders },
body: JSON.stringify(payload)
})
.then(() => {
Expand Down Expand Up @@ -278,7 +279,18 @@ const initialize = (apiKey: string, target: Target, options?: Options): Result =
if (closed) return

jwtToken = token
const decoded: { environment: string; clusterIdentifier: string } = jwt_decode(token)
const decoded: {
environment: string
environmentIdentifier: string
clusterIdentifier: string
accountID: string
} = jwt_decode(token)

standardHeaders = {
Authorization: `Bearer ${jwtToken}`,
'Harness-AccountID': decoded.accountID,
'Harness-EnvironmentID': decoded.environmentIdentifier
}

logDebug('Authenticated', decoded)

Expand Down Expand Up @@ -337,9 +349,7 @@ const initialize = (apiKey: string, target: Target, options?: Options): Result =
const res = await fetch(
`${configurations.baseUrl}/client/env/${environment}/target/${target.identifier}/evaluations?cluster=${clusterIdentifier}`,
{
headers: {
Authorization: `Bearer ${jwtToken}`
}
headers: standardHeaders
}
)
const data = await res.json()
Expand Down Expand Up @@ -368,9 +378,7 @@ const initialize = (apiKey: string, target: Target, options?: Options): Result =
const result = await fetch(
`${configurations.baseUrl}/client/env/${environment}/target/${target.identifier}/evaluations/${identifier}?cluster=${clusterIdentifier}`,
{
headers: {
Authorization: `Bearer ${jwtToken}`
}
headers: standardHeaders
}
)

Expand Down Expand Up @@ -421,8 +429,8 @@ const initialize = (apiKey: string, target: Target, options?: Options): Result =
}
eventSource = new EventSource(`${configurations.baseUrl}/stream?cluster=${clusterIdentifier}`, {
headers: {
Authorization: `Bearer ${jwtToken}`,
'API-Key': apiKey
'API-Key': apiKey,
...standardHeaders
}
})

Expand Down

0 comments on commit 6f98f3b

Please sign in to comment.