All files / api/src/middlewares user-data.ts

100% Statements 9/9
100% Branches 2/2
100% Functions 1/1
100% Lines 9/9

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 291x                               1x 5x 5x   5x 7x     7x   7x 7x  
import { ensureStorageReady } from '@api/connectors';
import type { UserDataStorageConnector } from '@api/types/connector';
import type { AppContext, AppEnv } from '@api/types/hono';
import type { MiddlewareHandler } from 'hono';
import type { Factory } from 'hono/factory';
 
/**
 * Middleware that sets up a connection with the UserDataStorageConnector
 * This middleware makes the UserDataStorageConnector available in the context
 * for use in routes that need to access user data (feedback, etc.)
 *
 * Takes a resolver rather than a connector because the backend is chosen from
 * the environment, and on Workers there is no environment until a request
 * arrives. This is also the first middleware to touch storage, so it is where
 * libSQL gets migrated.
 */
export const userDataMiddleware = (
  factory: Factory<AppEnv>,
  resolve: (c: AppContext) => UserDataStorageConnector,
): MiddlewareHandler =>
  factory.createMiddleware(async (c, next) => {
    await ensureStorageReady(c);
 
    // Set the connector in the context for use in routes
    c.set('user_data_storage_connector', resolve(c));
 
    await next();
  });