Compare commits

...

107 Commits

Author SHA1 Message Date
e228ed4ba0 update description 2024-05-29 14:17:42 +02:00
9425489f4f 3.0.37 2024-04-17 20:19:51 +02:00
46ef3857e9 fix(core): update 2024-04-17 20:19:51 +02:00
c173fd3761 3.0.36 2024-04-17 20:15:46 +02:00
98f224e6f4 fix(core): update 2024-04-17 20:15:46 +02:00
a7751006cc update tsconfig 2024-04-14 18:41:19 +02:00
bb4a2a660a update tsconfig 2024-04-01 21:42:43 +02:00
c32fc4c3be update npmextra.json: githost 2024-04-01 20:00:22 +02:00
6d943687ad update npmextra.json: githost 2024-03-30 21:49:20 +01:00
d216a5e8b9 3.0.35 2024-03-03 10:36:24 +01:00
adca345da1 fix(core): update 2024-03-03 10:36:24 +01:00
ab880d704a 3.0.34 2023-10-20 18:01:13 +02:00
5af1863264 fix(core): update 2023-10-20 18:01:12 +02:00
b43050ff0c 3.0.33 2023-08-27 16:33:11 +02:00
627fc42962 fix(core): update 2023-08-27 16:33:10 +02:00
763afa62cd 3.0.32 2023-07-27 13:59:38 +02:00
ee4ced4395 fix(core): update 2023-07-27 13:59:37 +02:00
8a06f09405 3.0.31 2023-07-27 13:59:22 +02:00
8030851d2e fix(core): update 2023-07-27 13:59:21 +02:00
a8bbec3c17 3.0.30 2023-07-10 22:48:02 +02:00
27f351c541 fix(core): update 2023-07-10 22:48:01 +02:00
342d93e34d 3.0.29 2023-07-10 22:31:55 +02:00
4a0f2288ca fix(core): update 2023-07-10 22:31:55 +02:00
5c7f9e5bfa 3.0.28 2023-07-09 17:24:53 +02:00
14bda0bfa3 fix(core): update 2023-07-09 17:24:52 +02:00
4a57f7070a 3.0.27 2023-07-09 17:20:12 +02:00
f36bdcb181 fix(core): update 2023-07-09 17:20:12 +02:00
d3c90b0e7e 3.0.26 2023-07-03 10:08:45 +02:00
2397e69572 fix(core): update 2023-07-03 10:08:44 +02:00
7f7738d69f 3.0.25 2023-07-03 10:08:16 +02:00
f375e36a99 fix(core): update 2023-07-03 10:08:16 +02:00
0e827e508e 3.0.24 2023-07-03 10:08:05 +02:00
43820d0278 fix(core): update 2023-07-03 10:08:04 +02:00
98a18f4243 3.0.23 2023-05-01 13:11:42 +02:00
ba231de502 fix(core): update 2023-05-01 13:11:41 +02:00
363bc1c025 3.0.22 2023-05-01 12:26:16 +02:00
f28f5b8f46 fix(core): update 2023-05-01 12:26:15 +02:00
b30a7b57d3 3.0.21 2023-03-07 16:04:08 +01:00
240b2fc69a fix(core): update 2023-03-07 16:04:08 +01:00
0e1da04c3c 3.0.20 2023-03-07 11:31:26 +01:00
2a715e5b80 fix(core): update 2023-03-07 11:31:25 +01:00
5ff76b5c9f 3.0.19 2023-03-07 11:30:01 +01:00
76733cc96a fix(core): update 2023-03-07 11:30:00 +01:00
c8f27a1935 3.0.18 2023-02-12 19:08:51 +01:00
3b7378386f fix(core): update 2023-02-12 19:08:50 +01:00
4164458d80 3.0.17 2023-02-12 19:07:30 +01:00
9af837124a fix(core): update 2023-02-12 19:07:30 +01:00
4685856ade 3.0.16 2023-02-12 17:54:01 +01:00
a46af3a431 fix(core): update 2023-02-12 17:54:00 +01:00
ac65f4c85f 3.0.15 2023-02-12 17:46:50 +01:00
d845e19934 fix(core): update 2023-02-12 17:46:49 +01:00
9bba1ef94c 3.0.14 2023-02-11 18:19:03 +01:00
1e58f77142 fix(core): update 2023-02-11 18:19:02 +01:00
f882b0a90f 3.0.13 2022-08-19 17:49:47 +02:00
0f1240d0db fix(core): update 2022-08-19 17:49:47 +02:00
089bb9282f 3.0.12 2022-08-05 12:21:35 +02:00
73c10c1db8 fix(core): update 2022-08-05 12:21:34 +02:00
59c0c4e60c 3.0.11 2022-08-05 11:53:42 +02:00
8e079cfdfd fix(core): update 2022-08-05 11:53:42 +02:00
d410517c8b 3.0.10 2022-08-02 18:19:27 +02:00
c02340813f fix(core): update 2022-08-02 18:19:27 +02:00
0d3f636af3 3.0.9 2022-07-28 16:23:11 +02:00
a64d47c02e fix(core): update 2022-07-28 16:23:11 +02:00
e6279c00f1 3.0.8 2022-07-27 17:32:54 +02:00
8d59494a75 fix(core): update 2022-07-27 17:32:54 +02:00
d1f3117376 3.0.7 2022-05-29 21:29:43 +02:00
14cca11c55 fix(core): update 2022-05-29 21:29:42 +02:00
f4374942ad 3.0.6 2022-05-29 20:53:16 +02:00
12dc003226 fix(core): update 2022-05-29 20:53:16 +02:00
d4f022a2d9 3.0.5 2022-05-29 20:22:42 +02:00
609c3833b1 fix(core): update 2022-05-29 20:22:42 +02:00
184bf11bd7 3.0.4 2022-05-27 19:50:25 +02:00
ea303b5c26 fix(core): update 2022-05-27 19:50:24 +02:00
b3f6b8db6a 3.0.3 2022-03-25 00:04:24 +01:00
ce9d02562f fix(core): update 2022-03-25 00:04:24 +01:00
e9e974e696 3.0.2 2022-03-24 13:20:35 +01:00
ee361eba4d fix(core): update 2022-03-24 13:20:35 +01:00
1d14312ccc 3.0.1 2022-03-16 16:34:55 +01:00
09ac10c27c fix(core): update 2022-03-16 16:34:55 +01:00
4b7a9d97f3 3.0.0 2022-03-16 16:26:31 +01:00
2dfd75aaea BREAKING CHANGE(core): switch to esm 2022-03-16 16:26:30 +01:00
7e7bb5f739 2.0.16 2022-03-16 16:23:32 +01:00
06caeaf7c8 fix(core): update 2022-03-16 16:23:32 +01:00
dec635f57f 2.0.15 2022-02-10 18:45:22 +01:00
7d28f15f23 fix(core): update 2022-02-10 18:45:22 +01:00
9c3187b2e9 2.0.14 2022-02-10 18:02:08 +01:00
adadd2c100 fix(core): update 2022-02-10 18:02:07 +01:00
1bb08bfb7f 2.0.13 2020-10-15 18:46:21 +00:00
11746c61bd fix(core): update 2020-10-15 18:46:21 +00:00
2b6d9c961c 2.0.12 2020-10-09 10:14:46 +00:00
0b53be66f2 fix(core): update 2020-10-09 10:14:45 +00:00
547b151ee2 2.0.11 2020-10-09 10:05:26 +00:00
9855311739 fix(core): update 2020-10-09 10:05:26 +00:00
1453abcd5f 2.0.10 2020-07-08 02:15:14 +00:00
99afb573cd fix(core): update 2020-07-08 02:15:14 +00:00
94360ff1da 2.0.9 2020-06-25 23:40:30 +00:00
3fd2075c26 fix(core): update 2020-06-25 23:40:29 +00:00
71782672d7 2.0.8 2020-06-25 23:35:00 +00:00
9dec755ea5 fix(core): update 2020-06-25 23:34:59 +00:00
01cd3fbb0b 2.0.7 2020-06-25 22:45:20 +00:00
c5f8e36104 fix(core): update 2020-06-25 22:45:19 +00:00
752bbd74b0 2.0.6 2019-09-27 21:20:06 +02:00
467f8d3254 fix(core): update 2019-09-27 21:20:06 +02:00
9ff7713103 2.0.5 2019-09-27 20:44:43 +02:00
30de4add37 2.0.4 2019-09-27 20:16:29 +02:00
434dbd718a fix(core): update 2019-09-27 20:16:28 +02:00
94cb734f9f 2.0.3 2019-09-27 14:05:43 +02:00
19 changed files with 5752 additions and 3172 deletions

