Getting Started

Getting Started

The utilities that are provided in the @flare-city/core package are designed with familiar APIs to easily get you started with creating a routing structure to handle calls to specific endpoints. Since this is designed specifically for Cloudflare Workers, this guide will be CF Workers convention heavy.

Create a new App instance

The App class will help you setup how your routes will be parsed, ingested, validated and then subsuquently run.

// index.ts
import { App, Env } from "@flare-city/core";
 
const myApp = new App("myAppName");

For more information on the App class, check out the App documentation

Add any middleware

Add any middlware that you feel you need. Since CF Workers are just short lived functions on the edge, the best practice is to put utilities, common use functions and other non-serializeable things on the execution context.

In the example below, we're calling a middlewarePrisma that we have defined locally to add a prisma/client to the execution context so we can easily access the prisma client in each handler to run type-safe DB queries.

import { middlewarePrisma } from "./lib";
 
myApp.addMiddleware(middlewarePrisma);

For more information on the Middleware type, check out the Middleware documentation

Define & add any routes

Routes define the API endpoints and their corresponding handlers.

For more information on the Route class, check out the Route documentation

import { RouteTest, RouteTeam } from "./features";
 
myApp.addRoute(RouteTest);
myApp.addRoute(RouteTeam);

Start the application

The start method exports a static object with a fetch property on it that is cast to the myApp.run method. You can either export the myApp.start() method or manually default export the fetch key yourself and run the myApp.run method

export default API.start();
 
// -- OR --
 
export default {
  fetch: async function (
    request: Request,
    env: Env,
    context: ExecutionContext
  ) {
    return myApp.run(request, env, context);
  },
};

Below, for reference, is an example of what the src/index.ts file should look like when creating your Cloudflare API.

// src/index.ts - CloudFlare fetch entrypoint
 
import { log, middlewarePrisma } from "./lib";
import { App, Env } from "@flare-city/core";
import { RouteTest, RouteTeam } from "./features";
 
// Declare a new application
export const API = new App("shank-city");
 
// Add Middleware
API.addMiddleware(middlewarePrisma);
 
// Add routes
API.addRoute(RouteTest);
API.addRoute(RouteTeam);
 
// Start the API
export default API.start();