import { UrlHandler } from "@akala/core"; import { type ICommandProcessor, Metadata } from "./index.browser.js"; import { Container } from "./model/container.js"; // export default function getHandler(protocol: string, url: URL) // { // const processors = getHandlers(protocol.substring(0, protocol.length - 1)); // return processors.reduce((previous, current) => // { // return previous.then(p => current(url, p)) // }, Promise.resolve>>(null)) // } // export function getHandlers(protocol: string) // { // const protocols = protocol.split('+'); // const result = protocols.map(v => handlers[v]); // let x: number; // if ((x = result.findIndex(x => x === undefined)) !== -1) // throw new ErrorWithStatus(404, `No handler could be found for the protocol ${protocols[x]}`) // return result; // } export type HandlerResult = { processor: T, getMetadata(): Promise }; export type handler = (arg1: URL, arg2: HandlerResult) => Promise export const protocolHandlers = new UrlHandler<[URL, { signal: AbortSignal, container?: Container }, Partial>], HandlerResult>(); export type ServerHandler = (container: Container, options: T) => Promise export const serverHandlers = new UrlHandler<[URL, Container, { signal: AbortSignal }, void], void>(false) as UrlHandler<[URL, Container, { signal: AbortSignal }, void], void> & { useProtocol(protocol: string, handler: (url: URL, container: Container, options: U & { signal: AbortSignal }) => Promise) }; export function parseQueryString(url: URL) { return Object.fromEntries(Array.from(url.searchParams).map<[string, boolean | number | string]>(e => { if (e[1].toLocaleLowerCase() in ['true', 'false']) return [e[0], e[1].toLocaleLowerCase() == 'true']; if (e[1].match(/^[\.\d]+$/)) return [e[0], Number(e[1])]; return [e[0], e[1]]; })) }