Package Exports
- nosdav-shim
- nosdav-shim/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (nosdav-shim) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
nosdav-shim
Documentation
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