2.2.0 • Published 2 months ago

@windyroad/fetch-fragment v2.2.0

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

@windyroad/fetch-fragment

A library that provides a function to fetch a JSON fragment from a response based on a URL fragment identifier.

Installation

npm install @windyroad/fetch-fragment

Usage

import { fetchFragment } from '@windyroad/fetch-fragment';

const response = await fetchFragment('https://example.com/data.json#/foo/bar');
if (response.ok) {
  const data = await response.json();
  // Use the data...
} else {
  console.error(`Failed to fetch fragment: ${response.status} ${response.statusText}`);
}

The fetchFragment function takes a URL with a fragment identifier, fetches the URL, and returns a new response with only the JSON fragment specified by the fragment identifier. If the response is not JSON the function returns a 415 response. If the the fragment is not found the function returns a 404 response.

The fetchFragment function is implemented using the addFragmentSupportToFetch function, which can be used to add fragment support to any fetch implementation. Here is an example:

import { addFragmentSupportToFetch } from '@windyroad/fetch-fragment';

const fetchWithFragmentSupport = addFragmentSupportToFetch(fetch);

const response = await fetchWithFragmentSupport('https://example.com/data.json#/foo/bar');
if (response.ok) {
  const data = await response.json();
  // Use the data...
} else {
  console.error(`Failed to fetch fragment: ${response.status} ${response.statusText}`);
}

API

fetchFragment(url: string): Promise<Response>

Fetches a JSON fragment from a response based on a URL fragment identifier.

addFragmentSupportToFetch(fetchImpl?: typeof fetch): typeof fetch

Adds fragment support to a fetch implementation and returns a new fetch function that supports fragments.

Contributing

Contributions are welcome! Please read the contributing guidelines for more information.

License

@windyroad/fetch-fragment is lovingly licensed under the MIT License. ❤️

2.2.0

2 months ago

2.1.1

7 months ago

2.1.0

8 months ago

2.0.4

8 months ago

2.0.3

8 months ago

2.0.2

8 months ago

2.0.1

9 months ago

2.0.0

9 months ago

1.1.0

9 months ago

1.0.0

10 months ago

0.9.0

10 months ago