Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/richytong/wweb
Web APIs -> environment-agnostic key value stores and more!
https://github.com/richytong/wweb
Last synced: about 2 months ago
JSON representation
Web APIs -> environment-agnostic key value stores and more!
- Host: GitHub
- URL: https://github.com/richytong/wweb
- Owner: richytong
- License: mit
- Created: 2019-01-26T14:54:23.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2019-02-15T22:13:12.000Z (almost 6 years ago)
- Last Synced: 2024-10-31T19:39:58.938Z (2 months ago)
- Language: JavaScript
- Homepage:
- Size: 52.7 KB
- Stars: 2
- Watchers: 3
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# wweb
wweb synchronizes Web APIs across the server and browser so you can stop writing environment conscious code and focus on building features. wweb exposes the same interfaces around Web APIs `location.search -> wweb.search`, `localStorage -> wweb.localStorage`, and `document.cookie -> wweb.cookie` so you can use them like they were key value stores. wweb assumes that your browser or your users' browsers have web APIs. Finally, wweb supports node all the way back to 6.4.0.# Installation
```bash
$ npm install wweb
```# Usage
```javascript
const wweb = require('wweb')wweb.redirect('https://myawesomewebsite.com')
// redirects to myawesomewebsite.com, same as window.location = '...'console.log(window.location.search)
// => '?foo=bar'const myQueryParam = wweb.search.get('foo')
// => 'bar'const search = wweb.search.getAll()
// => { foo: 'bar' }wweb.search.remove('foo')
console.log(window.location.search)
// => ''wweb.search.set('foo', 'bar')
wweb.search.set('baz', 'qux')
console.log(window.location.search)
// => '?foo=bar&baz=qux'wweb.search.update({ quux: 'quuz' })
console.log(window.location.search)
// => '?quux=quuz'wweb.search.clear()
console.log(window.location.search)
// => ''console.log(window.document.cookie)
// => 'foo=bar; baz=qux'const myCookie = wweb.cookies.get('foo')
// => 'bar'const cookies = wweb.cookies.getAll()
// => { foo: 'bar', baz: 'qux' }wweb.cookies.remove('foo')
console.log(window.document.cookie)
// => 'baz=qux'wweb.cookies.set('foo', 'bar')
console.log(window.document.cookie)
// => 'foo=bar; baz=qux'wweb.cookies.update({ quux: 'quuz' })
console.log(window.document.cookie)
// => 'quux=quuz'wweb.cookies.clear()
console.log(window.document.cookie)
// => ''wweb.localStorage.update({ foo: 'bar' })
console.log({ ...window.localStorage })
// => { foo: 'bar' }wweb.localStorage.get('foo')
// => 'bar'wweb.localStorage.set('quux', 'quuz')
console.log({ ...window.localStorage })
// => { foo: 'bar', quux: 'quuz' }wweb.localStorage.remove('quux')
console.log({ ...window.localStorage })
// => { foo: 'bar' }wweb.localStorage.clear()
console.log({ ...window.localStorage })
// => {}
```# API
wweb.redirect(url)
Redirects you to url. Same thing as using window.location = urlExample:
```javascript
wweb.redirect('https://example.com')
```wweb.search.get(name)
Gets a search parameter by name from the search query in the browser or req.query in the server. Returns null if parameter not foundExample:
```javascript
const foo = wweb.search.get('foo')
```wweb.search.getAll(name)
Gets all search parameters in the form of an object.Example:
```javascript
const searchObj = wweb.search.getAll()
```wweb.search.set(name, value)
Adds or updates a search parameterExample:
```javascript
wweb.search.set('foo', 'bar')
```wweb.search.update(obj)
Updates entire search to represent objExample:
```javascript
wweb.search.update({ foo: 'bar' })
```wweb.search.clear()
Clears the search queryExample:
```javascript
wweb.search.clear()
```wweb.cookie.get(name)
Gets a cookie by name from the document or req.cookies in the server. Returns null if parameter not foundExample:
```javascript
const foo = wweb.cookie.get('foo')
```wweb.cookie.getAll(name)
Gets all cookie names and their values in the form of an objectExample:
```javascript
const cookieObj = wweb.cookie.getAll()
```wweb.cookie.set(name, value, opts)
Adds or updates a cookie. [opts](https://www.npmjs.com/package/cookie#options)Example:
```javascript
wweb.cookie.set('foo', 'bar')
```wweb.cookie.update(obj)
Updates all cookies in document to represent obj. There is no support for options for this at the momentExample:
```javascript
wweb.cookie.update({ foo: 'bar' })
```wweb.cookie.clear()
Clears all cookies in documentExample:
```javascript
wweb.cookie.clear()
```wweb.localStorage.get(name)
Gets an item by name in localStorage. Returns null if not foundExample:
```javascript
const foo = wweb.localStorage.get('foo')
```wweb.localStorage.getAll(name)
Gets all items in localStorage in the form of an objectExample:
```javascript
const store = wweb.localStorage.getAll()
```wweb.localStorage.set(name, value)
Adds or updates an item in localStorageExample:
```javascript
wweb.localStorage.set('foo', 'bar')
```wweb.localStorage.update(obj)
Updates all items in localStorage to represent objExample:
```javascript
wweb.localStorage.update({ foo: 'bar' })
```wweb.localStorage.clear()
Clears all items in localStorageExample:
```javascript
wweb.localStorage.clear()
```# Notes
Don't see your favorite web storage mechanism? Raise an [issue](https://github.com/richytong/wweb/issues/new)# License
wweb © [richytong](https://github.com/richytong)