* Fetch template rich parameters from Coder API. * Uses an optional API session to avoid generating multiple tokens.
(
deploymentUrl: string,
versionId: string,
workspaceName: string,
session?: CoderApiSession,
signal?: AbortSignal
)
| 744 | * Uses an optional API session to avoid generating multiple tokens. |
| 745 | */ |
| 746 | private async getTemplateRichParameters( |
| 747 | deploymentUrl: string, |
| 748 | versionId: string, |
| 749 | workspaceName: string, |
| 750 | session?: CoderApiSession, |
| 751 | signal?: AbortSignal |
| 752 | ): Promise< |
| 753 | Array<{ |
| 754 | name: string; |
| 755 | defaultValue: string; |
| 756 | type: string; |
| 757 | ephemeral: boolean; |
| 758 | required: boolean; |
| 759 | }> |
| 760 | > { |
| 761 | const run = async (api: CoderApiSession) => { |
| 762 | const url = new URL( |
| 763 | `/api/v2/templateversions/${versionId}/rich-parameters`, |
| 764 | deploymentUrl |
| 765 | ).toString(); |
| 766 | |
| 767 | const controller = new AbortController(); |
| 768 | const timeout = setTimeout(() => controller.abort(), 30_000); |
| 769 | const onAbort = () => controller.abort(); |
| 770 | signal?.addEventListener("abort", onAbort, { once: true }); |
| 771 | if (signal?.aborted) { |
| 772 | controller.abort(); |
| 773 | } |
| 774 | |
| 775 | const response = await fetch(url, { |
| 776 | headers: { |
| 777 | "Coder-Session-Token": api.token, |
| 778 | }, |
| 779 | signal: controller.signal, |
| 780 | }).finally(() => { |
| 781 | clearTimeout(timeout); |
| 782 | signal?.removeEventListener("abort", onAbort); |
| 783 | }); |
| 784 | |
| 785 | if (!response.ok) { |
| 786 | throw new Error( |
| 787 | `Failed to fetch rich parameters: ${response.status} ${response.statusText}` |
| 788 | ); |
| 789 | } |
| 790 | |
| 791 | const data: unknown = await response.json(); |
| 792 | return this.parseRichParameters(data); |
| 793 | }; |
| 794 | |
| 795 | const tokenName = `mux-${workspaceName}`; |
| 796 | return session ? run(session) : this.withApiSession(tokenName, run); |
| 797 | } |
| 798 | |
| 799 | /** |
| 800 | * Encode a parameter string for the Coder CLI's --parameter flag. |
no test coverage detected