View File

@ -0,0 +1,66 @@
name: Default (not tags)
on:
push:
tags-ignore:
- '**'
env:
IMAGE: registry.gitlab.com/hosttoday/ht-docker-node:npmci
NPMCI_COMPUTED_REPOURL: https://${{gitea.repository_owner}}:${{secrets.GITEA_TOKEN}}@gitea.lossless.digital/${{gitea.repository}}.git
NPMCI_TOKEN_NPM: ${{secrets.NPMCI_TOKEN_NPM}}
NPMCI_TOKEN_NPM2: ${{secrets.NPMCI_TOKEN_NPM2}}
NPMCI_GIT_GITHUBTOKEN: ${{secrets.NPMCI_GIT_GITHUBTOKEN}}
NPMCI_URL_CLOUDLY: ${{secrets.NPMCI_URL_CLOUDLY}}
jobs:
security:
runs-on: ubuntu-latest
continue-on-error: true
container:
image: ${{ env.IMAGE }}
steps:
- uses: actions/checkout@v3
- name: Install pnpm and npmci
run: |
pnpm install -g pnpm
pnpm install -g @shipzone/npmci
- name: Run npm prepare
run: npmci npm prepare
- name: Audit production dependencies
run: |
npmci command npm config set registry https://registry.npmjs.org
npmci command pnpm audit --audit-level=high --prod
continue-on-error: true
- name: Audit development dependencies
run: |
npmci command npm config set registry https://registry.npmjs.org
npmci command pnpm audit --audit-level=high --dev
continue-on-error: true
test:
if: ${{ always() }}
needs: security
runs-on: ubuntu-latest
container:
image: ${{ env.IMAGE }}
steps:
- uses: actions/checkout@v3
- name: Test stable
run: |
npmci node install stable
npmci npm install
npmci npm test
- name: Test build
run: |
npmci node install stable
npmci npm install
npmci npm build

