Skip to content

Instantly share code, notes, and snippets.

@jpetitcolas
Created June 2, 2016 10:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jpetitcolas/23c2e00171464aa3511a010064227332 to your computer and use it in GitHub Desktop.
Save jpetitcolas/23c2e00171464aa3511a010064227332 to your computer and use it in GitHub Desktop.
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