Skip to content

Commit 160e9ba

Browse files
authored
Merge pull request #113 from TaloDev/develop
Release 0.16.0
2 parents 0c440e2 + 6fa13d9 commit 160e9ba

32 files changed

+100
-113
lines changed

_templates/service/new/api-service.ejs.t

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,17 @@
11
---
22
to: "<%= (typeof api !== 'undefined') ? `src/services/api/${name}-api.service.ts` : null %>"
33
---
4-
import { HasPermission, Request, Response } from 'koa-clay'
4+
import { HasPermission, Request, Response, ForwardTo, forwardRequest } from 'koa-clay'
55
import <%= h.changeCase.pascal(name) %>APIPolicy from '../../policies/api/<%= name %>-api.policy'
6-
import <%= h.changeCase.pascal(name) %>Service from '../<%= name %>.service'
76
import APIService from './api-service'
87
import APIKey from '../../entities/api-key'
98

10-
export default class <%= h.changeCase.pascal(name) %>APIService extends APIService<<%= h.changeCase.pascal(name) %>Service> {
11-
constructor() {
12-
super('<%= name %>s')
13-
}
14-
9+
export default class <%= h.changeCase.pascal(name) %>APIService extends APIService {
1510
@HasPermission(<%= h.changeCase.pascal(name) %>APIPolicy, 'post')
11+
@ForwardTo(<%= h.changeCase.dot(name) %>, 'post')
1612
async post(req: Request): Promise<Response> {
1713
const key: APIKey = await this.getAPIKey(req.ctx)
18-
return await this.forwardRequest('post', req, {
14+
return await forwardRequest(req, {
1915
body: {
2016
gameId: key.game.id
2117
}

_templates/service/new/protected-route.ejs.t

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ inject: true
33
to: src/config/protected-routes.ts
44
before: app\.use\(service
55
---
6-
app.use(service('/<%= name %>s', new <%= h.changeCase.pascal(name) %>Service()))
6+
app.use(service('/<%= name %>s', new <%= h.changeCase.pascal(name) %>Service(), serviceOpts))

_templates/service/new/service.ejs.t

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { HasPermission, Service, Request, Response, Validate } from 'koa-clay'
66
import <%= h.changeCase.pascal(name) %> from '../entities/<%= name %>'
77
import <%= h.changeCase.pascal(name) %>Policy from '../policies/<%= name %>.policy'
88

9-
export default class <%= h.changeCase.pascal(name) %>Service implements Service {
9+
export default class <%= h.changeCase.pascal(name) %>Service extends Service {
1010
@Validate({
1111
query: ['<%= h.changeCase.camel(name) %>Id']
1212
})

package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "game-services",
3-
"version": "0.15.0",
3+
"version": "0.16.0",
44
"description": "",
55
"main": "src/index.ts",
66
"scripts": {
@@ -68,13 +68,12 @@
6868
"jsonwebtoken": "^8.5.1",
6969
"koa": "^2.13.4",
7070
"koa-bodyparser": "^4.3.0",
71-
"koa-clay": "^5.3.0",
71+
"koa-clay": "^6.1.1",
7272
"koa-helmet": "^6.1.0",
7373
"koa-jwt": "^4.0.3",
7474
"koa-logger": "^3.2.1",
7575
"lodash.get": "^4.4.2",
7676
"lodash.groupby": "^4.6.0",
77-
"lodash.merge": "^4.6.2",
7877
"lodash.uniqwith": "^4.5.0",
7978
"otplib": "^12.0.1",
8079
"qrcode": "^1.5.0",

src/config/protected-routes.ts

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Koa, { Context, Next } from 'koa'
2-
import { service } from 'koa-clay'
2+
import { service, ServiceOpts } from 'koa-clay'
33
import OrganisationService from '../services/organisation.service'
44
import InviteService from '../services/invite.service'
55
import GameStatService from '../services/game-stat.service'
@@ -23,17 +23,23 @@ export default (app: Koa) => {
2323
await next()
2424
})
2525

26-
app.use(service('/billing', new BillingService()))
27-
app.use(service('/organisations', new OrganisationService()))
28-
app.use(service('/invites', new InviteService()))
29-
app.use(service('/game-stats', new GameStatService()))
30-
app.use(service('/game-activities', new GameActivityService()))
31-
app.use(service('/leaderboards', new LeaderboardService()))
32-
app.use(service('/data-exports', new DataExportService()))
33-
app.use(service('/api-keys', new APIKeyService()))
34-
app.use(service('/events', new EventService()))
35-
app.use(service('/players', new PlayerService()))
36-
app.use(service('/games', new GameService()))
37-
app.use(service('/users', new UserService()))
38-
app.use(service('/headlines', new HeadlineService()))
26+
const serviceOpts: ServiceOpts = {
27+
docs: {
28+
hidden: true
29+
}
30+
}
31+
32+
app.use(service('/billing', new BillingService(), serviceOpts))
33+
app.use(service('/organisations', new OrganisationService(), serviceOpts))
34+
app.use(service('/invites', new InviteService(), serviceOpts))
35+
app.use(service('/game-stats', new GameStatService(), serviceOpts))
36+
app.use(service('/game-activities', new GameActivityService(), serviceOpts))
37+
app.use(service('/leaderboards', new LeaderboardService(), serviceOpts))
38+
app.use(service('/data-exports', new DataExportService(), serviceOpts))
39+
app.use(service('/api-keys', new APIKeyService(), serviceOpts))
40+
app.use(service('/events', new EventService(), serviceOpts))
41+
app.use(service('/players', new PlayerService(), serviceOpts))
42+
app.use(service('/games', new GameService(), serviceOpts))
43+
app.use(service('/users', new UserService(), serviceOpts))
44+
app.use(service('/headlines', new HeadlineService(), serviceOpts))
3945
}

src/config/public-routes.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
import Koa from 'koa'
2-
import { service } from 'koa-clay'
2+
import { service, ServiceOpts } from 'koa-clay'
33
import DemoService from '../services/public/demo.service'
4+
import DocumentationService from '../services/public/documentation.service'
45
import InvitePublicService from '../services/public/invite-public.service'
56
import UserPublicService from '../services/public/user-public.service'
67
import WebhookService from '../services/public/webhook.service'
78

89
export default (app: Koa) => {
9-
app.use(service('/public/webhooks', new WebhookService()))
10-
app.use(service('/public/invites', new InvitePublicService()))
11-
app.use(service('/public/users', new UserPublicService()))
12-
app.use(service('/public/demo', new DemoService()))
10+
const serviceOpts: ServiceOpts = {
11+
docs: {
12+
hidden: true
13+
}
14+
}
15+
16+
app.use(service('/public/docs', new DocumentationService(), serviceOpts))
17+
app.use(service('/public/webhooks', new WebhookService(), serviceOpts))
18+
app.use(service('/public/invites', new InvitePublicService(), serviceOpts))
19+
app.use(service('/public/users', new UserPublicService(), serviceOpts))
20+
app.use(service('/public/demo', new DemoService(), serviceOpts))
1321
}

src/config/redis.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
export default {
2-
host: 'redis',
2+
host: process.env.REDIS_HOST ?? 'redis',
33
password: process.env.REDIS_PASSWORD
44
}

src/services/api-key.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export async function createToken(apiKey: APIKey, payloadParams?: ExtraTokenPayl
3535
method: 'DELETE'
3636
}
3737
])
38-
export default class APIKeyService implements Service {
38+
export default class APIKeyService extends Service {
3939
@Validate({
4040
body: ['gameId', 'scopes']
4141
})

src/services/api/api-service.ts

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,11 @@
1-
import { Service, Request, Response } from 'koa-clay'
1+
import { Service } from 'koa-clay'
22
import { Context } from 'koa'
33
import APIKey from '../../entities/api-key'
44
import { EntityManager } from '@mikro-orm/core'
5-
import merge from 'lodash.merge'
6-
7-
export default class APIService<T> implements Service {
8-
serviceName: string
9-
10-
constructor(serviceName: string) {
11-
this.serviceName = serviceName
12-
}
135

6+
export default class APIService extends Service {
147
async getAPIKey(ctx: Context): Promise<APIKey> {
158
const key = await (<EntityManager>ctx.em).getRepository(APIKey).findOne(ctx.state.user.sub)
169
return key
1710
}
18-
19-
getService(ctx: Context): T {
20-
return ctx.state.services[this.serviceName]
21-
}
22-
23-
forwardRequest(funcName: string, req: Request, extra?: Partial<Request>): Promise<Response> {
24-
const newRequest: Request = Object.assign({}, req)
25-
merge(newRequest, extra)
26-
27-
const service = this.getService(newRequest.ctx)
28-
const func = service[funcName]
29-
return func.call(service, newRequest)
30-
}
3111
}

src/services/api/event-api.service.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
11
import { EntityManager } from '@mikro-orm/core'
2-
import { HasPermission, Request, Response, Validate, ValidationCondition } from 'koa-clay'
2+
import { forwardRequest, ForwardTo, HasPermission, Request, Response, Validate, ValidationCondition } from 'koa-clay'
33
import Event from '../../entities/event'
44
import EventAPIPolicy from '../../policies/api/event-api.policy'
5-
import EventService from '../event.service'
65
import APIService from './api-service'
76
import APIKey from '../../entities/api-key'
87
import PlayerAlias from '../../entities/player-alias'
98
import groupBy from 'lodash.groupby'
109

11-
export default class EventAPIService extends APIService<EventService> {
12-
constructor() {
13-
super('events')
14-
}
15-
10+
export default class EventAPIService extends APIService {
1611
@Validate({
1712
body: {
1813
events: {
@@ -88,9 +83,10 @@ export default class EventAPIService extends APIService<EventService> {
8883
}
8984

9085
@HasPermission(EventAPIPolicy, 'index')
86+
@ForwardTo('events', 'index')
9187
async index(req: Request): Promise<Response> {
9288
const key: APIKey = await this.getAPIKey(req.ctx)
93-
return await this.forwardRequest('index', req, {
89+
return await forwardRequest(req, {
9490
query: {
9591
gameId: key.game.id.toString()
9692
}

0 commit comments

Comments
 (0)