神刀安全网

fetch-sync: a handy wrapper for the Background Sync API

Fetch Sync

A handy wrapper for theBackground Sync API

Made with ❤ at @outlandish

Fetch Sync allows you to proxy fetch requests through the Background Sync API.

Check out a live demo here .

Install

npm install fetch-sync --save 

Table of Contents

  • Requirements
  • Support
  • Features
  • Initialisation
  • Usage
  • Sync API
  • Todo
  • Test
  • Development
  • Contributing
  • Author & License

Requirements

The library utilises some new technologies so currently only works in some browsers. It definitely works in Chrome Canary with the experimental-web-platform-features flag enabled.

The browser must support:

Support

Chrome Canary Chrome Firefox IE Opera Safari

Features

  • Register a Background Sync operation with one call to fetchSync() .

  • Shares exactly the same API as fetch . Accepts a Request or String, and returns the Promise of a Response.

  • Manage sync operations with fetchSync.{get,getAll,cancel,cancelAll}() .

  • Named sync operations have their response stored within an IndexedDB store.

Initialise

Existing Service Worker

If your application already uses a Service Worker, you can import the Fetch Sync worker using importScripts :

importScripts('node_modules/fetch-sync/dist/fetch-sync.sw.min.js') 

And then call fetchSync.init() somewhere in your application’s initialisation procedure.

No Service Worker

Fetch Sync can handle registration if you don’t use a SW already…

Either serve the Fetch Sync worker file with a header "Service-Worker-Allowed : /" , or to avoid configuring headers, create a Service Worker script in the root of your project and use the method above for ‘Existing Service Worker’.

Then see the example underUsagefor the fetchSync.init() method.

Usage

fetchSync.init([options]) : Promise

Initialise fetchSync.

  • options{Object} (optional) options object

    options {   // The URL of the fetchSync worker script.   workerUrl {String} (required, default: null)    // The options object to pass to the worker registration function.   workerOptions {Object} (optional, default: null)    // Force the worker registration to update the worker script.   forceUpdate {Boolean} (optional, default: false) } 

Example:

    // Import client lib...      // ES6     import fetchSync from 'fetch-sync'      // ES5     var fetchSync = require('fetch-sync')      // Script, using bundled dist     <script src="/node_modules/fetch-sync/dist/fetch-sync.min.js"></script>      // Initialise, passing in worker lib location...      fetchSync.init({       workerUrl: 'node_modules/fetch-sync/dist/fetch-sync.sw.js',       workerOptions: {         scope: '<website address>' // e.g. 'http://localhost:8000'       }     }) 

fetchSync([name, ]request[, options]) : Promise<Response>

Perform a sync Background Sync operation.

  • [ name ] {String} (optional) name of the sync operation
  • request {String|Request} URL or an instance of fetch Request
  • [ options ] {Object} (optional) fetch options object

Returns a Promise that resolves on success of the fetch request.

If called with a name the response will be stored and can be retrieved later using fetchSync.get(<name>) and then sync.getResponse() . See theSync APIfor more details.

Examples:

  • named GET

    fetchSync('GetMessages', '/messages') 
  • unnamed POST

    fetchSync('/update-profile', {   method: 'POST',   body: { name: '' } }) 
  • named with options

    fetchSync('/send-message', {   body: 'Hello!' }) 
  • unnamed with Request

    fetchSync(   new Request('/messages') ) 

fetchSync.get(name) : Promise

Get a sync by its name.

See theSync APIdocs for available properties/methods.

  • name {String} name of the sync operation to get

fetchSync.getAll() : Array<Promise>

Get all sync operations.

Returns an array of all sync operations (named and unnamed).

fetchSync.cancel(name) : Promise

Cancel the sync with the given name .

  • name {String} name of the sync operation to cancel

fetchSync.cancelAll() : Promise

Cancel all syncs, named and unnamed.

Sync API

sync.cancel() : Promise

Cancels the sync operation.

Returns a Promise of success of the cancellation.

sync.getResponse() : Response

Response retrieved when the operation was completed. Or null if the operation is incomplete.

sync.id

The unique ID of the sync operation. This will be its name if it has one.

sync.name

The name of the sync operation if it has one.

sync.createdOn

The time that the sync operation was created.

sync.syncedOn

The time that the sync operation was completed.

Useful for named syncs that you want to retrieve later on.

Todo

Test

As the library depends on Service Workers and no headless browser has (good enough) support for Service Workers that would allow tests to be executed within the console, tests are ran through the browser usingMocha andChai.

On running npm test an Express server will be started at localhost:8000 .

Run the tests:

$ cd fetch-sync $ npm test 

Development

The library is bundled byWebpack and transpiled byBabel.

  • Install dependencies: npm install
  • Start Webpack in a console: npm run watch
  • Start the test server in another: npm test
  • Navigate to http://localhost:8000

Contributing

All pull requests and issues welcome!

If you’re not sure how, check out Kent C. Dodds’ great video tutorials on egghead.io !

Author & License

fetch-sync was created by Sam Gluck and is released under the MIT license.

转载本站任何文章请注明:转载至神刀安全网,谢谢神刀安全网 » fetch-sync: a handy wrapper for the Background Sync API

分享到:更多 ()

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址