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

vercel-serverless-api

v1.1.0

Published

A handler for Serverless Function in Vercel to develop API

Downloads

46

Readme

Vercel-Serverless-Api

Code Quality Status

Build Status Coverage Status

npm-vercel-serverless-api

Description

A handler for Serverless Function in Vercel to develop API

Installation

npm i vercel-serverless-api

API

Its a Class to help to create an API.

Getters

Ids

  • pathIds: object, Query Parameters or Path-Parameters for ids
    • Example: https://example.verce.app/api/message?pathIds.id=10
      • pathIds.id: '10'
    • Example: https://example.verce.app/api/message/?pathIds.emailId=11
      • pathIds.emailId: '11'

Body

  • data: object, Body

Queries

  • filters: object, Query parameters to filter

    • Example https://example.verce.app/api/message?name=John&filters.age=10
      • filters.name: 'John'
      • filters.age: '10'
  • this.sort: object, Query parameters to sort

    • Example: https://example.verce.app/api/message?sortBy=name&sortDirection=desc
      • sort.by: 'name'
      • sort.direction: 'desc'
  • query: object, Query parameters

    • Example: https://example.verce.app/api/message?other.foo=name
      • query.foo: 'name'

Request

Other request data

  • request
    • request.url: Request URL
    • request.method: Request REST Method
    • request.headers: Request Headers
    • request.cookies: Request cookies

Methods

  • setCode(code): To setup a custom response status-code

    • code: number
  • setHeader(header, value): To setup a custom response header.

    • header: string
    • value: string or number or boolean
  • setBody(body): To setup a custom response body. If you do not set a custom Content-type, this will be application/json

    • body: object (for JSON) or any (for other content-type)
  • validate: For validation. If you throw an error, will setup status-code 400 by default

    • async
  • process: The API itself. If you throw an error, will setup status-code 500 by default

    • async

Structure Validation

Can use [email protected] to validate body, ids, filters, sort, only must rewrite the following method

  • idsStruct
    • static
  • bodyStruct
    • static
  • filtersStruct
    • static
  • sortStruct
    • static
  • queryStruct
    • static

Usage

const { struct } = require('superstruct'); // only works up to 0.7.0 version
const { API } = require('vercel-serverless-api');

module.exports = class MyApi extends API {

    static get idsStruct() {
        return struct({
            id: 'string'
        });
    }

    static get bodyStruct() {
        return struct({
            name: 'string',
            age: 'string?'
        });
    }

    static get filtersStruct() {
        return struct({
            name: 'string|null?',
            age: 'string|null?'
        });
    }

    static get sortStruct() {
        return struct({
            by: 'string?',
            direction: 'string?'
        });
    }

    validate() {

        if(this.data.age < 10)
            throw new Error('Too Young'); // statusCode will be 400
    }

    process() {

        if(!this.data.name)
            throw new Error('Empty String is not valid'); // statusCode will be 500

        if(this.data.image) {
            this.setHeader('Content-Type', 'text/plain') // Set a Custom Content-Type
                .setHeader('X-Custom', 100) // Set a Custom Header
                .setBody('<h1>Secret</h1>') // Because setting a Custom Type will Response a Plain Text
        }

        this.setCode(201).setBody({
            name: this.data.name
            lastname: 'Stark',
            age: this.data.age + 1
        });
    }
}

Handler

The API Class and Handler can be combined to help to devolope Serverless Function in Vercel

// in ./api/message/post.js
const { hanlder } = require('vercel-serverless-api');

const MyApi = require('./my-api');

module.exports = (..args) => handler(MyApi, ...args);