Skip to content

Commit

Permalink
feat(@artusx/plugin-koa): add default config for find-my-way
Browse files Browse the repository at this point in the history
  • Loading branch information
thonatos committed Mar 25, 2024
1 parent 19f2f20 commit ed283cd
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 2 deletions.
10 changes: 10 additions & 0 deletions packages/apps/artusx-koa/src/module-api/api.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ export default class APIController {
ctx.body = 'api';
}

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

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

@GET('/mockApi')
@ContentType('application/json; charset=utf-8')
async getInfo(ctx: ArtusxContext) {
Expand Down
21 changes: 20 additions & 1 deletion packages/libs/utils/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,32 @@ export const avatar = (email: string) => {
};

// env utils

export const getBooleanFromEnv = (key: string, defaultValue: boolean = false): boolean => {
const value = (process.env[key] || '').toLowerCase();

if (!value) {
return defaultValue;
}

if (value === 'true' || value === '1') {
return true;
}

if (value === 'false' || value === '0') {
return true;
}

return defaultValue;
};

export const getEnv = <T>(key: string, type?: string): T => {
const value = process.env[key] || '';

let target: unknown = value;

if (type === 'boolean') {
target = Boolean(value);
target = getBooleanFromEnv(key);
}

if (type === 'number') {
Expand Down
11 changes: 10 additions & 1 deletion packages/plugins/koa/src/koa/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,20 @@ import Router from 'find-my-way';
import { Injectable, ScopeEnum } from '@artus/core';
import { ArtusXInjectEnum } from '../constants';
import { KoaRouter } from '../types';
import { getBooleanFromEnv } from '../util';

@Injectable({
id: ArtusXInjectEnum.KoaRouter,
scope: ScopeEnum.SINGLETON,
})
export default class KoaRouterClient extends (Router as any as KoaRouter) {}
export default class KoaRouterClient extends (Router as any as KoaRouter) {
constructor() {
super({
caseSensitive: getBooleanFromEnv('ROUTER_CASE_SENSITIVE', true),
ignoreTrailingSlash: getBooleanFromEnv('ROUTER_IGNORE_TRAILING_SLASH', true),
ignoreDuplicateSlashes: getBooleanFromEnv('ROUTER_IGNORE_DUPLICATE_SLASHES', true),
});
}
}

export { KoaRouterClient };
17 changes: 17 additions & 0 deletions packages/plugins/koa/src/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export const getBooleanFromEnv = (key: string, defaultValue: boolean = false): boolean => {
const value = (process.env[key] || '').toLowerCase();

if (!value) {
return defaultValue;
}

if (value === 'true' || value === '1') {
return true;
}

if (value === 'false' || value === '0') {
return true;
}

return defaultValue;
};

0 comments on commit ed283cd

Please sign in to comment.