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

knorry

v1.0.0

Published

The most lightweight pure esm browser only http client you could get πŸš€

Downloads

6

Readme

Knorry

Test npm bundle size GitHub npm npm

The most lightweight pure esm browser only http client you could get πŸš€

Why?

  • 🌳 Tree Shakable
  • πŸ—’οΈ Type Definitions
  • 🏭 Supports most features
  • ❌ Doesn't throw errors on requests with non 200 code
  • πŸͺΆ As lightweight as possible
  • ⚑ Pure ESM
  • πŸ“¦ Optimized for bundlers
  • 🀝 Promise based
  • ➑️ Automatically parses JSON
  • πŸ‘¨β€πŸ’» Splendid developer experience
  • πŸš€ Extremely good compatibility (~98.4%)

Installation

With a module bundler:

npm i -D knorry

No additional configuration required

Using a CDN (Not recommended):

const knorry = (await import('https://cdn.jsdelivr.net/npm/knorry@latest')).default

This method is not recommended because it performs additional requests and doesn't support tree shaking, so even if you only used the get method the bundle stays complete

When using node (Not recommended):

npm i knorry xmlhttprequest form-data urlsearchparams
const { defineKnorryOptions } = await import('knorry')
const { XMLHttpRequest } = require('xmlhttprequest')
global.FormData = require('form-data')
global.Blob = class Blob {}
global.URLSearchParams = require('urlsearchparams')
defineKnorryOptions({
    XHRClass: XMLHttpRequest
})

This method is not recommended because it doesn't supports all features and you aren't in need of a lightweight client in nodejs. Use a more robust client like axios instead

Usage

First, import all methods you need:

import { get, post } from 'knorry'

Now you can make requests like this:

const res = await get('example-api.com')
console.log(res.$plain())
// res -> String { 'response', $res: ... }

Alternatively you could use the default export:

import knorry from 'knorry'
knorry('POST', 'example.com', {
    key: 'value'
}, {
    headers: {
        myHeader: '12345'
    }
})

Docs

If you want to see a full documentation take a look at this

Some things to keep in mind when using knorry

Easy mode

By default knorry will use easy mode, which you can disable like this:

import { defineKnorryOptions, get } from 'knorry'
defineKnorryOptions({
    easyMode: false
})

// Or temporarily disable it for one request
await get('example.com', {
    easyMode: false
})

Easy mode will not directly return the response, but rather return the data containing the full response on the $res property. This will work using the constructor of a primitive type like String:

await get('example-api.com') // -> String {'response', $res: ...}
await get('example-api.com', { easyMode: false }) // -> {data: 'response', ...}

It is implemented like this because it's much cleaner to write:

<!-- Svelte file for demonstration -->
<script>
import { get } from 'knorry'
</script>
<main>
Hello, {await get('/whoami')}
</main>

than having to wrap every request in parentheses:

<!-- Svelte file for demonstration -->
<script>
import { get, defineKnorryOptions } from 'knorry'
defineKnorryOptions({
    easyMode: false
})
</script>
<main>
Hello, {(await get('/whoami')).data}
</main>

However this has some flaws, like equality & type checking. Here are some instructions teaching you what works and what doesn't:

import { get } from 'knorry'

// πŸ’©
await get('/whoami') === 'username'
typeof await get('/whoami') === 'string'
!!await get('/returns-false') // -> true

// πŸ‘Œ
await get('/whoami') == 'username'
await get('/whoami') instanceof String
!!(await get('/returns-false')).$plain() // -> false

If you are working on an enterprise project, you should disable easyMode by default.