import { workosRequest } from './client.ts' import type { DryRunResult, WorkosAuthInput } from './types.ts' import { optionalString, requireRecord, requireString } from './types.ts' export type DeleteOrganizationInput = WorkosAuthInput & { organization: string confirm?: boolean dryRun?: boolean } /** * Permanently delete a WorkOS organization. Requires `confirm: true`, or use * `dryRun: true`. * @example * import deleteOrganization from 'kody:@kody/workos/delete-organization' * const preview = await deleteOrganization({ * organization: 'org_01EXAMPLE', * dryRun: true, * }) */ export async function deleteOrganization( input: DeleteOrganizationInput, ): Promise<{ deleted: true; organization: string } | DryRunResult<{ method: string; path: string }>> { const organization = requireString(input.organization, 'organization') const path = `/organizations/${encodeURIComponent(organization)}` if (input.dryRun) { return { dryRun: true, wouldCall: { method: 'DELETE', path } } } if (input.confirm !== true) { throw new Error( 'Deleting a WorkOS organization requires confirm: true after explicit user approval, or dryRun: true.', ) } await workosRequest({ ...input, operation: 'organizations.write', method: 'DELETE', path, }) return { deleted: true, organization } } /** * Permanently delete a WorkOS organization. Requires `confirm: true`, or use * `dryRun: true`. * @example * import deleteOrganization from 'kody:@kody/workos/delete-organization' * const preview = await deleteOrganization({ * organization: 'org_01EXAMPLE', * dryRun: true, * }) */ export default async function deleteOrganizationEntrypoint( params: Partial & Record = {}, ) { const input = requireRecord(params, 'delete-organization') return deleteOrganization({ organization: requireString(input.organization, 'organization'), account: optionalString(input.account, 'account'), secretName: optionalString(input.secretName, 'secretName'), confirm: input.confirm === true, dryRun: input.dryRun === true, }) }