Skip to content

Commit

Permalink
first version of sync with express
Browse files Browse the repository at this point in the history
  • Loading branch information
kanekotic committed Dec 30, 2018
1 parent 4c6bc34 commit bb4298f
Show file tree
Hide file tree
Showing 11 changed files with 4,498 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,11 @@ typings/

# next.js build output
.next
dist
/cdn

#Project settings
.idea
.vscode
dist
coverage
12 changes: 12 additions & 0 deletions .npmignore
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
6 changes: 6 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"printWidth": 120,
"trailingComma": "all",
"singleQuote": true,
"semi": false
}
22 changes: 22 additions & 0 deletions .travis.yml
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
28 changes: 28 additions & 0 deletions lib/index.ts
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
}
67 changes: 67 additions & 0 deletions package.json
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"
}
}
120 changes: 120 additions & 0 deletions test/index.spec.ts
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);
});
});
})
13 changes: 13 additions & 0 deletions tsconfig.json
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/*"]
}
13 changes: 13 additions & 0 deletions tslint.json
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"
]
}
}
21 changes: 21 additions & 0 deletions upgrade.sh
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
Loading

0 comments on commit bb4298f

Please sign in to comment.