Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions UPGRADE-3.0.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Upgrade from 2.x to 3.0

# Event

- Remove `eventBusMiddleware` and use `httpKernel` instead.
- Replace all the types from `@koala-ts/framework/Event` with types from `@koala-ts/framework/Kernel`.

```diff
-- import { eventBusMiddleware } from '@koala-ts/framework/Event';
++ import { httpKernel } from '@koala-ts/framework/Kernel';
```
2 changes: 1 addition & 1 deletion src/Config/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { type EventSubscriber } from '@/Event';
import { type HttpMiddleware } from '@/Http';
import { type StaticFilesOptions } from '@/Http/Files';
import { type EventSubscriber } from '@/Kernel';

export type Controller = new (...args: unknown[]) => unknown;

Expand Down
31 changes: 0 additions & 31 deletions src/Event/EventBus.ts

This file was deleted.

14 changes: 12 additions & 2 deletions src/Event/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,12 @@
export * from './types';
export * from './EventBus';
/**
* @deprecated This module is deprecated in favor of using the Kernel module.
*/
export { httpKernel as eventBusMiddleware } from '@/Kernel/HttpKernel';
export type {
KernelStorage as EventBusStorage,
EventEmitter,
EventSubscriber,
useEmit,
useResponse,
useRequest,
} from '@/Kernel';
14 changes: 7 additions & 7 deletions src/Event/EventBus.test.ts → src/Kernel/HttpKernel.test.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import { describe, expect, test, vi } from 'vitest';
import { eventBusMiddleware, useEmit, useRequest, useResponse } from '@/Event/EventBus';
import { type EventBusStorage } from '@/Event/types';
import { type HttpScope, type ScopeStore } from '@/Http';
import { httpKernel, useEmit, useRequest, useResponse } from '@/Kernel/HttpKernel';
import { type KernelStorage } from '@/Kernel/types';

describe('Event bus', () => {
describe('Http Kernel', () => {
test('middleware should init storage', async () => {
const scope = { app: vi.fn() };
const next = vi.fn();
const storage = { run: vi.fn() };

await eventBusMiddleware(scope as unknown as HttpScope, next, storage as unknown as ScopeStore<EventBusStorage>);
await httpKernel(scope as unknown as HttpScope, next, storage as unknown as ScopeStore<KernelStorage>);

expect(storage.run).toHaveBeenCalledWith({ eventEmitter: scope.app, scope }, next);
});

test('useEmit should return emitter from storage', () => {
const storage = { get: vi.fn().mockReturnValue('eventEmitter') };

const emitter = useEmit(storage as unknown as ScopeStore<EventBusStorage>);
const emitter = useEmit(storage as unknown as ScopeStore<KernelStorage>);

expect(storage.get).toHaveBeenCalledWith('eventEmitter');
expect(emitter).toBe('eventEmitter');
Expand All @@ -27,7 +27,7 @@ describe('Event bus', () => {
const request = { headers: { host: 'localhost' } };
const storage = { get: vi.fn().mockReturnValue({ request }) };

const req = useRequest(storage as unknown as ScopeStore<EventBusStorage>);
const req = useRequest(storage as unknown as ScopeStore<KernelStorage>);

expect(storage.get).toHaveBeenCalledWith('scope');
expect(req).toBe(request);
Expand All @@ -37,7 +37,7 @@ describe('Event bus', () => {
const response = { status: 200, body: 'OK' };
const storage = { get: vi.fn().mockReturnValue({ response }) };

const res = useResponse(storage as unknown as ScopeStore<EventBusStorage>);
const res = useResponse(storage as unknown as ScopeStore<KernelStorage>);

expect(storage.get).toHaveBeenCalledWith('scope');
expect(res).toBe(response);
Expand Down
31 changes: 31 additions & 0 deletions src/Kernel/HttpKernel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import {
createStore,
type HttpRequest,
type HttpResponse,
type HttpScope,
type NextMiddleware,
type ScopeStore,
} from '@/Http';
import { type EventEmitter, type KernelStorage } from '@/Kernel/types';

const kernelStorage = createStore<KernelStorage>();

export async function httpKernel(
scope: HttpScope,
next: NextMiddleware,
storage: ScopeStore<KernelStorage> = kernelStorage,
): Promise<void> {
await storage.run({ eventEmitter: scope.app, scope }, next);
}

export function useEmit(storage: ScopeStore<KernelStorage> = kernelStorage): EventEmitter {
return storage.get('eventEmitter');
}

export function useRequest(storage: ScopeStore<KernelStorage> = kernelStorage): HttpRequest {
return storage.get('scope').request;
}

export function useResponse(storage: ScopeStore<KernelStorage> = kernelStorage): HttpResponse {
return storage.get('scope').response;
}
2 changes: 2 additions & 0 deletions src/Kernel/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './types';
export * from './HttpKernel';
2 changes: 1 addition & 1 deletion src/Event/types.ts → src/Kernel/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export interface EventEmitter {
emit(event: string, ...args: unknown[]): void;
}

export interface EventBusStorage {
export interface KernelStorage {
eventEmitter: EventEmitter;
scope: HttpScope;
}
Expand Down