export type GetApiIpPostalArgs = {ip: string}; export type GetApiIpPostalOptions = {baseUrl?: string | undefined, BasicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; /** * Returns a postal for the selected IP. * * GET /{ip}/postal * * Also accepts auth: BearerAuth, ApiKeyAuth */ export const getApiIpPostal = async (args: GetApiIpPostalArgs, options?: GetApiIpPostalOptions) => { const {baseUrl = "https://ipinfo.io", fetch: fetchFn = fetch, BasicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "text/plain") if (BasicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${BasicAuth.username}:${BasicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/${encodeURIComponent(String(args.ip))}/postal${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return res.text() };