@helia/verified-fetch 1.0.1 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +73 -0
- package/dist/index.min.js +7 -7
- package/dist/src/index.d.ts +73 -0
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +73 -0
- package/dist/src/index.js.map +1 -1
- package/dist/src/utils/parse-url-string.d.ts.map +1 -1
- package/dist/src/utils/parse-url-string.js +39 -20
- package/dist/src/utils/parse-url-string.js.map +1 -1
- package/dist/src/utils/responses.d.ts +8 -4
- package/dist/src/utils/responses.d.ts.map +1 -1
- package/dist/src/utils/responses.js +56 -7
- package/dist/src/utils/responses.js.map +1 -1
- package/dist/src/verified-fetch.d.ts.map +1 -1
- package/dist/src/verified-fetch.js +35 -19
- package/dist/src/verified-fetch.js.map +1 -1
- package/package.json +2 -1
- package/src/index.ts +73 -0
- package/src/utils/parse-url-string.ts +50 -26
- package/src/utils/responses.ts +76 -7
- package/src/verified-fetch.ts +36 -19
package/README.md
CHANGED
|
@@ -408,6 +408,79 @@ console.info(res.headers.get('accept')) // application/octet-stream
|
|
|
408
408
|
const buf = await res.arrayBuffer() // raw bytes, not processed as JSON
|
|
409
409
|
```
|
|
410
410
|
|
|
411
|
+
## Redirects
|
|
412
|
+
|
|
413
|
+
If a requested URL contains a path component, that path component resolves to
|
|
414
|
+
a UnixFS directory, but the URL does not have a trailing slash, one will be
|
|
415
|
+
added to form a canonical URL for that resource, otherwise the request will
|
|
416
|
+
be resolved as normal.
|
|
417
|
+
|
|
418
|
+
```typescript
|
|
419
|
+
import { verifiedFetch } from '@helia/verified-fetch'
|
|
420
|
+
|
|
421
|
+
const res = await verifiedFetch('ipfs://bafyfoo/path/to/dir')
|
|
422
|
+
|
|
423
|
+
console.info(res.url) // ipfs://bafyfoo/path/to/dir/
|
|
424
|
+
```
|
|
425
|
+
|
|
426
|
+
It's possible to prevent this behaviour and/or handle a redirect manually
|
|
427
|
+
through use of the [redirect](https://developer.mozilla.org/en-US/docs/Web/API/fetch#redirect)
|
|
428
|
+
option.
|
|
429
|
+
|
|
430
|
+
## Example - Redirect: follow
|
|
431
|
+
|
|
432
|
+
This is the default value and is what happens if no value is specified.
|
|
433
|
+
|
|
434
|
+
```typescript
|
|
435
|
+
import { verifiedFetch } from '@helia/verified-fetch'
|
|
436
|
+
|
|
437
|
+
const res = await verifiedFetch('ipfs://bafyfoo/path/to/dir', {
|
|
438
|
+
redirect: 'follow'
|
|
439
|
+
})
|
|
440
|
+
|
|
441
|
+
console.info(res.status) // 200
|
|
442
|
+
console.info(res.url) // ipfs://bafyfoo/path/to/dir/
|
|
443
|
+
console.info(res.redirected) // true
|
|
444
|
+
```
|
|
445
|
+
|
|
446
|
+
## Example - Redirect: error
|
|
447
|
+
|
|
448
|
+
This causes a `TypeError` to be thrown if a URL would cause a redirect.
|
|
449
|
+
|
|
450
|
+
```typescript
|
|
451
|
+
|
|
452
|
+
import { verifiedFetch } from '@helia/verified-fetch'
|
|
453
|
+
|
|
454
|
+
const res = await verifiedFetch('ipfs://bafyfoo/path/to/dir', {
|
|
455
|
+
redirect: 'error'
|
|
456
|
+
})
|
|
457
|
+
// throw TypeError('Failed to fetch')
|
|
458
|
+
```
|
|
459
|
+
|
|
460
|
+
## Example - Redirect: manual
|
|
461
|
+
|
|
462
|
+
Manual redirects allow the user to process the redirect. A [301](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/301)
|
|
463
|
+
is returned, and the location to redirect to is available as the "location"
|
|
464
|
+
response header.
|
|
465
|
+
|
|
466
|
+
This differs slightly from HTTP fetch which returns an opaque response as the
|
|
467
|
+
browser itself is expected to process the redirect and hide all details from
|
|
468
|
+
the user.
|
|
469
|
+
|
|
470
|
+
```typescript
|
|
471
|
+
|
|
472
|
+
import { verifiedFetch } from '@helia/verified-fetch'
|
|
473
|
+
|
|
474
|
+
const res = await verifiedFetch('ipfs://bafyfoo/path/to/dir', {
|
|
475
|
+
redirect: 'manual'
|
|
476
|
+
})
|
|
477
|
+
|
|
478
|
+
console.info(res.status) // 301
|
|
479
|
+
console.info(res.url) // ipfs://bafyfoo/path/to/dir
|
|
480
|
+
console.info(res.redirected) // false
|
|
481
|
+
console.info(res.headers.get('location')) // ipfs://bafyfoo/path/to/dir/
|
|
482
|
+
```
|
|
483
|
+
|
|
411
484
|
## Comparison to fetch
|
|
412
485
|
|
|
413
486
|
This module attempts to act as similarly to the `fetch()` API as possible.
|