Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/patrick11514/endpoints
https://github.com/patrick11514/endpoints
Last synced: about 2 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/patrick11514/endpoints
- Owner: patrick11514
- Created: 2023-10-10T16:14:06.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2023-12-22T15:29:43.000Z (about 1 year ago)
- Last Synced: 2024-04-23T20:06:27.359Z (9 months ago)
- Language: TypeScript
- Size: 45.9 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Endpoints
Small library for creating endpoints and executing fetch requests to them using Zod type safety.
## Usage
```TS
import { Endpoint } from "@patrick115/endpoints"
import z from "zod"//schema for the response
const schema = z.object({
status: z.literal(true),
data: z.object({
id: z.number(),
name: z.string(),
})
})//creating endpoint
const endpoint = new Endpoint("/api/path/to/endpoint", "POST", {
username: "patrick115",
password: "example123",
}, schema)//fetch endpoint and return Promise, that can be fullfilled with our data, or rejected with error
endpoint.fetch()/*
fetch endpoint and return object
{
status: true,
data: {
id: 1,
name: "patrick115"
}
}
or if api sends error, which match error schema, it will return
{
status: true,
errorSchema: true,
data: {
//error from api
status: false,
error: "Invalid password"
}
}
or if fetch fails
{
status: false,
error: "Text error" // or error can be object
}
*/
endpoint.fetchSafe()
```