mirror of
https://gitlab.com/soapbox-pub/ditto.git
synced 2025-12-06 03:19:46 +00:00
api: set vars while constructing DittoApp
This commit is contained in:
parent
30f4d45fca
commit
951c145138
3 changed files with 23 additions and 10 deletions
|
|
@ -1,10 +1,17 @@
|
|||
import { DittoConf } from '@ditto/conf';
|
||||
import { DittoDB } from '@ditto/db';
|
||||
import { Hono } from '@hono/hono';
|
||||
import { MockRelay } from '@nostrify/nostrify/test';
|
||||
|
||||
import { DittoApp } from './DittoApp.ts';
|
||||
import { DittoRoute } from './DittoRoute.ts';
|
||||
|
||||
Deno.test('DittoApp', () => {
|
||||
const app = new DittoApp();
|
||||
Deno.test('DittoApp', async () => {
|
||||
await using db = DittoDB.create('memory://');
|
||||
const conf = new DittoConf(new Map());
|
||||
const store = new MockRelay();
|
||||
|
||||
const app = new DittoApp({ conf, db, store });
|
||||
|
||||
const hono = new Hono();
|
||||
const route = new DittoRoute();
|
||||
|
|
|
|||
|
|
@ -1,8 +1,21 @@
|
|||
import { Hono } from '@hono/hono';
|
||||
|
||||
import type { HonoOptions } from '@hono/hono/hono-base';
|
||||
import type { DittoEnv } from './DittoEnv.ts';
|
||||
|
||||
export class DittoApp extends Hono<DittoEnv> {
|
||||
// @ts-ignore Require a DittoRoute for type safety.
|
||||
declare route: (path: string, app: Hono<DittoEnv>) => Hono<DittoEnv>;
|
||||
|
||||
constructor(vars: Omit<DittoEnv['Variables'], 'signal'>, opts: HonoOptions<DittoEnv> = {}) {
|
||||
super(opts);
|
||||
|
||||
this.use((c, next) => {
|
||||
c.set('db', vars.db);
|
||||
c.set('conf', vars.conf);
|
||||
c.set('store', vars.store);
|
||||
c.set('signal', c.req.raw.signal);
|
||||
return next();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
import { type Context, type ErrorHandler, Hono } from '@hono/hono';
|
||||
import { type ErrorHandler, Hono } from '@hono/hono';
|
||||
import { HTTPException } from '@hono/hono/http-exception';
|
||||
|
||||
import type { HonoOptions } from '@hono/hono/hono-base';
|
||||
|
|
@ -13,7 +13,6 @@ export class DittoRoute extends Hono<DittoEnv> {
|
|||
super(opts);
|
||||
|
||||
this.use((c, next) => {
|
||||
this.setSignal(c);
|
||||
this.assertVars(c.var);
|
||||
return next();
|
||||
});
|
||||
|
|
@ -21,12 +20,6 @@ export class DittoRoute extends Hono<DittoEnv> {
|
|||
this.onError(this._errorHandler);
|
||||
}
|
||||
|
||||
private setSignal(c: Context<DittoEnv>): void {
|
||||
if (!c.var.signal) {
|
||||
c.set('signal', c.req.raw.signal);
|
||||
}
|
||||
}
|
||||
|
||||
private assertVars(vars: Partial<DittoEnv['Variables']>): DittoEnv['Variables'] {
|
||||
if (!vars.db) this.throwMissingVar('db');
|
||||
if (!vars.conf) this.throwMissingVar('conf');
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue