const required = ['OUTRESERVE_SUPPLY_BASE_URL', 'OUTRESERVE_SUPPLY_API_KEY', 'OUTRESERVE_EXTERNAL_PROPERTY_ID', 'OUTRESERVE_COUNTRY_CODE', 'OUTRESERVE_STATE_CODE', 'OUTRESERVE_DESTINATION_SLUG', 'OUTRESERVE_PROPERTY_TIMEZONE']; for (const name of required) if (!process.env[name]) throw new Error(`Set ${name}`); if (process.env.OUTRESERVE_CONFIRM_CREATE !== 'yes') throw new Error('Set OUTRESERVE_CONFIRM_CREATE=yes to create a pending property and unit'); const base = process.env.OUTRESERVE_SUPPLY_BASE_URL.replace(/\/$/, ''); const { randomUUID } = await import('node:crypto'); const keys = { property: process.env.OUTRESERVE_PROPERTY_IDEMPOTENCY_KEY || randomUUID(), unit: process.env.OUTRESERVE_UNIT_IDEMPOTENCY_KEY || randomUUID(), submit: process.env.OUTRESERVE_SUBMIT_IDEMPOTENCY_KEY || randomUUID(), }; console.error('Persist these keys before any retry:', keys); async function request(path, method = 'GET', body, idempotencyKey) { const response = await fetch(`${base}${path}`, { method, headers: { Authorization: `Bearer ${process.env.OUTRESERVE_SUPPLY_API_KEY}`, ...(body ? { 'Content-Type': 'application/json' } : {}), ...(idempotencyKey ? { 'Idempotency-Key': idempotencyKey } : {}) }, ...(body ? { body: JSON.stringify(body) } : {}), }); const raw = await response.text(); const payload = response.headers.get('content-type')?.includes('json') && raw ? JSON.parse(raw) : raw; if (!response.ok) throw new Error(`Supply API ${response.status} ${payload?.code || ''}: ${JSON.stringify(payload)}`); return payload; } const connection = await request('/connection'); console.log('Connection:', JSON.stringify(connection.data.capabilities, null, 2)); const locationQuery = new URLSearchParams({ level: 'destinations', country_code: process.env.OUTRESERVE_COUNTRY_CODE, state_code: process.env.OUTRESERVE_STATE_CODE, destination_slug: process.env.OUTRESERVE_DESTINATION_SLUG, }); const location = await request(`/locations?${locationQuery}`); if (location.data.length !== 1) throw new Error('Expected exactly one matching destination; inspect GET /locations'); console.log('Selected location:', location.data[0].id); const property = await request('/properties', 'POST', { property: { external_property_id: process.env.OUTRESERVE_EXTERNAL_PROPERTY_ID, name: 'Example Cabin Property', currency: 'USD', country_code: process.env.OUTRESERVE_COUNTRY_CODE, state_code: process.env.OUTRESERVE_STATE_CODE, destination_slug: process.env.OUTRESERVE_DESTINATION_SLUG, timezone: process.env.OUTRESERVE_PROPERTY_TIMEZONE, }, }, keys.property); const id = property.data.id; console.log('Pending property:', id); const unit = await request(`/properties/${encodeURIComponent(id)}/units`, 'POST', { unit: { external_unit_id: process.env.OUTRESERVE_EXTERNAL_UNIT_ID || 'sample-cabin-1', name: 'Cabin 1', unit_type: 'Cabin', inventory_mode: 'physical', capacity: 1 }, }, keys.unit); console.log('Unit:', unit.data.id); const submitted = await request(`/properties/${encodeURIComponent(id)}/submit`, 'POST', undefined, keys.submit); console.log('Submitted for review:', JSON.stringify({ id: submitted.data.id, approval_status: submitted.data.approval_status, booking_ready: submitted.data.booking_ready }));