View File

@ -0,0 +1,124 @@
name: Default (tags)
on:
push:
tags:
- '*'
env:
IMAGE: registry.gitlab.com/hosttoday/ht-docker-node:npmci
NPMCI_COMPUTED_REPOURL: https://${{gitea.repository_owner}}:${{secrets.GITEA_TOKEN}}@gitea.lossless.digital/${{gitea.repository}}.git
NPMCI_TOKEN_NPM: ${{secrets.NPMCI_TOKEN_NPM}}
NPMCI_TOKEN_NPM2: ${{secrets.NPMCI_TOKEN_NPM2}}
NPMCI_GIT_GITHUBTOKEN: ${{secrets.NPMCI_GIT_GITHUBTOKEN}}
NPMCI_URL_CLOUDLY: ${{secrets.NPMCI_URL_CLOUDLY}}
jobs:
security:
runs-on: ubuntu-latest
continue-on-error: true
container:
image: ${{ env.IMAGE }}
steps:
- uses: actions/checkout@v3
- name: Prepare
run: |
pnpm install -g pnpm
pnpm install -g @shipzone/npmci
npmci npm prepare
- name: Audit production dependencies
run: |
npmci command npm config set registry https://registry.npmjs.org
npmci command pnpm audit --audit-level=high --prod
continue-on-error: true
- name: Audit development dependencies
run: |
npmci command npm config set registry https://registry.npmjs.org
npmci command pnpm audit --audit-level=high --dev
continue-on-error: true
test:
if: ${{ always() }}
needs: security
runs-on: ubuntu-latest
container:
image: ${{ env.IMAGE }}
steps:
- uses: actions/checkout@v3
- name: Prepare
run: |
pnpm install -g pnpm
pnpm install -g @shipzone/npmci
npmci npm prepare
- name: Test stable
run: |
npmci node install stable
npmci npm install
npmci npm test
- name: Test build
run: |
npmci node install stable
npmci npm install
npmci npm build
release:
needs: test
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/')
runs-on: ubuntu-latest
container:
image: ${{ env.IMAGE }}
steps:
- uses: actions/checkout@v3
- name: Prepare
run: |
pnpm install -g pnpm
pnpm install -g @shipzone/npmci
npmci npm prepare
- name: Release
run: |
npmci node install stable
npmci npm publish
metadata:
needs: test
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/')
runs-on: ubuntu-latest
container:
image: ${{ env.IMAGE }}
continue-on-error: true
steps:
- uses: actions/checkout@v3
- name: Prepare
run: |
pnpm install -g pnpm
pnpm install -g @shipzone/npmci
npmci npm prepare
- name: Code quality
run: |
npmci command npm install -g typescript
npmci npm install
- name: Trigger
run: npmci trigger
- name: Build docs and upload artifacts
run: |
npmci node install stable
npmci npm install
pnpm install -g @gitzone/tsdoc
npmci command tsdoc
continue-on-error: true

