Skip to content

Instantly share code, notes, and snippets.

@eur2
Forked from joshnuss/httpStore.js
Created November 15, 2021 09:12
Show Gist options
  • Select an option

  • Save eur2/b92f40edd224d91b38e99046eeb4e9d5 to your computer and use it in GitHub Desktop.

Select an option

Save eur2/b92f40edd224d91b38e99046eeb4e9d5 to your computer and use it in GitHub Desktop.

Revisions

  1. @joshnuss joshnuss revised this gist Jun 4, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion usage.js
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    import { http } from './httpStore.js'
    import http from './httpStore.js'

    // create store and set initial value
    const cart = http(window.cart)
  2. @joshnuss joshnuss revised this gist Jun 4, 2020. 2 changed files with 9 additions and 5 deletions.
    1 change: 1 addition & 0 deletions httpStore.js
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,7 @@
    import { writable } from 'svelte/store'

    // returns a store with HTTP access functions for get, post, patch, delete
    // anytime an HTTP request is made, the store is updated and all subscribers are notified.
    export default function(initial) {
    // create the underlying store
    const store = writable(initial)
    13 changes: 8 additions & 5 deletions usage.js
    Original file line number Diff line number Diff line change
    @@ -3,14 +3,17 @@ import { http } from './httpStore.js'
    // create store and set initial value
    const cart = http(window.cart)

    // subscribe to changes to cart
    // any component can subscribe to changes
    cart.subscribe($cart => console.log('Cart was updated: ', $cart))

    // add item to cart
    // issue HTTP POST to add item to cart
    cart.post('/cart', {productId: 123, quantity: 4})
    // prints: Cart was updated: ...

    // delete item '123' from cart
    // issue HTTP DELETE item '123' from cart
    cart.delete('/cart/123')
    // prints: Cart was updated: ...

    // refresh cart
    cart.get('/cart')
    // issue HTTP GET to refresh cart
    cart.get('/cart')
    // prints: Cart was updated: ...
  3. @joshnuss joshnuss revised this gist Jun 4, 2020. 1 changed file with 30 additions and 30 deletions.
    60 changes: 30 additions & 30 deletions httpStore.js
    Original file line number Diff line number Diff line change
    @@ -3,49 +3,49 @@ import { writable } from 'svelte/store'
    // returns a store with HTTP access functions for get, post, patch, delete
    export default function(initial) {
    // create the underlying store
    const store = writable(initial)
    const store = writable(initial)

    // define a request function that will do `fetch` and update store when request finishes
    store.request = async (method, url, params=null) => {
    // before we fetch, clear out previous errors and set `loading` to `true`
    store.request = async (method, url, params=null) => {
    // before we fetch, clear out previous errors and set `loading` to `true`
    store.update(data => {
    delete data.errors
    delete data.errors
    data.loading = true
    return data
    })

    return data
    })

    // define headers and body
    const headers = {
    "Content-type": "application/json"
    }
    const body = params ? JSON.stringify(params) : undefined
    const headers = {
    "Content-type": "application/json"
    }
    const body = params ? JSON.stringify(params) : undefined

    // execute fetch
    const response = await fetch(url, { method, body, headers })
    // pull out json body
    const response = await fetch(url, { method, body, headers })
    // pull out json body
    const json = await response.json()

    // if response is 2xx
    if (response.ok) {
    if (response.ok) {
    // update the store, which will cause subscribers to be notified
    store.set(json)
    } else {
    store.set(json)
    } else {
    // response failed, set `errors` and clear `loading` flag
    store.update(data => {
    store.update(data => {
    data.loading = false
    data.errors = json.errors
    return data
    })
    }
    }
    }
    }

    // convenience wrappers for get, post, patch, and delete
    store.get = (url) => store.request('GET', url)
    store.post = (url, params) => store.request('POST', url, params)
    store.patch = (url, params) => store.request('PATCH', url, params)
    store.delete = (url, params) => store.request('DELETE', url, params)
    store.get = (url) => store.request('GET', url)
    store.post = (url, params) => store.request('POST', url, params)
    store.patch = (url, params) => store.request('PATCH', url, params)
    store.delete = (url, params) => store.request('DELETE', url, params)

    // return the customized store
    return store
    return store
    }
  4. @joshnuss joshnuss created this gist Jun 4, 2020.
    51 changes: 51 additions & 0 deletions httpStore.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,51 @@
    import { writable } from 'svelte/store'

    // returns a store with HTTP access functions for get, post, patch, delete
    export default function(initial) {
    // create the underlying store
    const store = writable(initial)

    // define a request function that will do `fetch` and update store when request finishes
    store.request = async (method, url, params=null) => {
    // before we fetch, clear out previous errors and set `loading` to `true`
    store.update(data => {
    delete data.errors
    data.loading = true

    return data
    })

    // define headers and body
    const headers = {
    "Content-type": "application/json"
    }
    const body = params ? JSON.stringify(params) : undefined

    // execute fetch
    const response = await fetch(url, { method, body, headers })
    // pull out json body
    const json = await response.json()

    // if response is 2xx
    if (response.ok) {
    // update the store, which will cause subscribers to be notified
    store.set(json)
    } else {
    // response failed, set `errors` and clear `loading` flag
    store.update(data => {
    data.loading = false
    data.errors = json.errors
    return data
    })
    }
    }

    // convenience wrappers for get, post, patch, and delete
    store.get = (url) => store.request('GET', url)
    store.post = (url, params) => store.request('POST', url, params)
    store.patch = (url, params) => store.request('PATCH', url, params)
    store.delete = (url, params) => store.request('DELETE', url, params)

    // return the customized store
    return store
    }
    16 changes: 16 additions & 0 deletions usage.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    import { http } from './httpStore.js'

    // create store and set initial value
    const cart = http(window.cart)

    // subscribe to changes to cart
    cart.subscribe($cart => console.log('Cart was updated: ', $cart))

    // add item to cart
    cart.post('/cart', {productId: 123, quantity: 4})

    // delete item '123' from cart
    cart.delete('/cart/123')

    // refresh cart
    cart.get('/cart')