import isFinite from 'lodash/isFinite' import forEach from 'lodash/forEach' import { normalize } from 'normalizr' import * as types from './types/sdk' // TODO: unit test export const trimAmpersand = (str: string) => { if (typeof str !== 'string') return '' let result = str if (str.startsWith('&')) result = result.slice(1) if (str.endsWith('&')) result = result.slice(0, result.length - 1) return result } // TODO: unit test export const constructAppointmentsUrl = ({ status = [], limit = 10, order_by, }: types.AppointmentFuncArgs) => { let url = `/v2/appointment/?` forEach(status, (stat) => (url += `status=${stat}&`)) if (isFinite(limit)) url += `limit=${limit}&` if (order_by) url += `order_by=${order_by}` return trimAmpersand(url) } // TODO: unit test export const constructFindProviderUrl = ({ language, specialty, time, state, }: any) => { let url = '/v2/appointment/find_provider/?' if (Array.isArray(language)) url += `language=${language.join(',')}&` if (Array.isArray(specialty)) url += `specialty=${specialty.join(',')}&` if (time) url += `time=${time}&` if (state) url += `state=${state}` return trimAmpersand(url) } // TODO: unit test export const constructQueryAppointmentsUrl = ({ patients, providers, status, start_time, end_time, }: types.ConstructQueryAppointmentsUrlArgs) => { let url = '/v2/appointment/?' if (Array.isArray(patients)) { forEach(patients, (patient) => (url += `patients=${patient}&`)) } if (Array.isArray(providers)) { forEach(providers, (provider) => (url += `providers=${provider}&`)) } if (Array.isArray(status)) { forEach(status, (status) => (url += `status=${String(status)}&`)) } if (start_time) url += `start_time=${start_time}&` if (end_time) url += `end_time=${end_time}&` return trimAmpersand(url) } export const ensureArray = (val: any) => { if (!isArray(val)) return val.split(',') else return val } export const isArray = (val: any) => Array.isArray(val) export const isBlob = (blob: any) => !!blob && blob.constructor.name === 'Blob' export const isFile = (file: any) => !!file && file.constructor.name === 'File' export const isString = (val: any) => typeof val === 'string' export const isNumber = (val: any) => typeof val === 'number' export const prepareForQueryParams = (url: string): string | never => { // Already has queries appended to it if (url.includes('?')) { if (url.endsWith('&')) return url if (url.endsWith('/')) { const newUrl = url.slice(0, url.length - 1) if (newUrl.endsWith('&')) return newUrl else return `${newUrl}&` } if (url.endsWith('?')) return url } else return `${url}?` return url } export const log = (...args: any[]) => { console.log(...args) } export const logError = (...args: any[]) => { console.error(...args) } export const _normalize = ( key: string = '', data?: any[], schema?: any, ): types.NormalizedData => { const result = { [key]: {}, ids: [] } if (!key) return result // prettier-ignore const normalizedData = normalize( { [key]: data }, { [key]: [schema] }, ) const ids = normalizedData.result[key] const mappedData = normalizedData.entities[key] return { [key]: mappedData, ids } }