GitHub - hodgef/apiker: Create Serverless APIs with Cloudflare Workers, Durable...
source link: https://github.com/hodgef/apiker
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Create Serverless APIs using Cloudflare Workers, Durable Objects & Wrangler
Features
- Easy routing & state management
- Basic JWT-based Auth (register, login, refresh token, delete user)
- Automatically updates Durable Object migrations, classes and bindings so you don't have to.
- Get and set object state easily. e.g:
await state().get(paramInCommonObj);
orawait state("MyObjectName").put({ myParam });
Install
To get started with your Apiker project, run:
npx apiker your-site-name
Usage
Once your project is created, you can edit the app.toml
and src/
files as desired :
Counter Example
src/index.js
import { apiker } from "apiker"; import { getUserCounter } from "./controllers/counter"; import objects from "./objects.json"; const routes = { "/users/:id/counter": getUserCounter }; apiker.init({ routes, objects, exports, auth: false });
controllers/counter.ts
import { Handler, res } from "apiker"; export const getUserCounter: Handler = async ({ state }) => { const initialCount = (await state().get("counter")) ?? 0; const counter = initialCount + 1; await state().put({ counter }); return res({ counter }); };
> GET /users/test/counter
{"counter":1}
{"counter":2}
...
Demo: https://apiker-demo.spv.workers.dev/users/test/counter
Full example: Apiker Demo or npx apiker my-demo
Auth
Note: Auth is still in active development. Please do not use in production just yet.
When the auth option is set to true, Apiker will register the following default routes:
/auth/register
/auth/login
/auth/refresh
/auth/delete
> POST /auth/register
Request body:
email: xxxxx
password: xxxxx
Response (example):
{ "userId": "6e9a13f1577f397b9989c4a856f2524cfb9093b4e3d7feea728e6ec24aa0663c", "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiPiI2ZTlhMTNmMTU3N2YzOTdiOTk4OWM0YTg1NmYyNTc0Y2ZiOTA5M2I0ZTNkN2ZlZWE3MjhlNmVjMjRhYTA2NjNjIiwiY2xpZW50SWQiOiJYQ0VxT1FsSTllWjIwV1lwTmhwRjdGZ0pwQWhuamlHTTU2cHE0NW5iYnFJPSIsImV4cCI6MTYzNzIyNjY3MzU3OH0=.TRfp8bJeb9VBDobm8MAu4GirCCLwL+Cq+W+mIgSSizY=", "refreshToken": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI2ZTlhSTNmMTU3N2YzOTdiOTk4OWM0YTg1NmYyNTc0Y2ZiOTA5M2I0ZTNkN2ZlZWE3MjhlNmVjMjRhYTA2NjNjIiwiY2xpZW50SWQiOiJYQ0VxT1FsSTllWjIwV1lwTmhwRjdGZ0pwQWhuamlHTTU2cHE0NW5iYnFJPSJ9.Q535MhFUb4WhfsZPcxpAa18WzN4I1xKllT+2WHXyg7M=" }
For the implementation details, check out the source at Auth.ts
If you would like to implement your own auth, you can always copy the Auth.ts routes to your Apiker project and edit the authentication flow as needed.
Development & Deployment
- Install Cloudflare Wrangler
- Edit your Apiker project's app.toml
npm run build
wrangler publish
Note: Make sure you've read the Durable Object documentation if you need to install wrangler or have any doubts about
app.toml
(also known as wrangler.toml).
Contributing
PRs and issues are always welcome. Feel free to submit any issues you have at: https://github.com/hodgef/apiker/issues
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK