All files / actual/request request-channel-listener-injection-token.ts

100% Statements 56/56
100% Branches 2/2
100% Functions 2/2
100% Lines 56/56

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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 571x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x  
import type { DiContainerForInjection } from "@ogre-tools/injectable";
import { getInjectable, getInjectionToken } from "@ogre-tools/injectable";
 
export interface RequestChannel<Request, Response> {
  id: string;
  _requestSignature?: Request;
  _responseSignature?: Response;
}
 
export type RequestChannelHandler<Channel> = Channel extends RequestChannel<
  infer Request,
  infer Response
>
  ? (req: Request) => Promise<Response> | Response
  : never;
 
export interface RequestChannelListener<Channel> {
  id: string;
  channel: Channel;
  handler: RequestChannelHandler<Channel>;
}
 
export const requestChannelListenerInjectionToken = getInjectionToken<
  RequestChannelListener<RequestChannel<unknown, unknown>>
>({
  id: "request-channel-listener",
});
 
export interface GetRequestChannelListenerInjectableInfo<
  Channel extends RequestChannel<Request, Response>,
  Request,
  Response,
> {
  id: string;
  channel: Channel;
  getHandler: (di: DiContainerForInjection) => RequestChannelHandler<Channel>;
}
 
export const getRequestChannelListenerInjectable = <
  Channel extends RequestChannel<Request, Response>,
  Request,
  Response,
>(
  info: GetRequestChannelListenerInjectableInfo<Channel, Request, Response>,
) =>
  getInjectable({
    id: `${info.channel.id}-request-listener-${info.id}`,
 
    instantiate: (di) => ({
      id: `${info.channel.id}-request-listener-${info.id}`,
      channel: info.channel,
      handler: info.getHandler(di),
    }),
 
    injectionToken: requestChannelListenerInjectionToken,
  });