Skip to content

Commit

Permalink
feat: add decorator for koa
Browse files Browse the repository at this point in the history
  • Loading branch information
thonatos committed Jan 19, 2024
1 parent 377ac35 commit 80e4e2d
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 2 deletions.
23 changes: 23 additions & 0 deletions packages/apps/artusx-koa/src/module-info/api.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { ArtusInjectEnum, Inject } from '@artus/core';
import { GET, HTTPController } from '../types';
import type { ArtusxContext } from '../types';
import APIService from './api.service';

@HTTPController('/api')
export default class APIController {
@Inject(ArtusInjectEnum.Config)
config: Record<string, any>;

@Inject(APIService)
apiService: APIService;

@GET('/')
async home(ctx: ArtusxContext) {
ctx.body = 'home';
}

@GET('/info')
async getInfo(ctx: ArtusxContext) {
ctx.body = await this.apiService.mockApi();
}
}
15 changes: 15 additions & 0 deletions packages/apps/artusx-koa/src/module-info/api.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Inject, Injectable, ArtusInjectEnum, ArtusApplication } from '@artus/core';

@Injectable()
export default class APIService {
@Inject(ArtusInjectEnum.Application)
app: ArtusApplication;

async mockApi() {
return {
data: {
name: 'artusx'
}
};
}
}
20 changes: 20 additions & 0 deletions packages/plugins/koa/src/decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,26 @@ export const GET = (path: string) => {
return buildMethodFactory(HTTPMethod.GET, path);
};

export const PUT = (path: string) => {
return buildMethodFactory(HTTPMethod.PUT, path);
};

export const POST = (path: string) => {
return buildMethodFactory(HTTPMethod.POST, path);
};

export const HEAD = (path: string) => {
return buildMethodFactory(HTTPMethod.HEAD, path);
};

export const PATCH = (path: string) => {
return buildMethodFactory(HTTPMethod.PATCH, path);
};

export const DELETE = (path: string) => {
return buildMethodFactory(HTTPMethod.DELETE, path);
};

export const OPTIONS = (path: string) => {
return buildMethodFactory(HTTPMethod.OPTIONS, path);
};
2 changes: 1 addition & 1 deletion packages/plugins/koa/src/lifecycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export default class ApplicationHttpLifecycle implements ApplicationLifecycle {
handler: ArtusxHandler
) {
for (const routeMetadata of routeMetadataList) {
const routePath = path.normalize(controllerMetadata.prefix ?? '/' + routeMetadata.path);
const routePath = path.normalize((controllerMetadata.prefix ?? '/') + routeMetadata.path);

this.router.register(routePath, [routeMetadata.method], async (ctx, next) => {
// run pipeline
Expand Down
7 changes: 6 additions & 1 deletion packages/plugins/koa/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@ import KoaRouter from './koa/router';
import KoaApplication from './koa/application';

export enum HTTPMethod {
PUT = 'PUT',
GET = 'GET',
POST = 'POST'
POST = 'POST',
HEAD = 'HEAD',
PATCH = 'PATCH',
DELETE = 'DELETE',
OPTIONS = 'OPTIONS'
}

export interface ControllerMetadata {
Expand Down

0 comments on commit 80e4e2d

Please sign in to comment.