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

@kedoska/resty

v0.0.5

Published

Quick and dirty JSON CRUD REST API for your express servers

Downloads

1

Readme

resty

npm NPM

An NPM package for providing quick and dirty JSON CRUD REST API into your existing ExpressJS application servers.

npm install @kedoska/resty

Key concept

  • Define CRUD operations on the data-adapter.
  • Built-in extractors for pagination and query.

Define your resources

Example: './resources/users'

import resty, { Pagination, Query } from '@kedoska/resty'

export interface User {
  email: string
  username: string
}

const selectMany = (pagination: Pagination, query?: Query): Promise<User[]> =>
  new Promise((resolve, reject) => {
    try {
      resolve([])
    } catch ({message}) {
      reject(Error(`could not "select" the resources, ${message}`))
    }
  })

export default () => {
  return resty({
    version: 'v1',
    resource: 'users',
    dataAdapter: {
      // createOne,
      selectMany, 
      // selectOne, 
      // updateOne, 
      // deleteOne, 
      // deleteAll, 
    },
  })
}

Consume the resource

Example: '.server.ts'

// in your server
import express from 'express'
import users from './resources/users'

const app = express()
app.use(users())
app.listen(8080)

The Data Adapter

:warning: Everything is a Promise

Defines the data functions to mount the endpoints. The following functions can be defined into the data-adapter and passed as argument.

  • createOne (optional promise): creates the post endpoint.
  • selectMany (optional promise): creates the get endpoint.
  • selectOne (optional promise): creates the get ID endpoint.
  • updateOne (optional promise): creates the put ID endpoint.
  • deleteOne (optional promise): creates the delete ID endpoint.
  • deleteAll (optional promise): creates the delete endpoint.

selectMany

  const users = resty({
    version: 'v1',
    resource: 'users',
    dataAdapter: {
      selectMany: () => new Promise((resolve) => resolve([]))
    },
  })

  const app = express()
  app.use(users)

The above server exposes the GET endpoint for the Users resource, mounting the path /v1/users. The data returned by the promise selectMany, an empty array in the example, is sent back as JSON response body.

selectMany with default pagination

  const users = resty({
    version: 'v1',
    resource: 'users',
    dataAdapter: {
      selectMany: (pagination) => new Promise((resolve) => {
        const {page, limit} = pagination
        // limit your data...
        resolve([])
      }
    },
  })

  const app = express()
  app.use(users)

selectMany receives the pagination data as the first parameter. Limit and Page are parsed from the Querystring. Consider the below examples, the default pagination is very straightforward, the data coming from the query string is parsed and passed directly to the selectMany Function.

  • curl https://localhost:8080? becomes { limit: 0 page: 0 }
  • curl https://localhost:8080?limit=10&page=2 becomes { limit: 10 page: 2 }
  • ...

Examples

  • (TS) Copy/Paste Data Adapter Skeleton gits
  • (JS) How to build a CRUD REST API using Express, resty and Sqlite3 examples/sqllite3

Error Handling

The below example implements the errorHandler middleware from '@kedoska/resty' to catch the error sent by the createOne function. The function handles eventual rejections coming from the data-adapter.

// in your server
import express from 'express'
import resty, { errorHandler } from '@kedoska/resty'

const app = express()
app.use(
  resty({
    version: 'v1',
    resource: 'users',
    dataAdapter: {
      createOne: (resource: any) => new Promise((resolve, reject) => {
        reject(Error('Not Yet Implemented'))
      }),
    },
  })
)

app.use(errorHandler)
app.listen(8080)

The post endpoint created by createOne is /v1/users/. It will fail, returning status 200 OK, having the following body:

{
  "message": "createOne not yet implemented"
}