UNPKG

bam-ticketing-sdk

Version:

SDK for B.A.M Ticketing API

42 lines (33 loc) 993 B
import queryString from 'query-string' export interface Query { [key: string]: boolean | Query } export function buildQuery(query: Query): string { // Filter out simple boolean props const topAttributes = Object.keys(query).filter( (keyName) => query[keyName] === true ) // Filter out props with sub-objects which need to be serialized const objectProps = Object.keys(query).filter((prop) => isObject(query[prop])) const subAttributesSerialized = objectProps.map((prop) => { return `${prop}.${buildQuery(query[prop] as Query)}` }) const result = [...topAttributes, ...subAttributesSerialized].join() return `[${result}]` } function isObject(obj: any) { return typeof obj === 'object' && !Array.isArray(obj) && obj !== null } export function getStringifiedQuery(query: any) { return queryString.stringify( { ...query, with: !query.with ? undefined : buildQuery(query.with), }, { arrayFormat: 'comma', skipNull: true, skipEmptyString: true, } ) }