Skip to content

Built for people who want to own their automations. Join the waitlist for an invite.

Package listing

@kody/workos

src/delete-organization.ts

64 lines · 2.0 KB · TypeScript
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<DeleteOrganizationInput> & Record<string, unknown> = {},
) {
	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,
	})
}