1.0.4 • Published 11 months ago

@cabysis/candy-client v1.0.4

Weekly downloads
-
License
MIT
Repository
github
Last release
11 months ago

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);
  }
}
1.0.4

11 months ago

1.0.3

11 months ago

1.0.2

12 months ago

1.0.1

1 year ago

1.0.0

1 year ago