Skip to content
This repository has been archived by the owner on Mar 4, 2023. It is now read-only.

Commit

Permalink
Add X-KC-SOURCE header
Browse files Browse the repository at this point in the history
  • Loading branch information
CMeeg committed Sep 22, 2019
1 parent d74bd38 commit 99381a7
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 6 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,6 @@ typings/

# next.js build output
.next

# Project specific files
src/genversion.js
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1013,8 +1013,9 @@ The plugin defines the following namespaces:

| Namespace | Description |
| --- | --- |
| `gridsome-source-kentico-kontent` | Use this to log very basic info about when the plugin starts and finishes its work |
| `gridsome-source-kentico-kontent:source` | Use this to log detailed information about the work that the plugin is doing |
| `gridsome-source-kentico-kontent` | Use this to log basic info about when the plugin starts and finishes its work |
| `gridsome-source-kentico-kontent:source` | Use this to log information about the work that the plugin is doing |
| `gridsome-source-kentico-kontent:delivery-client` | Use this to log information from the Kentico Kontent delivery client |

Please read the debug docs for usage instructions.

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"eslint-plugin-node": "7.0.0",
"eslint-plugin-promise": "4.0.0",
"eslint-plugin-standard": "4.0.0",
"genversion": "^2.2.0",
"standard-version": "^7.0.0"
},
"publishConfig": {
Expand All @@ -46,7 +47,7 @@
},
"scripts": {
"lint": "eslint ./src",
"build": "yarn run lint",
"build": "genversion ./src/genversion.js -s && yarn run lint",
"release": "standard-version"
},
"standard-version": {
Expand Down
45 changes: 44 additions & 1 deletion src/GridsomeDeliveryClient.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
const { TypeResolver, DeliveryClient } = require('@kentico/kontent-delivery');
const version = require('./version');

class GridsomeDeliveryClient {
constructor(deliveryClientConfig) {
constructor(deliveryClientConfig, logger) {
this.deliveryClientConfig = deliveryClientConfig;
this.logger = logger.extend('delivery-client');

this.deliveryClient = null;
}
Expand All @@ -12,11 +14,52 @@ class GridsomeDeliveryClient {
return this.deliveryClient;
}

this.addSourceHeader(this.deliveryClientConfig);

this.deliveryClient = new DeliveryClient(this.deliveryClientConfig);

return this.deliveryClient;
}

addSourceHeader(deliveryClientConfig) {
if (version === '0.0.0') {
// version has not been generated - assume running in dev so don't add the header

return;
}

const sourceHeader = {
header: 'X-KC-SOURCE',
value: `gridsome-source-kentico-kontent;${version}`
};

deliveryClientConfig.globalQueryConfig = deliveryClientConfig.globalQueryConfig || {};

let headers = deliveryClientConfig.globalQueryConfig.customHeaders;

if (!headers) {
// There are no custom headers set so just add the source header and return

deliveryClientConfig.globalQueryConfig.customHeaders = [sourceHeader];

return;
}

// Check if the source header is already present, and remove it if so

if (headers.some(header => header.header === sourceHeader.header)) {
this.logger.log(`The provided "${sourceHeader.header}" header will be replaced by the source plugin.`);

headers = headers.filter(header => header.header !== sourceHeader.header);
}

// Add the source header

headers.push(sourceHeader);

deliveryClientConfig.globalQueryConfig.customHeaders = headers;
}

addTypeResolver(codename, createContentItemFunc) {
if (typeof(this.deliveryClientConfig.typeResolvers) === 'undefined') {
this.deliveryClientConfig.typeResolvers = [];
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class KenticoKontentSourcePlugin {
const logger = new Logger('gridsome-source-kentico-kontent');

api.loadSource(async store => {
const deliveryClient = new DeliveryClient(options.deliveryClientConfig);
const deliveryClient = new DeliveryClient(options.deliveryClientConfig, logger);
const contentItemFactory = new GridsomeContentItemFactory(options.contentItemConfig);
const taxonomyItemFactory = new GridsomeTaxonomyItemFactory(options.taxonomyConfig);

Expand Down
16 changes: 16 additions & 0 deletions src/version.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const fs = require('fs');
const path = require('path');

function getVersion() {
let version = '0.0.0';

const genversionPath = path.resolve(__dirname, './genversion.js');

if (fs.existsSync(genversionPath)) {
version = require('./genversion');
}

return version;
}

module.exports = getVersion();
36 changes: 35 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ [email protected]:
resolved "https://registry.yarnpkg.com/color-name/-/color-name-1.1.3.tgz#a7d0558bd89c42f795dd42328f740831ca53bc25"
integrity sha1-p9BVi9icQveV3UIyj3QIMcpTvCU=

commander@~2.20.0:
commander@^2.11.0, commander@~2.20.0:
version "2.20.0"
resolved "https://registry.yarnpkg.com/commander/-/commander-2.20.0.tgz#d58bb2b5c1ee8f87b0d340027e9e94e222c5a422"
integrity sha512-7j2y+40w61zy6YC2iRNpUe/NwhNyoXrYpHMrSunaMG64nRnaf96zO/KMQR4OyN/UnE5KLyEBnKHd4aG3rskjpQ==
Expand Down Expand Up @@ -911,6 +911,13 @@ file-entry-cache@^5.0.1:
dependencies:
flat-cache "^2.0.1"

find-package@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/find-package/-/find-package-1.0.0.tgz#d7738da67e3c5f055c24d3e19aa1aeed063c3e83"
integrity sha1-13ONpn48XwVcJNPhmqGu7QY8PoM=
dependencies:
parents "^1.0.1"

[email protected]:
version "4.1.0"
resolved "https://registry.yarnpkg.com/find-up/-/find-up-4.1.0.tgz#97afe7d6cdc0bc5928584b7c8d7b16e8a9aa5d19"
Expand Down Expand Up @@ -941,6 +948,11 @@ find-up@^3.0.0:
dependencies:
locate-path "^3.0.0"

firstline@^1.2.1:
version "1.3.1"
resolved "https://registry.yarnpkg.com/firstline/-/firstline-1.3.1.tgz#59e84af0fd858fbc6dac0a0ff97fd22a47e58084"
integrity sha512-ycwgqtoxujz1dm0kjkBFOPQMESxB9uKc/PlD951dQDIG+tBXRpYZC2UmJb0gDxopQ1ZX6oyRQN3goRczYu7Deg==

flat-cache@^2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/flat-cache/-/flat-cache-2.0.1.tgz#5d296d6f04bda44a4630a301413bdbc2ec085ec0"
Expand Down Expand Up @@ -984,6 +996,16 @@ functional-red-black-tree@^1.0.1:
resolved "https://registry.yarnpkg.com/functional-red-black-tree/-/functional-red-black-tree-1.0.1.tgz#1b0ab3bd553b2a0d6399d29c0e3ea0b252078327"
integrity sha1-GwqzvVU7Kg1jmdKcDj6gslIHgyc=

genversion@^2.2.0:
version "2.2.0"
resolved "https://registry.yarnpkg.com/genversion/-/genversion-2.2.0.tgz#2a4e31d966ac055080d683c9d7370f5e5e95c0de"
integrity sha512-EpNNOximLtBEajdIi6S+h2aqkYARPjkGIDEtQ3FVVgyD9HEhnG1SouSwj5NOZb5LL0RpuNZTUgKkw/ECtIWypA==
dependencies:
commander "^2.11.0"
find-package "^1.0.0"
firstline "^1.2.1"
mkdirp "^0.5.1"

get-caller-file@^2.0.1:
version "2.0.5"
resolved "https://registry.yarnpkg.com/get-caller-file/-/get-caller-file-2.0.5.tgz#4f94412a82db32f36e3b0b9741f8a97feb031f7e"
Expand Down Expand Up @@ -1683,6 +1705,13 @@ parent-module@^1.0.0:
dependencies:
callsites "^3.0.0"

parents@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/parents/-/parents-1.0.1.tgz#fedd4d2bf193a77745fe71e371d73c3307d9c751"
integrity sha1-/t1NK/GTp3dF/nHjcdc8MwfZx1E=
dependencies:
path-platform "~0.11.15"

parse-github-repo-url@^1.3.0:
version "1.4.1"
resolved "https://registry.yarnpkg.com/parse-github-repo-url/-/parse-github-repo-url-1.4.1.tgz#9e7d8bb252a6cb6ba42595060b7bf6df3dbc1f50"
Expand Down Expand Up @@ -1767,6 +1796,11 @@ path-parse@^1.0.6:
resolved "https://registry.yarnpkg.com/path-parse/-/path-parse-1.0.6.tgz#d62dbb5679405d72c4737ec58600e9ddcf06d24c"
integrity sha512-GSmOT2EbHrINBf9SR7CDELwlJ8AENk3Qn7OikK4nFYAu3Ote2+JYNVvkpAEQm3/TLNEJFD/xZJjzyxg3KBWOzw==

path-platform@~0.11.15:
version "0.11.15"
resolved "https://registry.yarnpkg.com/path-platform/-/path-platform-0.11.15.tgz#e864217f74c36850f0852b78dc7bf7d4a5721bf2"
integrity sha1-6GQhf3TDaFDwhSt43Hv31KVyG/I=

path-type@^1.0.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/path-type/-/path-type-1.1.0.tgz#59c44f7ee491da704da415da5a4070ba4f8fe441"
Expand Down

0 comments on commit 99381a7

Please sign in to comment.