@apolitical/content 0.2.2 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +10 -0
- package/README.md +1 -1
- package/build/index.js +1 -1
- package/package.json +44 -43
- package/.gitlab-ci.yml +0 -81
- package/.husky/pre-commit +0 -8
- package/default.env +0 -1
- package/lib/config.js +0 -27
- package/lib/helper/context.js +0 -79
- package/lib/helper/index.js +0 -8
- package/lib/index.js +0 -35
- package/lib/parser/asset.js +0 -19
- package/lib/parser/entry.js +0 -34
- package/lib/parser/fields.js +0 -22
- package/lib/parser/index.js +0 -31
- package/lib/parser/locale.js +0 -24
- package/test/unit/__snapshots__/lib.spec.js.snap +0 -600
- package/test/unit/lib.spec.js +0 -130
- package/test/unit/mocks/microcourse.mock.js +0 -819
- package/test/unit/mocks/microcourses.mock.js +0 -11
- package/webpack.config.js +0 -28
package/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,16 @@ All notable changes to this project will be documented in this file.
|
|
|
5
5
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|
6
6
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
7
|
|
|
8
|
+
|
|
9
|
+
## [0.3.0] - 2022-01-31
|
|
10
|
+
### Fixed
|
|
11
|
+
- Buffer dependency
|
|
12
|
+
- Node.js version
|
|
13
|
+
|
|
14
|
+
## [0.2.3] - 2021-10-11
|
|
15
|
+
### Changed
|
|
16
|
+
- Entry parser to return the item when there are no links on the array
|
|
17
|
+
|
|
8
18
|
## [0.2.2] - 2021-09-14
|
|
9
19
|
### Added
|
|
10
20
|
- Injecting `createdAt` and `updatedAt` from `sys` to flattened content
|
package/README.md
CHANGED
package/build/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t(require("contentful")):"function"==typeof define&&define.amd?define(["contentful"],t):"object"==typeof exports?exports.ApoliticalContent=t(require("contentful")):e.ApoliticalContent=t(e.contentful)}(this,(function(e){return function(){"use strict";var
|
|
1
|
+
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t(require("contentful"),require("query-string")):"function"==typeof define&&define.amd?define(["contentful","query-string"],t):"object"==typeof exports?exports.ApoliticalContent=t(require("contentful"),require("query-string")):e.ApoliticalContent=t(e.contentful,e["query-string"])}(this,(function(e,t){return function(){"use strict";var n={897:function(t){t.exports=e},48:function(e){e.exports=t}},r={};function o(e){var t=r[e];if(void 0!==t)return t.exports;var i=r[e]={exports:{}};return n[e](i,i.exports,o),i.exports}o.d=function(e,t){for(var n in t)o.o(t,n)&&!o.o(e,n)&&Object.defineProperty(e,n,{enumerable:!0,get:t[n]})},o.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},o.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})};var i={};return function(){o.r(i),o.d(i,{getEntries:function(){return m},getEntry:function(){return h},setContext:function(){return u}});var e=o(897),t=o(48),n={APOLITICAL:{WEBSITES:{BETA:"https://beta.apolitical.co",BETA_2:"https://beta-2.apolitical.co",LIVE:"https://apolitical.co",RC:"https://rc.apolitical.co"}},CONTENTFUL:{ENVIRONMENTS:{BETA:"beta",BETA_2:"beta",RC:"rc",LIVE:"master"},LOCALES:{DEFAULT:"en",SUPPORTED:["en","es","fr"]},SPACE:"txbhe1wabmyx",HOSTS:{CDN:"cdn.contentful.com",PREVIEW:"preview.contentful.com"}}};const{APOLITICAL:{WEBSITES:r},CONTENTFUL:{ENVIRONMENTS:s,SPACE:a,HOSTS:c}}=n;let l={apiKey:null,previewKey:null,environment:null,isPreview:!1,host:c.CDN,space:a};const u=({apiKey:e,environment:n,isPreview:o=!1,location:i,origin:c=null,previewKey:u,space:f=null})=>{l.apiKey=e,l.previewKey=u,l.environment=n||(e=>{let t,n=null;switch(n="undefined"!=typeof window?window.origin||window.location.origin:e,n){case r.BETA:t=s.BETA;break;case r.BETA_2:t=s.BETA_2;break;case r.LIVE:t=s.LIVE;break;case r.RC:t=s.RC;break;default:t=s.BETA}return t})(c),l.isPreview=o||(e=>{let n=!1;if(e&&e.search){const r=(0,t.parse)(e.search);n=r.isPreview&&"true"===r.isPreview}return n})(i),l.space=f||a},f=(e=!1)=>{const{apiKey:t,environment:n,isPreview:r,previewKey:o,space:i}=l;return!0===r||!0===e?{accessToken:o,environment:n,host:c.PREVIEW,space:i}:{accessToken:t,environment:n,host:c.CDN,space:i}},p=e=>"object"==typeof e&&null!==e,E=e=>Array.isArray(e)&&e.length,y=e=>{if(!p(e))throw new Error("Cannot flatten fields");const{fields:t,sys:n}=e;if(!t)throw new Error("Cannot flatten fields");let r={...t};if(p(n)){const{createdAt:e,updatedAt:t}=n;e&&Object.assign(r,{createdAt:e}),t&&Object.assign(r,{updatedAt:t})}return r},{CONTENTFUL:{LOCALES:w}}=n,d=(e,t)=>{const n=(e=>w.SUPPORTED.includes(e)?e:w.DEFAULT)(t);return((e,t)=>p(e)&&((e,t)=>Object.prototype.hasOwnProperty.call(e,t))(e,t))(e,n)?e[n]:e},T=e=>p(e)&&e.sys&&"Asset"===e.sys.type,A=(e,t)=>{const n=y(e),{file:r}=n;if(!r)throw new Error("Cannot flatten asset");const{url:o}=d(r,t);if(!o)throw new Error("Cannot flatten asset");return o},b=e=>p(e)&&e.sys&&"Entry"===e.sys.type,v=(e,t)=>{const n={},r=((e,t)=>{const n={};return Object.entries(e).forEach((([e,r])=>{n[e]=d(r,t)})),n})(y(e),t);return Object.entries(r).forEach((([e,r])=>{T(r)?n[e]=A(r):b(r)?n[e]=v(r,t):E(r)?n[e]=r.map((e=>T(e)?A(e):b(e)?v(e,t):e)):n[e]=r})),n},C=({items:e})=>{if(!E(e))throw new Error("Cannot read items");return e},O=async(t,n)=>{const r=f(n),o=await(0,e.createClient)(r);return await o.getEntries(t)},h=async(e={},t={},n=!1)=>((e,{flatten:t=!0,locale:n="en"})=>{const r=C(e);return t?v(r[0],n):r[0]})(await O(e,n),{...e,...t}),m=async(e={},t={},n=!1)=>((e,{flatten:t=!0,locale:n="en"})=>{const r=C(e);return t?((e,t)=>e.map((e=>v(e,t))))(r,n):r})(await O(e,n),{...e,...t})}(),i}()}));
|
package/package.json
CHANGED
|
@@ -1,21 +1,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@apolitical/content",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "React library to interact with Apolitical's content using Contentful CDA",
|
|
5
5
|
"author": "Apolitical Group Limited <engineering@apolitical.co>",
|
|
6
|
-
"contributors": [
|
|
7
|
-
"Anouska Hopkins <anouska.hopkins@apolitical.co>",
|
|
8
|
-
"Antonio Cordasco <antonio.cordasco@apolitical.co>",
|
|
9
|
-
"Dorothy Yau <dorothy.yau@apolitical.co>",
|
|
10
|
-
"Laura Hanna-White <laura.hanna-white@apolitical.co>",
|
|
11
|
-
"Fatimat Gbajabiamila <fatimat.gbajabiamila@apolitical.co>",
|
|
12
|
-
"Renzo Rozza <renzo.rozza@apolitical.co>",
|
|
13
|
-
"Rihards Jukna <rihards.jukna@apolitical.co>"
|
|
14
|
-
],
|
|
15
6
|
"license": "MIT",
|
|
16
7
|
"main": "build/index.js",
|
|
8
|
+
"files": [
|
|
9
|
+
"build/**"
|
|
10
|
+
],
|
|
17
11
|
"scripts": {
|
|
18
12
|
"test": "jest --runInBand",
|
|
13
|
+
"unit-test": "jest test/unit/* --bail --runInBand --passWithNoTests",
|
|
14
|
+
"integration-test": "jest test/integration/* --bail --runInBand --passWithNoTests",
|
|
19
15
|
"build": "webpack --config webpack.config.js",
|
|
20
16
|
"lint": "eslint --ext .js ./lib",
|
|
21
17
|
"format": "prettier --write 'lib/**/*.+(js|json)'",
|
|
@@ -28,46 +24,36 @@
|
|
|
28
24
|
"Contentful"
|
|
29
25
|
],
|
|
30
26
|
"dependencies": {
|
|
31
|
-
"contentful": "9.
|
|
27
|
+
"contentful": "9.1.6",
|
|
28
|
+
"query-string": "7.1.0"
|
|
32
29
|
},
|
|
33
30
|
"peerDependencies": {
|
|
34
|
-
"contentful": "9.
|
|
31
|
+
"contentful": "9.1.6",
|
|
32
|
+
"query-string": "7.1.0"
|
|
35
33
|
},
|
|
36
34
|
"devDependencies": {
|
|
37
|
-
"@
|
|
38
|
-
"babel
|
|
39
|
-
"babel-
|
|
40
|
-
"babel-
|
|
41
|
-
"
|
|
42
|
-
"
|
|
43
|
-
"
|
|
44
|
-
"
|
|
45
|
-
"
|
|
46
|
-
"
|
|
47
|
-
"
|
|
48
|
-
"prettier": "2.4.0",
|
|
49
|
-
"webpack": "5.52.1",
|
|
50
|
-
"webpack-cli": "4.8.0"
|
|
35
|
+
"@apolitical/eslint-config": "1.0.2",
|
|
36
|
+
"@babel/core": "7.16.12",
|
|
37
|
+
"@babel/eslint-parser": "7.16.5",
|
|
38
|
+
"@babel/plugin-transform-runtime": "7.16.10",
|
|
39
|
+
"@babel/preset-env": "7.16.11",
|
|
40
|
+
"husky": "7.0.4",
|
|
41
|
+
"jest": "27.4.7",
|
|
42
|
+
"jest-junit": "13.0.0",
|
|
43
|
+
"lint-staged": "12.3.2",
|
|
44
|
+
"webpack": "5.67.0",
|
|
45
|
+
"webpack-cli": "4.9.2"
|
|
51
46
|
},
|
|
52
47
|
"eslintConfig": {
|
|
53
48
|
"extends": [
|
|
54
|
-
"eslint
|
|
55
|
-
"plugin:prettier/recommended",
|
|
56
|
-
"plugin:jest/recommended"
|
|
49
|
+
"@apolitical/eslint-config/base.config"
|
|
57
50
|
],
|
|
58
51
|
"env": {
|
|
59
|
-
"
|
|
60
|
-
"es6": true,
|
|
61
|
-
"jest": true
|
|
52
|
+
"browser": true
|
|
62
53
|
},
|
|
63
|
-
"
|
|
64
|
-
"jest"
|
|
65
|
-
],
|
|
66
|
-
"parser": "babel-eslint"
|
|
67
|
-
},
|
|
68
|
-
"prettier": {
|
|
69
|
-
"singleQuote": true
|
|
54
|
+
"parser": "@babel/eslint-parser"
|
|
70
55
|
},
|
|
56
|
+
"prettier": "@apolitical/eslint-config/prettier.config",
|
|
71
57
|
"browserslist": {
|
|
72
58
|
"production": [
|
|
73
59
|
">0.2%",
|
|
@@ -85,15 +71,30 @@
|
|
|
85
71
|
],
|
|
86
72
|
"coveragePathIgnorePatterns": [
|
|
87
73
|
"<rootDir>/node_modules/"
|
|
88
|
-
]
|
|
74
|
+
],
|
|
75
|
+
"setupFiles": [
|
|
76
|
+
"./test/setupTests.js"
|
|
77
|
+
],
|
|
78
|
+
"reporters": [
|
|
79
|
+
"default",
|
|
80
|
+
"jest-junit"
|
|
81
|
+
],
|
|
82
|
+
"testResultsProcessor": "jest-junit"
|
|
89
83
|
},
|
|
90
84
|
"babel": {
|
|
91
85
|
"presets": [
|
|
92
|
-
"
|
|
93
|
-
]
|
|
86
|
+
"@babel/preset-env"
|
|
87
|
+
],
|
|
88
|
+
"env": {
|
|
89
|
+
"test": {
|
|
90
|
+
"plugins": [
|
|
91
|
+
"@babel/plugin-transform-runtime"
|
|
92
|
+
]
|
|
93
|
+
}
|
|
94
|
+
}
|
|
94
95
|
},
|
|
95
96
|
"engines": {
|
|
96
|
-
"node": ">=
|
|
97
|
+
"node": ">=16.13.0"
|
|
97
98
|
},
|
|
98
99
|
"lint-staged": {
|
|
99
100
|
"*.js": "eslint --cache --fix --ignore-path .gitignore",
|
package/.gitlab-ci.yml
DELETED
|
@@ -1,81 +0,0 @@
|
|
|
1
|
-
default:
|
|
2
|
-
image: node:12.20.1-alpine3.11
|
|
3
|
-
|
|
4
|
-
# Pipeline stages
|
|
5
|
-
stages:
|
|
6
|
-
- cache
|
|
7
|
-
- test
|
|
8
|
-
- build
|
|
9
|
-
- publish
|
|
10
|
-
|
|
11
|
-
# Cache jobs
|
|
12
|
-
|
|
13
|
-
fill-cache:
|
|
14
|
-
stage: cache
|
|
15
|
-
only:
|
|
16
|
-
changes:
|
|
17
|
-
- yarn.lock
|
|
18
|
-
cache:
|
|
19
|
-
key: $CI_COMMIT_REF_SLUG-$CI_PROJECT_DIR
|
|
20
|
-
paths:
|
|
21
|
-
- node_modules/
|
|
22
|
-
- .yarn
|
|
23
|
-
script:
|
|
24
|
-
- yarn install --cache-folder .yarn --production=false
|
|
25
|
-
|
|
26
|
-
# Test jobs
|
|
27
|
-
|
|
28
|
-
.use-cache:
|
|
29
|
-
cache:
|
|
30
|
-
key: $CI_COMMIT_REF_SLUG-$CI_PROJECT_DIR
|
|
31
|
-
paths:
|
|
32
|
-
- node_modules/
|
|
33
|
-
- .yarn
|
|
34
|
-
policy: pull
|
|
35
|
-
|
|
36
|
-
.lib-image:
|
|
37
|
-
extends: .use-cache
|
|
38
|
-
cache:
|
|
39
|
-
key: $CI_COMMIT_REF_SLUG-$CI_PROJECT_DIR
|
|
40
|
-
paths:
|
|
41
|
-
- node_modules/
|
|
42
|
-
before_script:
|
|
43
|
-
- yarn install --cache-folder .yarn --production=false
|
|
44
|
-
|
|
45
|
-
outdated:
|
|
46
|
-
extends: .lib-image
|
|
47
|
-
stage: test
|
|
48
|
-
allow_failure: true
|
|
49
|
-
script:
|
|
50
|
-
- yarn outdated
|
|
51
|
-
|
|
52
|
-
test:
|
|
53
|
-
extends: .lib-image
|
|
54
|
-
stage: test
|
|
55
|
-
allow_failure: false
|
|
56
|
-
script:
|
|
57
|
-
- yarn test
|
|
58
|
-
|
|
59
|
-
# Build jobs
|
|
60
|
-
|
|
61
|
-
build:
|
|
62
|
-
extends: .lib-image
|
|
63
|
-
stage: build
|
|
64
|
-
allow_failure: false
|
|
65
|
-
script:
|
|
66
|
-
- yarn build
|
|
67
|
-
|
|
68
|
-
# Publish jobs
|
|
69
|
-
|
|
70
|
-
publish-module:
|
|
71
|
-
extends: .lib-image
|
|
72
|
-
stage: publish
|
|
73
|
-
allow_failure: false
|
|
74
|
-
only:
|
|
75
|
-
- tags
|
|
76
|
-
- /^v\d+\.\d+\.\d+$/
|
|
77
|
-
before_script:
|
|
78
|
-
- yarn build
|
|
79
|
-
script:
|
|
80
|
-
- echo '//registry.npmjs.org/:_authToken=${NPM_TOKEN}' > ~/.npmrc
|
|
81
|
-
- yarn publish --access=public
|
package/.husky/pre-commit
DELETED
package/default.env
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
CONTENTFUL_CDA_TOKEN=cda-token-base64-encoded
|
package/lib/config.js
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
const config = {
|
|
2
|
-
APOLITICAL: {
|
|
3
|
-
WEBSITES: {
|
|
4
|
-
BETA: 'https://beta.apolitical.co',
|
|
5
|
-
LIVE: 'https://apolitical.co',
|
|
6
|
-
RC: 'https://rc.apolitical.co',
|
|
7
|
-
},
|
|
8
|
-
},
|
|
9
|
-
CONTENTFUL: {
|
|
10
|
-
ENVIRONMENTS: {
|
|
11
|
-
BETA: 'beta',
|
|
12
|
-
RC: 'rc',
|
|
13
|
-
LIVE: 'master',
|
|
14
|
-
},
|
|
15
|
-
LOCALES: {
|
|
16
|
-
DEFAULT: 'en',
|
|
17
|
-
SUPPORTED: ['en', 'es', 'fr'],
|
|
18
|
-
},
|
|
19
|
-
SPACE: 'txbhe1wabmyx',
|
|
20
|
-
HOSTS: {
|
|
21
|
-
CDN: 'cdn.contentful.com',
|
|
22
|
-
PREVIEW: 'preview.contentful.com',
|
|
23
|
-
},
|
|
24
|
-
},
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
export default config;
|
package/lib/helper/context.js
DELETED
|
@@ -1,79 +0,0 @@
|
|
|
1
|
-
import config from '../config';
|
|
2
|
-
|
|
3
|
-
const {
|
|
4
|
-
APOLITICAL: { WEBSITES },
|
|
5
|
-
CONTENTFUL: { ENVIRONMENTS, SPACE, HOSTS },
|
|
6
|
-
} = config;
|
|
7
|
-
|
|
8
|
-
const decodeBase64 = (value) =>
|
|
9
|
-
value && Buffer.from(value, 'base64').toString('ascii');
|
|
10
|
-
|
|
11
|
-
const getEnvironment = (origin) => {
|
|
12
|
-
let environment;
|
|
13
|
-
switch (origin) {
|
|
14
|
-
case WEBSITES.BETA:
|
|
15
|
-
environment = ENVIRONMENTS.BETA;
|
|
16
|
-
break;
|
|
17
|
-
case WEBSITES.LIVE:
|
|
18
|
-
environment = ENVIRONMENTS.LIVE;
|
|
19
|
-
break;
|
|
20
|
-
case WEBSITES.RC:
|
|
21
|
-
environment = ENVIRONMENTS.RC;
|
|
22
|
-
break;
|
|
23
|
-
default:
|
|
24
|
-
environment = ENVIRONMENTS.BETA;
|
|
25
|
-
}
|
|
26
|
-
return environment;
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
let context = {
|
|
30
|
-
accessToken: null,
|
|
31
|
-
previewToken: null,
|
|
32
|
-
environment: getEnvironment(),
|
|
33
|
-
isPreview: false,
|
|
34
|
-
host: HOSTS.CDN,
|
|
35
|
-
space: SPACE,
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
export const setContext = ({
|
|
39
|
-
space,
|
|
40
|
-
accessToken,
|
|
41
|
-
previewToken,
|
|
42
|
-
environment,
|
|
43
|
-
origin,
|
|
44
|
-
isPreview = false,
|
|
45
|
-
isEncodedToken = true,
|
|
46
|
-
}) => {
|
|
47
|
-
context.accessToken = isEncodedToken
|
|
48
|
-
? decodeBase64(accessToken)
|
|
49
|
-
: accessToken;
|
|
50
|
-
context.previewToken = isEncodedToken
|
|
51
|
-
? decodeBase64(previewToken)
|
|
52
|
-
: previewToken;
|
|
53
|
-
context.environment = environment || getEnvironment(origin);
|
|
54
|
-
context.isPreview = isPreview;
|
|
55
|
-
context.space = space || SPACE;
|
|
56
|
-
};
|
|
57
|
-
|
|
58
|
-
export const getContext = (isPreview = false) => {
|
|
59
|
-
const {
|
|
60
|
-
accessToken,
|
|
61
|
-
previewToken,
|
|
62
|
-
environment,
|
|
63
|
-
isPreview: contextIsPreview,
|
|
64
|
-
space,
|
|
65
|
-
} = context;
|
|
66
|
-
return contextIsPreview === true || isPreview === true
|
|
67
|
-
? {
|
|
68
|
-
accessToken: previewToken,
|
|
69
|
-
environment,
|
|
70
|
-
host: HOSTS.PREVIEW,
|
|
71
|
-
space,
|
|
72
|
-
}
|
|
73
|
-
: {
|
|
74
|
-
accessToken,
|
|
75
|
-
environment: environment,
|
|
76
|
-
host: HOSTS.CDN,
|
|
77
|
-
space,
|
|
78
|
-
};
|
|
79
|
-
};
|
package/lib/helper/index.js
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
export { getContext, setContext } from './context';
|
|
2
|
-
|
|
3
|
-
export const isObject = (value) => typeof value === 'object' && value !== null;
|
|
4
|
-
|
|
5
|
-
export const isArray = (value) => Array.isArray(value) && value.length;
|
|
6
|
-
|
|
7
|
-
export const hasProperty = (value, key) =>
|
|
8
|
-
Object.prototype.hasOwnProperty.call(value, key);
|
package/lib/index.js
DELETED
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
import { createClient } from 'contentful';
|
|
2
|
-
|
|
3
|
-
import { getContext } from './helper';
|
|
4
|
-
import { flattenFirstEntry, flattenAllEntries } from './parser';
|
|
5
|
-
|
|
6
|
-
const getRawEntries = async (query, isPreview) => {
|
|
7
|
-
const context = getContext(isPreview);
|
|
8
|
-
const client = await createClient(context);
|
|
9
|
-
const entries = await client.getEntries(query);
|
|
10
|
-
return entries;
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
/*
|
|
14
|
-
* Gets an entry from Contentful
|
|
15
|
-
* @param {Object} query - Object with search parameters (check the Contentful docs for more details).
|
|
16
|
-
* @param {Object} opts - Object with parsing parameters.
|
|
17
|
-
* @return {Object} entry - Object with entry content.
|
|
18
|
-
*/
|
|
19
|
-
export const getEntry = async (query = {}, opts = {}, isPreview = false) => {
|
|
20
|
-
const entries = await getRawEntries(query, isPreview);
|
|
21
|
-
return flattenFirstEntry(entries, { ...query, ...opts });
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
/*
|
|
25
|
-
* Gets a collection of entries from Contentful
|
|
26
|
-
* @param {Object} query - Object with search parameters (check the Contentful docs for more details).
|
|
27
|
-
* @param {Object} opts - Object with parsing parameters.
|
|
28
|
-
* @return {Array} entries - Array with entries content.
|
|
29
|
-
*/
|
|
30
|
-
export const getEntries = async (query = {}, opts = {}, isPreview = false) => {
|
|
31
|
-
const entries = await getRawEntries(query, isPreview);
|
|
32
|
-
return flattenAllEntries(entries, { ...query, ...opts });
|
|
33
|
-
};
|
|
34
|
-
|
|
35
|
-
export { setContext } from './helper';
|
package/lib/parser/asset.js
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { isObject } from '../helper';
|
|
2
|
-
import { flattenFields } from './fields';
|
|
3
|
-
import { flattenLocale } from './locale';
|
|
4
|
-
|
|
5
|
-
export const isAsset = (value) =>
|
|
6
|
-
isObject(value) && value.sys && value.sys.type === 'Asset';
|
|
7
|
-
|
|
8
|
-
export const flattenAsset = (asset, locale) => {
|
|
9
|
-
const fields = flattenFields(asset);
|
|
10
|
-
const { file } = fields;
|
|
11
|
-
if (!file) {
|
|
12
|
-
throw new Error('Cannot flatten asset');
|
|
13
|
-
}
|
|
14
|
-
const { url } = flattenLocale(file, locale);
|
|
15
|
-
if (!url) {
|
|
16
|
-
throw new Error('Cannot flatten asset');
|
|
17
|
-
}
|
|
18
|
-
return url;
|
|
19
|
-
};
|
package/lib/parser/entry.js
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import { isAsset, flattenAsset } from './asset';
|
|
2
|
-
import { flattenFields } from './fields';
|
|
3
|
-
import { flattenLocales } from './locale';
|
|
4
|
-
import { isArray, isObject } from '../helper';
|
|
5
|
-
|
|
6
|
-
const isEntry = (value) =>
|
|
7
|
-
isObject(value) && value.sys && value.sys.type === 'Entry';
|
|
8
|
-
|
|
9
|
-
export const flattenEntry = (entry, locale) => {
|
|
10
|
-
const result = {};
|
|
11
|
-
const fields = flattenLocales(flattenFields(entry), locale);
|
|
12
|
-
Object.entries(fields).forEach(([key, value]) => {
|
|
13
|
-
if (isAsset(value)) {
|
|
14
|
-
result[key] = flattenAsset(value);
|
|
15
|
-
} else if (isEntry(value)) {
|
|
16
|
-
result[key] = flattenEntry(value, locale);
|
|
17
|
-
} else if (isArray(value)) {
|
|
18
|
-
result[key] = value.map((item) => {
|
|
19
|
-
if (isAsset(item)) {
|
|
20
|
-
return flattenAsset(item);
|
|
21
|
-
} else if (isEntry(item)) {
|
|
22
|
-
return flattenEntry(item, locale);
|
|
23
|
-
}
|
|
24
|
-
return {};
|
|
25
|
-
});
|
|
26
|
-
} else {
|
|
27
|
-
result[key] = value;
|
|
28
|
-
}
|
|
29
|
-
});
|
|
30
|
-
return result;
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
export const flattenEntries = (entries, locale) =>
|
|
34
|
-
entries.map((entry) => flattenEntry(entry, locale));
|
package/lib/parser/fields.js
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import { isObject } from '../helper';
|
|
2
|
-
|
|
3
|
-
export const flattenFields = (value) => {
|
|
4
|
-
if (!isObject(value)) {
|
|
5
|
-
throw new Error('Cannot flatten fields');
|
|
6
|
-
}
|
|
7
|
-
const { fields, sys } = value;
|
|
8
|
-
if (!fields) {
|
|
9
|
-
throw new Error('Cannot flatten fields');
|
|
10
|
-
}
|
|
11
|
-
let result = { ...fields };
|
|
12
|
-
if (isObject(sys)) {
|
|
13
|
-
const { createdAt, updatedAt } = sys;
|
|
14
|
-
if (createdAt) {
|
|
15
|
-
Object.assign(result, { createdAt });
|
|
16
|
-
}
|
|
17
|
-
if (updatedAt) {
|
|
18
|
-
Object.assign(result, { updatedAt });
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
return result;
|
|
22
|
-
};
|
package/lib/parser/index.js
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import { isArray } from '../helper';
|
|
2
|
-
import { flattenEntry, flattenEntries } from './entry';
|
|
3
|
-
|
|
4
|
-
const flattenItems = ({ items }) => {
|
|
5
|
-
if (!isArray(items)) {
|
|
6
|
-
throw new Error('Cannot read items');
|
|
7
|
-
}
|
|
8
|
-
return items;
|
|
9
|
-
};
|
|
10
|
-
|
|
11
|
-
export const flattenFirstEntry = (
|
|
12
|
-
entries,
|
|
13
|
-
{ flatten = true, locale = 'en' }
|
|
14
|
-
) => {
|
|
15
|
-
const items = flattenItems(entries);
|
|
16
|
-
if (!flatten) {
|
|
17
|
-
return items[0];
|
|
18
|
-
}
|
|
19
|
-
return flattenEntry(items[0], locale);
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
export const flattenAllEntries = (
|
|
23
|
-
entries,
|
|
24
|
-
{ flatten = true, locale = 'en' }
|
|
25
|
-
) => {
|
|
26
|
-
const items = flattenItems(entries);
|
|
27
|
-
if (!flatten) {
|
|
28
|
-
return items;
|
|
29
|
-
}
|
|
30
|
-
return flattenEntries(items, locale);
|
|
31
|
-
};
|
package/lib/parser/locale.js
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import config from '../config';
|
|
2
|
-
import { isObject, hasProperty } from '../helper';
|
|
3
|
-
|
|
4
|
-
const {
|
|
5
|
-
CONTENTFUL: { LOCALES },
|
|
6
|
-
} = config;
|
|
7
|
-
|
|
8
|
-
const hasLocale = (value, key) => isObject(value) && hasProperty(value, key);
|
|
9
|
-
|
|
10
|
-
const findLocale = (locale) =>
|
|
11
|
-
LOCALES.SUPPORTED.includes(locale) ? locale : LOCALES.DEFAULT;
|
|
12
|
-
|
|
13
|
-
export const flattenLocale = (value, locale) => {
|
|
14
|
-
const key = findLocale(locale);
|
|
15
|
-
return hasLocale(value, key) ? value[key] : value;
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
export const flattenLocales = (values, locale) => {
|
|
19
|
-
const result = {};
|
|
20
|
-
Object.entries(values).forEach(([key, value]) => {
|
|
21
|
-
result[key] = flattenLocale(value, locale);
|
|
22
|
-
});
|
|
23
|
-
return result;
|
|
24
|
-
};
|