0.0.22 • Published 3 months ago
ajaxness v0.0.22
AJAXNESS
Ajaxness contains an extended fetch API with SWR caching extensions. It also contains a specialized Synch API for RESTful CRUD and a minimalist Auth API for bearer authentication with JWT and oAuth.
fetch API
Extended fetch api with SWR settings features (caching, retry, etc).
import { fetch } from 'react-'
// swr caching support
const cache = 42000
const cacheKeys = ['todos', 'ok']
fetch(url, { cache })
fetch(url, { cache: "7min" })
fetch(url, { cache, cacheKeys })
fetch(url, { cache: "no-cache" })
fetch(url, { cache: "force-cache" })
// revalidate (clear cache)
fetch.clear(cacheKeys)
fetch.clear() // all
// swr features
const callback = e => console.log(e)
const retry = { repeat:3, interval:1000 }
const reget = { interval:1000, callback }
await fetch("http://etc.com", { retry })
await fetch("http://etc.com", { reget })
// auth token state
fetch.token = `Bearer ${token}`
// easy interceptors
fetch.on("request", req => ...)
fetch.on("response", res => ...)
fetch.on("exception", err => ...)
Synch API
RESTful fluent API for synchornization semantics.
import { syncher } from 'react-'
const messageApi = syncher<User>(true)
.fetch("http://message.api/baseURL")
.catch(e => "not found...")
.match(x => x, "id")
// message Api is a ISync type
interface ISync<T,E> {
value: T
await: boolean
error: E|undefined
async(mutate?): Promise<void>
}
// sync search/mutate flag
sync() // querying sync
sync(true) // mutation sync
// support SWR methods
const messageApi = syncher<User>(true)
.retry(3, 1000)
.cache(1000, ["messages", 1])
.reget(1000, x => "pooling...")
.fetch("http://message.api/baseURL")
.catch(e => "not found...")
.match(x => x, "id")
Auth API
Simple fluent authentication API for JWT Bearer and oAuth authentication.
// JWT bearer + basic Auth
interface Token { access: string }
interface User { role: string }
const session = authenticate<User>()
.fetch("http://www.api.com/login")
.catch(x => "Login fails", "/login")
.match<Token>(x => x.access, "/home")
// oAuth provider support
const googleAuth: OAuth = {
scopings:"public_profile,email",
clientId:"asdfasfsadfasdfasdf",
secretId:"asdfasdfasdfasdfasd",
}
const session = authenticate(googleAuth)
.fetch("http://www.google.com/login")
.catch(x => "Login fails", "/login")
.match<Token>(x => x.token, "/home")
// login and logout behavior
await session.login(username, password)
const user = session.logged
session.logout()
0.0.20
3 months ago
0.0.21
3 months ago
0.0.22
3 months ago
0.0.19
3 months ago
0.0.12
3 months ago
0.0.13
3 months ago
0.0.14
3 months ago
0.0.15
3 months ago
0.0.16
3 months ago
0.0.17
3 months ago
0.0.18
3 months ago
0.0.11
3 months ago
0.0.9
3 months ago
0.0.8
3 months ago
0.0.5
3 months ago
0.0.4
3 months ago
0.0.3
3 months ago
0.0.2
3 months ago
0.0.1
3 months ago