From a7751006cc8e0ee0cb510dc717f9248c394c1ca2 Mon Sep 17 00:00:00 2001 From: Philipp Kunz Date: Sun, 14 Apr 2024 18:41:19 +0200 Subject: [PATCH] update tsconfig --- npmextra.json | 19 ++++- package.json | 17 ++++- readme.hints.md | 1 + readme.md | 193 +++++++++++++++++++++++++++++++++++++++++------- 4 files changed, 199 insertions(+), 31 deletions(-) create mode 100644 readme.hints.md diff --git a/npmextra.json b/npmextra.json index 7c75186..71a726a 100644 --- a/npmextra.json +++ b/npmextra.json @@ -9,12 +9,25 @@ "githost": "code.foss.global", "gitscope": "push.rocks", "gitrepo": "webrequest", - "description": "securely request from browsers", + "description": "A module for making secure web requests from browsers with support for caching and fault tolerance.", "npmPackagename": "@push.rocks/webrequest", - "license": "MIT" + "license": "MIT", + "keywords": [ + "webrequest", + "HTTP", + "secure", + "browsers", + "caching", + "fault tolerance", + "json", + "abort", + "timeout", + "multi-endpoint", + "fetch API" + ] } }, - "tsdocs": { + "tsdoc": { "legal": "\n## License and Legal Information\n\nThis 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. \n\n**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.\n\n### Trademarks\n\nThis 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.\n\n### Company Information\n\nTask Venture Capital GmbH \nRegistered at District court Bremen HRB 35230 HB, Germany\n\nFor any legal inquiries or if you require further information, please contact us via email at hello@task.vc.\n\nBy 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.\n" } } \ No newline at end of file diff --git a/package.json b/package.json index e3cb7a7..0c1fd1b 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "@push.rocks/webrequest", "version": "3.0.35", "private": false, - "description": "securely request from browsers", + "description": "A module for making secure web requests from browsers with support for caching and fault tolerance.", "main": "dist_ts/index.js", "typings": "dist_ts/index.d.ts", "type": "module", @@ -43,5 +43,18 @@ ], "browserslist": [ "last 1 chrome versions" + ], + "keywords": [ + "webrequest", + "HTTP", + "secure", + "browsers", + "caching", + "fault tolerance", + "json", + "abort", + "timeout", + "multi-endpoint", + "fetch API" ] -} +} \ No newline at end of file diff --git a/readme.hints.md b/readme.hints.md new file mode 100644 index 0000000..0519ecb --- /dev/null +++ b/readme.hints.md @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/readme.md b/readme.md index 8d980f7..29d09c1 100644 --- a/readme.md +++ b/readme.md @@ -1,37 +1,178 @@ -# @pushrocks/webrequest +# @push.rocks/webrequest securely request from browsers -## Availabililty and Links -* [npmjs.org (npm package)](https://www.npmjs.com/package/@pushrocks/webrequest) -* [gitlab.com (source)](https://gitlab.com/pushrocks/webrequest) -* [github.com (source mirror)](https://github.com/pushrocks/webrequest) -* [docs (typedoc)](https://pushrocks.gitlab.io/webrequest/) +## Install -## Status for master +To use `@push.rocks/webrequest` in your project, install it using npm or yarn: -Status Category | Status Badge --- | -- -GitLab Pipelines | [![pipeline status](https://gitlab.com/pushrocks/webrequest/badges/master/pipeline.svg)](https://lossless.cloud) -GitLab Pipline Test Coverage | [![coverage report](https://gitlab.com/pushrocks/webrequest/badges/master/coverage.svg)](https://lossless.cloud) -npm | [![npm downloads per month](https://badgen.net/npm/dy/@pushrocks/webrequest)](https://lossless.cloud) -Snyk | [![Known Vulnerabilities](https://badgen.net/snyk/pushrocks/webrequest)](https://lossless.cloud) -TypeScript Support | [![TypeScript](https://badgen.net/badge/TypeScript/>=%203.x/blue?icon=typescript)](https://lossless.cloud) -node Support | [![node](https://img.shields.io/badge/node->=%2010.x.x-blue.svg)](https://nodejs.org/dist/latest-v10.x/docs/api/) -Code Style | [![Code Style](https://badgen.net/badge/style/prettier/purple)](https://lossless.cloud) -PackagePhobia (total standalone install weight) | [![PackagePhobia](https://badgen.net/packagephobia/install/@pushrocks/webrequest)](https://lossless.cloud) -PackagePhobia (package size on registry) | [![PackagePhobia](https://badgen.net/packagephobia/publish/@pushrocks/webrequest)](https://lossless.cloud) -BundlePhobia (total size when bundled) | [![BundlePhobia](https://badgen.net/bundlephobia/minzip/@pushrocks/webrequest)](https://lossless.cloud) +```bash +npm install @push.rocks/webrequest --save +# or with yarn +yarn add @push.rocks/webrequest +``` + +This package is designed to be used in an environment where ECMAScript Modules (ESM) and TypeScript are supported. ## Usage -Use TypeScript for best in class intellisense. +`@push.rocks/webrequest` is a powerful module designed to simplify making web requests securely from browsers. It leverages modern JavaScript features and TypeScript for a type-safe development experience. Below are comprehensive examples demonstrating how to utilize the module effectively: -## Contribution +### Setting Up -We are always happy for code contributions. If you are not the code contributing type that is ok. Still, maintaining Open Source repositories takes considerable time and thought. If you like the quality of what we do and our modules are useful to you we would appreciate a little monthly contribution: You can [contribute one time](https://lossless.link/contribute-onetime) or [contribute monthly](https://lossless.link/contribute). :) +First, import `WebRequest` from the module: -For further information read the linked docs at the top of this readme. +```typescript +import { WebRequest } from '@push.rocks/webrequest'; +``` -## Legal -> MIT licensed | **©** [Task Venture Capital GmbH](https://task.vc) -| By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy) +Create an instance of `WebRequest`. You can optionally pass configuration options: + +```typescript +const webRequest = new WebRequest({ + logging: true // Optional: enables logging, defaults to true +}); +``` + +### Making GET Requests + +To fetch JSON data: + +```typescript +// Fetch JSON data using GET request +async function fetchJsonData() { + const url = 'https://api.example.com/data'; + try { + const jsonData = await webRequest.getJson(url); + console.log(jsonData); + } catch (error) { + console.error(error); + } +} + +fetchJsonData(); +``` + +### POST, PUT, and DELETE Requests + +Similarly, you can make POST, PUT, and DELETE requests to send or manipulate data: + +```typescript +// Example POST request to submit JSON data +async function postJsonData() { + const url = 'https://api.example.com/submit'; + const data = { key: 'value' }; + + try { + const result = await webRequest.postJson(url, data); + console.log(result); + } catch (error) { + console.error(error); + } +} + +postJsonData(); + +// PUT and DELETE can be similarly used +``` + +### Using Caches + +The library provides mechanisms to cache responses, which is useful for reducing network load and improving performance. Here’s how to fetch data with caching: + +```typescript +// Fetch with caching enabled +async function fetchDataWithCache() { + const url = 'https://api.example.com/data'; + try { + // The second parameter enables caching + const jsonData = await webRequest.getJson(url, true); + console.log(jsonData); + } catch (error) { + console.error(error); + } +} + +fetchDataWithCache(); +``` + +### Handling Multiple Endpoints + +`@push.rocks/webrequest` supports querying multiple endpoints with fallbacks to handle the situation where some endpoints may fail or be unavailable: + +```typescript +// Attempt to request from multiple endpoints +async function requestFromMultipleEndpoints() { + const endpoints = [ + 'https://api.primary-example.com/data', + 'https://api.backup-example.com/data' + ]; + try { + const response = await webRequest.requestMultiEndpoint(endpoints, { + method: 'GET' + }); + const data = await response.json(); + console.log(data); + } catch (error) { + console.error('Failed to retrieve data from any endpoint', error); + } +} + +requestFromMultipleEndpoints(); +``` + +### Advanced Usage + +For advanced scenarios, you can directly use the `request` method to fully customize the request options including headers, request method, and body (for POST/PUT requests): + +```typescript +// Custom request with timeout +async function customRequest() { + const url = 'https://api.example.com/advanced'; + try { + const response = await webRequest.request(url, { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ custom: 'data' }), + timeoutMs: 10000 // Timeout in milliseconds + }); + if (response.ok) { + const result = await response.json(); + console.log(result); + } else { + console.error('Response error:', response.status); + } + } catch (error) { + console.error('Request failed:', error); + } +} + +customRequest(); +``` + +### Conclusion + +`@push.rocks/webrequest` offers a streamlined, secure way to handle web requests from browsers, supporting various HTTP methods, response caching, and requests to multiple endpoints with fault tolerance. Its TypeScript integration ensures type safety and enhances developer productivity by enabling IntelliSense in supported IDEs. + + + + + + +## 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. + +### 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 hello@task.vc. + +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.