@cabysis/candy-client
v1.0.4
Published
HTTP Client for making request with native fetch library
Downloads
76
Maintainers
Readme
HTTP Client
Make HTTP Requests in a pretty easy way! Configure all your microservices endpoints in different modules and send requests!
Usage
You can use this library after installation as this example:
import { CandyClient, CandyClientOptions } from "@cabysis/candy-client";
// Example static function to retrieve jwt token
function getToken(): string {
return "generic-jwt-token";
}
// Generic options to replicate every sub-client (if applicable)
const sharedOptions: CandyClientOptions = {
baseUrl: "https://example.com/",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
},
getToken: getToken, // Function reference to retrieve the JWT token
credentials: true, // Include cookies
};
class User {
id: number;
name: string;
email: string;
}
// Example client class of an auth service
class AuthClient extends CandyClient {
constructor() {
super(sharedOptions);
}
async signIn(email: string, password: string): Promise<void> {
return this.post<void>("/auth/signin", { email, password });
}
async getUsers(): Promise<User[]> {
return this.get<User[]>("/auth/users");
}
}
// Example use case for the sign-in request
async function signIn(email: string, password: string): Promise<void> {
try {
const authClient = new AuthClient();
await authClient.signIn(email, password);
console.log("Sign-in successful");
} catch (error) {
console.error("Sign-in failed:", error);
}
}