import type { Resource, AnyResource } from "./resource.ts"; import * as Output from "./output.ts"; // special runtime-only symbol for probing the Ref proxy for its metadata const RefMetadata = Symbol.for("alchemy/RefMetadata"); export const isRef = (s: any): s is Ref => s && s[RefMetadata] !== undefined; export const getRefMetadata = >( ref: Ref, ): RefMetadata => (ref as any)[RefMetadata]; export interface Ref< R extends Resource = AnyResource, > { /** @internal phantom */ Ref: R; } export interface RefMetadata> { stack?: string; stage?: string; resourceId: R["id"]; } export const ref = >({ stack, resourceId, stage, }: RefMetadata): Ref => { const ref = new Proxy( {}, { get: (_, prop) => { if (prop === RefMetadata) { return { stack, stage, resourceId, }; } return (Output.of(ref) as any)[prop]; }, }, ) as Ref; return ref; };