@lemonldap-ng/fetch
TypeScript icon, indicating that this package has built-in type declarations

0.1.0 • Public • Published

@lemonldap-ng/fetch

Provide a fetch() like function but which catches LemonLDAP::NG rejection when session is expired. When session is expired, it redirects user to the portal to renew its authentication.

Synopsis

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 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:
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:
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:

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

Package Sidebar

Install

npm i @lemonldap-ng/fetch

Weekly Downloads

0

Version

0.1.0

License

GPL-3.0

Unpacked Size

6.5 kB

Total Files

8

Last publish

Collaborators

  • xguimard