-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
kanekotic
committed
Dec 30, 2018
1 parent
4c6bc34
commit bb4298f
Showing
11 changed files
with
4,498 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -59,3 +59,11 @@ typings/ | |
|
||
# next.js build output | ||
.next | ||
dist | ||
/cdn | ||
|
||
#Project settings | ||
.idea | ||
.vscode | ||
dist | ||
coverage |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
./vscode | ||
/examples | ||
/lib | ||
/test | ||
.gitignore | ||
.prettierrc | ||
.travis.yml | ||
jestconfig.json | ||
README.md | ||
tsconfig.json | ||
tslint.json | ||
coverage |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"printWidth": 120, | ||
"trailingComma": "all", | ||
"singleQuote": true, | ||
"semi": false | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
os: osx | ||
language: node_js | ||
node_js: | ||
- node | ||
script: | ||
- yarn test:cov | ||
after_success: | ||
- if [[ "${TRAVIS_EVENT_TYPE}" = "cron" ]]; then ./upgrade.sh; fi | ||
deploy: | ||
- provider: npm | ||
skip_cleanup: true | ||
email: $NPM_EMAIL | ||
api_key: $NPM_TOKEN | ||
on: | ||
tags: true | ||
- provider: releases | ||
skip_cleanup: true | ||
file_glob: true | ||
file: cdn/*.js | ||
api_key: $GH_TOKEN | ||
on: | ||
tags: true |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { Router, Request, Response } from 'express' | ||
import { ActorSystem } from 'tarant' | ||
|
||
export default function SyncController(system: ActorSystem, config: any) { | ||
const router: Router = Router() | ||
|
||
router.get(`${config.paths.pull}/:id`, async (req: Request, res: Response) => { | ||
try { | ||
const actor = (await system.actorFor(req.params.id)) as any | ||
res.json(await actor.toJson()) | ||
} catch (_) { | ||
res.sendStatus(404) | ||
} | ||
}) | ||
|
||
router.post(`${config.paths.push}/:id`, async (req: Request, res: Response) => { | ||
let actor | ||
try { | ||
actor = (await system.actorFor(req.params.id)) as any | ||
} catch (_) { | ||
actor = (await system.actorOf(config.ActorTypes[req.body.type], [req.params.id])) as any | ||
} | ||
actor.updateFrom(req.body) | ||
res.sendStatus(200) | ||
}) | ||
|
||
return router | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
{ | ||
"name": "tarant-sync-router-express", | ||
"version": "0.0.1", | ||
"description": "Actor model for reactive and scalable applications.", | ||
"main": "dist/index.js", | ||
"types": "dist/index.d.ts", | ||
"homepage": "https://www.tarant.io/", | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"keywords": [ | ||
"actor system", | ||
"actor", | ||
"DDD", | ||
"actor-system", | ||
"backend", | ||
"back end", | ||
"sync", | ||
"express" | ||
], | ||
"scripts": { | ||
"prepare": "yarn build", | ||
"build": "tsc", | ||
"format": "prettier --write \"**/*.ts\" && tslint -p tsconfig.json --fix lib/**/*.ts -t verbose", | ||
"lint": "tslint -p tsconfig.json", | ||
"test": "yarn format && yarn lint && jest test", | ||
"test:dev": "jest --watchAll test", | ||
"test:cov": "yarn format && yarn lint && jest --coverage && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js", | ||
"push": "yarn test && git push", | ||
"deploy:major": "yarn version --major", | ||
"deploy:minor": "yarn version --minor", | ||
"deploy:patch": "yarn version --patch", | ||
"deploy:push": "git push && git push --tags", | ||
"preversion": "yarn test" | ||
}, | ||
"contributors": [ | ||
"Kevin Mas Ruiz <[email protected]>", | ||
"Kanekotic <[email protected]>" | ||
], | ||
"repository": "[email protected]:tarantx/tarant-sync-router-express.git", | ||
"license": "MIT", | ||
"devDependencies": { | ||
"@types/express": "4.16.0", | ||
"@types/faker": "4.1.4", | ||
"@types/jest": "23.3.10", | ||
"@types/node": "10.12.18", | ||
"body-parser": "1.18.3", | ||
"coveralls": "3.0.2", | ||
"faker": "4.1.0", | ||
"jest": "23.6.0", | ||
"prettier": "1.15.3", | ||
"supertest": "3.3.0", | ||
"ts-jest": "23.10.5", | ||
"tslint": "5.12.0", | ||
"tslint-config-prettier": "1.17.0", | ||
"typescript": "3.2.2" | ||
}, | ||
"dependencies": { | ||
"@types/supertest": "^2.0.7", | ||
"express": "4.16.4", | ||
"tarant": "2.6.2", | ||
"tslib": "1.9.3" | ||
}, | ||
"jest": { | ||
"preset": "ts-jest" | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
import SyncController from '../lib/index' | ||
import { ActorSystem } from 'tarant' | ||
import Actor from 'tarant/dist/actor-system/actor' | ||
import * as request from 'supertest' | ||
import * as express from "express" | ||
import * as faker from 'faker' | ||
import { json } from "body-parser"; | ||
|
||
class FakeActor extends Actor {} | ||
let config: any = { | ||
sync: { | ||
active: true, | ||
delay: 1000, | ||
}, | ||
paths: { | ||
pull: '/pull', | ||
push: '/push', | ||
}, | ||
ActorTypes: { FakeActor }, | ||
} | ||
describe('index exports function that returns express router', () => { | ||
let supertest: any | ||
let app: express.Express | ||
|
||
beforeEach(() => { | ||
app = express() | ||
app.use(json()) | ||
supertest = request(app) | ||
}) | ||
|
||
function setupController(system: ActorSystem){ | ||
const controller = SyncController(system, config) | ||
app.use(controller) | ||
} | ||
|
||
describe('pull', () => { | ||
it('should pull actor data if found', async () => { | ||
|
||
const id = faker.random.uuid(), | ||
expectedresult = { stuff: faker.random.uuid() }, | ||
ActorSystemMock = { | ||
actorFor: jest.fn() | ||
} | ||
|
||
let system: ActorSystem = jest.fn<ActorSystem>(() => ActorSystemMock)() | ||
setupController(system) | ||
ActorSystemMock.actorFor.mockResolvedValue({ | ||
toJson: () => Promise.resolve(expectedresult) | ||
}) | ||
|
||
const result = await supertest.get(`${config.paths.pull}/${id}`) | ||
expect(result.body).toEqual(expectedresult); | ||
expect(result.statusCode).toEqual(200); | ||
}) | ||
|
||
it('should return 404 if unable to find actor', async () => { | ||
|
||
const id = faker.random.uuid(), | ||
ActorSystemMock = { | ||
actorFor: jest.fn() | ||
} | ||
|
||
let system: ActorSystem = jest.fn<ActorSystem>(() => ActorSystemMock)() | ||
setupController(system) | ||
ActorSystemMock.actorFor.mockRejectedValue("") | ||
|
||
const result = await supertest.get(`${config.paths.pull}/${id}`) | ||
|
||
expect(ActorSystemMock.actorFor).toHaveBeenCalledWith(id) | ||
expect(result.statusCode).toEqual(404); | ||
}) | ||
}); | ||
|
||
describe('push', () => { | ||
it('should update state if actor exists', async () => { | ||
const id = faker.random.uuid(), | ||
body = { stuff: faker.random.uuid() }, | ||
ActorSystemMock = { | ||
actorFor: jest.fn() | ||
}, | ||
ActorMock = { | ||
updateFrom : jest.fn() | ||
} | ||
|
||
let system: ActorSystem = jest.fn<ActorSystem>(() => ActorSystemMock)() | ||
setupController(system) | ||
ActorSystemMock.actorFor.mockResolvedValue(ActorMock) | ||
|
||
const result = await supertest.post(`${config.paths.push}/${id}`).send(body) | ||
|
||
expect(ActorSystemMock.actorFor).toHaveBeenCalledWith(id) | ||
expect(ActorMock.updateFrom).toHaveBeenCalledWith(body) | ||
expect(result.statusCode).toEqual(200); | ||
}); | ||
|
||
it('should crate actor and initialize state if actor does not exists', async () => { | ||
const id = faker.random.uuid(), | ||
body = { stuff: faker.random.uuid(), type: "FakeActor" }, | ||
ActorSystemMock = { | ||
actorFor: jest.fn(), | ||
actorOf: jest.fn() | ||
}, | ||
ActorMock = { | ||
updateFrom : jest.fn() | ||
} | ||
|
||
let system: ActorSystem = jest.fn<ActorSystem>(() => ActorSystemMock)() | ||
setupController(system) | ||
ActorSystemMock.actorFor.mockRejectedValue("") | ||
ActorSystemMock.actorOf.mockResolvedValue(ActorMock) | ||
|
||
const result = await supertest.post(`${config.paths.push}/${id}`).send(body) | ||
|
||
expect(ActorSystemMock.actorFor).toHaveBeenCalledWith(id) | ||
expect(ActorSystemMock.actorOf).toHaveBeenCalledWith(FakeActor, [id]) | ||
expect(ActorMock.updateFrom).toHaveBeenCalledWith(body) | ||
expect(result.statusCode).toEqual(200); | ||
}); | ||
}); | ||
}) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "es5", | ||
"module": "commonjs", | ||
"declaration": true, | ||
"outDir": "./dist", | ||
"strict": true, | ||
"lib": ["es6", "dom"], | ||
"importHelpers": true | ||
}, | ||
"files": ["./lib/index.ts"], | ||
"exclude": ["node_modules", "**/test/*"] | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"rules": { | ||
"semicolon": [true, "never"], | ||
"only-arrow-functions": true | ||
}, | ||
"extends": ["tslint:recommended", "tslint-config-prettier"], | ||
"linterOptions": { | ||
"exclude": [ | ||
"examples/**/*.ts", | ||
"dist/**/*.js" | ||
] | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#!/bin/sh | ||
|
||
set -e | ||
|
||
git config --global user.email $GH_EMAIL | ||
git config --global user.name $GH_USER | ||
|
||
git remote add origin-master https://${GH_TOKEN}@github.com/${TRAVIS_REPO_SLUG}.git > /dev/null 2>&1 | ||
|
||
git fetch origin-master | ||
git checkout -b master-local origin-master/master | ||
|
||
yarn upgrade --latest | ||
git add . | ||
git commit --allow-empty -m "updated dependencies [skip ci]" | ||
|
||
yarn test | ||
yarn deploy:patch | ||
|
||
git push --quiet origin-master master-local:master | ||
git push --quiet origin-master master-local:master --tags |
Oops, something went wrong.