A rest api write library as cute and tough as its name
In the first step, let's define a /api/index.get.ts file and add this code block in it
// http://localhost:3000 (/api/index.get.ts)
import { ManpasiResponse, define } from '@/core/index'
import { json } from '@/core/options'
export default define(() => {
return ManpasiResponse(json([{ name: 'Jett Madison' }, { name: 'Luther King' }]), {
status: 200
})
})
Then let's send http://localhost:3000/ get request via curl or Postman. That's it! You will get this as reponse output:
[
{
"name": "Jett Madison."
},
{
"name": "Luther King"
}
]
Usage for POST
// http://localhost:3000/[email protected] (/api/user/index.post.ts)
import { ManpasiResponse, define } from '@/core/index'
import { json } from '@/core/options'
import ManpasiHTTP from "@/types/http.type";
export default define((req: ManpasiHTTP.request) => {
return ManpasiResponse(json(req), {
status: 200
})
})
{
"queryParams": {
"email": "[email protected]"
},
"bodyData": {
"name": "test"
}
}
You can continue api operations by creating a dynamic folder. This is actually very easy. You just create a /user/ folder in the /api/ folder and the process is over.
For example: /api/user/index.get.ts
or /api/user/index.post.ts
Your filename does not have to be "index", you can put any name you want, but it must have .get, .post, .update, .put, .delete, or .patch
at the end because these are our method names for our crud operations. For example: user.get.ts, user.post.ts, user.update.ts.
The library will be very nice with new updates.
This repo uses Bun. To initiate the project bun index.ts