diff --git a/src/middleware/userStore.ts b/src/middleware/userStore.ts new file mode 100644 index 00000000..bd3a4029 --- /dev/null +++ b/src/middleware/userStore.ts @@ -0,0 +1,22 @@ +import { AppMiddleware } from '@/app.ts'; +import { UserStore } from '@/storages/UserStore.ts'; +import { eventsDB } from '@/storages.ts'; +import { HTTPException } from 'hono'; + +/** User Store middleware. + * Throw a 500 if can't set the `userStore` */ +const setUserStore: AppMiddleware = async (c, next) => { + const pubkey = c.get('pubkey') as string; + + try { + const store = new UserStore(pubkey, eventsDB); + c.set('userStore', store); + } catch (e) { + console.log(e); + throw new HTTPException(500); + } + + await next(); +}; + +export { setUserStore };