// JSON:API request builders for App Store Connect. Pure: commands build `Req`s, print them on --dry and send them // with the client otherwise. Request / response format: https://developer.apple.com/documentation/appstoreconnectapi import { log } from '../lib/log'; export interface Req { method: 'GET' | 'POST' | 'PATCH' | 'DELETE'; path: string; body?: unknown; note?: string } export const one = (type: string, id: string) => ({ data: { type, id } }); export const many = (type: string, ids: string[]) => ({ data: ids.map(id => ({ type, id })) }); /** One-line description of a request for dry runs and logs. Attribute values are cut short; `hide` keys (emails, * passwords) are masked. */ export function describeReq(r: Req, hide: string[] = ['email', 'demoAccountPassword', 'contactEmail', 'contactPhone']): string { const attrs = (r.body as { data?: { attributes?: Record } } | undefined)?.data?.attributes; const show = (k: string, v: unknown) => (hide.includes(k) ? '"***"' : typeof v === 'string' && v.length > 40 ? JSON.stringify(v.slice(0, 37) + '...') : JSON.stringify(v)); const keys = attrs ? Object.entries(attrs).map(([k, v]) => `${k}=${show(k, v)}`) : []; return `${r.method} ${r.path}${keys.length ? ` {${keys.join(', ')}}` : ''}${r.note ? ` # ${r.note}` : ''}`; } /** Logs every planned request; sends them in order unless dry. Returns the responses (empty on dry). */ // eslint-disable-next-line @typescript-eslint/no-explicit-any export async function execute(asc: (m: string, p: string, b?: unknown) => Promise, reqs: Req[], o: { dry: boolean; tag: string }): Promise { const out: unknown[] = []; for (const r of reqs) { if (o.dry) { log(o.tag, `would ${describeReq(r)}`); continue; } out.push(await asc(r.method, r.path, r.body)); log(o.tag, `${describeReq(r)}: done`); } return out; }