npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@lazy/oauth2-service-worker-vault

v1.0.0-rc.1

Published

Keep your OAuth 2.0 tokens secure and up-to-date.

Downloads

1

Readme

Lazy OAuth 2.0 Token Vault


Table of Contents

Example

application.ts
navigator.serviceWorker.register(
  './service-worker.js?' +
    new URLSearchParams({
      lazy_oauth2_client_id: 'example-client-id',
      lazy_oauth2_token_url: 'https://api.example.com/token',
      lazy_oauth2_protected_hostname: 'api.example.com',
      lazy_oauth2_protected_pathname: '/v1/*',
    }),
  { type: 'module' }
)
./service-worker.ts
import { addOauth2Vault } from '@lazy/oauth2-token-vault'

addEventListener('install', (event) => {
  skipWaiting()
})

addEventListener('activate', (event) => {
  event.waitUntil(clients.claim())
})

addOauth2Vault()

Configuration

The configuration options are appended as query string parameters to the service worker registration. You can see the example above for a implementation guide.

The lazy_oauth2_protected_* options allow you to limit which requests the credentials are added to. By default if you don't specify anything then all requests have credentials.

| Option | Description | Required | | -------------------------------- | ------------------------------------------------------- | -------- | | lazy_oauth2_client_id | The OAuth 2.0 Client ID | Yes | | lazy_oauth2_token_url | The OAuth 2.0 Token URL | Yes | | lazy_oauth2_protected_protocol | The URLPattern protocol for the protected resource. | | | lazy_oauth2_protected_username | The URLPattern username for the protected resource. | | | lazy_oauth2_protected_password | The URLPattern password for the protected resource. | | | lazy_oauth2_protected_hostname | The URLPattern hostname for the protected resource. | | | lazy_oauth2_protected_port | The URLPattern port for the protected resource. | | | lazy_oauth2_protected_pathname | The URLPattern pathname for the protected resource. | | | lazy_oauth2_protected_search | The URLPattern search for the protected resource. | | | lazy_oauth2_protected_hash | The URLPattern hash for the protected resource. | |

API

addOauth2Vault

Add the Oauth2 Vault in the Service Worker.

Example

import { addOauth2Vault } from '@lazy/oauth2-token-vault'

addOauth2Vault()

Returns () => void

fetchWithCredentials

Exactly like the fetch API, except it will add and remove credentials as specified in the query string parameters of the Service Worker.

Parameters

  • resource - string | Request - The resource that you wish to fetch.
  • init - object - An object containing any custom settings that you want to apply to the request.

Example

import { fetchWithCredentials } from '@lazy/oauth2-token-vault'

addEventListener('fetch', (event) => {
  event.respondWith(fetchWithCredentials(event.request))
})

Returns Promise<Response>

fetchWithCredentialRefresh

Exactly like the fetch API, except it will add and remove credentials as specified in the query string parameters of the Service Worker. If the network request fails with a 401 Unauthorized, it will attempt to re try the request once after exchanging the refresh token for a new access token.

Parameters

  • resource - string | Request - The resource that you wish to fetch.
  • init - object - An object containing any custom settings that you want to apply to the request.

Example

import { fetchWithCredentialRefresh } from '@lazy/oauth2-token-vault'

addEventListener('fetch', (event) => {
  event.respondWith(fetchWithCredentialRefresh(event.request))
})

Returns Promise<Response>