Skip to content

Instantly share code, notes, and snippets.

@jpetitcolas

jpetitcolas/test.js

Created Jun 2, 2016
Embed
What would you like to do?
const typeFinder = type => function* (params = {}, ref = 'master') {
const search = function* (searchParams) {
const Api = yield initApi;
const reference = ref === 'master' ? Api.master() : ref;
const preparedForm = Api.form('everything')
.ref(reference)
.pageSize(200);
const predicates = [];
if (type) {
predicates.push(Prismic.Predicates.at('document.type', type));
if (searchParams.hasOwnProperty('uid')) {
predicates.push(Prismic.Predicates.at(`my.${type}.uid`, searchParams.uid));
}
}
if (searchParams.hasOwnProperty('id')) {
predicates.push(Prismic.Predicates.at('document.id', searchParams.id));
}
preparedForm.query(...predicates);
console.log(predicates)
return yield preparedForm.submit.bind(preparedForm);
};
console.time('Prismic');
const response = yield search(params);
console.timeEnd('Prismic');
if (!response.results.length) {
const err = new Error(`Unable to find ${type} for ${JSON.stringify(params)}.`);
err.status = 404;
throw err;
}
return response.results;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.