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.
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:
`@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:
```typescript
import { VirtualStream } from '@api.global/typedrequest';
// Instantiate a VirtualStream for handling real-time data
const myStream = new VirtualStream<ArrayBufferLike>();
`@api.global/typedrequest` provides a `TypedResponseError` class to facilitate error handling in typed requests:
```typescript
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](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.
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.