Compare commits
41 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 |
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
|
128
.gitlab-ci.yml
128
.gitlab-ci.yml
@ -1,128 +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
|
||||
|
||||
before_script:
|
||||
- pnpm install -g pnpm
|
||||
- pnpm install -g @shipzone/npmci
|
||||
- npmci npm prepare
|
||||
|
||||
# ====================
|
||||
# security stage
|
||||
# ====================
|
||||
# ====================
|
||||
# security stage
|
||||
# ====================
|
||||
auditProductionDependencies:
|
||||
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||
stage: security
|
||||
script:
|
||||
- npmci command npm config set registry https://registry.npmjs.org
|
||||
- npmci command pnpm audit --audit-level=high --prod
|
||||
tags:
|
||||
- lossless
|
||||
- docker
|
||||
allow_failure: true
|
||||
|
||||
auditDevDependencies:
|
||||
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||
stage: security
|
||||
script:
|
||||
- npmci command npm config set registry https://registry.npmjs.org
|
||||
- npmci command pnpm audit --audit-level=high --dev
|
||||
tags:
|
||||
- lossless
|
||||
- docker
|
||||
allow_failure: true
|
||||
|
||||
# ====================
|
||||
# test stage
|
||||
# ====================
|
||||
|
||||
testStable:
|
||||
stage: test
|
||||
script:
|
||||
- npmci node install stable
|
||||
- npmci npm install
|
||||
- npmci npm test
|
||||
coverage: /\d+.?\d+?\%\s*coverage/
|
||||
tags:
|
||||
- docker
|
||||
|
||||
testBuild:
|
||||
stage: test
|
||||
script:
|
||||
- npmci node install stable
|
||||
- npmci npm install
|
||||
- npmci npm build
|
||||
coverage: /\d+.?\d+?\%\s*coverage/
|
||||
tags:
|
||||
- docker
|
||||
|
||||
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
|
||||
only:
|
||||
- tags
|
||||
script:
|
||||
- npmci command npm install -g typescript
|
||||
- npmci npm prepare
|
||||
- npmci npm install
|
||||
tags:
|
||||
- lossless
|
||||
- docker
|
||||
- priv
|
||||
|
||||
trigger:
|
||||
stage: metadata
|
||||
script:
|
||||
- npmci trigger
|
||||
only:
|
||||
- tags
|
||||
tags:
|
||||
- lossless
|
||||
- docker
|
||||
- notpriv
|
||||
|
||||
pages:
|
||||
stage: metadata
|
||||
script:
|
||||
- npmci node install stable
|
||||
- npmci npm install
|
||||
- npmci command npm run buildDocs
|
||||
tags:
|
||||
- lossless
|
||||
- docker
|
||||
- notpriv
|
||||
only:
|
||||
- tags
|
||||
artifacts:
|
||||
expire_in: 1 week
|
||||
paths:
|
||||
- public
|
||||
allow_failure: true
|
@ -6,12 +6,28 @@
|
||||
"gitzone": {
|
||||
"projectType": "npm",
|
||||
"module": {
|
||||
"githost": "gitlab.com",
|
||||
"gitscope": "pushrocks",
|
||||
"githost": "code.foss.global",
|
||||
"gitscope": "push.rocks",
|
||||
"gitrepo": "webrequest",
|
||||
"description": "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"
|
||||
}
|
||||
}
|
53
package.json
53
package.json
@ -1,8 +1,8 @@
|
||||
{
|
||||
"name": "@pushrocks/webrequest",
|
||||
"version": "3.0.19",
|
||||
"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",
|
||||
@ -14,21 +14,20 @@
|
||||
"buildDocs": "tsdoc"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@gitzone/tsbuild": "^2.1.65",
|
||||
"@gitzone/tsbundle": "^2.0.7",
|
||||
"@gitzone/tsrun": "^1.2.39",
|
||||
"@gitzone/tstest": "^1.0.73",
|
||||
"@pushrocks/smartexpress": "^4.0.21",
|
||||
"@pushrocks/tapbundle": "^5.0.4",
|
||||
"@types/node": "^18.14.6"
|
||||
"@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": {
|
||||
"@adobe/fetch": "^4.0.4",
|
||||
"@pushrocks/smartdelay": "^2.0.13",
|
||||
"@pushrocks/smartenv": "^5.0.2",
|
||||
"@pushrocks/smartjson": "^5.0.1",
|
||||
"@pushrocks/smartpromise": "^3.1.7",
|
||||
"@pushrocks/webstore": "^2.0.5"
|
||||
"@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/**/*",
|
||||
@ -44,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"
|
||||
}
|
||||
}
|
3978
pnpm-lock.yaml
generated
3978
pnpm-lock.yaml
generated
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,37 +1,178 @@
|
||||
# @pushrocks/webrequest
|
||||
# @push.rocks/webrequest
|
||||
securely request from browsers
|
||||
|
||||
## Availabililty and Links
|
||||
* [npmjs.org (npm package)](https://www.npmjs.com/package/@pushrocks/webrequest)
|
||||
* [gitlab.com (source)](https://gitlab.com/pushrocks/webrequest)
|
||||
* [github.com (source mirror)](https://github.com/pushrocks/webrequest)
|
||||
* [docs (typedoc)](https://pushrocks.gitlab.io/webrequest/)
|
||||
## Install
|
||||
|
||||
## Status for master
|
||||
To use `@push.rocks/webrequest` in your project, install it using npm or yarn:
|
||||
|
||||
Status Category | Status Badge
|
||||
-- | --
|
||||
GitLab Pipelines | [](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)
|
||||
```bash
|
||||
npm install @push.rocks/webrequest --save
|
||||
# or with yarn
|
||||
yarn add @push.rocks/webrequest
|
||||
```
|
||||
|
||||
This package is designed to be used in an environment where ECMAScript Modules (ESM) and TypeScript are supported.
|
||||
|
||||
## Usage
|
||||
|
||||
Use TypeScript for best in class intellisense.
|
||||
`@push.rocks/webrequest` is a powerful module designed to simplify making web requests securely from browsers. It leverages modern JavaScript features and TypeScript for a type-safe development experience. Below are comprehensive examples demonstrating how to utilize the module effectively:
|
||||
|
||||
## Contribution
|
||||
### Setting Up
|
||||
|
||||
We are always happy for code contributions. If you are not the code contributing type that is ok. Still, maintaining Open Source repositories takes considerable time and thought. If you like the quality of what we do and our modules are useful to you we would appreciate a little monthly contribution: You can [contribute one time](https://lossless.link/contribute-onetime) or [contribute monthly](https://lossless.link/contribute). :)
|
||||
First, import `WebRequest` from the module:
|
||||
|
||||
For further information read the linked docs at the top of this readme.
|
||||
```typescript
|
||||
import { WebRequest } from '@push.rocks/webrequest';
|
||||
```
|
||||
|
||||
## Legal
|
||||
> MIT licensed | **©** [Task Venture Capital GmbH](https://task.vc)
|
||||
| By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy)
|
||||
Create an instance of `WebRequest`. You can optionally pass configuration options:
|
||||
|
||||
```typescript
|
||||
const webRequest = new WebRequest({
|
||||
logging: true // Optional: enables logging, defaults to true
|
||||
});
|
||||
```
|
||||
|
||||
### Making GET Requests
|
||||
|
||||
To fetch JSON data:
|
||||
|
||||
```typescript
|
||||
// Fetch JSON data using GET request
|
||||
async function fetchJsonData() {
|
||||
const url = 'https://api.example.com/data';
|
||||
try {
|
||||
const jsonData = await webRequest.getJson(url);
|
||||
console.log(jsonData);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
}
|
||||
|
||||
fetchJsonData();
|
||||
```
|
||||
|
||||
### POST, PUT, and DELETE Requests
|
||||
|
||||
Similarly, you can make POST, PUT, and DELETE requests to send or manipulate data:
|
||||
|
||||
```typescript
|
||||
// Example POST request to submit JSON data
|
||||
async function postJsonData() {
|
||||
const url = 'https://api.example.com/submit';
|
||||
const data = { key: 'value' };
|
||||
|
||||
try {
|
||||
const result = await webRequest.postJson(url, data);
|
||||
console.log(result);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
}
|
||||
|
||||
postJsonData();
|
||||
|
||||
// PUT and DELETE can be similarly used
|
||||
```
|
||||
|
||||
### Using Caches
|
||||
|
||||
The library provides mechanisms to cache responses, which is useful for reducing network load and improving performance. Here’s how to fetch data with caching:
|
||||
|
||||
```typescript
|
||||
// Fetch with caching enabled
|
||||
async function fetchDataWithCache() {
|
||||
const url = 'https://api.example.com/data';
|
||||
try {
|
||||
// The second parameter enables caching
|
||||
const jsonData = await webRequest.getJson(url, true);
|
||||
console.log(jsonData);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
}
|
||||
|
||||
fetchDataWithCache();
|
||||
```
|
||||
|
||||
### Handling Multiple Endpoints
|
||||
|
||||
`@push.rocks/webrequest` supports querying multiple endpoints with fallbacks to handle the situation where some endpoints may fail or be unavailable:
|
||||
|
||||
```typescript
|
||||
// Attempt to request from multiple endpoints
|
||||
async function requestFromMultipleEndpoints() {
|
||||
const endpoints = [
|
||||
'https://api.primary-example.com/data',
|
||||
'https://api.backup-example.com/data'
|
||||
];
|
||||
try {
|
||||
const response = await webRequest.requestMultiEndpoint(endpoints, {
|
||||
method: 'GET'
|
||||
});
|
||||
const data = await response.json();
|
||||
console.log(data);
|
||||
} catch (error) {
|
||||
console.error('Failed to retrieve data from any endpoint', error);
|
||||
}
|
||||
}
|
||||
|
||||
requestFromMultipleEndpoints();
|
||||
```
|
||||
|
||||
### Advanced Usage
|
||||
|
||||
For advanced scenarios, you can directly use the `request` method to fully customize the request options including headers, request method, and body (for POST/PUT requests):
|
||||
|
||||
```typescript
|
||||
// Custom request with timeout
|
||||
async function customRequest() {
|
||||
const url = 'https://api.example.com/advanced';
|
||||
try {
|
||||
const response = await webRequest.request(url, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ custom: 'data' }),
|
||||
timeoutMs: 10000 // Timeout in milliseconds
|
||||
});
|
||||
if (response.ok) {
|
||||
const result = await response.json();
|
||||
console.log(result);
|
||||
} else {
|
||||
console.error('Response error:', response.status);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Request failed:', error);
|
||||
}
|
||||
}
|
||||
|
||||
customRequest();
|
||||
```
|
||||
|
||||
### Conclusion
|
||||
|
||||
`@push.rocks/webrequest` offers a streamlined, secure way to handle web requests from browsers, supporting various HTTP methods, response caching, and requests to multiple endpoints with fault tolerance. Its TypeScript integration ensures type safety and enhances developer productivity by enabling IntelliSense in supported IDEs.
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
## License and Legal Information
|
||||
|
||||
This repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository.
|
||||
|
||||
**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.
|
||||
|
||||
### Trademarks
|
||||
|
||||
This project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.
|
||||
|
||||
### Company Information
|
||||
|
||||
Task Venture Capital GmbH
|
||||
Registered at District court Bremen HRB 35230 HB, Germany
|
||||
|
||||
For any legal inquiries or if you require further information, please contact us via email at hello@task.vc.
|
||||
|
||||
By using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { expect, tap } from '@pushrocks/tapbundle';
|
||||
import { expect, tap } from '@push.rocks/tapbundle';
|
||||
import * as webrequest from '../ts/index.js';
|
||||
|
||||
tap.test('should run multiendpoint request', async (tools) => {
|
||||
|
14
test/test.ts
14
test/test.ts
@ -1,13 +1,13 @@
|
||||
import { expect, tap } from '@pushrocks/tapbundle';
|
||||
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,
|
||||
@ -15,7 +15,7 @@ tap.test('setup test server', async () => {
|
||||
|
||||
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,7 +31,7 @@ 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',
|
||||
|
@ -2,7 +2,7 @@
|
||||
* autocreated commitinfo by @pushrocks/commitinfo
|
||||
*/
|
||||
export const commitinfo = {
|
||||
name: '@pushrocks/webrequest',
|
||||
version: '3.0.19',
|
||||
description: 'securely request from browsers'
|
||||
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.'
|
||||
}
|
||||
|
68
ts/index.ts
68
ts/index.ts
@ -8,44 +8,6 @@ export interface IWebrequestContructorOptions {
|
||||
* web request
|
||||
*/
|
||||
export class WebRequest {
|
||||
private static polyfillStatusEvaluated = false;
|
||||
private static neededPolyfillsLoadedDeferred = plugins.smartpromise.defer<{
|
||||
fetch: typeof fetch;
|
||||
Response: typeof Response;
|
||||
AbortController?: typeof AbortController;
|
||||
helixTimeout?: any;
|
||||
}>();
|
||||
|
||||
/**
|
||||
* loads the needed polyfill
|
||||
* @returns
|
||||
*/
|
||||
public static async loadNeededPolyfills() {
|
||||
if (this.polyfillStatusEvaluated) {
|
||||
return this.neededPolyfillsLoadedDeferred.promise;
|
||||
}
|
||||
this.polyfillStatusEvaluated = true;
|
||||
const smartenv = new plugins.smartenv.Smartenv();
|
||||
if (!smartenv.isBrowser) {
|
||||
console.log('webrequest using node context.');
|
||||
this.polyfillStatusEvaluated = true;
|
||||
const fetchMod = await smartenv.getSafeNodeModule('@adobe/fetch');
|
||||
this.neededPolyfillsLoadedDeferred.resolve({
|
||||
fetch: fetchMod.fetch,
|
||||
Response: fetchMod.Response,
|
||||
helixTimeout: fetchMod.timeoutSignal,
|
||||
});
|
||||
} else {
|
||||
console.log('webrequest using browser context.');
|
||||
this.neededPolyfillsLoadedDeferred.resolve({
|
||||
fetch: (...args) => {
|
||||
return fetch(...args);
|
||||
},
|
||||
Response: globalThis.Response,
|
||||
AbortController: globalThis.AbortController,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public cacheStore = new plugins.webstore.WebStore({
|
||||
dbName: 'webrequest',
|
||||
@ -59,11 +21,9 @@ export class WebRequest {
|
||||
logging: true,
|
||||
...optionsArg,
|
||||
};
|
||||
WebRequest.loadNeededPolyfills();
|
||||
}
|
||||
|
||||
public async getJson(urlArg: string, useCacheArg: boolean = false) {
|
||||
await WebRequest.neededPolyfillsLoadedDeferred.promise;
|
||||
const response: Response = await this.request(urlArg, {
|
||||
method: 'GET',
|
||||
useCache: useCacheArg,
|
||||
@ -77,7 +37,6 @@ export class WebRequest {
|
||||
* postJson
|
||||
*/
|
||||
public async postJson(urlArg: string, requestBody?: any, useCacheArg: boolean = false) {
|
||||
await WebRequest.neededPolyfillsLoadedDeferred.promise;
|
||||
const response: Response = await this.request(urlArg, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
@ -95,7 +54,6 @@ export class WebRequest {
|
||||
* put js
|
||||
*/
|
||||
public async putJson(urlArg: string, requestBody?: any, useStoreAsFallback: boolean = false) {
|
||||
await WebRequest.neededPolyfillsLoadedDeferred.promise;
|
||||
const response: Response = await this.request(urlArg, {
|
||||
method: 'PUT',
|
||||
headers: {
|
||||
@ -112,7 +70,6 @@ export class WebRequest {
|
||||
* put js
|
||||
*/
|
||||
public async deleteJson(urlArg: string, useStoreAsFallback: boolean = false) {
|
||||
await WebRequest.neededPolyfillsLoadedDeferred.promise;
|
||||
const response: Response = await this.request(urlArg, {
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
@ -140,20 +97,10 @@ export class WebRequest {
|
||||
...optionsArg,
|
||||
};
|
||||
|
||||
const fetchObject = await WebRequest.neededPolyfillsLoadedDeferred.promise;
|
||||
let controller: AbortController;
|
||||
if (optionsArg.timeoutMs) {
|
||||
if (fetchObject.AbortController) {
|
||||
controller = new fetchObject.AbortController();
|
||||
plugins.smartdelay.delayFor(optionsArg.timeoutMs).then(() => {
|
||||
controller.abort();
|
||||
});
|
||||
} else {
|
||||
controller = {
|
||||
signal: fetchObject.helixTimeout(optionsArg.timeoutMs),
|
||||
} as any;
|
||||
}
|
||||
}
|
||||
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))) {
|
||||
@ -162,9 +109,8 @@ export class WebRequest {
|
||||
} else {
|
||||
cachedResponseDeferred.resolve(null);
|
||||
}
|
||||
let response: Response = await fetchObject
|
||||
.fetch(urlArg, {
|
||||
signal: controller ? controller.signal : null,
|
||||
let response: Response = await fetch(urlArg, {
|
||||
signal: controller.signal,
|
||||
method: optionsArg.method,
|
||||
headers: {
|
||||
...(optionsArg.headers || {}),
|
||||
@ -203,7 +149,7 @@ export class WebRequest {
|
||||
headers?: HeadersInit;
|
||||
}
|
||||
): Promise<Response> {
|
||||
await WebRequest.neededPolyfillsLoadedDeferred.promise;
|
||||
|
||||
let allUrls: string[];
|
||||
let usedUrlIndex = 0;
|
||||
|
||||
|
@ -1,7 +1,7 @@
|
||||
import * as smartdelay from '@pushrocks/smartdelay';
|
||||
import * as smartenv from '@pushrocks/smartenv';
|
||||
import * as smartjson from '@pushrocks/smartjson';
|
||||
import * as smartpromise from '@pushrocks/smartpromise';
|
||||
import * as webstore from '@pushrocks/webstore';
|
||||
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, smartpromise, webstore };
|
||||
|
@ -3,8 +3,12 @@
|
||||
"experimentalDecorators": true,
|
||||
"useDefineForClassFields": false,
|
||||
"target": "ES2022",
|
||||
"module": "ES2022",
|
||||
"moduleResolution": "nodenext",
|
||||
"esModuleInterop": true
|
||||
}
|
||||
"module": "NodeNext",
|
||||
"moduleResolution": "NodeNext",
|
||||
"esModuleInterop": true,
|
||||
"verbatimModuleSyntax": true
|
||||
},
|
||||
"exclude": [
|
||||
"dist_*/**/*.d.ts"
|
||||
]
|
||||
}
|
||||
|
Reference in New Issue
Block a user