-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
working -> renamed from nestjs-http-promise-retry
- Loading branch information
0 parents
commit 8f4d747
Showing
19 changed files
with
3,728 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.eslintrc.js | ||
index.ts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
module.exports = { | ||
parser: '@typescript-eslint/parser', | ||
parserOptions: { | ||
project: './tsconfig.json', | ||
sourceType: 'module', | ||
}, | ||
plugins: ['@typescript-eslint/eslint-plugin'], | ||
extends: [ | ||
'plugin:@typescript-eslint/eslint-recommended', | ||
'plugin:@typescript-eslint/recommended', | ||
'prettier', | ||
], | ||
root: true, | ||
env: { | ||
node: true, | ||
jest: true, | ||
}, | ||
rules: { | ||
'@typescript-eslint/interface-name-prefix': 'off', | ||
'@typescript-eslint/explicit-function-return-type': 'off', | ||
'@typescript-eslint/no-explicit-any': 'off', | ||
'@typescript-eslint/no-use-before-define': 'off', | ||
'@typescript-eslint/no-non-null-assertion': 'off', | ||
}, | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
--- | ||
name: Bug report | ||
about: Create a report to help us improve | ||
title: '' | ||
labels: '' | ||
assignees: '' | ||
|
||
--- | ||
|
||
**Describe the bug** | ||
A clear and concise description of what the bug is. | ||
|
||
**To Reproduce** | ||
Steps to reproduce the behavior: | ||
1. Go to '...' | ||
2. Click on '....' | ||
3. Scroll down to '....' | ||
4. See error | ||
|
||
**Expected behavior** | ||
A clear and concise description of what you expected to happen. | ||
|
||
**Screenshots** | ||
If applicable, add screenshots to help explain your problem. | ||
|
||
**Desktop (please complete the following information):** | ||
- OS: [e.g. iOS] | ||
- Browser [e.g. chrome, safari] | ||
- Version [e.g. 22] | ||
|
||
**Smartphone (please complete the following information):** | ||
- Device: [e.g. iPhone6] | ||
- OS: [e.g. iOS8.1] | ||
- Browser [e.g. stock browser, safari] | ||
- Version [e.g. 22] | ||
|
||
**Additional context** | ||
Add any other context about the problem here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
--- | ||
name: Feature request | ||
about: Suggest an idea for this project | ||
title: '' | ||
labels: '' | ||
assignees: '' | ||
|
||
--- | ||
|
||
**Is your feature request related to a problem? Please describe.** | ||
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] | ||
|
||
**Describe the solution you'd like** | ||
A clear and concise description of what you want to happen. | ||
|
||
**Describe alternatives you've considered** | ||
A clear and concise description of any alternative solutions or features you've considered. | ||
|
||
**Additional context** | ||
Add any other context or screenshots about the feature request here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
name: publish-and-tag | ||
env: | ||
CI: true | ||
on: | ||
push: | ||
branches: | ||
- master | ||
tags: | ||
- "!*" | ||
jobs: | ||
publish-to-npm: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: checkout | ||
uses: actions/checkout@v2 | ||
- name: setup Node | ||
uses: actions/setup-node@v2 | ||
with: | ||
node-version: 14.x | ||
registry-url: 'https://registry.npmjs.org' | ||
- name: install | ||
run: npm install | ||
- name: test | ||
run: npm test | ||
# Publish to npm if this version is not published | ||
- name: publish | ||
run: npm run publish:npm | ||
env: | ||
NODE_AUTH_TOKEN: ${{ secrets.NPM_AUTH_TOKEN }} | ||
create-tag: | ||
runs-on: ubuntu-latest | ||
needs: publish-to-npm | ||
steps: | ||
- name: checkout | ||
uses: actions/checkout@v2 | ||
# Push tag to GitHub if package.json version's tag is not tagged | ||
- name: package-version | ||
run: node -p -e '`PACKAGE_VERSION=${require("./package.json").version}`' >> $GITHUB_ENV | ||
- name: package-version-to-git-tag | ||
uses: pkgdeps/git-tag-action@v2 | ||
with: | ||
github_token: ${{ secrets.GITHUB_TOKEN }} | ||
github_repo: ${{ github.repository }} | ||
version: ${{ env.PACKAGE_VERSION }} | ||
git_commit_sha: ${{ github.sha }} | ||
git_tag_prefix: "v" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# dependencies | ||
/node_modules | ||
|
||
# IDE | ||
/.idea | ||
/.awcache | ||
/.vscode | ||
|
||
# misc | ||
npm-debug.log | ||
.DS_Store | ||
|
||
# tests | ||
/test | ||
/coverage | ||
/.nyc_output | ||
|
||
# dist | ||
dist |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
|
||
{ | ||
"trailingComma": "all", | ||
"singleQuote": true, | ||
"arrowParens": "avoid" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2021 benhason1 | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
# nestjs-http-promise | ||
|
||
## description | ||
nestjs module that just doing little modification to the original and good **nestjs** http module. | ||
|
||
|
||
## features | ||
* axios - the most used package for http requests in npm. | ||
* promise based - most of us using the current http module and on every call do `.toPromise()` | ||
because most of the time we do not need the features of observable. | ||
* retries - one of the powerful features of observable is its ability to do in very simple way, | ||
you can do it by just adding the retry operator, this package will provide you the same feature even in easier way, | ||
just pass `{ retries: NUMBER_OF_RETRIES }` in the config of the http module. | ||
## quick start | ||
### installing | ||
Using npm: | ||
``` | ||
$ npm install nestjs-http-promise | ||
``` | ||
|
||
Using yarn: | ||
``` | ||
$ yarn add nestjs-http-promise | ||
``` | ||
|
||
### usage - just like every nest.js module | ||
import the module: | ||
```ts | ||
import { HttpModule } from 'nestjs-http-promise' | ||
|
||
@Module({ | ||
imports: [HttpModule] | ||
}) | ||
``` | ||
|
||
inject the service in the class: | ||
```ts | ||
import { HttpService } from 'nestjs-http-promise' | ||
|
||
class Demo { | ||
constructor(private readonly httpService: HttpService) {} | ||
} | ||
``` | ||
|
||
use the service: | ||
```ts | ||
public callSomeServer(): Promise<object> { | ||
return this.httpService.get('http://fakeService') | ||
} | ||
``` | ||
##configuration | ||
the service uses axios and axios-retry, so you can pass any [AxiosRequestConfig](https://github.com/axios/axios#request-config) | ||
And/Or [AxiosRetryConfig](https://github.com/softonic/axios-retry#options) | ||
just pass it in the `.register()` method as you would do in the original nestjs httpModule | ||
```ts | ||
import { HttpModule } from 'nestjs-http-promise' | ||
|
||
@Module({ | ||
imports: [HttpModule.register( | ||
{ | ||
timeout: 1000, | ||
retries: 5, | ||
... | ||
} | ||
)] | ||
}) | ||
``` | ||
|
||
### default configuration | ||
just the default config of axios-retry : https://github.com/softonic/axios-retry#options | ||
|
||
## async configuration | ||
When you need to pass module options asynchronously instead of statically, use the `registerAsync()` method **just like in nest httpModule**. | ||
|
||
you have a couple of techniques to do it: | ||
* with the useFactory | ||
```ts | ||
HttpModule.registerAsync({ | ||
useFactory: () => ({ | ||
timeout: 1000, | ||
retries: 5, | ||
... | ||
}), | ||
}); | ||
``` | ||
|
||
* using class | ||
|
||
```ts | ||
HttpModule.registerAsync({ | ||
useClass: HttpConfigService, | ||
}); | ||
``` | ||
Note that in this example, the HttpConfigService has to implement HttpModuleOptionsFactory interface as shown below. | ||
```ts | ||
@Injectable() | ||
class HttpConfigService implements HttpModuleOptionsFactory { | ||
async createHttpOptions(): Promise<HttpModuleOptions> { | ||
const configurationData = await sonmeAsyncMethod(); | ||
return { | ||
timeout: configurationData.timeout, | ||
retries: 5, | ||
... | ||
}; | ||
} | ||
} | ||
``` | ||
If you want to reuse an existing options provider instead of creating a copy inside the HttpModule, | ||
use the useExisting syntax. | ||
```ts | ||
HttpModule.registerAsync({ | ||
imports: [ConfigModule], | ||
useExisting: ConfigService, | ||
}); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './lib' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export const AXIOS_INSTANCE_TOKEN = 'AXIOS_INSTANCE_TOKEN'; | ||
export const HTTP_MODULE_ID = 'HTTP_MODULE_ID'; | ||
export const HTTP_MODULE_OPTIONS = 'HTTP_MODULE_OPTIONS'; |
Oops, something went wrong.