export async function fetchPrismicPosts(prismicCtx, postType = 'blog', options = { orderings: '[my.blog.date desc]', pageSize: GlobalConfig.maxPosts, }) { const prismicContext = typeof prismicCtx === 'object' ? prismicCtx : await fetchPrismicContext(); try { // return Prismic API post query return await prismicContext.api.query( Prismic.Predicates.at('document.type', postType), options, (err, posts) => { if (posts) { // Return posts return { posts }; } else { console.error('Prismic api fetch error: ', err); } } ); } catch(err) { console.warn(`Primic context does not yet exist:\n${err}`); // eslint-disable-line no-console return await null; // ensure to return a promise } }
fetchHome(props) { if (props.prismicCtx) { props.prismicCtx.api.query(Prismic.Predicates.at('document.type', 'longform'), { orderings : '[my.longform.date desc]' }, (err, response) => { this.setState({ longform: response.results, }); }); } }
prismic.api( core.config.api.access, apiOptions ).then(( api ) => { const done = function ( json ) { resolve( json.results ); }; const fail = function ( error ) { resolve({ error: error }); }; const form = getForm( req, api, type ); let query = []; // query: type? if ( !api.data.forms[ type ] ) { // Only if type? is NOT a search form collection query.push( prismic.Predicates.at( "document.type", type ) ); } // @hook: query if ( listener && listener.handlers.query ) { query = listener.handlers.query( prismic, api, query, cache, req ); } // query: promise? if ( query instanceof Promise ) { query.then( done ).catch( fail ); } else { // query? if ( query.length ) { form.query( query ); } // @hook: orderings if ( listener && listener.handlers.orderings ) { listener.handlers.orderings( prismic, cache.api, form, cache, req ); } // @hook: fetchLinks if ( listener && listener.handlers.fetchLinks ) { listener.handlers.fetchLinks( prismic, cache.api, form, cache, req ); } // submit form.submit().then( done ).catch( fail ); } });
.then(response => { let content = { page: { name: 'works', url: 'https://mintdesign.no/works/', }, projects: response.results }; req.prismic.api.query(Prismic.Predicates.at('document.type', 'works'), { ref: res.locals.prismicRef }) .then(response => { res.locals.works = response.results[0]; res.render('works', content); }) })
module.exports = function(req, res, next) { req.prismic.api.query(Prismic.Predicates.at('document.type', 'project'), { pageSize : 50, page : 1, orderings : '[my.project.published desc]', fetch: [ 'project.uid', 'project.image', 'project.name', 'project.description', ], ref: res.locals.prismicRef }) .then(response => { let content = { page: { name: 'works', url: 'https://mintdesign.no/works/', }, projects: response.results }; req.prismic.api.query(Prismic.Predicates.at('document.type', 'works'), { ref: res.locals.prismicRef }) .then(response => { res.locals.works = response.results[0]; res.render('works', content); }) }) .catch(err => { next(`Error: ${err.message}`); }); }
const doQuery = function ( type, uid ) { let query = []; const navi = getNavi( type ); const form = getForm( req, cache.api, type ); const isNaviNoForm = (navi && !cache.api.data.forms[ type ]); const done = function ( json ) { if ( !json.results.length ) { // Static page with no CMS data attached to it... if ( core.template.cache.pages.indexOf( `${type}.html` ) !== -1 ) { resolve( data ); } else { reject( `Prismic has no data for the content-type "${type}".` ); } } else { // all data.items = json.results; // uid if ( uid || isNaviNoForm ) { data.item = getDoc( (isNaviNoForm ? navi.uid : uid), json.results ); if ( !data.item ) { reject( `The document with UID "${isNaviNoForm ? navi.uid : uid}" could not be found by Prismic.` ); } } resolve( data ); } }; const fail = function ( error ) { reject( error ); }; // query: type? if ( isNaviNoForm ) { query.push( prismic.Predicates.at( "document.type", navi.type ) ); query.push( prismic.Predicates.at( "document.id", navi.id ) ); } else if ( !cache.api.data.forms[ type ] ) { // Only if type? is NOT a search form collection query.push( prismic.Predicates.at( "document.type", type ) ); } // @hook: query if ( listener && listener.handlers.query ) { query = listener.handlers.query( prismic, cache.api, query, cache, req ); } // query: promise? if ( query instanceof Promise ) { query.then( done ).catch( fail ); } else { // query? if ( query.length ) { form.query( query ); } // @hook: orderings if ( listener && listener.handlers.orderings ) { listener.handlers.orderings( prismic, cache.api, form, cache, req ); } // @hook: fetchLinks if ( listener && listener.handlers.fetchLinks ) { listener.handlers.fetchLinks( prismic, cache.api, form, cache, req ); } // submit form.submit().then( done ).catch( fail ); } };