npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@api.global/typedrequest

v3.0.23

Published

A TypeScript library for making typed requests towards APIs, including facilities for handling requests, routing, and virtual stream handling.

Downloads

939

Readme

@api.global/typedrequest

A TypeScript library for making typed requests towards APIs, providing interfaces and classes to handle request/response cycles using typed definitions. It supports building and handling requests, routing based on request types, and virtual stream handling over networks.

Install

To install @api.global/typedrequest, you can use npm or yarn. Run the following command in your project directory:

npm install @api.global/typedrequest --save

Or, if you prefer yarn:

yarn add @api.global/typedrequest

This package is designed to facilitate making typed requests to APIs, ensuring that both requests and responses adhere to predefined interfaces. This enhances the predictability and reliability of network communication in applications that interact with APIs.

Usage

Utilizing @api.global/typedrequest involves several core concepts, including the creation of typed requests, handling virtual streams, defining request handlers, and routing requests based on their type. Below are comprehensive examples and explanations of how to use these features in your applications.

Setting Up a Typed Request

The foundation of @api.global/typedrequest is the TypedRequest class, which allows you to define requests that are strictly typed. This ensures that both the request sent and the response received match the expected structures.

First, define an interface that represents your request and response data structure:

// Define an interface for your request/response structure
interface IUserRequest {
  method: 'getUser';
  request: { userId: string };
  response: { username: string; email: string; };
}

Next, create an instance of TypedRequest using this interface, and use it to make an API call:

import { TypedRequest } from '@api.global/typedrequest';

// Construct a TypedRequest instance for fetching user data
const getUserRequest = new TypedRequest<IUserRequest>('https://your-api.com/users', 'getUser');

// Execute the request with a specific userId and log the response
const userResponse = await getUserRequest.fire({ userId: 'user-123' });
console.log(userResponse.username);

Handling Virtual Streams

@api.global/typedrequest introduces the concept of virtual streams, allowing you to manage real-time data transfer over the network as streams. You can create a virtual stream, send data, and read data from it as shown below:

import { VirtualStream } from '@api.global/typedrequest';

// Instantiate a VirtualStream for handling real-time data
const myStream = new VirtualStream<ArrayBufferLike>();

// Use the stream to send data
await myStream.sendData(new TextEncoder().encode("Hello, World!"));

// Retrieve data from the stream
const receivedData = await myStream.fetchData();
console.log(new TextDecoder().decode(receivedData));

Defining and Using Handlers

To process requests, you can define handlers that are responsible for specific types of requests:

import { TypedHandler } from '@api.global/typedrequest';

interface IAdditionRequest {
  method: 'add';
  request: { a: number; b: number; };
  response: { result: number; };
}

// Create a handler for processing addition requests
const additionHandler = new TypedHandler<IAdditionRequest>('add', async (req) => {
  return { result: req.a + req.b };
});

Routing Requests

TypedRouter is used to route requests to their appropriate handlers based on the request type:

import { TypedRouter } from '@api.global/typedrequest';

const router = new TypedRouter();

// Associate the additionHandler with the router
router.addTypedHandler(additionHandler);

// The router now directs 'add' requests to the additionHandler

Error Handling

@api.global/typedrequest provides a TypedResponseError class to facilitate error handling in typed requests:

import { TypedResponseError } from '@api.global/typedrequest';

// Throw a TypedResponseError with details about the error
throw new TypedResponseError('An error occurred', { detail: 'Error details' });

This comprehensive overview covers the essential features provided by @api.global/typedrequest. By adhering to defined request and response structures, you can create more reliable and maintainable applications that interact seamlessly with APIs.

License and Legal Information

This repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the license file within this repository.

Please note: The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.

Trademarks

This project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.

Company Information

Task Venture Capital GmbH
Registered at District court Bremen HRB 35230 HB, Germany

For any legal inquiries or if you require further information, please contact us via email at [email protected].

By using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.