# @svelte-lean/query/http

> http(): the fetch transport with base URL, headers, timeout, a profile, uploads with progress and file downloads.

Page: https://svelte-lean.vercel.app/query/api/http · Markdown: https://svelte-lean.vercel.app/query/api/http.md · Index: https://svelte-lean.vercel.app/llms.txt

http(): the fetch transport. Base URL, headers per request, a timeout per attempt, a platform profile, uploads with progress and file downloads.

## Import

`import`

```ts
import { http } from '@svelte-lean/query/http';
```

## Usage

`src/lib/api/transport.ts`

```ts
// src/lib/api/transport.ts
import { http } from '@svelte-lean/query/http';
import { session } from '$api/session.svelte';

export const transport = http({
	base: 'https://api.example.com/',
	headers: () => ({ authorization: `Bearer ${session.token}` }),
	timeout: '20s',
	// A 401: refresh the token once and send the request again.
	async intercept(response, _request, retry) {
		if (response.status !== 401) return response;
		session.token = await refreshToken();
		return retry();
	}
});
```

## Size

Production Vite builds of a small consumer entry, Svelte external. A marginal row is the bytes the entry adds over the row it names.

| Build | Over | gzip | brotli | Budget (gzip) |
| --- | --- | --- | --- | --- |
| `http (marginal)` | `query` | 2239 B | 2079 B | 2400 B |

Read from `packages/query/artifacts/size.json`. Every entry and its method: [Performance](https://svelte-lean.vercel.app/query/performance.md).

## Functions

One function; pass its result to createClient({ transport }).

### Exports

Runtime exports of the entry.

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| # `http` | `(init: HttpInit) => Transport` | – | A transport over fetch (XMLHttpRequest for uploads with progress); requests per origin are limited by the client. |

## Options

HttpInit, the argument of http().

### HttpInit

Everything but base is optional.

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| # `base` required | `string \| ((request) => string)` | – | Base URL of every path; a function picks one per request. |
| # `headers` | `(request) => HeadersInit \| undefined` | – | Headers read per request (an auth token). |
| # `profile` | `Profile` | `JSON in, JSON out, no envelope` | The platform's conventions: envelope, errors, paginators, query strings. |
| # `fetch` | `typeof fetch` | `globalThis.fetch` | The fetch to call: SvelteKit's event.fetch on the server, a fake in tests. |
| # `timeout` | `Duration` | `'30s'` | Active-time timeout per attempt. |
| # `intercept` | `(response, request, retry, body) => Promise<Response>` | – | Last word on a response: refresh a token and retry() once, sign out on a dead session. |

## Downloads

An endpoint whose result is file() (or transport: { responseType: 'blob' }) resolves to a FileResult.

### FileResult

The file name comes from Content-Disposition.

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| # `blob` | `Blob` | – | The body. |
| # `filename` | `string \| undefined` | – | From filename\* or filename, directory parts removed. |
| # `type` | `string` | – | The content type. |

## Guide

How to use it, with examples: [Client setup](https://svelte-lean.vercel.app/query/client.md). All entry points: [Svelte Lean Query](https://svelte-lean.vercel.app/query.md).
