Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds the possibility to filter out datasources from being fetched in sourceNodes #665

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
166 changes: 106 additions & 60 deletions lib/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,104 +5,150 @@ const getStoryParams = require('./src/getStoryParams');
const stringify = require('json-stringify-safe');
const { createRemoteFileNode } = require(`gatsby-source-filesystem`);

exports.sourceNodes = async function ({ actions }, options) {
exports.sourceNodes = async function ({ actions, reporter }, options) {
const { createNode, setPluginStatus } = actions;
const { plugins, ...apiOptions } = options
storyblokInit({ use: [apiPlugin], apiOptions })

const client = useStoryblokApi()

reporter.verbose("Initializing Storyblok source plugin")
Sync.init({
createNode,
setPluginStatus,
client,
});

const space = await Sync.getSpace({typePrefix: apiOptions.typePrefix});
const languages = options.languages ? options.languages : space.language_codes;
languages.push('');
const languages = [];
if ("languages" in options) {
languages.push(...options.languages);
} else {
const space = await Sync.getSpace({ typePrefix: apiOptions.typePrefix });
languages.push(...space.language_codes);
}

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
if (!item.content.hasOwnProperty(prop) || ['_editable', '_uid'].indexOf(prop) > -1) {
continue;
}
const objectType = Object.prototype.toString
.call(item.content[prop])
.replace('[object ', '')
.replace(']', '')
.toLowerCase();

if (['number', 'boolean', 'string'].indexOf(objectType) === -1) {
continue;
reporter.verbose(`Fetching stories for language "${language}"`);
try {
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
if (!item.content.hasOwnProperty(prop) || ['_editable', '_uid'].indexOf(prop) > -1) {
continue;
}
const objectType = Object.prototype.toString
.call(item.content[prop])
.replace('[object ', '')
.replace(']', '')
.toLowerCase();

if (['number', 'boolean', 'string'].indexOf(objectType) === -1) {
continue;
}

const type = prop == 'component' ? '' : '_' + objectType;

item['field_' + prop + type] = item.content[prop];
}

const type = prop == 'component' ? '' : '_' + objectType;
item.content = stringify(item.content);
},
});
} catch (e) {
reporter.panic(`Failed to fetch stories for language "${language}"`, e);
}
}

item['field_' + prop + type] = item.content[prop];
}

item.content = stringify(item.content);
},
});
if (options.includeTags !== false) {
reporter.verbose('Fetching tags');
try {
await Sync.getAll('tags', {
node: 'StoryblokTag',
params: getStoryParams('', options),
process: (item) => {
item.id = item.name;
},
});
} catch (e) {
reporter.panic('Failed to fetch tags', e);
}
}

await Sync.getAll('tags', {
node: 'StoryblokTag',
params: getStoryParams('', options),
process: (item) => {
item.id = item.name;
},
});

if (options.includeLinks === true) {
await Sync.getAll('links', {
node: 'StoryblokLink',
params: getStoryParams('', options),
});
reporter.verbose('Fetching links');
try {
await Sync.getAll('links', {
node: 'StoryblokLink',
params: getStoryParams('', options),
});
} catch (e) {
reporter.panic('Failed to fetch links', e);
}
}

const datasources = await Sync.getAll('datasources', {
node: 'StoryblokDatasource',
typePrefix: options.typePrefix,
});
let datasources = [];
try {
reporter.verbose('Fetching datasources');
datasources = await Sync.getAll('datasources', {
node: 'StoryblokDatasource',
typePrefix: options.typePrefix,
});
} catch (e) {
reporter.panic('Failed to fetch datasources', e);
}

for (const datasource of datasources) {
const datasourceSlug = datasource.slug;
// If a datasource filter is set it acts as a whitelist and ignores all datasources not explicitly listed.
if("datasourceFilter" in options && Array.isArray(options.datasourceFilter) && !options.datasourceFilter.includes(datasource.slug)) {
reporter.verbose(`Skipping datasource ${datasource.slug} as it is not in the datasource filter`);
continue;
}

await Sync.getAll('datasource_entries', {
node: 'StoryblokDatasourceEntry',
typePrefix: options.typePrefix,
params: {
datasource: datasourceSlug,
},
process: (item) => {
item.data_source_dimension = null;
item.data_source = datasourceSlug;
},
});
reporter.verbose(`Fetching datasource entries for datasource "${datasource.slug}"`);

const datasourceDimensions = datasource.dimensions || [];
const datasourceSlug = datasource.slug;

for (const dimension of datasourceDimensions) {
try {
await Sync.getAll('datasource_entries', {
node: 'StoryblokDatasourceEntry',
typePrefix: options.typePrefix,
params: {
datasource: datasourceSlug,
dimension: dimension.entry_value,
},
process: (item) => {
item.data_source_dimension = dimension.entry_value;
item.data_source_dimension = null;
item.data_source = datasourceSlug;
},
});
} catch (e) {
reporter.panic(`Failed to fetch datasource entries for datasource "${datasource.slug}"`, e);
}

const datasourceDimensions = datasource.dimensions || [];

for (const dimension of datasourceDimensions) {
reporter.verbose(`Fetching datasource entries for datasource "${datasource.slug}" and dimension "${dimension.entry_value}"`);
try {
await Sync.getAll('datasource_entries', {
node: 'StoryblokDatasourceEntry',
typePrefix: options.typePrefix,
params: {
datasource: datasourceSlug,
dimension: dimension.entry_value,
},
process: (item) => {
item.data_source_dimension = dimension.entry_value;
item.data_source = datasourceSlug;
},
});
} catch (e) {
reporter.panic(`Failed to fetch datasource entries for datasource "${datasource.slug}" and dimension "${dimension.entry_value}"`, e);
}
}
}
};
Expand Down