(function() { var API_BASE = '/api/admin'; var AUTH_BASE = '/api/auth'; var operationEtags = Object.create(null); var agentEtags = Object.create(null); function headers(extra) { return Object.assign({ 'Accept': 'application/json', }, extra || {}); } function csrfExempt(path) { return /\/api\/auth\/(?:login|bootstrap\/complete|session\/csrf)$/.test(path); } function attachCsrf(path, method, requestOptions) { if (method === 'GET' || method === 'HEAD' || method === 'OPTIONS' || csrfExempt(path)) { return; } if (!(path.indexOf('/api/auth/') === 0 || path.indexOf('/api/admin/') === 0)) { return; } var token = window.CrankAuth && typeof window.CrankAuth.getCsrfToken === 'function' ? window.CrankAuth.getCsrfToken() : ''; if (token) { requestOptions.headers = headers(requestOptions.headers); requestOptions.headers['x-csrf-token'] = token; } } function attachCorrelation(error, response) { var requestId = response.headers.get('x-request-id'); var traceId = response.headers.get('x-trace-id'); if (requestId && requestId.length <= 128 && /^[!-~]+$/.test(requestId) && requestId.indexOf(',') === -1 && requestId.indexOf(';') === -1) { error.requestId = requestId; } if (traceId && /^[0-9a-f]{32}$/.test(traceId) && traceId !== '00000000000000000000000000000000') { error.traceId = traceId; } return error; } function operationMutationResource(path, method) { if (!method || method === 'GET') { return null; } var match = path.match(/^(\/api\/admin\/workspaces\/[^/]+\/operations\/[^/?]+)(?:\/(publish|archive|versions))?(?:\?.*)?$/); if (!match) { return null; } if (method === 'PATCH' || method === 'DELETE' || (method === 'POST' && match[2])) { return match[1]; } return null; } function operationDetailResource(path, method) { if (method && method !== 'GET') { return null; } var match = path.match(/^(\/api\/admin\/workspaces\/[^/]+\/operations\/[^/?]+)$/); return match ? match[1] : null; } function agentMutationResource(path, method) { if (!method || method === 'GET') { return null; } var match = path.match(/^(\/api\/admin\/workspaces\/[^/]+\/agents\/[^/?]+)(?:\/(bindings|publish|unpublish|archive))?(?:\?.*)?$/); if (!match) { return null; } if (method === 'PATCH' || method === 'DELETE' || (method === 'POST' && match[2])) { return match[1]; } return null; } function agentDetailResource(path, method) { if (method && method !== 'GET') { return null; } var match = path.match(/^(\/api\/admin\/workspaces\/[^/]+\/agents\/[^/?]+)$/); return match ? match[1] : null; } async function request(path, options) { var requestOptions = Object.assign({ credentials: 'same-origin', headers: headers(), }, options || {}); var method = (requestOptions.method || 'GET').toUpperCase(); attachCsrf(path, method, requestOptions); var mutationResource = operationMutationResource(path, method); var agentMutation = agentMutationResource(path, method); if (mutationResource) { if (!operationEtags[mutationResource]) { await request(mutationResource); } requestOptions.headers = headers(requestOptions.headers); requestOptions.headers['If-Match'] = operationEtags[mutationResource]; } if (agentMutation) { if (!agentEtags[agentMutation]) { await request(agentMutation); } requestOptions.headers = headers(requestOptions.headers); requestOptions.headers['If-Match'] = agentEtags[agentMutation]; } var response = await fetch(path, requestOptions); var detailResource = operationDetailResource(path, method); var agentDetail = agentDetailResource(path, method); var responseEtag = response.headers.get('etag'); if (detailResource && responseEtag) { operationEtags[detailResource] = responseEtag; } if (agentDetail && responseEtag) { agentEtags[agentDetail] = responseEtag; } if (response.status === 204) { if (mutationResource) { delete operationEtags[mutationResource]; } return null; } var payload = null; var text = await response.text(); if (text) { try { payload = JSON.parse(text); } catch (_error) {} } if (!response.ok) { if (mutationResource && (response.status === 409 || response.status === 428)) { delete operationEtags[mutationResource]; } if (agentMutation && (response.status === 409 || response.status === 428)) { delete agentEtags[agentMutation]; } if (response.status === 401 && window.CrankAuth && typeof window.CrankAuth.handleUnauthorized === 'function') { window.CrankAuth.handleUnauthorized(); } var looksLikeHtml = /^\s*