Created
June 2, 2016 10:39
-
-
Save jpetitcolas/23c2e00171464aa3511a010064227332 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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