@cloudflare/util-api

Set of utilities to help interact with the v4 API.

Usage no npm install needed!

<script type="module">
  import cloudflareUtilApi from 'https://cdn.skypack.dev/@cloudflare/util-api';
</script>

README

util-api

Handy utilities to help interact with the v4 API.

Lightweight. Asynchronous. In TypeScript.

Installation

$ npm install @cloudflare/util-api

Usage

Assuming your node service/worker is behind api-gateway:

import { apiGateway } from '@cloudflare/util-api';

async function handleRequest(request: Request) {
  try {
    const decodedJWT = await apiGateway.authenticateRequest(request);

    console.log(decodedJWT.user.email);
  } catch (e) {
    // Authentication failed
  }
}

If you only have a Bearer token:

import { apiGateway } from '@cloudflare/util-api';

async function handleBearer(bearer: string) {
  try {
    const decodedJWT = await apiGateway.authenticateBearer(bearer);

    console.log(decodedJWT.user.email);
  } catch (e) {
    // Authentication failed
  }
}

If you only have a JWT:

import { apiGateway } from '@cloudflare/util-api';

async function handleJWT(jwt: string) {
  try {
    const decodedJWT = await apiGateway.authenticateJWT(jwt);

    console.log(decodedJWT.user.email);
  } catch (e) {
    // Authentication failed
  }
}