Created
September 25, 2018 08:00
-
-
Save pyldin601/edd4c5581b1e3b412827b380f7a31211 to your computer and use it in GitHub Desktop.
inversify + koa + 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
import * as Application from 'koa'; | |
import * as KoaRouter from 'koa-router'; | |
import { IRegistrableController } from './RegistrableController'; | |
import { injectable } from 'inversify'; | |
@injectable() | |
export abstract class AbstractRouterController implements IRegistrableController { | |
public abstract prefix: string; | |
public abstract setup(router: KoaRouter); | |
public register(app: Application) { | |
const router = new KoaRouter({ prefix: this.prefix }); | |
this.setup(router); | |
app.use(router.routes()); | |
app.use(router.allowedMethods()); | |
} | |
} |
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
import * as Application from 'koa'; | |
import * as KoaRouter from 'koa-router'; | |
import { injectable } from 'inversify'; | |
import { IRegistrableController } from './RegistrableController'; | |
@injectable() | |
export abstract class OneRouteController implements IRegistrableController { | |
public middleware = []; | |
public abstract method: string; | |
public abstract path: string; | |
public abstract handle(ctx: Application.Context); | |
public register(app: Application) { | |
const router = new KoaRouter(); | |
this.middleware.forEach(mw => router.use(mw)); | |
router[this.method](this.path, (ctx: Application.Context) => this.handle(ctx)); | |
app.use(router.routes()); | |
app.use(router.allowedMethods()); | |
} | |
} |
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
import { OneRouteController } from '../OneRouteController'; | |
import { ifNotAuthenticated, redirectToHome } from '../../middlewares/fragments'; | |
import { Context } from 'koa'; | |
import { StripeAccountService } from '../../services/db/StripeAccountService'; | |
import { inject } from 'inversify'; | |
import { StripeAccountServiceType } from '../../ioc/inversify.types'; | |
import UserEntity from '../../models/UserEntity'; | |
export class SettingsPageController extends OneRouteController { | |
public path = '/settings'; | |
public method = 'get'; | |
public middleware = [ifNotAuthenticated(redirectToHome)]; | |
constructor( | |
@inject(StripeAccountServiceType) private stripeAccountService: StripeAccountService, | |
) { | |
super(); | |
} | |
async handle(ctx: Context) { | |
const { user }: { user: UserEntity } = ctx.state; | |
const isConfigured = user.isExchangeConfigured(); | |
const hasStripeAccount = await this.stripeAccountService.hasStripeAccount(user); | |
const initialState = { user, isConfigured, hasStripeAccount }; | |
await ctx.render('settings', { initialState }); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment