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

create-majel

v0.0.1

Published

SvelteKit libary for interacting with APIs with support for optional loging of requests and responses

Downloads

5

Readme

sveltekit-superfetch

SvelteKit libary for interacting with APIs with support for optional logging of requests and responses

Usage

Usage is similar to using the standard fetch API. No options are required. The fetcher will default to a timeout of 8 seconds and 3 retries if no values for those options are specified.

(The examples below work with v2.0.0 and above.)

Creating a New Instance

This is often best done in a lib that creates a singleton that can be imported as needed elsewhere in your project. If you just want to use the defaults for all options, you can do:

lib/superfetch.ts

import { SuperFetch } from 'sveltekit-superfetch'
export default const superFetch = new SuperFetch() 

You can also customize the fetcher. Example options:

lib/superfetch.ts

import { SuperFetch } from 'sveltekit-superfetch'
export default const superFetch = new SuperFetch({
   retry: 3,
   timeout: 8000, // 8 seconds
   ttl: 1000, // 1 second. Max age of cached responses.  Only individual queries with a 'key' specified in the options will be cached.
   logger: logger, // injected logger instance, default is `console`, must implement info() and error()
   logFormat: 'json', // text or json, default is json
   logLevel: 'verbose' | 'limited' | 'silent', // default is 'limited' in dev mode, 'silent' in prod
   excludedPaths: ['/api/auth'], // an array of strings, fetches to routes containing these strings will not be logged
   limitedPaths: ['/'] // an array of strings, log entries will not contain headers, bodies, cookies, or url params
})

Using the root path ('/') in the array of limitedPaths will make all log entries contain only limited information. This could be useful if you need to trace an error, but your data is sensitive.

Example fetch with POST method, headers, and body

import superFetch from '$lib/superFetch'
const response = await superFetch.query({
   url: 'https://example.org', 
   method: 'POST',
   headers: {
      'Content-Type': 'application/json'
   },
   body: JSON.stringify({ key: 'value' })
   // ...any other properties supported in basic fetch request
   // see https://developer.mozilla.org/en-US/docs/Web/API/fetch
})

Example fetch that will be cached server-side with optional ttl override

import superFetch from '$lib/superFetch'
const response = await superFetch.query({
   url: 'https://example.org/api/product', 
   key: 'products',
   ttl: 10000 // 10 seconds
})

Even a cache with a relatively short ttl, such as 1 second, can provide a large performance boost and reduce hits to third-party APIs on high-traffic sites. Do not attempt to cache request types other than GET. Do not cache sensitive or dynamic endpoints, such as customer profiles.

Basic example with no options

import superFetch from '$lib/superFetch'
const response = await superFetch.query("https://example.org")

If you create a new instance of SuperFetch without passing in a logger instance, it will use console (console.log() and console.error()) by default.