Skip to content

Commit 331d666

Browse files
authored
Merge pull request #25 from AsenaJs/patch/config-name-undefined
Patch: Config name not found bug fixed
2 parents d807b29 + f3c1e22 commit 331d666

File tree

9 files changed

+79
-115
lines changed

9 files changed

+79
-115
lines changed

.changeset/funny-buses-appear.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@asenajs/asena': patch
3+
---
4+
5+
Config service name undefined bug fixed

README.md

Lines changed: 28 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
# Asena
22

3-
Asena is a NestJS-like IoC web framework built on top of Hono and Bun. It combines the power of dependency injection
3+
Asena is a NestJS-like IoC web framework built on top of Bun. It combines the power of dependency injection
44
with the performance of Bun runtime and the flexibility of Adapter design system.
55

66
## Documentation
77

8-
For detailed documentation, please visit [not ready yet](https://asena.sh). Documentation is still in progress, but updates are being made regularly.
8+
For detailed documentation, please visit [not ready yet](https://asena.sh). Documentation is still in progress, but updates are being made regularly. You can check this project [AsenaExample](https://github.com/LibirSoft/AsenaExample). I am always updating it to latest usages.
99

1010
## Key Features
1111

@@ -19,12 +19,6 @@ For detailed documentation, please visit [not ready yet](https://asena.sh). Docu
1919
- **TypeScript Support**: Full TypeScript support with strict mode
2020
- **Modular Architecture**: Easy to extend and customize
2121

22-
## Installation
23-
24-
```bash
25-
bun add @asenajs/asena
26-
```
27-
2822
## Quick Start
2923

3024
The easiest way to create a new Asena project is using the CLI:
@@ -36,6 +30,7 @@ asena create
3630
```
3731

3832
This will create a new project with the following structure:
33+
3934
```
4035
├── src/
4136
│ ├── controllers/
@@ -47,6 +42,8 @@ This will create a new project with the following structure:
4742
├── .eslintignore
4843
└── .prettierrc.js
4944
```
45+
## Alternative start
46+
5047

5148
Alternatively, you can create a project manually:
5249

@@ -89,24 +86,26 @@ For decorators working properly, you need to add some settings to your tsconfig.
8986
}
9087
}
9188
```
92-
93-
Then, install the required packages:
94-
89+
After that you need to make sure to edit your tsConfig.json it should look like this
90+
Then, install these packages:
91+
* `@asenajs/asena`: Base package
92+
* `@asenajs/hono-adapter`: For web-server. Asena currently only support hono adapter.
93+
* `@asenajs/asena-logger`: For better logs
9594
```bash
96-
bun add @asenajs/asena @asenajs/hono-adapter
95+
bun install @asenajs/asena @asenajs/hono-adapter @asenajs/asena-logger
9796
```
9897

99-
Add @asenajs/asena-cli to your package. This package provides a CLI for creating and managing Asena projects.
98+
* `@asenajs/asena-cli`: This package provides a CLI for creating and managing Asena projects.
10099

101100
```bash
102-
bun add -D @asenajs/asena-cli
101+
bun install -D @asenajs/asena-cli
103102
```
104-
105-
Then, create a new asena-config.ts file using the CLI:
103+
After installation, you need to create asena-config.ts file you can create it with asena-cli
106104

107105
```bash
108106
asena init
109107
```
108+
this will create you asena-config file.
110109

111110
`Note`: Built options directly copy of bun options, you can check bun documentation for more
112111
options. [Bun Documentation](https://bun.sh/docs/bundler#reference)
@@ -115,15 +114,14 @@ Create index.ts file under your src folder:
115114

116115
```typescript
117116
// src/index.ts
118-
import { AsenaServer } from '@asenajs/asena';
119-
import { DefaultLogger } from "@asenajs/asena/logger";
120-
import { createHonoAdapter } from '@asenajs/hono-adapter';
121-
122-
const [adapter, logger] = createHonoAdapter(new DefaultLogger());
123-
await new AsenaServer(adapter)
124-
.logger(logger)
125-
.port(3000)
126-
.start(true);
117+
import {AsenaServer} from '@asenajs/asena';
118+
import {createHonoAdapter} from '@asenajs/hono-adapter';
119+
import {logger} from './logger/logger';
120+
121+
const [honoAdapter,asenaLogger] = createHonoAdapter(logger);
122+
123+
124+
await new AsenaServer(honoAdapter, asenaLogger).port(3000).start();
127125
```
128126

129127
To run asena you need at least one controller. Create a new controller:
@@ -136,10 +134,10 @@ import { Get } from '@asenajs/asena/web';
136134

137135
@Controller('/hello')
138136
export class TestController {
139-
@Get('/world')
140-
public async getHello(context: Context) {
141-
return context.send('Hello World');
142-
}
137+
@Get('/world')
138+
public async getHello(context: Context) {
139+
return context.send('Hello World');
140+
}
143141
}
144142
```
145143

@@ -160,8 +158,7 @@ bun index.asena.js
160158
## CLI Commands
161159

162160
For more information about CLI commands and usage, please visit:
163-
[Asena CLI Documentation](https://github.com/AsenaJs/Asena-cli/blob/master/README.md)
164-
161+
[Asena CLI Documentation](https://asena.sh/docs/cli/overview.html)
165162

166163
## Project Structure
167164

@@ -172,7 +169,6 @@ lib/
172169
├── ioc/ # Dependency injection container
173170
├── utils/ # Utility functions
174171
├── test/ # Test utilities
175-
└── logger/ # Logging system
176172
```
177173

178174
## Core Concepts

lib/server/AsenaServer.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export class AsenaServer<A extends AsenaAdapter<any, any>> {
6666
const config = await readConfigFile();
6767

6868
if (!config) {
69-
this._logger.warn('Config file not found');
69+
this._logger.warn('asena-config file not found');
7070
}
7171

7272
this._ioc = new IocEngine(config);
@@ -264,7 +264,7 @@ export class AsenaServer<A extends AsenaAdapter<any, any>> {
264264
}
265265
}
266266

267-
const name = getTypedMetadata<string>(ComponentConstants.NameKey, configInstance);
267+
const name = getOwnTypedMetadata<string>(ComponentConstants.NameKey, configInstance.constructor);
268268

269269
this._logger.info(`Config ${yellow(name)} applied`);
270270
}

lib/server/src/services/PrepareConfigService.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export class PrepareConfigService extends PrepareService {
3737

3838
const name = getTypedMetadata<string>(ComponentConstants.NameKey, config[0].constructor);
3939

40-
this.logger.info(`Config found ${yellow(name)}`);
40+
this.logger.info(`AsenaConfig service found ${yellow(name)}`);
4141

4242
return configInstance;
4343
}

lib/server/src/services/PrepareMiddlewareService.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,20 @@ export class PrepareMiddlewareService extends PrepareService {
2222

2323
for (const middleware of middlewares) {
2424
const name = getTypedMetadata<string>(ComponentConstants.NameKey, middleware);
25-
const override = getTypedMetadata<string[]>(ComponentConstants.OverrideKey, middleware);
26-
const isOverride = override ? override.includes('handle') : false;
2725

2826
const instances = await this.container.resolve<AsenaMiddlewareService>(name);
2927

3028
if (!instances) continue;
3129

3230
const normalizedInstances = Array.isArray(instances) ? instances : [instances];
31+
let override: string[];
32+
let isOverride: boolean;
3333

3434
for (const instance of normalizedInstances) {
35+
override = getTypedMetadata<string[]>(ComponentConstants.OverrideKey, instance);
36+
37+
isOverride = override ? override.includes('handle') : false;
38+
3539
preparedMiddlewares.push({
3640
handle: instance.handle.bind(instance),
3741
override: isOverride,

lib/server/src/services/PrepareStaticServeConfigService.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { PrepareService } from '../PrepareService';
2-
import { getTypedMetadata } from '../../../utils/typedMetadata';
2+
import { getOwnTypedMetadata, getTypedMetadata } from '../../../utils/typedMetadata';
33
import { ComponentConstants } from '../../../ioc/constants';
44
import type { AsenaStaticServeService, StaticServeClass } from '../../web/middleware';
55
import type { BaseStaticServeParams } from '../../../adapter';
@@ -17,21 +17,21 @@ export class PrepareStaticServeConfigService extends PrepareService {
1717
return;
1818
}
1919

20-
const name = getTypedMetadata<string>(ComponentConstants.NameKey, staticServeClass);
21-
const root = getTypedMetadata<string>(ComponentConstants.StaticServeRootKey, staticServeClass);
20+
const name = getOwnTypedMetadata<string>(ComponentConstants.NameKey, staticServeClass);
21+
const root = getOwnTypedMetadata<string>(ComponentConstants.StaticServeRootKey, staticServeClass);
2222

23-
const staticServeService: AsenaStaticServeService<any>[] | AsenaStaticServeService<any> =
23+
const staticServeServiceInstance: AsenaStaticServeService<any>[] | AsenaStaticServeService<any> =
2424
await this.container.resolve<AsenaStaticServeService<any>>(name);
2525

26-
if (!staticServeService) {
26+
if (!staticServeServiceInstance) {
2727
throw new Error(`Static Serve service ${name} not found.`);
2828
}
2929

30-
if (Array.isArray(staticServeService)) {
30+
if (Array.isArray(staticServeServiceInstance)) {
3131
throw new Error('Static serve service cannot be array');
3232
}
3333

34-
const overrides: string[] = getTypedMetadata<string[]>(ComponentConstants.OverrideKey, staticServeService);
34+
const overrides: string[] = getTypedMetadata<string[]>(ComponentConstants.OverrideKey, staticServeServiceInstance);
3535

3636
const baseStaticServeParams: BaseStaticServeParams = {
3737
extra: undefined,
@@ -41,24 +41,24 @@ export class PrepareStaticServeConfigService extends PrepareService {
4141
onNotFound: undefined,
4242
};
4343

44-
if (staticServeService.extra) {
45-
baseStaticServeParams.extra = staticServeService.extra;
44+
if (staticServeServiceInstance.extra) {
45+
baseStaticServeParams.extra = staticServeServiceInstance.extra;
4646
}
4747

48-
if (staticServeService.rewriteRequestPath) {
49-
baseStaticServeParams.rewriteRequestPath = staticServeService.rewriteRequestPath;
48+
if (staticServeServiceInstance.rewriteRequestPath) {
49+
baseStaticServeParams.rewriteRequestPath = staticServeServiceInstance.rewriteRequestPath;
5050
}
5151

52-
if (staticServeService.onFound) {
52+
if (staticServeServiceInstance.onFound) {
5353
baseStaticServeParams.onFound = {
54-
handler: staticServeService.onFound.bind(staticServeService),
54+
handler: staticServeServiceInstance.onFound.bind(staticServeServiceInstance),
5555
override: overrides?.includes('onFound'),
5656
};
5757
}
5858

59-
if (staticServeService.onNotFound) {
59+
if (staticServeServiceInstance.onNotFound) {
6060
baseStaticServeParams.onNotFound = {
61-
handler: staticServeService.onNotFound.bind(staticServeService),
61+
handler: staticServeServiceInstance.onNotFound.bind(staticServeServiceInstance),
6262
override: overrides?.includes('onNotFound'),
6363
};
6464
}

lib/test/server/src/PrepareConfigService.test.ts

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { beforeEach, describe, expect, mock, test } from 'bun:test';
22
import { PrepareConfigService } from '../../../server/src/services/PrepareConfigService';
33
import { ComponentType } from '../../../ioc/types';
4-
import { ComponentConstants } from '../../../ioc/constants';
54
import type { AsenaConfig } from '../../../server/config';
65
import { Config } from '../../../server/decorators';
76
import { yellow } from '../../../logger';
@@ -55,21 +54,11 @@ describe('PrepareConfigService', () => {
5554

5655
mockContainer.resolveAll.mockImplementation(() => [mockConfig]);
5756

58-
const getTypedMetadataMock = mock().mockReturnValue('TestConfig');
59-
60-
mock.module('../../../utils/typedMetadata', () => {
61-
return {
62-
getTypedMetadata: getTypedMetadataMock,
63-
};
64-
});
65-
6657
const result = await service.prepare();
6758

6859
expect(mockContainer.resolveAll).toHaveBeenCalledWith(ComponentType.CONFIG);
69-
expect(getTypedMetadataMock).toHaveBeenCalledWith(ComponentConstants.NameKey, mockConfig.constructor);
70-
expect(mockLogger.info).toHaveBeenCalledWith(`Config found ${yellow('TestConfig')}`);
60+
expect(mockLogger.info).toHaveBeenCalledWith(`AsenaConfig service found ${yellow('TestConfig')}`);
7161
expect(result).toBe(mockConfig);
72-
getTypedMetadataMock.mockClear();
7362
});
7463

7564
test('should return when config not found in instance object', async () => {

0 commit comments

Comments
 (0)