# svelte-ajax

> Svelte component for making AJAX requests with few lines of code

Latest version **1.0.1** (published 2022-07-24) · 0 weekly downloads

## Install

```sh
npm install svelte-ajax
pnpm add svelte-ajax
yarn add svelte-ajax
bun add svelte-ajax
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.1 |
| Published | 2022-07-24 |
| First published | 2022-07-23 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 16.1 KB |
| Known vulnerabilities | 0 (+7 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 0 |
| Maintainers | gak4u |
| Keywords | svelte, ajax, fetch, json, api, rest, api-client, api-client-svelte |

## Links

- npm: https://www.npmjs.com/package/svelte-ajax
- Repository: https://github.com/gak4u/svelte-ajax
- Homepage: https://github.com/gak4u/svelte-ajax#readme
- Issues: https://github.com/gak4u/svelte-ajax/issues
- npm.io page: https://npm.io/package/svelte-ajax

## Dependencies (1)

- [svelte](https://npm.io/package/svelte.md) ^3.0.0

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 1.0.1 (latest) — 2022-07-24
- 1.0.0 — 2022-07-23

## README

# Svelte AJAX

Svelte AJAX is a set of drop-in replacement components to help you handle REST requests with only a few lines of code.


## Installation
```
npm install --save svelte-ajax
```

## Usage

### Form

```Form``` is a replacement for HTML ```<form>``` tag to handle REST calls

```
<script>
    import {Form} from 'ajax-svelte';

    let response;
    let error;
</script>
<Form action="http://example.com/api/posts" bind:data={response} {error}>
    Title: <input type="text" name="title"><br/>
    Description: <textarea name="description"/><br/>
    <input type="submit" value="Submit">
</Form>
```

**Attributes**

|```action```  |  Required  | REST api endpoint | |
|```medthod``` |  Optional  | HTTP verb. Allowed: POST, PUT, PATCH, DELETE | Default: POST  |
|```data```    |  Optional  | Bindable attribute which returns data on successful response with status code 200 | |
|```error```   |  Optional  | Returns error when status code other than 200 | |

**Events**

on:response     Returns event with data on REST response


## Setup

If you want to setup headers and endpoint globally, you can use ```SetupAjax``` in ```App.svelte``` as follows

App.svelte
```
<script>
import {SetupAjax} from 'svelte-ajax';

SetupAjax({
    endpoint: 'https://example.com/api/',
    headers: {
        apikey: 'xyz'
    }
})
</script>
```

Now you can use trailing URI in your ```Form``` like below.
```
<!-- Following will call POST https://example.com/api/posts -->
<Form action="posts">
...
</Form>
```

**Note: This project is in active development, stay connected for more features**

---
_Source: https://npm.io/package/svelte-ajax · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
