Skip to content

Commit

Permalink
Merge pull request #382 from YujohnNattrass/master
Browse files Browse the repository at this point in the history
feat: add support for type prefix
  • Loading branch information
schabibi1 authored Aug 23, 2023
2 parents 27a6752 + 2094f1a commit 392b740
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 13 deletions.
16 changes: 16 additions & 0 deletions lib/__tests__/sync.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,20 @@ test('horizontal_rule to generate hr tag', () => {
})

Sync.createNode('Item', { id: 123 })
})

test(`typePrefix is included in internal type`, () => {
const createNode = function (node) {
expect(node.internal.type).toBe(`StoryblokItem`)
}
const setPluginStatus = function () { }
const client = new StoryblokClient({})

Sync.init({
createNode,
setPluginStatus,
client
})

Sync.createNode('Item', { id: 123 }, `Storyblok`)
})
6 changes: 5 additions & 1 deletion lib/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ exports.sourceNodes = async function ({ actions }, options) {
client,
});

const space = await Sync.getSpace();
const space = await Sync.getSpace({typePrefix: apiOptions.typePrefix});
const languages = options.languages ? options.languages : space.language_codes;
languages.push('');

for (const language of languages) {
await Sync.getAll('stories', {
node: 'StoryblokEntry',
params: getStoryParams(language, options),
typePrefix: options.typePrefix,
process: (item) => {
for (var prop in item.content) {
// eslint-disable-next-line no-prototype-builtins
Expand Down Expand Up @@ -69,13 +70,15 @@ exports.sourceNodes = async function ({ actions }, options) {

const datasources = await Sync.getAll('datasources', {
node: 'StoryblokDatasource',
typePrefix: options.typePrefix,
});

for (const datasource of datasources) {
const datasourceSlug = datasource.slug;

await Sync.getAll('datasource_entries', {
node: 'StoryblokDatasourceEntry',
typePrefix: options.typePrefix,
params: {
datasource: datasourceSlug,
},
Expand All @@ -90,6 +93,7 @@ exports.sourceNodes = async function ({ actions }, options) {
for (const dimension of datasourceDimensions) {
await Sync.getAll('datasource_entries', {
node: 'StoryblokDatasourceEntry',
typePrefix: options.typePrefix,
params: {
datasource: datasourceSlug,
dimension: dimension.entry_value,
Expand Down
5 changes: 5 additions & 0 deletions lib/src/getStoryParams.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* @param {Array<String>} options.resolveRelations? resolve_relations field
* @param {String} options.resolveLinks? can be story or url
* @param {String} options.version? can be draft or released
* @param {String} options.typePrefix? can be alphanumeric string
*/
const getStoryParams = function(language = '', options = {}) {
let params = {}
Expand All @@ -25,6 +26,10 @@ const getStoryParams = function(language = '', options = {}) {
params.language = language
}

if (options.typePrefix) {
params.typePrefix = options.typePrefix
}

return params
}

Expand Down
27 changes: 15 additions & 12 deletions lib/src/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ module.exports = {
this.$cacheVersion = 0;
},

async getSpace() {
async getSpace(options) {
const space = await this.getOne('space', 'spaces/me', {
node: 'StoryblokSpace',
typePrefix: options.typePrefix,
});
this.$cacheVersion = space.version;
return space;
Expand All @@ -27,44 +28,46 @@ module.exports = {
return this.$client.get(`cdn/${type}`, params);
},

createNode(name, item) {
const nodeObject = this.builderNode(name, item);
createNode(name, item, typePrefix) {
const nodeObject = this.builderNode(name, item, typePrefix);

this.$createNode(nodeObject);
},

builderNode(name, item) {
builderNode(name, item, typePrefix) {
if (name === 'StoryblokDatasourceEntry') {
return this.factoryDatasourceEntryNode(name, item);
return this.factoryDatasourceEntryNode(name, item, typePrefix);
}

return this.factoryDefaultNode(name, item);
return this.factoryDefaultNode(name, item, typePrefix);
},

factoryDefaultNode(name, item) {
factoryDefaultNode(name, item, typePrefix) {
const lang = item.lang || 'default';
const typeName = typePrefix ? `${typePrefix}${name}` : name;

return Object.assign({}, item, {
id: `${name.toLowerCase()}-${item.id}-${lang}`,
internalId: item.id,
parent: null,
children: [],
internal: {
type: name,
type: typeName,
contentDigest: crypto.createHash(`md5`).update(stringify(item)).digest(`hex`),
},
});
},

factoryDatasourceEntryNode(name, item) {
factoryDatasourceEntryNode(name, item, typePrefix) {
const dimension = item.data_source_dimension || 'default';
const typeName = typePrefix ? `${typePrefix}${name}` : name;
return Object.assign({}, item, {
id: `${name.toLowerCase()}-${item.id}-${dimension}`,
internalId: item.id,
parent: null,
children: [],
internal: {
type: name,
type: typeName,
contentDigest: crypto.createHash(`md5`).update(stringify(item)).digest(`hex`),
},
});
Expand All @@ -73,7 +76,7 @@ module.exports = {
async getOne(single, type, options) {
const resp = await this.$client.get(`cdn/${type}`, options.params);
const item = resp.data[single];
this.createNode(options.node, item);
this.createNode(options.node, item, options.typePrefix);
return item;
},

Expand All @@ -96,7 +99,7 @@ module.exports = {
if (options.process) {
options.process(item);
}
this.createNode(options.node, item);
this.createNode(options.node, item, options.typePrefix);
});

return all;
Expand Down

0 comments on commit 392b740

Please sign in to comment.