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

skapi

v1.0.9

Published

Skapi makes SvelteKit API development simpler.

Downloads

8

Readme

Svapi - SvelteKit API Helper

Svapi makes SvelteKit API development simpler by providing a sort of pseudo-middleware to handle things like token authentication and error/data responses.

What it does

You pass in the standard SvelteKit request object, and returns a req (which is the same as the original request but with some added stuff) and a res which helps with formatting the JSON response in a DRY and consistent way.

Example POST with plain SvelteKit and Mongoose

// POST /books
export async function post(request) {
  let error;
  const book = await Book.create(request.body).catch(err => error = err);
  if (error) {
    return {
      status: 400,
      body: {
        error: error
      }
    }
  }
  if (book) {
  	return {
      status: 200,
      body: {
        data: book
      }
    }  
  }
}

Now here's the same thing using Svapi.

// POST /books
export async function post(request) {
  const {req, res} = svapi(request);
  const company = await Company.create(req.body).catch(err => res.setError(err));
  return res.send(company);
}

Wait. What just happened?

We always start by passing the request object to svapi() which returns req, and res objects.

req

The req object has all the attributes of the original SvelteKit request object (so stuff like body and params, etc. will all be there) – but then Svapi adds a couple other juicy nuggets as well. For example, if a JWT was included in the headers, it will automatically be validated, decoded and available at req.token. More about that later.

res

The res object provides a few handy methods for formatting your response. To return a success response containing data, just return res.send(myData) – which is identical to:

return {
	status: 200,
	body: {
		status: 200
		data: myData
	}
}

(but saves a lot of typing).

You also may have noticed the catch block passes the err object into res.setError(). This saves the error to the res object so the last line will return that error instead of the data. The res.send() call gets hijacked if you've previously called res.setError().