DEV Community

Paul Chin Jr.
Paul Chin Jr.

Posted on

Serverless web API with Deno and Begin - Part 2

In a previous post, we wrote a single serverless function in TypeScript and Deno to display a random quote with HTML. The response was overwhelming, and now the public demands an API they can integrate into their applications. Let's add a /api route!

Adding a new route

We're going to add a new route for our API by modifying the app.arc file.

# app.arc
@app
your-app-name

@http
get /
get /api
Enter fullscreen mode Exit fullscreen mode

Next, we will make a new directory and file for our HTTP function. Copy the following code into src/http/get-api/index.ts.

// src/http/get-api/index.ts
import { quote } from "https://gist.githubusercontent.com/pchinjr/75027d05c5844d2f2364e1acbb2c8c37/raw/2ff84587ba01d6d262125fe8c22c27d4b704a837/quote-zotic.js"
export async function handler (req: object) {
return {
  statusCode: 200,
  headers: {
  'cache-control': 'no-cache, no-store, must-revalidate, max-age=0, s-maxage=0', 'content-type': 'application/json; charset=utf8'},
  body: JSON.stringify( {data: quote() } )
  }
}
Enter fullscreen mode Exit fullscreen mode

We also need to supply a small config file to change from the default Node.js runtime to Deno with a .arc-config file in the function folder.

# src/http/get-api/.arc-config
@aws
runtime deno
Enter fullscreen mode Exit fullscreen mode

The get-api function uses the same quote() logic from get-index but now we've got more quotes to choose from, and it returns JSON that can be consumed by other applications.

To see it in action, run npm start from your command line and navigate to http://localhost:3333/api

Importing code with Deno

Deno uses ES Modules to import third-party code. I wanted to give this feature a try by taking the quote() logic from our get-index function and hosting it in a GitHub Gist.

You can see the gist here:
https://gist.github.com/pchinjr/75027d05c5844d2f2364e1acbb2c8c37

// gist.js
export function quote() {
 let quotes = ['first quote', 'second quote', 'third']
 let min = 0
 let max = quotes.length - 1
 let rando = ~~(Math.random() * (max-min) + min)
 return quotes[rando]
}
Enter fullscreen mode Exit fullscreen mode

Using this kind of import gives us more control over dependencies without a centralized package manager. You can create your private package registry without any extra tooling. A change like this causes me to think more intentionally about the code I'm going to import.

For more info, the Deno docs have a helpful explanation about linking to third party code.

Deploy with CI/CD on Begin

A new API is only func when it becomes available to the public. Deploying is just one git push away. Each push to your default branch will start a new deployment to staging. Go ahead and show it off by sharing it with us on Twitter.

It's an exciting time to try new workflows as development tools continue to fade into the background. It allows all of us to focus on and explore what's possible in our core application logic.

Top comments (0)