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

Commit

Permalink
Update to use latest kontent-delivery and apply changes in upgrade …
Browse files Browse the repository at this point in the history
…docs
  • Loading branch information
CMeeg committed Sep 21, 2019
1 parent 0e1ce3c commit 04b9add
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 95 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -970,11 +970,11 @@ class PostSeriesContentItem extends GridsomeContentItem {
module.exports = PostSeriesContentItem;
```

> When you are inside an instance function such as `addFields` you also have access to all of the properties of a regular `ContentItem` such as `this.system` and `this.elements` should you need them.
> When you are inside an instance function such as `addFields` you also have access to all of the properties of a regular `ContentItem` such as `this.system` and `this._raw.elements` should you need them.
#### Custom Rich Text and Link resolvers

`GridsomeContentItem` uses custom [richTextResolver](https://github.com/Kentico/kentico-cloud-js/blob/master/packages/delivery/DOCS.md#globally) and [linkResolver](https://github.com/Kentico/kentico-cloud-js/blob/master/packages/delivery/DOCS.md#resolving-url-slugs-globally) functions to aid in the approach for [rendering Rich Text fields](#rendering-rich-text-fields), but if you decide to [opt out](#opting-out-of-this-approach) of that approach you may want to provide your own resolver functions.
`GridsomeContentItem` uses custom [richTextResolver](https://github.com/Kentico/kentico-cloud-js/blob/master/packages/delivery/DOCS.md#globally) and [urlSlugResolver](https://github.com/Kentico/kentico-kontent-js/blob/delivery%406.2.0/packages/delivery/DOCS.md#resolving-url-slugs-globally) functions to aid in the approach for [rendering Rich Text fields](#rendering-rich-text-fields), but if you decide to [opt out](#opting-out-of-this-approach) of that approach you may want to provide your own resolver functions.

You can do so like this:

Expand All @@ -992,8 +992,8 @@ class PostContentItem extends GridsomeContentItem {
richTextResolver: (item, context) => {
return `<h3 class="resolved-item">${item.name.text}</h3>`;
},
linkResolver: (link, context) => {
return `/posts/${url_slug}`;
urlSlugResolver: (link, context) => {
return { url: `/posts/${url_slug}` };
}
};

Expand Down Expand Up @@ -1049,9 +1049,9 @@ These options are identical to the Kentico Cloud delivery client configuration o

| Key | Type | Default value | Notes |
| --- | --- | --- | --- |
| `contentItemsDepth` | `Number` | `3` | Sets the `depth` parameter on content queries, which can be used to [handle missing referenced linked items](https://github.com/Kentico/kentico-cloud-js/blob/delivery%405.7.2/packages/delivery/DOCS.md#handling-missing-referenced-linked-items) |
| `contentItemsDepth` | `Number` | `3` | Sets the `depth` parameter on content queries, which can be used to [handle missing referenced linked items](https://github.com/Kentico/kentico-kontent-js/blob/delivery%406.2.0/packages/delivery/DOCS.md#handling-missing-referenced-linked-items) |

Please see the [Kentico Cloud documentation](https://github.com/Kentico/kentico-cloud-js/blob/delivery%405.7.2/packages/delivery/DOCS.md#client-configuration) for a description of all other available options, which include options for setting preview mode, secure mode, and language.
Please see the [Kentico Cloud documentation](https://github.com/Kentico/kentico-kontent-js/blob/delivery%406.2.0/packages/delivery/DOCS.md#client-configuration) for a description of all other available options, which include options for setting preview mode, secure mode, and language.

### `contentItemConfig` options

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@
"src/*.js"
],
"dependencies": {
"@kentico/kontent-delivery": "^7.0.0",
"@sindresorhus/slugify": "^0.9.1",
"change-case": "^3.1.0",
"cheerio": "^1.0.0-rc.3",
"debug": "^4.1.1",
"kentico-cloud-delivery": "^5.7.2",
"lodash": "^4.17.15",
"rxjs": "^6.5.2"
},
"peerDependencies": {
"gridsome": "^0.6.7"
"gridsome": "~0.6.7"
},
"devDependencies": {
"eslint": "^5.16.0",
Expand Down
57 changes: 17 additions & 40 deletions src/GridsomeContentItem.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
const { ContentItem } = require('kentico-cloud-delivery');
const { ContentItem } = require('@kentico/kontent-delivery');
const changeCase = require('change-case');
const { merge } = require('lodash');
const slugify = require('@sindresorhus/slugify');

class GridsomeContentItem extends ContentItem {
constructor(typeName, route, richTextHtmlTransformer, data) {
const defaultData = {
propertyResolver: (fieldName) => {
return this.resolveProperty(fieldName);
propertyResolver: (elementName) => {
return this.resolveProperty(elementName);
}
};

Expand All @@ -18,8 +18,9 @@ class GridsomeContentItem extends ContentItem {
}

if (richTextHtmlTransformer.canTransformLinks()) {
defaultData.linkResolver = (link, context) => {
defaultData.urlSlugResolver = (link, context) => {
// TODO: Ask Kentico Cloud why this seems to be being ignored
// TODO: Does this now work?
// Removing this results in warnings from the DeliveryClient when advanced logging is turned on
return this.resolveLink(link, context);
};
Expand All @@ -40,8 +41,8 @@ class GridsomeContentItem extends ContentItem {
this.richTextHtmlTransformer = richTextHtmlTransformer;
}

resolveProperty(fieldName) {
return this.getFieldName(fieldName);
resolveProperty(elementName) {
return this.getFieldName(elementName);
}

getFieldName(fieldName) {
Expand All @@ -62,7 +63,7 @@ class GridsomeContentItem extends ContentItem {
const text = context.linkText;

return {
asHtml: this.richTextHtmlTransformer.getLinkHtml(id, text)
html: this.richTextHtmlTransformer.getLinkHtml(id, text)
}
}

Expand Down Expand Up @@ -98,7 +99,7 @@ class GridsomeContentItem extends ContentItem {
route: nodeRoute, // Components are not independent content and so will not have a route
isComponent: isComponent,
date: new Date(lastModified),
slug: defaultSlug // Will be overwritten if a `URL slug` type field is present on the content type
slug: defaultSlug // Will be overwritten if a `URL slug` type element is present on the content type
},
assetFields: [],
linkedItemFields: [],
Expand All @@ -111,48 +112,24 @@ class GridsomeContentItem extends ContentItem {
addFields(node) {
// Add Content Elements as fields to the node

for (const codename in this.elements) {
const element = this.elements[codename];
for (const codename in this._raw.elements) {
const element = this._raw.elements[codename];
const fieldName = this.getFieldName(codename);
let field = this[fieldName];

if (element.type === 'modular_content') {
// "Linked items" fields are different to all others for some reason so we force it to be more uniform
// TODO : Ask Kentico Cloud to make it consistent

field = {
name: element.name,
type: element.type,
value: element.value,
linkedItems: field
};
}

if (element.type === 'asset') {
// The AssetModel doesn't have width and height, but the element value does so we will map those values across
// TODO: Ask Kentico Cloud to add width and height

field.assets = field.assets.map(asset => {
const url = asset.url;

element.value
.filter(elementAsset => elementAsset.url === url)
.map(elementAsset => {
asset.width = elementAsset.width;
asset.height = elementAsset.height;
});

field.value = field.value.map(asset => {
// We will use the asset url as the id as it is unique, and the id is not provided
// TODO: Ask Kentico Cloud if it can be provided

asset.id = url;
asset.id = asset.url;

return asset;
});
}

// Get a Field Resolver and use it to add the field and its value to the node
// TODO: Custom element fields
// TODO: Custom elements

field.fieldName = fieldName;

Expand Down Expand Up @@ -227,7 +204,7 @@ class GridsomeContentItem extends ContentItem {

modularContentTypeFieldResolver(node, field) {
const fieldName = field.fieldName;
const linkedItems = field.linkedItems;
const linkedItems = field.value;
const value = linkedItems.map(linkedItem => linkedItem.system.id);

const linkedItemField = {
Expand All @@ -242,7 +219,7 @@ class GridsomeContentItem extends ContentItem {

taxonomyTypeFieldResolver(node, field) {
const fieldName = field.fieldName;
const value = field.taxonomyTerms.map(term => term.codename);
const value = field.value.map(term => term.codename);
const taxonomyGroup = field.taxonomyGroup;

const taxonomyField = {
Expand All @@ -257,7 +234,7 @@ class GridsomeContentItem extends ContentItem {

assetTypeFieldResolver(node, field) {
const fieldName = field.fieldName;
const assets = field.assets;
const assets = field.value;
const value = assets.map(asset => asset.id);

const assetField = {
Expand Down
8 changes: 4 additions & 4 deletions src/GridsomeDeliveryClient.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { TypeResolver, DeliveryClient } = require('kentico-cloud-delivery');
const { TypeResolver, DeliveryClient } = require('@kentico/kontent-delivery');

class GridsomeDeliveryClient {
constructor(deliveryClientConfig) {
Expand Down Expand Up @@ -40,7 +40,7 @@ class GridsomeDeliveryClient {

const contentTypes = await deliveryClient
.types()
.getPromise();
.toPromise();

return contentTypes;
}
Expand All @@ -53,7 +53,7 @@ class GridsomeDeliveryClient {
.items()
.type(codename)
.depthParameter(depth)
.getPromise();
.toPromise();

return contentItems;
}
Expand All @@ -63,7 +63,7 @@ class GridsomeDeliveryClient {

const taxonomyGroups = await deliveryClient
.taxonomies()
.getPromise();
.toPromise();

return taxonomyGroups;
}
Expand Down
7 changes: 4 additions & 3 deletions src/GridsomeRichTextHtmlTransformer.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class GridsomeRichTextHtmlTransformer {
}

transformRichTextHtml(field) {
let html = field.getHtml();
let html = field.resolveHtml();

if (!this.canTransformRichText()) {
return html;
Expand All @@ -48,7 +48,8 @@ class GridsomeRichTextHtmlTransformer {
}

// Transform item links
// N.B. This shouldn't be necessary, but the `linkResolver` feature of the Kentico Cloud SDK doesn't appear to work
// N.B. This shouldn't be necessary, but the `urlSlugResolver` feature of the Kentico Cloud SDK doesn't appear to work
// TODO: Does the feature now work, and this can be removed?

this.transformItemLinks($);

Expand Down Expand Up @@ -123,7 +124,7 @@ class GridsomeRichTextHtmlTransformer {
}

getLinkHtml(id, text) {
// Links to content items in rich text fields will be rendered as Vue components
// Links to content items in rich text elements will be rendered as Vue components

const componentName = this.getComponentName(this.options.itemLinkComponentName);

Expand Down
5 changes: 3 additions & 2 deletions src/KenticoCloudSource.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { ImageUrlBuilder, ImageCompressionEnum, ImageFormatEnum } = require('kentico-cloud-delivery');
const { ImageUrlBuilder, ImageCompressionEnum, ImageFormatEnum } = require('@kentico/kontent-delivery');

class KenticoCloudSource {
constructor(deliveryClient, contentItemFactory, taxonomyItemFactory, logger) {
Expand Down Expand Up @@ -113,7 +113,8 @@ class KenticoCloudSource {
const codename = contentType.system.codename;

const content = await this.deliveryClient.getContent(codename);
const { items: contentItems, linkedItems } = content;
const { items: contentItems } = content;
const linkedItems = Object.keys(content.linkedItems).map(key => content.linkedItems[key]);

if (contentItems.length === 0) {
// There are no content items to process so we go no further
Expand Down
76 changes: 38 additions & 38 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,22 @@
esutils "^2.0.2"
js-tokens "^4.0.0"

"@kentico/[email protected]":
version "2.0.0"
resolved "https://registry.yarnpkg.com/@kentico/kontent-core/-/kontent-core-2.0.0.tgz#4a7d8e127a0ee0c5564d1ca2e49793912fbea260"
integrity sha512-Iclap7JOyirb9zKQUfhb0rxkdqe5gNK823dHdOREFyLvByCE2ZaTrpWI04qJMBct2lFgFfgLviIQo2Kx5x5P+w==
dependencies:
axios "0.19.0"

"@kentico/kontent-delivery@^7.0.0":
version "7.0.0"
resolved "https://registry.yarnpkg.com/@kentico/kontent-delivery/-/kontent-delivery-7.0.0.tgz#d8ebefa9277fdf2e9237583bb4e7976c7d20619d"
integrity sha512-P0PdBfGIBsuRB+tmCaERafeeAorRWKyTJmklB+HuFkGQ+nuN06LCvjIvMYP+6zaNDIKBMX2q4M/9TE+X1b7MvQ==
dependencies:
"@kentico/kontent-core" "2.0.0"
parse5 "5.1.0"
reflect-metadata "0.1.13"

"@sindresorhus/slugify@^0.9.1":
version "0.9.1"
resolved "https://registry.yarnpkg.com/@sindresorhus/slugify/-/slugify-0.9.1.tgz#892ad24d70b442c0a14fe519cb4019d59bc5069f"
Expand Down Expand Up @@ -108,13 +124,13 @@ astral-regex@^1.0.0:
resolved "https://registry.yarnpkg.com/astral-regex/-/astral-regex-1.0.0.tgz#6c8c3fb827dd43ee3918f27b82782ab7658a6fd9"
integrity sha512-+Ryf6g3BKoRc7jfp7ad8tM4TtMiaWvbF/1/sQcZPkkS7ag3D5nMBCe2UfOTONtAkaG0tO0ij3C5Lwmf1EiyjHg==

axios@0.18.0:
version "0.18.0"
resolved "https://registry.yarnpkg.com/axios/-/axios-0.18.0.tgz#32d53e4851efdc0a11993b6cd000789d70c05102"
integrity sha1-MtU+SFHv3AoRmTts0AB4nXDAUQI=
axios@0.19.0:
version "0.19.0"
resolved "https://registry.yarnpkg.com/axios/-/axios-0.19.0.tgz#8e09bff3d9122e133f7b8101c8fbdd00ed3d2ab8"
integrity sha512-1uvKqKQta3KBxIz14F2v06AEHZ/dIoeKfbTRkK1E5oqjDnuEerLmYTgJB5AiQZHJcljpg1TuRzdjDR06qNk0DQ==
dependencies:
follow-redirects "^1.3.0"
is-buffer "^1.1.5"
follow-redirects "1.5.10"
is-buffer "^2.0.2"

balanced-match@^1.0.0:
version "1.0.0"
Expand Down Expand Up @@ -521,20 +537,20 @@ dateformat@^3.0.0:
resolved "https://registry.yarnpkg.com/dateformat/-/dateformat-3.0.3.tgz#a6e37499a4d9a9cf85ef5872044d62901c9889ae"
integrity sha512-jyCETtSl3VMZMWeRo7iY1FL19ges1t55hMo5yaam4Jrsm5EPL89UQkoQRyiI+Yf4k8r2ZpdngkV8hr1lIdjb3Q==

debug@=3.1.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/debug/-/debug-3.1.0.tgz#5bb5a0672628b64149566ba16819e61518c67261"
integrity sha512-OX8XqP7/1a9cqkxYw2yXss15f26NKWBpDXQd0/uK/KPqdQhxbPa994hnzjcE2VqQpDslf55723cKPUOGSmMY3g==
dependencies:
ms "2.0.0"

debug@^2.6.8, debug@^2.6.9:
version "2.6.9"
resolved "https://registry.yarnpkg.com/debug/-/debug-2.6.9.tgz#5d128515df134ff327e90a4c93f4e077a536341f"
integrity sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA==
dependencies:
ms "2.0.0"

debug@^3.2.6:
version "3.2.6"
resolved "https://registry.yarnpkg.com/debug/-/debug-3.2.6.tgz#e83d17de16d8a7efb7717edbe5fb10135eee629b"
integrity sha512-mel+jf7nrtEl5Pn1Qx46zARXKDpBbvzezse7p7LqINmdoIk8PYP5SySaxEmYv6TZ0JyEKA1hsCId6DIhgITtWQ==
dependencies:
ms "^2.1.1"

debug@^4.0.1, debug@^4.1.1:
version "4.1.1"
resolved "https://registry.yarnpkg.com/debug/-/debug-4.1.1.tgz#3b72260255109c6b589cee050f1d516139664791"
Expand Down Expand Up @@ -939,12 +955,12 @@ flatted@^2.0.0:
resolved "https://registry.yarnpkg.com/flatted/-/flatted-2.0.1.tgz#69e57caa8f0eacbc281d2e2cb458d46fdb449e08"
integrity sha512-a1hQMktqW9Nmqr5aktAux3JMNqaucxGcjtjWnZLHX7yyPCmlSV3M54nGYbqT8K+0GhF3NBgmJCc3ma+WOgX8Jg==

follow-redirects@^1.3.0:
version "1.7.0"
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.7.0.tgz#489ebc198dc0e7f64167bd23b03c4c19b5784c76"
integrity sha512-m/pZQy4Gj287eNy94nivy5wchN3Kp+Q5WgUPNy5lJSZ3sgkVKSYV/ZChMAQVIgx1SqfZ2zBZtPA2YlXIWxxJOQ==
follow-redirects@1.5.10:
version "1.5.10"
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.5.10.tgz#7b7a9f9aea2fdff36786a94ff643ed07f4ff5e2a"
integrity sha512-0V5l4Cizzvqt5D44aTXbFZz+FtyXV1vrDN6qrelxtfYQKW0KO0W2T/hkE8xvGa/540LkZlkaUjO4ailYTFtHVQ==
dependencies:
debug "^3.2.6"
debug "=3.1.0"

[email protected]:
version "1.0.1"
Expand Down Expand Up @@ -1182,10 +1198,10 @@ is-arrayish@^0.2.1:
resolved "https://registry.yarnpkg.com/is-arrayish/-/is-arrayish-0.2.1.tgz#77c99840527aa8ecb1a8ba697b80645a7a926a9d"
integrity sha1-d8mYQFJ6qOyxqLppe4BkWnqSap0=

is-buffer@^1.1.5:
version "1.1.6"
resolved "https://registry.yarnpkg.com/is-buffer/-/is-buffer-1.1.6.tgz#efaa2ea9daa0d7ab2ea13a97b2b8ad51fefbe8be"
integrity sha512-NcdALwpXkTm5Zvvbk7owOUSvVvBKDgKP5/ewfXEznmQFfs4ZRmanOeKBTjRVjka3QFoN6XJ+9F3USqfHqTaU5w==
is-buffer@^2.0.2:
version "2.0.3"
resolved "https://registry.yarnpkg.com/is-buffer/-/is-buffer-2.0.3.tgz#4ecf3fcf749cbd1e472689e109ac66261a25e725"
integrity sha512-U15Q7MXTuZlrbymiz95PJpZxu8IlipAp4dtS3wOdgPXx3mqBnslrWU14kxfHB+Py/+2PVKSr37dMAgM2A4uArw==

is-finite@^1.0.0:
version "1.0.2"
Expand Down Expand Up @@ -1288,22 +1304,6 @@ jsonparse@^1.2.0:
resolved "https://registry.yarnpkg.com/jsonparse/-/jsonparse-1.3.1.tgz#3f4dae4a91fac315f71062f8521cc239f1366280"
integrity sha1-P02uSpH6wxX3EGL4UhzCOfE2YoA=

[email protected]:
version "1.4.3"
resolved "https://registry.yarnpkg.com/kentico-cloud-core/-/kentico-cloud-core-1.4.3.tgz#c40e39bbd8f1c2787e81d18e9eda1633d1357732"
integrity sha512-YJHpyCSSXX4fBAyy4126Y9IAsVM0N9AWQ8myOGe9b41ecm3WBMRfUH8D3a/HQB5owHAob5WPgiKlHGQ9kAwUJg==
dependencies:
axios "0.18.0"

kentico-cloud-delivery@^5.7.2:
version "5.7.2"
resolved "https://registry.yarnpkg.com/kentico-cloud-delivery/-/kentico-cloud-delivery-5.7.2.tgz#716ca7d46f32e031e3a78480d8fc5cab5b338d0e"
integrity sha512-5iudO7Mc25PhgA7l+Js+VyJXYLb1FBb/ZwoAONXAGfhfhwtIAIX9BYGO/vMxKZ6TLsiIhn7ZBFkflckLTIXRCg==
dependencies:
kentico-cloud-core "1.4.3"
parse5 "5.1.0"
reflect-metadata "0.1.13"

levn@^0.3.0, levn@~0.3.0:
version "0.3.0"
resolved "https://registry.yarnpkg.com/levn/-/levn-0.3.0.tgz#3b09924edf9f083c0490fdd4c0bc4421e04764ee"
Expand Down

0 comments on commit 04b9add

Please sign in to comment.