-
Notifications
You must be signed in to change notification settings - Fork 17
Update typings. Add typescript example. Cleanup package.json #41
base: master
Are you sure you want to change the base?
Changes from 3 commits
ced9f42
6a2853e
e28633a
36499a8
a1e21bd
2f68a77
99a33fb
705dd8e
7529853
05e9fb8
43d5400
4be8601
3dcdd4b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# Tells the .editorconfg plugin to stop searching once it finds this file | ||
root = true | ||
|
||
[*] | ||
indent_size = 2 | ||
indent_style = space | ||
charset = utf-8 | ||
end_of_line = lf | ||
insert_final_newline = true | ||
trim_trailing_whitespace = true | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,14 +2,16 @@ language: node_js | |
sudo: required | ||
node_js: | ||
- "0.10" | ||
- "4" | ||
- "4.4.3" | ||
- "6" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should be testing against recent versions of Node.js. I kept 4.4.3 since I believe we have some requirements around supporting that explicit version There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good spot! Let's remove 0.10 and 4 - it's gone for long time. |
||
- "8" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. lts/* will be better here. It my suggest that we support 8 :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @wtrocki not sure I follow, do you mean we can put "*" instead of "8"? |
||
services: | ||
- docker | ||
before_install: | ||
- sudo apt-get update | ||
- sudo apt-get install --assume-yes apache2-utils | ||
- npm install -g [email protected] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we still be using npm 2.x, even when running on node 6/8? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that's a fair question. Perhaps @wtrocki can confirm if there's a requirement from some users? |
||
- npm install -g grunt-cli | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removed this since it's now in "devDependencies" There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👎 it is for the CI process to run the script and test the project to allow the PR is required the grunt-cli |
||
- npm config set strict-ssl false | ||
install: npm install | ||
env: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{ | ||
"name": "basic-express-typescript", | ||
"version": "0.0.1", | ||
"description": "Example of using TypeScript with fh-sync", | ||
"main": "server.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1", | ||
"start": "tsc && node server.js" | ||
}, | ||
"author": "Evan Shortiss <[email protected]> (http://evanshortiss.com/)", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not against that, but having personalized author in organization code seems to be antipatern. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @wtrocki that's a mistake. |
||
"license": "MIT", | ||
"dependencies": { | ||
"bluebird": "~3.5.1", | ||
"body-parser": "~1.18.2", | ||
"cors": "~2.8.4", | ||
"express": "~4.16.2" | ||
}, | ||
"devDependencies": { | ||
"@types/bluebird": "~3.5.18", | ||
"@types/body-parser": "~1.16.8", | ||
"@types/cors": "~2.8.3", | ||
"@types/express": "~4.0.39", | ||
"typescript": "~2.6.1" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
|
||
import * as express from 'express' | ||
import * as parsers from 'body-parser' | ||
import * as cors from 'cors' | ||
import * as sync from '../../fh-sync' | ||
|
||
const router = express.Router() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No semis? Daring. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm trying it out here too 😂 |
||
|
||
// Mobile clients typically require CORS headers to be set | ||
router.use(cors()) | ||
|
||
// Need to parse incoming JSON bodies | ||
router.use(parsers.json()) | ||
|
||
// All sync requests are performed using a HTTP POST | ||
router.post('/:datasetId', (req: express.Request, res: express.Response, next: express.NextFunction) => { | ||
// Invoke action in sync for specific dataset | ||
sync.invoke(req.params.datasetId, req.body, function (err, result) { | ||
if (err) { | ||
next(err) | ||
} else { | ||
res.json(result) | ||
} | ||
}) | ||
}) | ||
|
||
export default router |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
|
||
import * as express from 'express' | ||
import * as sync from './sync' | ||
import syncRouter from './route' | ||
|
||
const app = express() | ||
|
||
sync.init() | ||
.then(startApplicationServer) | ||
.catch((e) => { | ||
console.log('error occurred during startup', e) | ||
process.exit(1) | ||
}) | ||
|
||
function startApplicationServer (err: any) { | ||
if (err) { | ||
console.log('error starting sync server:') | ||
throw err | ||
} | ||
|
||
console.log('Sync initialised') | ||
|
||
// Sync express api required for sync clients. All sync clients will call this endpoint to sync data | ||
app.use('/sync', syncRouter) | ||
|
||
// Default route. Can be used to check application is up and running | ||
app.get('/', (req, res) => { | ||
res.send('Sample application is running!') | ||
}) | ||
|
||
app.listen(3000, (err: any) => { | ||
if (err) throw err | ||
|
||
console.log('\nExample app listening on port 3000!') | ||
console.log('\nRun the following from a terminal to get records via sync:') | ||
console.log('curl http://localhost:3000/sync/messages -X POST --data \'{"fn": "syncRecords"}\' -H "content-type:application/json"\n') | ||
}); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
|
||
import * as sync from '../../fh-sync' | ||
import * as Promise from 'bluebird' | ||
|
||
// Sync framework requires mongodb and redis to be running | ||
const MONGO_CONN_STRING = process.env.MONGO_CONNECTION_URL || 'mongodb://127.0.0.1:27017/sync'; | ||
const REDIS_CONN_STRING = process.env.REDIS_CONNECTION_URL || 'redis://127.0.0.1:6379'; | ||
|
||
// Options to pass to the mongodb driver | ||
const MONGO_OPTS = {} | ||
|
||
// Define our dataset name and the option such as how often to sync to system of record | ||
const DATASET_NAME = 'messages' | ||
const DATASET_OPTS = { | ||
syncFrequency: 10 // seconds | ||
}; | ||
|
||
|
||
interface Query { | ||
username: string | ||
} | ||
|
||
interface Meta { | ||
trackingId: string | ||
} | ||
|
||
function initialiseDataset () { | ||
return new Promise((resolve, reject) => { | ||
sync.init(DATASET_NAME, DATASET_OPTS, (err) => { | ||
if (err) { | ||
reject(err) | ||
} else { | ||
// Sample list handler. Uses a custom query and metadata interface to provide | ||
// better typings in the handler logic. | ||
sync.handleList(DATASET_NAME, (dataset, query: Query, meta: Meta, done) => { | ||
console.log(`received request from ${query.username} with tracking ID ${meta.trackingId}`) | ||
|
||
done(null, { | ||
'00001': { | ||
'item': 'item1' | ||
}, | ||
'00002': { | ||
'item': 'item2' | ||
}, | ||
'00003': { | ||
'item': 'item3' | ||
} | ||
}) | ||
}) | ||
|
||
resolve() | ||
} | ||
}) | ||
}) | ||
} | ||
|
||
function connect () { | ||
return new Promise((resolve, reject) => { | ||
sync.connect(MONGO_CONN_STRING, MONGO_OPTS, REDIS_CONN_STRING, (err) => { | ||
if (err) { | ||
reject(err) | ||
} else { | ||
resolve() | ||
} | ||
}); | ||
}) | ||
} | ||
|
||
export function init () { | ||
return connect().then(initialiseDataset) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"compilerOptions": { | ||
"module": "commonjs", | ||
"noImplicitAny": true, | ||
"removeComments": true, | ||
"preserveConstEnums": true, | ||
"sourceMap": true, | ||
"target": "es5" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will take a look at that 👍 |
||
}, | ||
"include": [ | ||
"server.ts" | ||
] | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should help keep things more standardised across folks machines.