Skip to content

Commit

Permalink
Merge pull request #604 from storyblok/feat/int-976-adding-default-he…
Browse files Browse the repository at this point in the history
…ader

feat(int-976): Adding default header
  • Loading branch information
ademarCardoso authored Jul 10, 2023
2 parents 7ca60a4 + 61306c7 commit 069d51a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<a href="https://www.storyblok.com?utm_source=github.com&utm_medium=readme&utm_campaign=storyblok-js-client" align="center">
<img src="https://a.storyblok.com/f/88751/1776x360/4d075611c6/sb-js-sdk.png" alt="Storyblok Logo">
</a>
<h1 align="center">Universal JavaScript SDK for Storyblok's API</h1>
<h1 align="center">Universal JavaScript Client for Storyblok's API</h1>
<p align="center">This client is a thin wrapper for the <a href="http://www.storyblok.com?utm_source=github.com&utm_medium=readme&utm_campaign=storyblok-js-client" target="_blank">Storyblok</a> API's to use in Node.js and the browser.</p>
</div>

Expand Down
8 changes: 8 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,11 @@ type ObjectValues<T> = T[keyof T]
type Method = ObjectValues<typeof METHOD>

export default Method

export const STORYBLOK_AGENT = 'SB-Agent'

export const STORYBLOK_JS_CLIENT_AGENT = {
defaultAgentName: 'SB-JS-CLIENT',
defaultAgentVersion: 'SB-Agent-Version',
packageVersion: process.env.npm_package_version || '5.0.0'
}
14 changes: 10 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import throttledQueue from './throttlePromise'
import RichTextResolver from './richTextResolver'
import { SbHelpers } from './sbHelpers'
import SbFetch from './sbFetch'
import { STORYBLOK_AGENT, STORYBLOK_JS_CLIENT_AGENT } from './constants'

import Method from './constants'
import {
Expand Down Expand Up @@ -99,11 +100,16 @@ class Storyblok {
headers.set('Content-Type', 'application/json')
headers.set('Accept', 'application/json')

headers.forEach((value, key) => {
if (config.headers && config.headers[key]) {
headers.set(key, config.headers[key])
if (config.headers) {
for (const header in config.headers) {
headers.set(header, config.headers[header])
}
})
}

if (!headers.has(STORYBLOK_AGENT)) {
headers.set(STORYBLOK_AGENT, STORYBLOK_JS_CLIENT_AGENT.defaultAgentName)
headers.set( STORYBLOK_JS_CLIENT_AGENT.defaultAgentVersion, STORYBLOK_JS_CLIENT_AGENT.packageVersion)
}

let rateLimit = 5 // per second for cdn api

Expand Down

0 comments on commit 069d51a

Please sign in to comment.