express-prismic
Advanced tools
Comparing version
73
index.js
@@ -8,4 +8,2 @@ var Prismic = require('prismic.io').Prismic, | ||
exports.previewCookie = Prismic.previewCookie; | ||
var configuration = {}; | ||
@@ -24,2 +22,4 @@ | ||
exports.previewCookie = Prismic.previewCookie; | ||
function prismicWithCTX(ctxPromise, req, res) { | ||
@@ -37,19 +37,4 @@ var self = { | ||
}, | ||
'getByUID' : function(type, uid, onThen , onNotFound) { | ||
self.query(['at','my.'+type+'.uid',uid],function(err, response) { | ||
var document = response.results[0]; | ||
if(err) { | ||
configuration.onPrismicError && configuration.onPrismicError(err, req, response); | ||
} else { | ||
if(document) { | ||
onThen && onThen(document); | ||
} else { | ||
if(onNotFound){ | ||
onNotFound(); | ||
} else { | ||
res.send(404, 'Missing document ' + uid); | ||
} | ||
} | ||
} | ||
}); | ||
'getByUID' : function(type, uid, callback) { | ||
self.queryFirst(['at','my.'+type+'.uid',uid],callback); | ||
}, | ||
@@ -61,5 +46,5 @@ 'getBookmark' : function(bookmark, callback) { | ||
if(id) { | ||
self.getDocument(ctx, id, undefined, callback); | ||
self.getByID(ctx, id, callback); | ||
} else { | ||
callback(); | ||
callback(new Error("Error retrieving boomarked id")); | ||
} | ||
@@ -69,27 +54,18 @@ }); | ||
'getByIDs' : function(ids, callback) { | ||
ctxPromise.then(function(ctx){ | ||
res.locals.ctx = ctx; | ||
if(ids && ids.length) { | ||
ctx.api.forms('everything').ref(ctx.ref).query('[[:d = any(document.id, [' + ids.map(function(id) { return '"' + id + '"';}).join(',') + '])]]').submit(function(err, response) { | ||
callback(err, response.results); | ||
}); | ||
self.query(['any', 'document.id', ids], callback); | ||
}, | ||
'getByID' : function(id, callback) { | ||
self.queryFirst(['at', 'document.id', id], callback); | ||
}, | ||
'queryFirst' : function(q, callback) { | ||
self.query(q, function(err, response) { | ||
if(err){ | ||
callback(err, null); | ||
} else if(response && response.results && response.results[0]) { | ||
callback(null, response.results[0]); | ||
} else { | ||
callback(null, []); | ||
callback(new Error("empty response"), null); | ||
} | ||
}); | ||
}, | ||
'getByID' : function(id, slug, onThen, onNewSlug, onNotFound) { | ||
ctxPromise.then(function(ctx){ | ||
res.locals.ctx = ctx; | ||
ctx.api.forms('everything').ref(ctx.ref).query('[[:d = at(document.id, "' + id + '")]]').submit(function(err, response) { | ||
var results = response.results; | ||
var doc = results && results.length ? results[0] : undefined; | ||
if (err) onThen(err); | ||
else if(doc && (!slug || doc.slug == slug)) onDone(null, doc); | ||
else if(doc && doc.slugs.indexOf(slug) > -1 && onNewSlug) onNewSlug(doc); | ||
else if(onNotFound) onNotFound(); | ||
else onThen(); | ||
}); | ||
}); | ||
}, | ||
'query' : function(q, callback){ | ||
@@ -108,8 +84,2 @@ ctxPromise.then(function(ctx){ | ||
exports.withContext = function(req, res, callback) { | ||
if (!configuration.apiEndpoint) { | ||
throw new Error("Missing apiEndpoint in configuration: make sure to call init() at the beginning of your script"); | ||
} | ||
if (!configuration.linkResolver) { | ||
throw new Error("Missing linkResolver in configuration: make sure to call init() at the beginning of your script"); | ||
} | ||
var accessToken = (req.session && req.session['ACCESS_TOKEN']) || configuration.accessToken; | ||
@@ -119,5 +89,8 @@ var ctxPromise = new Promise(function (fulfill) { | ||
exports.getApiHome(accessToken, function(err, Api) { | ||
if (!configuration.linkResolver) { | ||
throw new Error("Missing linkResolver in configuration: make sure to call init() at the beginning of your script"); | ||
} | ||
if (err) { | ||
configuration.onPrismicError && configuration.onPrismicError(err, req, Api); | ||
return; | ||
exports.onPrismicError(err, req, res); | ||
return; | ||
} | ||
@@ -124,0 +97,0 @@ var ctx = { |
{ | ||
"name": "express-prismic", | ||
"description": "Prismic.io support for Prismic.io", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"license": "Apache-2.0", | ||
"homepage": "http://github.com/prismicio/express-prismic", | ||
@@ -15,3 +16,3 @@ "repository": { | ||
"dependencies": { | ||
"prismic.io": "1.2.1", | ||
"prismic.io": "1.3.0", | ||
"promise": "^7.0.4" | ||
@@ -18,0 +19,0 @@ }, |
15797
-6.73%99
-21.43%+ Added
- Removed
Updated