Skip to content

Commit

Permalink
removed controller singleton exports
Browse files Browse the repository at this point in the history
  • Loading branch information
adamjakab committed Dec 27, 2020
1 parent 65d4df0 commit 592e780
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 17 deletions.
3 changes: 2 additions & 1 deletion src/app/workerApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ export class CloudflareWorkerApp {
}

private setupRoutes () {
this.restApiWorker.register('/', 'GET', RootController.list)
const rootController = new RootController()
this.restApiWorker.register('/', 'GET', rootController.list)
this.restApiWorker.useRoutingTable('/cards', CardRoutingTable)
}
}
5 changes: 1 addition & 4 deletions src/controller/card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
CardRepository
} from '../index'

class CardController extends Controller {
export class CardController extends Controller {
/**
* Path: /cards (GET)
* It will return the index and not the full item list
Expand Down Expand Up @@ -133,6 +133,3 @@ class CardController extends Controller {
return res.send(card, status)
}
}

// Singleton export
export = new CardController()
5 changes: 1 addition & 4 deletions src/controller/root.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
RestApiResponse
} from '../index'

class RootController extends Controller {
export class RootController extends Controller {
/**
* Path: / (GET)
*
Expand All @@ -21,6 +21,3 @@ class RootController extends Controller {
return res.send(reply)
}
}

// Singleton export
export = new RootController()
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ import { Platform } from './util/platform'

// Controllers
import { Controller } from './controller/controller'
import RootController from './controller/root'
import CardController from './controller/card'
import { RootController } from './controller/root'
import { CardController } from './controller/card'

// Routing
import { CardRoutingTable } from './router/card'
Expand Down
12 changes: 7 additions & 5 deletions src/router/card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import {
} from '../index'

/**
* @fixme: this requires the cardController to be created before the app is initialized
* Export a simple table of routes to be handled by the controller
*/
const cardController = new CardController()
export const CardRoutingTable: RawRouteItem[] = [
{ path: '/', method: 'GET', callback: CardController.list },
{ path: '/:id', method: 'GET', callback: CardController.getOne },
{ path: '/', method: 'POST', callback: CardController.create },
{ path: '/:id', method: 'PUT', callback: CardController.update },
{ path: '/:id', method: 'DELETE', callback: CardController.delete }
{ path: '/', method: 'GET', callback: cardController.list },
{ path: '/:id', method: 'GET', callback: cardController.getOne },
{ path: '/', method: 'POST', callback: cardController.create },
{ path: '/:id', method: 'PUT', callback: cardController.update },
{ path: '/:id', method: 'DELETE', callback: cardController.delete }
]
2 changes: 1 addition & 1 deletion test/functional/rest.api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,6 @@ describe('Rest API', () => {
route: '/non_existent_route',
method: 'GET'
}])
console.log(reply)
// console.log(reply)
})
})

0 comments on commit 592e780

Please sign in to comment.