1import { ACCOUNTS_URL } from 'constants' 2import { Customer } from 'view_models/customer' 3export const API = { 4 Account: { 5 async find(query) { 6 const response = await fetch(`${ACCOUNTS_URL}?search[email]=${query}`, { 7 headers: { accept: 'application/json' }, 8 }) 9 const result = await response.json() 10 if (result.length) { 11 const account = result[0] 12 return Customer.parse({ ...account.customer, email: account.email }) 13 } else { 14 return null 15 } 16 }, 17 }, 18 19 Payment: { 20 async confirm(url) { 21 return API.poll(url, ({ state }) => ['complete', 'active'].includes(state)) 22 }, 23 }, 24 25 Agent: { 26 async find(id) { 27 const response = await fetch(`/admin/agents/${id}`) 28 const result = await response.json() 29 if (result.length) { 30 const account = result[0] 31 return Customer.parse({ ...account.customer, email: account.email }) 32 } else { 33 return null 34 } 35 }, 36 }, 37 38 wait(ms) { 39 return new Promise((resolve) => setTimeout(resolve, ms)) 40 }, 41 42 async poll(url, fn, maxAttempts = 10, attempts = 0) { 43 const response = await fetch(url, { headers: { accept: 'application/json' } }) 44 if (!response.ok) { 45 throw new Error('Request Failed') 46 } 47 48 const object = await response.json() 49 const valid = fn(object) 50 if (!valid && attempts < maxAttempts) { 51 await this.wait(1000) 52 return await API.poll(url, fn, maxAttempts, attempts + 1) 53 } 54 55 return valid 56 }, 57};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.