0.0.4 • Published 8 months ago

nosdav-shim v0.0.4

Weekly downloads
-
License
MIT
Repository
github
Last release
8 months ago


GitHub license npm npm Github Stars

Introduction

This shim intercepts all requests made by a web page using the "fetch" API. If the request method is "PUT", it creates a Nostr event object, signs it, and adds the signed object to the "authorization" header of the request before sending it out. This allows the web page to authenticate the request with Nostr. Otherwise, the shim simply passes the request through to the original "fetch" method.

Usage

<script src="https://unpkg.com/nosdav-shim"></script>

Code

const originalFetch = window.fetch;

window.fetch = async function(url, options) {
  const newOptions = { ...options };


  if (newOptions.method === 'PUT') {
    const event = {
      kind: 27235,
      created_at: Math.floor(Date.now() / 1000),
      tags: [['u', url]],
      content: ''
    }
    const signedEvent = await window.nostr.signEvent(event)
    var auth = `Nostr ${btoa(JSON.stringify(signedEvent))}`
  

    newOptions.headers = {
      ...newOptions.headers,
      'authorization': auth
    };
  }

  return originalFetch.call(this, url, newOptions);
};

License

  • MIT
0.0.3

8 months ago

0.0.4

8 months ago

0.0.1

1 year ago