Skip to content
← Public packages

@kody/doordash

Order food on DoorDash as a customer through the consumer MCP used by dd-cli.

src/address.ts

59 lines · 1.7 KB · TypeScript
import { callMcpTool, TOOLS, type DoorDashMcpAuthInput } from './mcp.ts'

const ACTIONS = ['list', 'find', 'add', 'set'] as const
export type AddressAction = (typeof ACTIONS)[number]

export type AddressInput = DoorDashMcpAuthInput & {
	action?: AddressAction
	addressId?: string
	query?: string
	/** Structured fields for save — pass through as MCP arguments when provided. */
	address?: Record<string, unknown>
	confirm?: boolean
	dryRun?: boolean
}

function shouldDryRun(input: AddressInput, mutating: boolean) {
	if (input.dryRun === true) return true
	if (mutating && input.confirm !== true && input.dryRun !== false) return true
	return false
}

export default async function address(input: AddressInput = {}) {
	const action = (input.action ?? 'list') as AddressAction
	if (!ACTIONS.includes(action)) {
		throw new Error('Unknown address action: ' + action)
	}
	if (action === 'list') {
		return callMcpTool({ ...input, name: TOOLS.listAddresses, arguments: {} })
	}
	if (action === 'find') {
		const query = String(input.query ?? '').trim()
		if (!query) throw new Error('query is required for find.')
		return callMcpTool({
			...input,
			name: TOOLS.findAddress,
			arguments: { query },
		})
	}
	if (action === 'set') {
		const addressId = String(input.addressId ?? '').trim()
		if (!addressId) throw new Error('addressId is required for set.')
		return callMcpTool({
			...input,
			name: TOOLS.setDeliveryAddress,
			arguments: { address_id: addressId },
			dryRun: shouldDryRun(input, true),
		})
	}
	// add / save
	if (!input.address || typeof input.address !== 'object') {
		throw new Error('address object is required for add.')
	}
	return callMcpTool({
		...input,
		name: TOOLS.saveAddress,
		arguments: input.address,
		dryRun: shouldDryRun(input, true),
	})
}