GithubHelp home page GithubHelp logo

typed-promise's Introduction

@enzo418/typed-promise Library

This library provides a TypeScript implementation of Promises, with additional features such as cancellation and typed responses. It also includes an HTTP client that returns these typed promises.

Installation

To install the library, use npm:

npm install @enzo418/typed-promise

Usage

TypedPromise

Import the TypedPromise class from the library:

import { TypedPromise } from '@enzo418/typed-promise';

You can then create a new TypedPromise and use it like a standard Promise, with additional type safety:

import { TypedPromise } from "@enzo418/typed-promise";

let promise = new TypedPromise<number, MyError>((resolve, reject) => {
    if (Math.random() > 0.5) {
        resolve(42);
    } else {
        reject(new MyError({ message: 'Something went wrong', code: 42 }));
    }
});

HttpClient and ProcessPromise

The library also includes an HTTP client that returns Promises but, if you know the backend's error types, you can convert them to TypedPromises using the process method:

import { TypedPromise, HttpClient, IProblemJson, processPromise } from "@enzo418/typed-promise";

interface ResponseDTO {
    name: string;
    age: number;
}

interface ResponseError extends IProblemJson {
    traceId: string;
}

const client = new HttpClient('myapiurl.com');

const parameters = {
    include: 'name,age',
};

const init: RequestInit = {
    headers: { // Example authorization header
        'Authorization': 'Bearer ' + 'mytoken',
    },
};

processPromise<ResponseDTO, ResponseError>(
    client.get('/users/1', parameters, init),
).ok((dto: ResponseDTO) => {
    // Success! status is in the range of 200 - 299.
    // handle success with dto
}).fail((error: ResponseError) => {
    // Error. status is not in the range of 200 - 299 or there was a network error.
    // handle error with error.traceId
});

typed-promise's People

Contributors

enzo418 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.