Compare commits
91 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 |
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
|
127
.gitlab-ci.yml
127
.gitlab-ci.yml
@ -1,127 +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:
|
||||
- lossless
|
||||
- docker
|
||||
- notpriv
|
||||
|
||||
audit:
|
||||
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||
stage: security
|
||||
script:
|
||||
- npmci npm prepare
|
||||
- npmci command npm install --ignore-scripts
|
||||
- npmci command npm config set registry https://registry.npmjs.org
|
||||
- npmci command npm audit --audit-level=high
|
||||
tags:
|
||||
- lossless
|
||||
- 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:
|
||||
- lossless
|
||||
- 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:
|
||||
- lossless
|
||||
- docker
|
||||
- notpriv
|
||||
|
||||
release:
|
||||
stage: release
|
||||
script:
|
||||
- npmci node install stable
|
||||
- npmci npm publish
|
||||
only:
|
||||
- tags
|
||||
tags:
|
||||
- lossless
|
||||
- docker
|
||||
- notpriv
|
||||
|
||||
# ====================
|
||||
# metadata stage
|
||||
# ====================
|
||||
codequality:
|
||||
stage: metadata
|
||||
allow_failure: true
|
||||
script:
|
||||
- npmci command npm install -g tslint typescript
|
||||
- npmci npm prepare
|
||||
- npmci npm install
|
||||
- npmci command "tslint -c tslint.json ./ts/**/*.ts"
|
||||
tags:
|
||||
- lossless
|
||||
- docker
|
||||
- priv
|
||||
|
||||
trigger:
|
||||
stage: metadata
|
||||
script:
|
||||
- npmci trigger
|
||||
only:
|
||||
- tags
|
||||
tags:
|
||||
- lossless
|
||||
- docker
|
||||
- notpriv
|
||||
|
||||
pages:
|
||||
stage: metadata
|
||||
script:
|
||||
- npmci node install lts
|
||||
- npmci command npm install -g @gitzone/tsdoc
|
||||
- npmci npm prepare
|
||||
- npmci npm install
|
||||
- npmci command tsdoc
|
||||
tags:
|
||||
- lossless
|
||||
- docker
|
||||
- notpriv
|
||||
only:
|
||||
- tags
|
||||
artifacts:
|
||||
expire_in: 1 week
|
||||
paths:
|
||||
- public
|
||||
allow_failure: true
|
24
.vscode/launch.json
vendored
24
.vscode/launch.json
vendored
@ -2,28 +2,10 @@
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
{
|
||||
"name": "current file",
|
||||
"type": "node",
|
||||
"command": "npm test",
|
||||
"name": "Run npm test",
|
||||
"request": "launch",
|
||||
"args": [
|
||||
"${relativeFile}"
|
||||
],
|
||||
"runtimeArgs": ["-r", "@gitzone/tsrun"],
|
||||
"cwd": "${workspaceRoot}",
|
||||
"protocol": "inspector",
|
||||
"internalConsoleOptions": "openOnSessionStart"
|
||||
},
|
||||
{
|
||||
"name": "test.ts",
|
||||
"type": "node",
|
||||
"request": "launch",
|
||||
"args": [
|
||||
"test/test.ts"
|
||||
],
|
||||
"runtimeArgs": ["-r", "@gitzone/tsrun"],
|
||||
"cwd": "${workspaceRoot}",
|
||||
"protocol": "inspector",
|
||||
"internalConsoleOptions": "openOnSessionStart"
|
||||
"type": "node-terminal"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
2
.vscode/settings.json
vendored
2
.vscode/settings.json
vendored
@ -15,7 +15,7 @@
|
||||
"properties": {
|
||||
"projectType": {
|
||||
"type": "string",
|
||||
"enum": ["website", "element", "service", "npm"]
|
||||
"enum": ["website", "element", "service", "npm", "wcc"]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -6,12 +6,28 @@
|
||||
"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"
|
||||
}
|
||||
}
|
11015
package-lock.json
generated
11015
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
57
package.json
57
package.json
@ -1,32 +1,33 @@
|
||||
{
|
||||
"name": "@pushrocks/webrequest",
|
||||
"version": "2.0.11",
|
||||
"name": "@push.rocks/webrequest",
|
||||
"version": "3.0.37",
|
||||
"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",
|
||||
"author": "Lossless GmbH",
|
||||
"license": "MIT",
|
||||
"scripts": {
|
||||
"test": "(tstest test/ --web)",
|
||||
"build": "(tsbuild --web && tsbundle npm)",
|
||||
"format": "(gitzone format)"
|
||||
"build": "(tsbuild --web --allowimplicitany && tsbundle npm)",
|
||||
"buildDocs": "tsdoc"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@gitzone/tsbuild": "^2.1.24",
|
||||
"@gitzone/tsbundle": "^1.0.72",
|
||||
"@gitzone/tstest": "^1.0.38",
|
||||
"@pushrocks/smartexpress": "^3.0.73",
|
||||
"@pushrocks/tapbundle": "^3.2.7",
|
||||
"@types/node": "^14.0.19",
|
||||
"tslint": "^6.1.2",
|
||||
"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.9",
|
||||
"@pushrocks/smartenv": "^4.0.10",
|
||||
"@pushrocks/smartjson": "^4.0.5",
|
||||
"node-fetch": "^2.6.0"
|
||||
"@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/**/*",
|
||||
@ -42,5 +43,23 @@
|
||||
],
|
||||
"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 @@
|
||||
|
193
readme.md
193
readme.md
@ -1,39 +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 | [](https://lossless.cloud)
|
||||
GitLab Pipline Test Coverage | [](https://lossless.cloud)
|
||||
npm | [](https://lossless.cloud)
|
||||
Snyk | [](https://lossless.cloud)
|
||||
TypeScript Support | [](https://lossless.cloud)
|
||||
node Support | [](https://nodejs.org/dist/latest-v10.x/docs/api/)
|
||||
Code Style | [](https://lossless.cloud)
|
||||
PackagePhobia (total standalone install weight) | [](https://lossless.cloud)
|
||||
PackagePhobia (package size on registry) | [](https://lossless.cloud)
|
||||
BundlePhobia (total size when bundled) | [](https://lossless.cloud)
|
||||
Platform support | [](https://lossless.cloud) [](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';
|
||||
```
|
||||
|
||||
> MIT licensed | **©** [Lossless GmbH](https://lossless.gmbh)
|
||||
| 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:
|
||||
|
||||
[](https://maintainedby.lossless.com)
|
||||
```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.
|
||||
|
@ -1,16 +1,11 @@
|
||||
import { expect, tap } from '@pushrocks/tapbundle';
|
||||
import * as webrequest from '../ts/index';
|
||||
import { expect, tap } from '@push.rocks/tapbundle';
|
||||
import * as webrequest from '../ts/index.js';
|
||||
|
||||
tap.test('first test', async tools => {
|
||||
const done = tools.defer();
|
||||
const response = await new webrequest.WebRequest().request([
|
||||
'https://lossless.com'
|
||||
], {
|
||||
method: 'GET'
|
||||
}).catch(e => {
|
||||
done.resolve();
|
||||
tap.test('should run multiendpoint request', async (tools) => {
|
||||
const response = await new webrequest.WebRequest().request('https://api.signup.software', {
|
||||
method: 'GET',
|
||||
});
|
||||
await done.promise;
|
||||
console.log(JSON.stringify(await response.text()));
|
||||
});
|
||||
|
||||
tap.start();
|
||||
|
62
test/test.ts
62
test/test.ts
@ -1,21 +1,21 @@
|
||||
import { expect, tap } from '@pushrocks/tapbundle';
|
||||
import * as webrequest from '../ts/index';
|
||||
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();
|
||||
})
|
||||
@ -23,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();
|
||||
})
|
||||
@ -31,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',
|
||||
});
|
||||
})
|
||||
);
|
||||
@ -42,29 +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();
|
||||
|
||||
const response2 = await new webrequest.WebRequest().getJson('http://localhost:2345/apiroute3');
|
||||
|
||||
console.log(response);
|
||||
console.log(response2);
|
||||
console.log('response 1: ' + JSON.stringify(response));
|
||||
console.log('response 2: ' + JSON.stringify(response2));
|
||||
|
||||
expect(response)
|
||||
.property('hithere')
|
||||
.to.equal('hi');
|
||||
expect(response2)
|
||||
.property('hithere')
|
||||
.to.equal('hi');
|
||||
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
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.'
|
||||
}
|
145
ts/index.ts
145
ts/index.ts
@ -1,25 +1,32 @@
|
||||
import * as plugins from './webrequest.plugins';
|
||||
import * as plugins from './webrequest.plugins.js';
|
||||
|
||||
export interface IWebrequestContructorOptions {
|
||||
logging?: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* web request
|
||||
*/
|
||||
export class WebRequest {
|
||||
private static polyfillsLoaded = false;
|
||||
public static loadNeededPolyfills() {
|
||||
const smartenv = new plugins.smartenv.Smartenv();
|
||||
if (!smartenv.isBrowser && !this.polyfillsLoaded) {
|
||||
this.polyfillsLoaded = true;
|
||||
globalThis.fetch = smartenv.getSafeNodeModule('node-fetch');
|
||||
}
|
||||
|
||||
public cacheStore = new plugins.webstore.WebStore({
|
||||
dbName: 'webrequest',
|
||||
storeName: 'webrequest',
|
||||
});
|
||||
|
||||
public options: IWebrequestContructorOptions;
|
||||
|
||||
constructor(public optionsArg: IWebrequestContructorOptions = {}) {
|
||||
this.options = {
|
||||
logging: true,
|
||||
...optionsArg,
|
||||
};
|
||||
}
|
||||
|
||||
constructor() {
|
||||
WebRequest.loadNeededPolyfills();
|
||||
}
|
||||
|
||||
public async getJson(urlArg: string | string[]) {
|
||||
public async getJson(urlArg: string, useCacheArg: boolean = false) {
|
||||
const response: Response = await this.request(urlArg, {
|
||||
method: 'GET'
|
||||
method: 'GET',
|
||||
useCache: useCacheArg,
|
||||
});
|
||||
const responseText = await response.text();
|
||||
const responseResult = plugins.smartjson.parse(responseText);
|
||||
@ -29,10 +36,14 @@ export class WebRequest {
|
||||
/**
|
||||
* 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, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: plugins.smartjson.stringify(requestBody),
|
||||
method: 'POST'
|
||||
useCache: useCacheArg,
|
||||
});
|
||||
const responseText = await response.text();
|
||||
const responseResult = plugins.smartjson.parse(responseText);
|
||||
@ -42,10 +53,13 @@ export class WebRequest {
|
||||
/**
|
||||
* 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, {
|
||||
method: 'PUT',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: plugins.smartjson.stringify(requestBody),
|
||||
method: 'PUT'
|
||||
});
|
||||
const responseText = await response.text();
|
||||
const responseResult = plugins.smartjson.parse(responseText);
|
||||
@ -55,25 +69,87 @@ export class WebRequest {
|
||||
/**
|
||||
* 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',
|
||||
});
|
||||
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;
|
||||
|
||||
@ -106,30 +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');
|
||||
}
|
||||
console.log(`Getting ${urlToUse} with method ${optionsArg.method}`);
|
||||
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(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 smartenv from '@pushrocks/smartenv';
|
||||
import * as smartjson from '@pushrocks/smartjson';
|
||||
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, smartenv, smartjson };
|
||||
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