-
Notifications
You must be signed in to change notification settings - Fork 1
feat: added http client #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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 |
|---|---|---|
| @@ -1,5 +1,65 @@ | ||
| # ts-npm-package-boilerplate | ||
| # mc-utils | ||
| this is general utilities for usage in Map Colonies project. | ||
|
|
||
| After cloning this template, please do the following: | ||
| 1. insert secrets to repo secrets for the github actions. | ||
| 2. replace every string "ts-npm-package-boilerplate" with your package name. | ||
| # included components | ||
| - [http client](#http-client) | ||
|
|
||
| # usage | ||
| ## http client | ||
| this is abstract base class for sending http request with logging and request retries. | ||
|
|
||
| this class constructor requires the following parameters: | ||
| - [ILogger](#ilogger) instance. | ||
| - base url to use for all requests. | ||
| - optional name for target service to be used in logs. | ||
| - optional [retry configuration](#ihttpretryconfig). | ||
|
|
||
| the class have the following protected attributes | ||
| -```axiosOptions``` the options used by axios when sending requests | ||
|
|
||
| the class have the protected methods for sending http requests: | ||
| - ```protected async get<T>(url: string, queryParams?: Record<string, unknown>, retryConfig?: IAxiosRetryConfig): Promise<T>``` | ||
| - ```protected async post<T>(url: string, body?: unknown, queryParams?: Record<string, unknown>, retryConfig?: IAxiosRetryConfig): Promise<T>``` | ||
| - ```protected async put<T>(url: string, body?: unknown, queryParams?: Record<string, unknown>, retryConfig?: IAxiosRetryConfig): Promise<T>``` | ||
| - ```protected async delete<T>(url: string, queryParams?: Record<string, unknown>, retryConfig?: IAxiosRetryConfig): Promise<T>``` | ||
| - ```protected async head<T>(url: string, queryParams?: Record<string, unknown>, retryConfig?: IAxiosRetryConfig): Promise<T>``` | ||
| - ```protected async options<T>(url: string, queryParams?: Record<string, unknown>, retryConfig?: IAxiosRetryConfig): Promise<T>``` | ||
| - ```protected async patch<T>(url: string, body?: unknown, queryParams?: Record<string, unknown>, retryConfig?: IAxiosRetryConfig): Promise<T>``` | ||
|
|
||
| function parameters list: | ||
| - `url`: url path to send the request to (not including the base url). | ||
| - `body`: optional object to be used as request body (in relevant request types). | ||
| - `queryParams`: optional dictionary with query parameters and value. | ||
| - `retryConfig`: optional override to the class retry configuration. | ||
|
|
||
| usage example: | ||
| ```typescript | ||
| class myServiceClient extends HttpClient { | ||
| public constructor(logger: ILogger){ | ||
| super(logger,'https://myService.com','myService',{ | ||
| attempts: 3, | ||
| delay: 'exponential', | ||
| shouldResetTimeout: true | ||
| }) | ||
| } | ||
|
|
||
| public async getName(): string { | ||
| const name = await this.get<string>('api/name',{queryParam1: 'name'}); | ||
| return name; | ||
| } | ||
| } | ||
| ``` | ||
| # models | ||
| ## ILogger | ||
| logger interface | ||
| with the flowing log methods | ||
| - ``` error(message:string) ``` | ||
| - ```warn(message:string)``` | ||
| - ```info(message: string)``` | ||
| - ```debug(message: string)``` | ||
|
|
||
| ## IHttpRetryConfig | ||
| http requests retry configuration interface with the following attributes: | ||
| - ```attempts``` - the number of request to send until valid response (not 500+ status code). this value must be integer and greater then 0. | ||
| - ```delay``` - the amount of time in ms to wait between attempts (for constant delay) or ```'exponential'``` for exponential backoff. | ||
| - ```shouldResetTimeout``` boolean value to indicate if the request timeout should be for each request (true) or global for all attempts (false) | ||
This file contains hidden or 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
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.