4
.gitignore vendored
View File

@ -15,8 +15,6 @@ node_modules/
# builds
dist/
dist_web/
dist_serve/
dist_ts_web/
dist_*/
# custom

View File

@ -1,119 +0,0 @@
# gitzone ci_default
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
cache:
paths:
- .npmci_cache/
key: "$CI_BUILD_STAGE"
stages:
- security
- test
- release
- metadata
# ====================
# security stage
# ====================
mirror:
stage: security
script:
- npmci git mirror
tags:
- docker
- notpriv
snyk:
stage: security
script:
- npmci npm prepare
- npmci command npm install -g snyk
- npmci command npm install --ignore-scripts
- npmci command snyk test
tags:
- docker
- notpriv
# ====================
# test stage
# ====================
testStable:
stage: test
script:
- npmci npm prepare
- npmci node install stable
- npmci npm install
- npmci npm test
coverage: /\d+.?\d+?\%\s*coverage/
tags:
- docker
- priv
testBuild:
stage: test
script:
- npmci npm prepare
- npmci node install stable
- npmci npm install
- npmci command npm run build
coverage: /\d+.?\d+?\%\s*coverage/
tags:
- docker
- notpriv
release:
stage: release
script:
- npmci node install stable
- npmci npm publish
only:
- tags
tags:
- docker
- notpriv
# ====================
# metadata stage
# ====================
codequality:
stage: metadata
allow_failure: true
script:
- npmci command npm install -g tslint typescript
- npmci npm install
- npmci command "tslint -c tslint.json ./ts/**/*.ts"
tags:
- docker
- priv
trigger:
stage: metadata
script:
- npmci trigger
only:
- tags
tags:
- docker
- notpriv
pages:
image: hosttoday/ht-docker-dbase:npmci
services:
- docker:stable-dind
stage: metadata
script:
- npmci command npm install -g @gitzone/tsdoc
- npmci npm prepare
- npmci npm install
- npmci command tsdoc
tags:
- docker
- notpriv
only:
- tags
artifacts:
expire_in: 1 week
paths:
- public
allow_failure: true

11
.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,11 @@
{
"version": "0.2.0",
"configurations": [
{
"command": "npm test",
"name": "Run npm test",
"request": "launch",
"type": "node-terminal"
}
]
}

26
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,26 @@
{
"json.schemas": [
{
"fileMatch": ["/npmextra.json"],
"schema": {
"type": "object",
"properties": {
"npmci": {
"type": "object",
"description": "settings for npmci"
},
"gitzone": {
"type": "object",
"description": "settings for gitzone",
"properties": {
"projectType": {
"type": "string",
"enum": ["website", "element", "service", "npm", "wcc"]
}
}
}
}
}
}
]
}

View File

@ -4,13 +4,30 @@
"npmAccessLevel": "public"
},
"gitzone": {
"projectType": "npm",
"module": {
"githost": "gitlab.com",
"gitscope": "pushrocks",
"githost": "code.foss.global",
"gitscope": "push.rocks",
"gitrepo": "webrequest",
"shortDescription": "securely request from browsers",
"npmPackagename": "@pushrocks/webrequest",
"license": "MIT"
"description": "A module for making secure web requests from browsers with support for caching and fault tolerance.",
"npmPackagename": "@push.rocks/webrequest",
"license": "MIT",
"keywords": [
"webrequest",
"HTTP",
"secure",
"browsers",
"caching",
"fault tolerance",
"json",
"abort",
"timeout",
"multi-endpoint",
"fetch API"
]
}
},
"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"
}
}

2924
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,39 +1,65 @@
{
"name": "@pushrocks/webrequest",
"version": "2.0.2",
"name": "@push.rocks/webrequest",
"version": "3.0.37",
"private": false,
"description": "securely request from browsers",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"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",
"author": "Lossless GmbH",
"license": "MIT",
"scripts": {
"test": "(tstest test/ --web)",
"build": "(tsbuild --web)",
"format": "(gitzone format)"
"build": "(tsbuild --web --allowimplicitany && tsbundle npm)",
"buildDocs": "tsdoc"
},
"devDependencies": {
"@gitzone/tsbuild": "^2.1.17",
"@gitzone/tstest": "^1.0.24",
"@pushrocks/smartexpress": "^3.0.40",
"@pushrocks/tapbundle": "^3.0.13",
"@types/node": "^12.7.8",
"node-fetch": "^2.6.0",
"tslint": "^5.20.0",
"tslint-config-prettier": "^1.18.0"
"@api.global/typedserver": "^3.0.27",
"@git.zone/tsbuild": "^2.1.72",
"@git.zone/tsbundle": "^2.0.15",
"@git.zone/tsrun": "^1.2.46",
"@git.zone/tstest": "^1.0.88",
"@push.rocks/tapbundle": "^5.0.23",
"@types/node": "^20.12.7"
},
"dependencies": {
"@pushrocks/smartdelay": "^2.0.3"
"@push.rocks/smartdelay": "^3.0.5",
"@push.rocks/smartenv": "^5.0.12",
"@push.rocks/smartjson": "^5.0.19",
"@push.rocks/smartpromise": "^4.0.3",
"@push.rocks/webstore": "^2.0.13"
},
"files": [
"ts/*",
"ts_web/*",
"dist/*",
"dist_web/*",
"dist_ts_web/*",
"assets/*",
"ts/**/*",
"ts_web/**/*",
"dist/**/*",
"dist_*/**/*",
"dist_ts/**/*",
"dist_ts_web/**/*",
"assets/**/*",
"cli.js",
"npmextra.json",
"readme.md"
]
}
],
"browserslist": [
"last 1 chrome versions"
],
"keywords": [
"webrequest",
"HTTP",
"secure",
"browsers",
"caching",
"fault tolerance",
"json",
"abort",
"timeout",
"multi-endpoint",
"fetch API"
],
"homepage": "https://code.foss.global/push.rocks/webrequest",
"repository": {
"type": "git",
"url": "https://code.foss.global/push.rocks/webrequest.git"
}
}

5073
pnpm-lock.yaml generated Normal file

File diff suppressed because it is too large Load Diff

1
readme.hints.md Normal file
View File

@ -0,0 +1 @@

188
readme.md
View File

