Skip to content
This repository has been archived by the owner on Dec 14, 2023. It is now read-only.

Commit

Permalink
Merge pull request #9 from prismicio/alw/clean
Browse files Browse the repository at this point in the history
Clean code and update dependencies
  • Loading branch information
srenault authored Jun 8, 2017
2 parents 1c4a51b + 0871a96 commit 85273e0
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 52 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ node_js:
- '5'
- '6'
- '7'
- '8'
script: npm run lint
deploy:
provider: heroku
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ Contribution is open to all developer levels, read our "[Contribute to the offic

This software is licensed under the Apache 2 license, quoted below.

Copyright 2013 Zengularity (http://www.zengularity.com).
Copyright 2017 Prismic.io (https://prismic.io).

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this project except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.

Expand Down
59 changes: 29 additions & 30 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const Prismic = require('prismic-nodejs');
const request = require('request');
const Cookies = require('cookies');
const PrismicConfig = require('./prismic-configuration');
const Onboarding = require('./onboarding');
const app = require('./config');
Expand All @@ -11,41 +12,27 @@ app.listen(PORT, () => {
process.stdout.write(`Point your browser to: http://localhost:${PORT}\n`);
});

/*
* Initialize prismic context and api
*/
// Middleware to inject prismic context
app.use((req, res, next) => {
Prismic.api(PrismicConfig.apiEndpoint, { accessToken: PrismicConfig.accessToken, req })
.then((api) => {
res.locals.ctx = {
endpoint: PrismicConfig.apiEndpoint,
linkResolver: PrismicConfig.linkResolver,
};

Prismic.api(PrismicConfig.apiEndpoint, {
accessToken: PrismicConfig.accessToken,
req,
}).then((api) => {
req.prismic = { api };
res.locals.ctx = {
endpoint: PrismicConfig.apiEndpoint,
linkResolver: PrismicConfig.linkResolver,
};
next();
}).catch((err) => {
const message = err.status === 404 ? 'There was a problem connecting to your API, please check your configuration file for errors.' : `Error 500: ${err.message}`;
res.status(err.status).send(message);
}).catch((error) => {
next(error.message);
});
});

// INSERT YOUR ROUTES HERE
app.get('/page/:uid', (req, res) => {
// We store the param uid in a variable
const uid = req.params.uid;
// We are using the function to get a document by its uid
req.prismic.api.getByUID('page', uid).then((pageContent) => {
if (pageContent) {
// pageContent is a document, or null if there is no match
res.render('page', {
// Where 'page' is the name of your pug template file (page.pug)
pageContent,
});
} else {
res.status(404).send('404 not found');
}
});
});
/*
* --[ INSERT YOUR ROUTES HERE ]--
*/

/*
* Route with documentation to build your project with prismic
Expand All @@ -71,5 +58,17 @@ app.get('/help', (req, res) => {
* Preconfigured prismic preview
*/
app.get('/preview', (req, res) => {
Prismic.preview(req.prismic.api, PrismicConfig.linkResolver, req, res);
const token = req.query.token;
if (token) {
req.prismic.api.previewSession(token, PrismicConfig.linkResolver, '/')
.then((url) => {
const cookies = new Cookies(req, res);
cookies.set(Prismic.previewCookie, token, { maxAge: 30 * 60 * 1000, path: '/', httpOnly: false });
res.redirect(302, url);
}).catch((err) => {
res.status(500).send(`Error 500 in preview: ${err.message}`);
});
} else {
res.send(400, 'Missing token from querystring');
}
});
21 changes: 11 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,27 @@
"test": "node -c *.js"
},
"dependencies": {
"body-parser": "~1.17.1",
"debug": "~2.6.3",
"body-parser": "~1.17.2",
"cookies": "~0.7.0",
"debug": "~2.6.8",
"errorhandler": "~1.5.0",
"express": "~4.15.2",
"method-override": "~2.3.7",
"morgan": "~1.8.1",
"express": "~4.15.3",
"method-override": "~2.3.9",
"morgan": "~1.8.2",
"prismic-nodejs": "~1.5.0",
"pug": "~2.0.0-beta11",
"pug": "~2.0.0-rc.2",
"request": "~2.81.0",
"serve-favicon": "~2.4.1"
"serve-favicon": "~2.4.3"
},
"repository": {
"type": "git",
"url": "http://github.com/prismicio/nodejs-sdk.git"
},
"devDependencies": {
"babel-eslint": "~7.2.0",
"babel-eslint": "~7.2.3",
"eslint": "~3.19.0",
"eslint-config-airbnb-base": "~11.1.0",
"eslint-plugin-import": "~2.2.0"
"eslint-config-airbnb-base": "~11.2.0",
"eslint-plugin-import": "~2.3.0"
},
"engines": {
"node": ">=4.8.1",
Expand Down
12 changes: 8 additions & 4 deletions views/help.pug
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,12 @@ body
code
// Using NodeJS
|// add a new route in ./app.js
|app.get('/page/:uid', (req, res) => {
|app.get('/page/:uid', (req, res, next) => {
| // We store the param uid in a variable
| var uid = req.params.uid;
| const uid = req.params.uid;
| // We are using the function to get a document by its uid
| req.prismic.api.getByUID('#{'<your-custom-type-id>'}', uid).then((pageContent) => {
| req.prismic.api.getByUID('#{'<your-custom-type-id>'}', uid)
| .then((pageContent) => {
| if (pageContent) {
| // pageContent is a document, or null if there is no match
| res.render('page', {
Expand All @@ -102,6 +103,9 @@ body
| } else {
| res.status(404).send('404 not found');
| }
| })
| .catch((error) => {
| next(`error when retriving page ${error.message}`);
| });
|});

Expand All @@ -122,7 +126,7 @@ body
|extends ./layout.pug
| //- Create a block
|block body
| div
| div(data-wio-id=pageContent.id)
| //- This is how to get an image into your template
| img(src=pageContent.getImage('#{'<Your-custom-type-id>.<Your-field-image-id>'}').url)
| //- This is how to get a structured text into your template
Expand Down
7 changes: 0 additions & 7 deletions views/page.pug

This file was deleted.

0 comments on commit 85273e0

Please sign in to comment.