import {hyphenate} from 'hyphenate'; import _ from 'lodash'; import type {Intersection} from 'tslang'; import {ProcedureDefinition} from '../rpc.js'; import {isModuleOrNamespace} from '../utils.js'; declare global { namespace Backit { // eslint-disable-next-line @typescript-eslint/consistent-type-definitions interface APINamespaces {} type APINamespace = Intersection; } } export const API = {} as Backit.APINamespace; export function extendAPI( namespace: Backit.APINamespaces[keyof Backit.APINamespaces], ): void { recursivelyAnnotateRoutes(namespace, ''); _.merge(API, namespace); } export function formatAPIPathSegment(segment: string): string { return hyphenate(segment, {lowerCase: true}); } function recursivelyAnnotateRoutes(namespace: object, upperPath: string): void { for (const [key, value] of Object.entries(namespace)) { const path = `${upperPath}/${formatAPIPathSegment(key)}`; if (value instanceof ProcedureDefinition) { value.path = path; } else if (isModuleOrNamespace(value)) { recursivelyAnnotateRoutes(value, path); } } }