# @lemonldap-ng/fetch

> Provide a fetch() like function but which catches [LemonLDAP::NG](https://lemonldap-ng.org) rejection when session is expired. When session is expired, it redirects user to the portal to renew its authentication.

Latest version **0.1.0** (published 2022-08-16) · GPL-3.0 license · 0 weekly downloads

## Install

```sh
npm install @lemonldap-ng/fetch
pnpm add @lemonldap-ng/fetch
yarn add @lemonldap-ng/fetch
bun add @lemonldap-ng/fetch
```

## Health

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

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.0 |
| Published | 2022-08-16 |
| First published | 2022-08-16 |
| Weekly downloads | 0 |
| License | GPL-3.0 |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 6.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | xguimard |
| Keywords | Lemonldap::NG, SSO, SSOaaS |

## Links

- npm: https://www.npmjs.com/package/@lemonldap-ng/fetch
- Homepage: https://lemonldap-ng.org
- Issues: https://gitlab.ow2.org/lemonldap-ng/lemonldap-ng/issues
- npm.io page: https://npm.io/package/@lemonldap-ng/fetch

## Recent versions

- 0.1.0 (latest) — 2022-08-16

## README

# @lemonldap-ng/fetch

Provide a fetch() like function but which catches [LemonLDAP::NG](https://lemonldap-ng.org)
rejection when session is expired. When session is expired, it redirects user
to the portal to renew its authentication.

## Synopsis

```js
import llngFetch from '@lemonldap-ng/fetch'

// use it like windows.fetch
llngFetch(url)
  .then( res => res.json() )
  .then( data => console.log(data) )
```

## Description

**llngFetch()** acts like window.fetch(). Differences:

 * if [LemonLDAP::NG](https://lemonldap-ng.org) portal ask for authentication,
   llngFetch() automatically replaces window.location to call the portal with
   the current URL as return URL. It adds a "authRequired" boolean value to
   the request:

```js
llngFetch(url)
  .then(res => {
    if (res.authRequired) {
      console.log("Let's wait for the Portal call")
    } else if (res.ok) {
      res.json().then( data => {
        console.log('result', data)
      })
    }
  })
```

 * if Lemonldap::NG handler refused request with a 403 code, llngFetch()
   doesn't change anything except that it adds a "forbidden" boolean value to
   the request:

```js
llngFetch(url)
  .then(res => {
    if (res.forbidden) {
      alert("You're not authorized to see this")
    } else if (res.ok) {
      res.json().then( data => {
        console.log('result', data)
      })
    }
  })
```

## Replacing window.fetch

You can replace window.fetch globally to avoid rewriting an existing
application:

```js
import llngFetch from '@lemonldap-ng/fetch'
delete window.fetch
window.fetch = llngFetch
```

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