diff --git a/README.md b/README.md index 73f12ce4..914bdb31 100755 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ Storyblok Logo -

Universal JavaScript SDK for Storyblok's API

+

Universal JavaScript Client for Storyblok's API

This client is a thin wrapper for the Storyblok API's to use in Node.js and the browser.

diff --git a/src/constants.ts b/src/constants.ts index 36f41338..70cb7a46 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -9,3 +9,11 @@ type ObjectValues = T[keyof T] type Method = ObjectValues 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' +} diff --git a/src/index.ts b/src/index.ts index 3babf6a0..9ece96fe 100755 --- a/src/index.ts +++ b/src/index.ts @@ -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 { @@ -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