0.4.20 • Published 2 years ago

@analytics/remote-storage-utils v0.4.20

Weekly downloads
58
License
MIT
Repository
github
Last release
2 years ago

Analytics Remote Storage Utils

Utilities for cross domain localStorage access.

Basic Usage

import RemoteStorage from '@analytics/remote-storage-utils'

// Connect to remote storage domain
const storage = new RemoteStorage('https://remote-site.com/storage.html')

// Get item
storage.getItem('the_remote_local_storage_key').then((value) => {
  console.log('value', value)
})

// Set item
storage.setItem({
  key: 'the_remote_local_storage_key',
  value: 'foobar'
}).then(() => {
  console.log('Value set')
})

API

getItem

Get localStorage from another domain

import { RemoteStorage } from '@analytics/remote-storage-utils'

const remoteStorage = new RemoteStorage('https://remote-site.com/storage.html')

remoteStorage.getItem('the_remote_local_storage_key').then(() => {
  console.log('remote value is', value)
})

getRemoteItem

You can also use the standalone getRemoteItem function with a cross storage client instance passed into it.

import { getRemoteItem, CrossStorageClient } from '@analytics/remote-storage-utils'

// Create an instance to use in standalone functions
const storageInstance = new CrossStorageClient('https://remote-site.com/storage.html')

const localStorageKeys = [
  'the_remote_local_storage_key',
  'another_remote_local_storage_key',
  'xyz',
]

getRemoteItem(localStorageKeys, storageInstance).then((values) => {
  console.log('remote values', values)
})

setItem

Set a localStorage value in remote domain

import { RemoteStorage } from '@analytics/remote-storage-utils'

const remoteStorage = new RemoteStorage('https://remote-site.com/storage.html')

remoteStorage.setItem('the_remote_local_storage_key', 'foobar').then(() => {
  console.log('remote value stored')
})

You can also use the standalone setRemoteItem function with a cross storage client instance passed into it.

import { setRemoteItem, CrossStorageClient } from '@analytics/remote-storage-utils'

// Create an instance to use in standalone functions
const storageInstance = new CrossStorageClient('https://remote-site.com/storage.html')

// Set remote value
setRemoteItem({
  key: 'foobar',
  value: JSON.stringify(userId)
}, storageInstance)

// Handler if values are different
function customConflictResolver(localValue, remoteValue) {
  if (localValue === remoteValue) {
    // Return empty to abort setting remote value
    return
  }
  if (remoteValue === 'cool') {
    // Return empty to abort setting remote value
    return
  }
  // Return value to set in remote
  return 'this-value-will-be-set'
}

setRemoteItem({
  key: 'baz',
  value: 123,
  resolve: customConflictResolver
}, crossStorage)
0.4.20

2 years ago

0.4.19

2 years ago

0.4.18

3 years ago

0.4.17

3 years ago

0.4.15

3 years ago

0.4.16

3 years ago

0.4.14

4 years ago

0.4.13

4 years ago

0.4.12

4 years ago

0.4.11

4 years ago

0.4.10

4 years ago

0.4.8

4 years ago

0.4.7

4 years ago

0.4.6

4 years ago

0.4.5

4 years ago

0.4.4

4 years ago

0.4.3

4 years ago

0.4.2

4 years ago

0.4.1

4 years ago

0.4.0

4 years ago

0.3.0

4 years ago

0.2.1

4 years ago

0.2.0

4 years ago

0.1.0

5 years ago

0.0.5

5 years ago

0.0.4

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago