export type CreateApiCitiesArgs = {Authorization: string, body: {name: string, latitude: number, longitude: number, population: number, countryCode: string, regionCode: string}}; export type CreateApiCitiesOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; /** * Add a city * * POST /cities */ export const createApiCities = async (args: CreateApiCitiesArgs, options?: CreateApiCitiesOptions) => { const {baseUrl = "http://127.0.0.1:8443", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/x-www-form-urlencoded") if (args.Authorization != null) headers.set("Authorization", String(args.Authorization)) const url = `${baseUrl.replace(/\/+$/, '')}/cities${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: new URLSearchParams(Object.entries(args.body).flatMap(([key, value]) => (value == null ? [] : [[key, String(value)]])))}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return undefined };