This example shows how to implement a REST API with TypeScript using Hono / Sutando / Cloudflare Workers / Cloudflare D1.
Clone this repository:
git clone git@github.com:sutandojs/sutando-examples.git --depth=1
Install npm dependencies:
cd sutando-examples/typescript/rest-hono-cf-d1
npm install
Run the command to create your D1 database
npx wrangler d1 create test-sutando
Update wrangler.toml database_id
Import test data to local database
npx wrangler d1 execute test-sutando --local --file=./migrations/0000_lucky_gideon.sql
npm run dev --local
The server is now running on http://localhost:8787. You can send the API requests implemented in index.js, e.g. http://localhost:8787/posts.
You can access the REST API of the server using the following endpoints:
/posts/:id: Fetch a single post by itsid/posts?searchString={searchString}&page={page}&perPage={perPage}&orderBy={orderBy}: Fetch all published posts- Query Parameters
searchString(optional): This filters posts bytitleorcontentpage(optional): This specifies which page should be returned toperPage(optional): This specifies how many objects should be returned per pageorderBy(optional): The sort order for posts in either ascending or descending order. The value can eitherascordesc
- Query Parameters
/users/:id/drafts: Fetch user's drafts by theirid/users: Fetch all users
/posts: Create a new post- Body:
title: String(required): The title of the postcontent: String(optional): The content of the postauthorEmail: String(required): The email of the user that creates the post
- Body:
/users: Create a new user- Body:
email: String(required): The email address of the username: String(optional): The name of the user
- Body:
/publish/:id: Toggle the publish value of a post by itsid/posts/:id/views: Increases theviews_countof aPostby oneid
/posts/:id: Delete a post by itsid
- Check out the Sutando docs
- Create issues and ask questions on GitHub