Last active
January 6, 2017 12:18
-
-
Save thejhh/aee1f6aa69caca5067d7e2b069b6afd5 to your computer and use it in GitHub Desktop.
Sample of Sendanor's backend API framework with Express
This file contains hidden or 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
| "use strict"; | |
| var debug = require('nor-debug'); | |
| var erp = require('../../erp/index.js'); | |
| /** Prepare client object for public view */ | |
| function prepare_client(client) { | |
| client = client || {}; | |
| debug.assert(client).is('object'); | |
| return { | |
| '$id': client.client_id, | |
| 'client_id': client.client_id, | |
| 'updated': client.updated, | |
| 'creation': client.creation, | |
| 'date': client.date, | |
| 'termination_date': client.termination_date, | |
| 'company': client.company, | |
| 'company_code': client.company_code, | |
| 'firstname': client.firstname, | |
| 'lastname': client.lastname, | |
| 'address': client.address, | |
| 'postcode': client.postcode, | |
| 'postname': client.postname, | |
| 'country': client.country, | |
| 'email': client.email, | |
| 'phone': client.phone, | |
| 'mobile': client.mobile, | |
| 'fax': client.fax, | |
| 'billing_lang': client.billing_lang, | |
| 'send_email': client.send_email, | |
| 'send_post': client.send_post | |
| }; | |
| } | |
| /** Returns the root of the auth API */ | |
| module.exports = function api_client_root (db, authenticated, account) { | |
| debug.assert(authenticated).is('boolean'); | |
| if (!authenticated) { | |
| throw new HTTPError(401); | |
| } | |
| debug.assert(account).is('object'); | |
| debug.assert(account.email).is('email'); | |
| return erp.getClientsByEmail(account.email).then(function api_client_root_2 (clients) { | |
| debug.assert(clients).is('array'); | |
| // Authenticated resource | |
| var body = { | |
| 'email': account.email, | |
| 'clients': clients.map(prepare_client) | |
| }; | |
| return body; | |
| }); | |
| }; |
This file contains hidden or 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
| "use strict"; | |
| var express = require('express'); | |
| var wrapper = require('../../route-wrapper.js'); | |
| var api = module.exports = express.Router(); | |
| api.get('/', wrapper(require('./get.js'))); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment