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

next-coke-api

v1.0.21

Published

Typed communication between NextJS server/client.

Downloads

9

Readme

Demo

Edit next-coke-api

Description

  • This package provides simple methods to develop client/server communication using NextJS.
  • Next has a huge potential, but the communication can be tricky for starters, that's why we exist.
  • Coke works by creating a proxy from the server routes and abstracting its methods with "client-ready" functionality.
  • It does not use a custom server, we use the default 'pages' file system, allowing next to preserve optimizations.

Features

  • 🛰️ Dynamic communication with the server made easy.
  • 🚯 No duplicated typings between server and client.
  • ✍️ Scalable for any type of authentication method.
  • 🚀 Extremely lightweight and ultra-flexible API.
  • ✅ REST methods are also supported.

Examples

Installation

npm i next-coke-api

You need to structure your api folder to use the [...route].ts slug

📂 pages
 ├─📂 api
 │  └─📄 [...route].ts
 ├─📄 _app.tsx
 ├─📄 _document.tsx
 ├─📄 index.tsx 

If you need to change this structure, make sure you specify a customUrl in the client options.

Usage

Server

// define API methods
const routes = {
    getName: async (body) => {
        return "your name is " + body.name 
    }
}

// export types to the client
export type AppRoutes = typeof routes

// export nextCokeHandler
export default function handler(req, res) {
    return nextCokeHandler(req, res, routes)
}

Client

// define coke client
const { coke } = nextCokeClient<AppRoutes>()

// call API methods
coke.getName({ name: "John" }).then((res) => {
    console.log(res)
})

Using REST

Server

// define REST methods
const routes = {
    users: {
        POST: async (req, res) => {
            return "You created a user. Congratulations " + req.body.name 
        }
    } 
}

// export types to the client
export type AppRoutes = typeof routes

// export nextCokeHandler with last arg as TRUE
export default function handler(req, res) {
    return nextCokeHandler(req, res, routes, true)
}

Client

// define coke client with isREST = true
const { coke } = nextCokeClient<AppRoutes>({ isREST: true })

// call REST methods
coke.users.POST({ name: "John" }).then((res) => {
    console.log(res)
})

Using Authorization Tokens

You can check out a complete example using Firebase Authentication in the examples folder of this repository.

Firebase example requires your project config in the .env.local file.

Server

// define API methods
const routes = {
    getName: async (body) => {
        return "your name is " + body.name 
    }
}

// export types to the client
export type AppRoutes = typeof routes

export default function handler(req, res) {

  // check firebase authentication 
  // this is only an example, please validate the user token with your authentication provider methods)
  if (!req.headers.authorization) {
    return res.status(500).send({ message: 'NO-AUTHENTICATION' })
  }
  
  // return coke handler
  return nextCokeHandler(req, res, routes)
}

Client

// define coke client

// here we export useCoke instead of coke, because it allows an authorization token to be used
const { useCoke } = nextCokeClient<AppRoutes>()
const coke = useCoke("YOUR-AUTHORIZATION-TOKEN-HERE")

// call API methods
coke.getName({ name: "John" }).then((res) => {
    console.log(res)
})

TODO

  • Support full headers instead of authorization only.
  • Create more examples.