5.1.1 • Published 5 months ago

typesafe-api-call v5.1.1

Weekly downloads
-
License
ISC
Repository
github
Last release
5 months ago

Type safe API Caller

  • Typesafe API Caller is a minimalistic JS library that allows you to call APIs in a more functional way (an API will be either successful or not successful).

  • The API callers ensures that the API call will always return either of following result:

    • API Success
    • API Failure
  • No need to worry about handling exceptions & errors. Just focus on handling the API Success and API Failure.

Installation

npm install typesafe-api-caller

Usage

  • A simple example of calling a GET API and handling the response.
import { APICaller, APIResponse, type APIRequest, APISuccess } from 'typesafe-api-call';

const serverEndpoint = 'https://jsonplaceholder.typicode.com';

async function getAllPosts(): Promise<APIResponse<Post[], unknown>> {
  const apiRequest: APIRequest = {
    url: new URL(`${serverEndpoint}/posts`),
    method: 'GET'
  };
  const apiResponse = await APICaller.call(apiRequest, (successResponse: unknown) => {
    // Handle success response decoding here
  }, (errorResponse: unknown) => {
    // Handle error response decoding here in case the response was not successfully decoded into success response
  });
  return apiResponse;
}

const getAllPostResult = await getAllPosts();

if (getAllPostResult instanceof APISuccess) {
  // Handle successful response here
} else {
  // Handle error response here
}

Development

  • Clone the repository
  • Install dependencies using pnpm install
  • Run pnpm run build to build the library.
  • You can use examples/index.ts to test your changes.

Contribution

  • The library is open to all your suggestions & bugfixes and I would love to see your contributions.
  • To contribute, kindly fork the repository & raise changes to the release branch.

Useful Libraries

  • The following libraries goes really well when clubbed with typesafe-api-call .
  • When combined together, you only need to write types in YAML and the library will generate the types & decoders for you. You can later use these types & decoders in typesafe-api-call to decode the API response.
  • Checkout the Example: Yaml & Example: Generated Types
5.1.1

5 months ago

5.1.0

5 months ago

5.0.0

7 months ago

4.0.0

7 months ago

2.0.2

1 year ago

1.0.0

1 year ago

3.1.0

1 year ago

3.0.0

1 year ago

2.0.1

1 year ago

2.0.0

1 year ago

0.1.0

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago