Compare commits
117 Commits
Author | SHA1 | Date | |
---|---|---|---|
e228ed4ba0 | |||
9425489f4f | |||
46ef3857e9 | |||
c173fd3761 | |||
98f224e6f4 | |||
a7751006cc | |||
bb4a2a660a | |||
c32fc4c3be | |||
6d943687ad | |||
d216a5e8b9 | |||
adca345da1 | |||
ab880d704a | |||
5af1863264 | |||
b43050ff0c | |||
627fc42962 | |||
763afa62cd | |||
ee4ced4395 | |||
8a06f09405 | |||
8030851d2e | |||
a8bbec3c17 | |||
27f351c541 | |||
342d93e34d | |||
4a0f2288ca | |||
5c7f9e5bfa | |||
14bda0bfa3 | |||
4a57f7070a | |||
f36bdcb181 | |||
d3c90b0e7e | |||
2397e69572 | |||
7f7738d69f | |||
f375e36a99 | |||
0e827e508e | |||
43820d0278 | |||
98a18f4243 | |||
ba231de502 | |||
363bc1c025 | |||
f28f5b8f46 | |||
b30a7b57d3 | |||
240b2fc69a | |||
0e1da04c3c | |||
2a715e5b80 | |||
5ff76b5c9f | |||
76733cc96a | |||
c8f27a1935 | |||
3b7378386f | |||
4164458d80 | |||
9af837124a | |||
4685856ade | |||
a46af3a431 | |||
ac65f4c85f | |||
d845e19934 | |||
9bba1ef94c | |||
1e58f77142 | |||
f882b0a90f | |||
0f1240d0db | |||
089bb9282f | |||
73c10c1db8 | |||
59c0c4e60c | |||
8e079cfdfd | |||
d410517c8b | |||
c02340813f | |||
0d3f636af3 | |||
a64d47c02e | |||
e6279c00f1 | |||
8d59494a75 | |||
d1f3117376 | |||
14cca11c55 | |||
f4374942ad | |||
12dc003226 | |||
d4f022a2d9 | |||
609c3833b1 | |||
184bf11bd7 | |||
ea303b5c26 | |||
b3f6b8db6a | |||
ce9d02562f | |||
e9e974e696 | |||
ee361eba4d | |||
1d14312ccc | |||
09ac10c27c | |||
4b7a9d97f3 | |||
2dfd75aaea | |||
7e7bb5f739 | |||
06caeaf7c8 | |||
dec635f57f | |||
7d28f15f23 | |||
9c3187b2e9 | |||
adadd2c100 | |||
1bb08bfb7f | |||
11746c61bd | |||
2b6d9c961c | |||
0b53be66f2 | |||
547b151ee2 | |||
9855311739 | |||
1453abcd5f | |||
99afb573cd | |||
94360ff1da | |||
3fd2075c26 | |||
71782672d7 | |||
9dec755ea5 | |||
01cd3fbb0b | |||
c5f8e36104 | |||
752bbd74b0 | |||
467f8d3254 | |||
9ff7713103 | |||
30de4add37 | |||
434dbd718a | |||
94cb734f9f | |||
e03edc6bbf | |||
03c4340e4c | |||
f048bd407b | |||
93a43d7ff5 | |||
0d594dbe6b | |||
9a9523cc13 | |||
a8e6462f55 | |||
5dfc650f2c | |||
0a423e10cc | |||
35b3ff8021 |
66
.gitea/workflows/default_nottags.yaml
Normal file
66
.gitea/workflows/default_nottags.yaml
Normal 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
|
124
.gitea/workflows/default_tags.yaml
Normal file
124
.gitea/workflows/default_tags.yaml
Normal 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
|
16
.gitignore
vendored
16
.gitignore
vendored
@ -1,6 +1,20 @@
|
||||
.nogit/
|
||||
node_modules/
|
||||
|
||||
# artifacts
|
||||
coverage/
|
||||
public/
|
||||
pages/
|
||||
|
||||
# installs
|
||||
node_modules/
|
||||
|
||||
# caches
|
||||
.yarn/
|
||||
.cache/
|
||||
.rpt2_cache
|
||||
|
||||
# builds
|
||||
dist/
|
||||
dist_*/
|
||||
|
||||
# custom
|
125
.gitlab-ci.yml
125
.gitlab-ci.yml
@ -1,125 +0,0 @@
|
||||
# gitzone standard
|
||||
image: 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
|
||||
# ====================
|
||||
|
||||
testLTS:
|
||||
stage: test
|
||||
script:
|
||||
- npmci npm prepare
|
||||
- npmci node install lts
|
||||
- npmci npm install
|
||||
- npmci npm test
|
||||
coverage: /\d+.?\d+?\%\s*coverage/
|
||||
tags:
|
||||
- docker
|
||||
- notpriv
|
||||
|
||||
testSTABLE:
|
||||
stage: test
|
||||
script:
|
||||
- npmci npm prepare
|
||||
- npmci node install stable
|
||||
- npmci npm install
|
||||
- npmci npm test
|
||||
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
|
||||
image: docker:stable
|
||||
allow_failure: true
|
||||
services:
|
||||
- docker:stable-dind
|
||||
script:
|
||||
- export SP_VERSION=$(echo "$CI_SERVER_VERSION" | sed 's/^\([0-9]*\)\.\([0-9]*\).*/\1-\2-stable/')
|
||||
- docker run
|
||||
--env SOURCE_CODE="$PWD"
|
||||
--volume "$PWD":/code
|
||||
--volume /var/run/docker.sock:/var/run/docker.sock
|
||||
"registry.gitlab.com/gitlab-org/security-products/codequality:$SP_VERSION" /code
|
||||
artifacts:
|
||||
paths: [codeclimate.json]
|
||||
tags:
|
||||
- docker
|
||||
- priv
|
||||
|
||||
trigger:
|
||||
stage: metadata
|
||||
script:
|
||||
- npmci trigger
|
||||
only:
|
||||
- tags
|
||||
tags:
|
||||
- docker
|
||||
- notpriv
|
||||
|
||||
pages:
|
||||
image: hosttoday/ht-docker-node:npmci
|
||||
stage: metadata
|
||||
script:
|
||||
- npmci command npm install -g typedoc typescript
|
||||
- npmci npm prepare
|
||||
- npmci npm install
|
||||
- npmci command typedoc --module "commonjs" --target "ES2016" --out public/ ts/
|
||||
tags:
|
||||
- docker
|
||||
- notpriv
|
||||
only:
|
||||
- tags
|
||||
artifacts:
|
||||
expire_in: 1 week
|
||||
paths:
|
||||
- public
|
||||
allow_failure: true
|
11
.vscode/launch.json
vendored
Normal file
11
.vscode/launch.json
vendored
Normal 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
26
.vscode/settings.json
vendored
Normal 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"]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
@ -2,5 +2,32 @@
|
||||
"npmci": {
|
||||
"npmGlobalTools": [],
|
||||
"npmAccessLevel": "public"
|
||||
},
|
||||
"gitzone": {
|
||||
"projectType": "npm",
|
||||
"module": {
|
||||
"githost": "code.foss.global",
|
||||
"gitscope": "push.rocks",
|
||||
"gitrepo": "webrequest",
|
||||
"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"
|
||||
}
|
||||
}
|
||||
}
|
2431
package-lock.json
generated
2431
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
71
package.json
71
package.json
@ -1,28 +1,65 @@
|
||||
{
|
||||
"name": "@pushrocks/webrequest",
|
||||
"version": "1.0.6",
|
||||
"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.3",
|
||||
"@gitzone/tstest": "^1.0.17",
|
||||
"@pushrocks/smartexpress": "^2.0.4",
|
||||
"@pushrocks/tapbundle": "^3.0.7",
|
||||
"@types/node": "^10.12.12",
|
||||
"node-fetch": "^2.3.0",
|
||||
"tslint": "^5.11.0",
|
||||
"tslint-config-prettier": "^1.15.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.2"
|
||||
"@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_*/**/*",
|
||||
"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
5073
pnpm-lock.yaml
generated
Normal file
File diff suppressed because it is too large
Load Diff
1
readme.hints.md
Normal file
1
readme.hints.md
Normal file
@ -0,0 +1 @@
|
||||
|
178
readme.md
Normal file
178
readme.md
Normal file
@ -0,0 +1,178 @@
|
||||
# @push.rocks/webrequest
|
||||
securely request from browsers
|
||||
|
||||
## Install
|
||||
|
||||
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
|
||||
|
||||
`@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:
|
||||
|
||||
### Setting Up
|
||||
|
||||
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. 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.
|
11
test/test.browser.ts
Normal file
11
test/test.browser.ts
Normal 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();
|
108
test/test.ts
108
test/test.ts
@ -1,66 +1,78 @@
|
||||
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) => {
|
||||
res.status(429);
|
||||
res.end();
|
||||
}));
|
||||
testServer.addRoute(
|
||||
'/apiroute1',
|
||||
new typedserver.servertools.Handler('GET', (req, res) => {
|
||||
res.status(429);
|
||||
res.end();
|
||||
})
|
||||
);
|
||||
|
||||
testServer.addRoute('/apiroute2', new smartexpress.Handler("GET", (req, res) => {
|
||||
res.status(500);
|
||||
res.end();
|
||||
}));
|
||||
testServer.addRoute(
|
||||
'/apiroute2',
|
||||
new typedserver.servertools.Handler('GET', (req, res) => {
|
||||
res.status(500);
|
||||
res.end();
|
||||
})
|
||||
);
|
||||
|
||||
testServer.addRoute('/apiroute3', new smartexpress.Handler("GET", (req, res) => {
|
||||
res.status(200);
|
||||
res.send({
|
||||
hithere: 'hi'
|
||||
});
|
||||
}));
|
||||
testServer.addRoute(
|
||||
'/apiroute3',
|
||||
new typedserver.servertools.Handler('GET', (req, res) => {
|
||||
res.status(200);
|
||||
res.send({
|
||||
hithere: 'hi',
|
||||
});
|
||||
})
|
||||
);
|
||||
|
||||
await testServer.start()
|
||||
})
|
||||
await testServer.start();
|
||||
});
|
||||
|
||||
tap.test('first test', async (tools) => {
|
||||
const response = await (new webrequest.WebRequest()).request([
|
||||
'http://localhost:2345/apiroute1',
|
||||
'http://localhost:2345/apiroute2',
|
||||
'http://localhost:2345/apiroute4',
|
||||
'http://localhost:2345/apiroute3'
|
||||
], {
|
||||
method: 'GET'
|
||||
})
|
||||
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));
|
||||
|
||||
tap.test('tear down server', async () => {
|
||||
testServer.stop();
|
||||
})
|
||||
expect(response).toHaveProperty('hithere'); //.to.equal('hi');
|
||||
expect(response2).toHaveProperty('hithere'); //.to.equal('hi');
|
||||
});
|
||||
|
||||
tap.start()
|
||||
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
8
ts/00_commitinfo_data.ts
Normal 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.'
|
||||
}
|
181
ts/index.ts
181
ts/index.ts
@ -1,33 +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 {
|
||||
/**
|
||||
* gets json
|
||||
*/
|
||||
async getJson(url: 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',
|
||||
useCache: useCacheArg,
|
||||
});
|
||||
const responseText = await response.text();
|
||||
const responseResult = plugins.smartjson.parse(responseText);
|
||||
return responseResult;
|
||||
}
|
||||
|
||||
/**
|
||||
* postJson
|
||||
*/
|
||||
postJson() {}
|
||||
public async postJson(urlArg: string, requestBody?: any, useCacheArg: boolean = false) {
|
||||
const response: Response = await this.request(urlArg, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: plugins.smartjson.stringify(requestBody),
|
||||
useCache: useCacheArg,
|
||||
});
|
||||
const responseText = await response.text();
|
||||
const responseResult = plugins.smartjson.parse(responseText);
|
||||
return responseResult;
|
||||
}
|
||||
|
||||
/**
|
||||
* put js
|
||||
*/
|
||||
putJson() {}
|
||||
public async putJson(urlArg: string, requestBody?: any, useStoreAsFallback: boolean = false) {
|
||||
const response: Response = await this.request(urlArg, {
|
||||
method: 'PUT',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: plugins.smartjson.stringify(requestBody),
|
||||
});
|
||||
const responseText = await response.text();
|
||||
const responseResult = plugins.smartjson.parse(responseText);
|
||||
return responseResult;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* put js
|
||||
*/
|
||||
async request(
|
||||
public async deleteJson(urlArg: string, useStoreAsFallback: boolean = false) {
|
||||
const response: Response = await this.request(urlArg, {
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
method: 'GET',
|
||||
});
|
||||
const responseText = await response.text();
|
||||
const responseResult = plugins.smartjson.parse(responseText);
|
||||
return responseResult;
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
@ -40,50 +162,59 @@ export class WebRequest {
|
||||
|
||||
const requestHistory: string[] = []; // keep track of the request history
|
||||
|
||||
const doHistoryCheck = async ( // check history for a
|
||||
const doHistoryCheck = async (
|
||||
// check history for a
|
||||
historyEntryTypeArg: string
|
||||
) => {
|
||||
requestHistory.push(historyEntryTypeArg);
|
||||
if (historyEntryTypeArg === '429') {
|
||||
console.log('got 429, so waiting a little bit.')
|
||||
await plugins.smartdelay.delayFor(
|
||||
Math.floor(Math.random() * (2000 - 1000 +1)) + 1000
|
||||
); // wait between 1 and 10 seconds
|
||||
console.log('got 429, so waiting a little bit.');
|
||||
await plugins.smartdelay.delayFor(Math.floor(Math.random() * (2000 - 1000 + 1)) + 1000); // wait between 1 and 10 seconds
|
||||
}
|
||||
|
||||
let numOfHistoryType = 0;
|
||||
for (const entry of requestHistory) {
|
||||
if (entry === historyEntryTypeArg) numOfHistoryType++;
|
||||
}
|
||||
if (numOfHistoryType > (2 * allUrls.length * usedUrlIndex)) {
|
||||
if (numOfHistoryType > 2 * allUrls.length * usedUrlIndex) {
|
||||
usedUrlIndex++;
|
||||
}
|
||||
};
|
||||
|
||||
// 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,
|
||||
});
|
||||
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());
|
||||
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)
|
||||
return JSON.parse(await finalResponse.text());
|
||||
const finalResponse: Response = await doRequest(allUrls[usedUrlIndex]);
|
||||
return finalResponse;
|
||||
}
|
||||
|
||||
public log(logArg: string) {
|
||||
if (this.options.logging) {
|
||||
console.log(logArg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +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
14
tsconfig.json
Normal file
@ -0,0 +1,14 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"experimentalDecorators": true,
|
||||
"useDefineForClassFields": false,
|
||||
"target": "ES2022",
|
||||
"module": "NodeNext",
|
||||
"moduleResolution": "NodeNext",
|
||||
"esModuleInterop": true,
|
||||
"verbatimModuleSyntax": true
|
||||
},
|
||||
"exclude": [
|
||||
"dist_*/**/*.d.ts"
|
||||
]
|
||||
}
|
17
tslint.json
17
tslint.json
@ -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"
|
||||
}
|
Reference in New Issue
Block a user