@ -1,26 +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
[![build status](https://gitlab.com/pushrocks/webrequest/badges/master/build.svg)](https://gitlab.com/pushrocks/webrequest/commits/master)
[![coverage report](https://gitlab.com/pushrocks/webrequest/badges/master/coverage.svg)](https://gitlab.com/pushrocks/webrequest/commits/master)
[![npm downloads per month](https://img.shields.io/npm/dm/@pushrocks/webrequest.svg)](https://www.npmjs.com/package/@pushrocks/webrequest)
[![Known Vulnerabilities](https://snyk.io/test/npm/@pushrocks/webrequest/badge.svg)](https://snyk.io/test/npm/@pushrocks/webrequest)
[![TypeScript](https://img.shields.io/badge/TypeScript->=%203.x-blue.svg)](https://nodejs.org/dist/latest-v10.x/docs/api/)
[![node](https://img.shields.io/badge/node->=%2010.x.x-blue.svg)](https://nodejs.org/dist/latest-v10.x/docs/api/)
[![JavaScript Style Guide](https://img.shields.io/badge/code%20style-prettier-ff69b4.svg)](https://prettier.io/)
To use `@push.rocks/webrequest` in your project, install it using npm or yarn:
```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
For further information read the linked docs at the top of this readme.
`@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:
> MIT licensed | **©** [Lossless GmbH](https://lossless.gmbh)
| By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy)
### Setting Up
[![repo-footer](https://lossless.gitlab.io/publicrelations/repofooter.svg)](https://maintainedby.lossless.com)
First, import `WebRequest` from the module:
```typescript
import { WebRequest } from '@push.rocks/webrequest';
```
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. Heres 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.

11
test/test.browser.ts Normal file
View File

@ -0,0 +1,11 @@
import { expect, tap } from '@push.rocks/tapbundle';
import * as webrequest from '../ts/index.js';
tap.test('should run multiendpoint request', async (tools) => {
const response = await new webrequest.WebRequest().request('https://api.signup.software', {
method: 'GET',
});
console.log(JSON.stringify(await response.text()));
});
tap.start();

View File

@ -1,31 +1,21 @@
import { expect, tap } from '@pushrocks/tapbundle';
import * as webrequest from '../ts/index';
import * as fetch from 'node-fetch';
declare global {
namespace NodeJS {
interface Global {
fetch: any;
}
}
}
global.fetch = fetch;
import { expect, tap } from '@push.rocks/tapbundle';
import * as webrequest from '../ts/index.js';
// test dependencies
import * as smartexpress from '@pushrocks/smartexpress';
import * as typedserver from '@api.global/typedserver';
let testServer: smartexpress.Server;
let testServer: typedserver.servertools.Server;
tap.test('setup test server', async () => {
testServer = new smartexpress.Server({
testServer = new typedserver.servertools.Server({
cors: false,
forceSsl: false,
port: 2345
port: 2345,
});
testServer.addRoute(
'/apiroute1',
new smartexpress.Handler('GET', (req, res) => {
new typedserver.servertools.Handler('GET', (req, res) => {
res.status(429);
res.end();
})
@ -33,7 +23,7 @@ tap.test('setup test server', async () => {
testServer.addRoute(
'/apiroute2',
new smartexpress.Handler('GET', (req, res) => {
new typedserver.servertools.Handler('GET', (req, res) => {
res.status(500);
res.end();
})
@ -41,10 +31,10 @@ tap.test('setup test server', async () => {
testServer.addRoute(
'/apiroute3',
new smartexpress.Handler('GET', (req, res) => {
new typedserver.servertools.Handler('GET', (req, res) => {
res.status(200);
res.send({
hithere: 'hi'
hithere: 'hi',
});
})
);
@ -52,23 +42,37 @@ tap.test('setup test server', async () => {
await testServer.start();
});
tap.test('first test', async tools => {
const response = await new webrequest.WebRequest().getJson([
'http://localhost:2345/apiroute1',
'http://localhost:2345/apiroute2',
'http://localhost:2345/apiroute4',
'http://localhost:2345/apiroute3'
]);
tap.test('first test', async (tools) => {
const response = await (
await new webrequest.WebRequest().requestMultiEndpoint(
[
'http://localhost:2345/apiroute1',
'http://localhost:2345/apiroute2',
'http://localhost:2345/apiroute4',
'http://localhost:2345/apiroute3',
],
{
method: 'GET',
}
)
).json();
console.log(response);
const response2 = await new webrequest.WebRequest().getJson('http://localhost:2345/apiroute3');
expect(response)
.property('hithere')
.to.equal('hi');
console.log('response 1: ' + JSON.stringify(response));
console.log('response 2: ' + JSON.stringify(response2));
expect(response).toHaveProperty('hithere'); //.to.equal('hi');
expect(response2).toHaveProperty('hithere'); //.to.equal('hi');
});
tap.test('tear down server', async () => {
testServer.stop();
tap.test('should cache response', async () => {
const webrequestInstance = new webrequest.WebRequest();
const response = await webrequestInstance.getJson('http://localhost:2345/apiroute3', true);
expect(response).toHaveProperty('hithere');
await testServer.stop();
const response2 = await webrequestInstance.getJson('http://localhost:2345/apiroute3', true);
expect(response2).toHaveProperty('hithere');
});
tap.start();

8
ts/00_commitinfo_data.ts Normal file
View File

@ -0,0 +1,8 @@
/**
* autocreated commitinfo by @pushrocks/commitinfo
*/
export const commitinfo = {
name: '@push.rocks/webrequest',
version: '3.0.37',
description: 'A module for making secure web requests from browsers with support for caching and fault tolerance.'
}

View File

@ -1,58 +1,155 @@
import * as plugins from './webrequest.plugins';
import * as plugins from './webrequest.plugins.js';
export interface IWebrequestContructorOptions {
logging?: boolean;
}
/**
* web request
*/
export class WebRequest {
public async getJson(urlArg: string | string[]) {
public cacheStore = new plugins.webstore.WebStore({
dbName: 'webrequest',
storeName: 'webrequest',
});
public options: IWebrequestContructorOptions;
constructor(public optionsArg: IWebrequestContructorOptions = {}) {
this.options = {
logging: true,
...optionsArg,
};
}
public async getJson(urlArg: string, useCacheArg: boolean = false) {
const response: Response = await this.request(urlArg, {
method: 'GET'
method: 'GET',
useCache: useCacheArg,
});
return response.json();
const responseText = await response.text();
const responseResult = plugins.smartjson.parse(responseText);
return responseResult;
}
/**
* postJson
*/
public async postJson(urlArg: string, requestBody?: any) {
public async postJson(urlArg: string, requestBody?: any, useCacheArg: boolean = false) {
const response: Response = await this.request(urlArg, {
body: requestBody,
method: 'POST'
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: plugins.smartjson.stringify(requestBody),
useCache: useCacheArg,
});
return response.json();
const responseText = await response.text();
const responseResult = plugins.smartjson.parse(responseText);
return responseResult;
}
/**
* put js
*/
public async putJson(urlArg: string, requestBody?: any) {
public async putJson(urlArg: string, requestBody?: any, useStoreAsFallback: boolean = false) {
const response: Response = await this.request(urlArg, {
body: requestBody,
method: 'PUT'
method: 'PUT',
headers: {
'Content-Type': 'application/json',
},
body: plugins.smartjson.stringify(requestBody),
});
return response.json();
const responseText = await response.text();
const responseResult = plugins.smartjson.parse(responseText);
return responseResult;
}
/**
* put js
*/
public async deleteJson(urlArg: string) {
public async deleteJson(urlArg: string, useStoreAsFallback: boolean = false) {
const response: Response = await this.request(urlArg, {
method: 'GET'
headers: {
'Content-Type': 'application/json',
},
method: 'GET',
});
return response.json();
const responseText = await response.text();
const responseResult = plugins.smartjson.parse(responseText);
return responseResult;
}
/**
* a fault tolerant request
*/
public async request(
urlArg: string,
optionsArg: {
method: 'GET' | 'POST' | 'PUT' | 'DELETE';
body?: any;
headers?: HeadersInit;
useCache?: boolean;
timeoutMs?: number;
}
) {
optionsArg = {
timeoutMs: 60000,
useCache: false,
...optionsArg,
};
let controller = new AbortController();
plugins.smartdelay.delayFor(optionsArg.timeoutMs).then(() => {
controller.abort();
});
let cachedResponseDeferred = plugins.smartpromise.defer<Response>();
let cacheUsed = false;
if (optionsArg.useCache && (await this.cacheStore.check(urlArg))) {
const responseBuffer: ArrayBuffer = await this.cacheStore.get(urlArg);
cachedResponseDeferred.resolve(new Response(responseBuffer, {}));
} else {
cachedResponseDeferred.resolve(null);
}
let response: Response = await fetch(urlArg, {
signal: controller.signal,
method: optionsArg.method,
headers: {
...(optionsArg.headers || {}),
},
body: optionsArg.body,
})
.catch(async (err) => {
if (optionsArg.useCache && (await cachedResponseDeferred.promise)) {
cacheUsed = true;
const cachedResponse = cachedResponseDeferred.promise;
return cachedResponse;
} else {
return err;
}
});
if (optionsArg.useCache && (await cachedResponseDeferred.promise) && response.status === 500) {
cacheUsed = true;
response = await cachedResponseDeferred.promise;
}
if (!cacheUsed && optionsArg.useCache && response.status < 300) {
const buffer = await response.clone().arrayBuffer();
await this.cacheStore.set(urlArg, buffer);
}
this.log(`${urlArg} answers with status: ${response.status}`);
return response;
}
/**
* a multi endpoint, fault tolerant request function
*/
public async requestMultiEndpoint(
urlArg: string | string[],
optionsArg: {
method: 'GET' | 'POST' | 'PUT' | 'DELETE';
body?: any;
headers?: HeadersInit;
}
): Promise<Response> {
let allUrls: string[];
let usedUrlIndex = 0;
@ -85,31 +182,39 @@ export class WebRequest {
};
// lets go recursive
const doRequest = async (urlToUse: string) => {
const doRequest = async (urlToUse: string): Promise<any> => {
if (!urlToUse) {
throw new Error('request failed permanently');
}
this.log(`Getting ${urlToUse} with method ${optionsArg.method}`);
const response = await fetch(urlToUse, {
method: optionsArg.method,
headers: {
'Content-Type': 'application/json'
'Content-Type': 'application/json',
...(optionsArg.headers || {}),
},
body: optionsArg.body
body: optionsArg.body,
});
console.log(`${urlToUse} answers with status: ${response.status}`);
this.log(`${urlToUse} answers with status: ${response.status}`);
if (response.status >= 200 && response.status < 300) {
return response;
} else {
// lets perform a history check to determine failed urls
await doHistoryCheck(response.status.toString());
// tslint:disable-next-line: no-return-await
return await doRequest(allUrls[usedUrlIndex]);
// lets fire the request
const result = await doRequest(allUrls[usedUrlIndex]);
return result;
}
};
const finalResponse: Response = await doRequest(urlArg[usedUrlIndex]);
console.log(finalResponse);
const finalResponse: Response = await doRequest(allUrls[usedUrlIndex]);
return finalResponse;
}
public log(logArg: string) {
if (this.options.logging) {
console.log(logArg);
}
}
}

View File

@ -1,3 +1,7 @@
import * as smartdelay from '@pushrocks/smartdelay';
import * as smartdelay from '@push.rocks/smartdelay';
import * as smartenv from '@push.rocks/smartenv';
import * as smartjson from '@push.rocks/smartjson';
import * as smartpromise from '@push.rocks/smartpromise';
import * as webstore from '@push.rocks/webstore';
export { smartdelay };
export { smartdelay, smartenv, smartjson, smartpromise, webstore };

14
tsconfig.json Normal file
View File

@ -0,0 +1,14 @@
{
"compilerOptions": {
"experimentalDecorators": true,
"useDefineForClassFields": false,
"target": "ES2022",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"esModuleInterop": true,
"verbatimModuleSyntax": true
},
"exclude": [
"dist_*/**/*.d.ts"
]
}

View File

@ -1,17 +0,0 @@
{
"extends": ["tslint:latest", "tslint-config-prettier"],
"rules": {
"semicolon": [true, "always"],
"no-console": false,
"ordered-imports": false,
"object-literal-sort-keys": false,
"member-ordering": {
"options":{
"order": [
"static-method"
]
}
}
},
"defaultSeverity": "warning"
}