-
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 8 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 |
---|---|---|
@@ -1,15 +1,15 @@ | ||
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. |
||
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: | ||
|
@@ -30,4 +30,3 @@ matrix: | |
- npm install | ||
- npm link fh-mbaas-api | ||
- npm test | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
## Sync typescript sample application example | ||
|
||
## Requirements | ||
|
||
|
||
`MONGO_CONNECTION_URL` environment variable needs to point to mongodb instance | ||
|
||
By default using: mongodb://127.0.0.1:27017/sync | ||
|
||
|
||
`REDIS_CONNECTION_URL` environment variable needs to point to running redis instance | ||
|
||
By default using: redis://127.0.0.1:6379 | ||
|
||
## Running | ||
|
||
npm install | ||
npm run start | ||
|
||
## Testing | ||
|
||
Please refer to documentation for information how to setup sync client. |
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() | ||
|
||
// 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: any, result: any) { | ||
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 Any reason you changed the types of the callback parameters to 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 did not compile for me - were complaining about missing types :) 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 this might be a |
||
if (err) { | ||
next(err) | ||
} else { | ||
res.json(result) | ||
} | ||
}) | ||
}) | ||
|
||
export default router |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
|
||
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,24 @@ | ||
{ | ||
"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": "ts-node server.ts" | ||
}, | ||
"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", | ||
"ts-node": "^3.3.0" | ||
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 You don't want to make this change to ts-node. Or at least you definitely don't want to remove typescript as a dependency. Right now, ts-node will use whatever version of typescript is installed globally, and there's no guarantee that it's a compatible version, if it's installed at all. (Also, I don't think it's common to use ts-node on prod apps, so this wouldn't be ideal as a starting point.) As an update, I found this regarding ts-node in production. Using ts-node in prod isn't too bad, but it's a bit slower to restart the app. 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 point. Reason why this worked for me is that I had typescript installed from previous npm install. I forgot that ts-node requires typescript to be available globally. 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 don't have typescript installed locally so What version do you have installed globally? I think the missing types issue that my other comment was about might have to do with that, as I was able to revert your changes and it compiled fine. (And intellisense was correct.) |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
|
||
import * as sync from './lib/sync' | ||
import syncRouter from './lib/route' | ||
import * as express from 'express' | ||
|
||
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: express.Request, res: express.Response) => { | ||
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,14 @@ | ||
{ | ||
"compilerOptions": { | ||
"module": "commonjs", | ||
"noImplicitAny": true, | ||
"removeComments": true, | ||
"preserveConstEnums": true, | ||
"sourceMap": true, | ||
"target": "es5", | ||
"strict": true | ||
}, | ||
"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.