import qs from 'query-string'

import {IS_PRODUCTION} from '@frogpond/constants'

let root: string

export function setApiRoot(url: string): void {
	root = url
}

export const API = (path: string, query: unknown = null): string => {
	if (!IS_PRODUCTION) {
		if (!path.startsWith('/')) {
			throw new Error('invalid path requested from the api!')
		}
	}

	let url = root + path

	if (query) {
		url += `?${qs.stringify(query)}`
	}

	return url
}

import qs from 'query-string'

import {IS_PRODUCTION} from '@frogpond/constants'

let root: string

export function setApiRoot(url: string): void {
	root = url
}

export const API = (path: string, query?: Record<string, any>): string => {
	if (!IS_PRODUCTION) {
		if (!path.startsWith('/')) {
			throw new Error('invalid path requested from the api!')
		}
	}

	let url = root + path

	if (query) {
		url += `?${qs.stringify(query)}`
	}

	return url
}

Fix API(string, Record<string, any>) signature
m/api: Fix API(string, Record<string, any>) signature

While the previous type was technically correct, this commit definitely
makes the types we expect more explicit.  Something Record-y with string
keys _can_ be passed as the second parameter.  If it's not passed, then
it'd be undefined, not null.

Signed-off-by: Kristofer Rye <1ed31cfd0b53bc3d1689a6fee6dbfc9507dffd22@gmail.com>
