-
Notifications
You must be signed in to change notification settings - Fork 4
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
feat: request module #53
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2021 Lido | ||
|
||
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. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
# Request | ||
|
||
NestJS Fetch for Lido Finance projects. | ||
Part of [Lido NestJS Modules](https://github.com/lidofinance/lido-nestjs-modules/#readme) | ||
|
||
The module is based on the [node-fetch](https://www.npmjs.com/package/node-fetch) package. | ||
|
||
## Install | ||
|
||
```bash | ||
yarn add @lido-nestjs/request | ||
``` | ||
|
||
## Usage | ||
|
||
### Basic usage | ||
|
||
```ts | ||
// Import | ||
import { Module } from '@nestjs/common'; | ||
import { RequestModule } from '@lido-nestjs/request'; | ||
import { MyService } from './my.service'; | ||
|
||
@Module({ | ||
imports: [RequestModule.forFeature()], | ||
providers: [MyService], | ||
exports: [MyService], | ||
}) | ||
export class MyModule {} | ||
|
||
// Usage | ||
import { RequestService } from '@lido-nestjs/request'; | ||
|
||
export class MyService { | ||
constructor(private requestService: RequestService) {} | ||
|
||
async myFetch() { | ||
return await this.requestService.json({ url: '/url' }); | ||
} | ||
} | ||
``` | ||
|
||
The `requestService` provides 2 methods: `json` and `text`, which are based on a call to the `fetch` function followed by a call to `.json()` or `.text()`. Method arguments are compatible with the `fetch`. | ||
|
||
### Global usage | ||
|
||
```ts | ||
import { Module } from '@nestjs/common'; | ||
import { RequestModule } from '@lido-nestjs/request'; | ||
|
||
@Module({ | ||
imports: [RequestModule.forRoot()], | ||
}) | ||
export class MyModule {} | ||
``` | ||
|
||
### Async usage | ||
|
||
```ts | ||
import { Module } from '@nestjs/common'; | ||
import { RequestModule } from '@lido-nestjs/request'; | ||
import { ConfigModule, ConfigService } from './my.service'; | ||
|
||
@Module({ | ||
imports: [ | ||
ConfigModule, | ||
RequestModule.forRootAsync({ | ||
async useFactory(configService: ConfigService) { | ||
return { url: configService.url }; | ||
}, | ||
inject: [ConfigService], | ||
}), | ||
], | ||
}) | ||
export class MyModule {} | ||
``` | ||
|
||
### Middlewares | ||
|
||
Middlewares - is a main difference between request module and fetch module | ||
|
||
#### Global middlewares | ||
|
||
You can add global middlewares into the Module instance. These middlewares will be used for each request. | ||
For example this _standard_ middlewares will be repeated request on each network error 4 times and change the base url on each failure. | ||
|
||
Let's setup our module | ||
|
||
```ts | ||
import { Module } from '@nestjs/common'; | ||
import { | ||
RequestModule, | ||
repeat, | ||
rotate, | ||
notOkError, | ||
} from '@lido-nestjs/request'; | ||
import { ConfigModule, ConfigService } from './my.service'; | ||
|
||
@Module({ | ||
imports: [ | ||
ConfigModule, | ||
RequestModule.forRootAsync({ | ||
async useFactory(configService: ConfigService) { | ||
return { | ||
middlewares: [repeat(5), rotate(configService.baseUrls), notOkError], | ||
}; | ||
}, | ||
inject: [ConfigService], | ||
}), | ||
], | ||
}) | ||
export class MyModule {} | ||
``` | ||
|
||
And use it | ||
|
||
```ts | ||
import { RequestService } from '@lido-nestjs/request'; | ||
|
||
export class MyService { | ||
constructor(private requestService: RequestService) {} | ||
|
||
async myFetch() { | ||
return await this.requestService.json({ url: '/url' }); | ||
} | ||
} | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
{ | ||
"name": "@lido-nestjs/request", | ||
"version": "0.0.0-semantic-release", | ||
"main": "dist/index.js", | ||
"types": "dist/index.d.ts", | ||
"license": "MIT", | ||
"homepage": "https://github.com/lidofinance/lido-nestjs-modules", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/lidofinance/lido-nestjs-modules.git", | ||
"directory": "packages/request" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/lidofinance/lido-nestjs-modules/issues" | ||
}, | ||
"sideEffects": false, | ||
"keywords": [ | ||
"lido", | ||
"lido-nestjs", | ||
"lido-nestjs-modules", | ||
"lidofinance" | ||
], | ||
"files": [ | ||
"dist/*" | ||
], | ||
"publishConfig": { | ||
"registry": "https://registry.npmjs.org/", | ||
"access": "public" | ||
}, | ||
"dependencies": { | ||
"@lido-nestjs/middleware": "workspace:*", | ||
"@types/node-fetch": "^2.5.12", | ||
"node-fetch": "^2.6.7", | ||
"prom-client": "^14.1.0" | ||
}, | ||
"peerDependencies": { | ||
"@nestjs/common": "^8.2.5", | ||
"@nestjs/core": "^8.2.5", | ||
"reflect-metadata": "^0.1.13", | ||
"rxjs": "^7.5.2", | ||
"tslib": "^2.3.1" | ||
}, | ||
"devDependencies": { | ||
"@nestjs/common": "^8.2.5", | ||
"@nestjs/core": "^8.2.5", | ||
"@nestjs/testing": "^8.2.5", | ||
"nock": "^13.2.9", | ||
"reflect-metadata": "^0.1.13", | ||
"rxjs": "^7.5.2" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
export const deepClone = (obj: any) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it's better to move this to packages/utils |
||
if (typeof obj !== 'object' || obj === null) { | ||
return obj; | ||
} | ||
|
||
if (obj instanceof Date) { | ||
return new Date(obj.getTime()); | ||
} | ||
|
||
if (obj instanceof Array) { | ||
return obj.reduce((arr, item, i) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. perhaps we should take the cloneDeep from lodash? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I do. I took it only for prototyping. |
||
arr[i] = deepClone(item); | ||
return arr; | ||
}, []); | ||
} | ||
|
||
if (obj instanceof Object) { | ||
return Object.keys(obj).reduce((newObj, key) => { | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. pls, don't use |
||
newObj[key] = deepClone(obj[key]); | ||
return newObj; | ||
}, {}); | ||
} | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { Response, FetchError } from 'node-fetch'; | ||
import { HttpException } from '@nestjs/common'; | ||
|
||
export const extractErrorBody = async (response: Response) => { | ||
try { | ||
return await response.json(); | ||
} catch (error) { | ||
return response.statusText; | ||
} | ||
}; | ||
|
||
export const isNotServerError = (error: Error) => | ||
!(error instanceof HttpException) && !(error instanceof FetchError); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from './clone'; | ||
export * from './http'; | ||
export * from './url'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { RequestInfo } from 'node-fetch'; | ||
|
||
export const getUrl = ( | ||
baseUrl: RequestInfo | undefined, | ||
url: RequestInfo, | ||
): RequestInfo => { | ||
if (typeof url !== 'string') return url; | ||
if (baseUrl == null) return url; | ||
if (isAbsoluteUrl(url)) return url; | ||
|
||
return `${baseUrl}${url}`; | ||
}; | ||
|
||
export const isAbsoluteUrl = (url: RequestInfo): boolean => { | ||
const regexp = new RegExp('^(?:[a-z]+:)?//', 'i'); | ||
return regexp.test(url.toString()); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { InternalConfig, Middleware, RequestConfig } from '../interfaces'; | ||
import fetch, { Response } from 'node-fetch'; | ||
import { deepClone, getUrl } from '../common'; | ||
|
||
const fetchCall = ({ url, baseUrl, ...rest }: RequestConfig) => | ||
fetch(getUrl(baseUrl, url), rest); | ||
|
||
export function compose(middleware: Middleware[]) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this module is written in more of a functional style and not very similar to the architecture of nestjs modules. It would be great if we could keep to one code style There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think a better way to create this module without some framework abstraction and wrote Nestjs binding. Because we don't need to use Nestjs abstraction inside this module. This way provides more clear code and it is a very popular way described in Uncle Bob's book. |
||
return (requestConfig: RequestConfig) => { | ||
// copy object bcs we can mutate it | ||
const internalConfig = deepClone(requestConfig) as InternalConfig; | ||
internalConfig.attempt = 0; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess that in service with a middleware design we should not have this configuration on a global level, but have it in the configs of the specific middleware responsible for repeating queries There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you mean? Can I pass this variable to the |
||
|
||
async function chain( | ||
config: InternalConfig, | ||
middleware: Middleware[], | ||
): Promise<Response> { | ||
if (middleware.length === 0) return fetchCall(config); | ||
return middleware[0](config, (config) => | ||
chain(config, middleware.slice(1)), | ||
); | ||
} | ||
|
||
return chain(internalConfig, middleware); | ||
}; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './compose'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export * from './interfaces'; | ||
|
||
export * from './middlewares'; | ||
export * from './request'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { RequestInfo, RequestInit, Response } from 'node-fetch'; | ||
|
||
export interface RequestConfig extends RequestInit { | ||
url: RequestInfo; | ||
baseUrl?: RequestInfo; | ||
} | ||
|
||
export interface InternalConfig extends RequestConfig { | ||
attempt: number; | ||
} | ||
|
||
export type Middleware = ( | ||
config: InternalConfig, | ||
next: (config: InternalConfig) => Promise<Response>, | ||
) => Promise<Response>; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './compose.interface'; | ||
export * from './request.interface'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { ModuleMetadata } from '@nestjs/common'; | ||
import { Middleware, RequestConfig } from './index'; | ||
|
||
export interface RequestModuleOptions { | ||
readonly middlewares?: Middleware[]; | ||
readonly globalConfig?: RequestConfig; | ||
} | ||
export interface RequestModuleAsyncOptions | ||
extends Pick<ModuleMetadata, 'imports'> { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
useFactory: (...args: any[]) => Promise<RequestModuleOptions>; | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
inject?: any[]; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export * from './logger'; | ||
export * from './not-ok-error'; | ||
export * from './retry'; | ||
export * from './rotate'; |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,14 @@ | ||||||||||||||||||||||
import { Response } from 'node-fetch'; | ||||||||||||||||||||||
import { LoggerService } from '@nestjs/common'; | ||||||||||||||||||||||
import { Middleware } from '../interfaces'; | ||||||||||||||||||||||
|
||||||||||||||||||||||
export const logger = | ||||||||||||||||||||||
(logger: LoggerService): Middleware => | ||||||||||||||||||||||
async (config, next) => { | ||||||||||||||||||||||
let response!: Response; | ||||||||||||||||||||||
logger.log('Start request', config); | ||||||||||||||||||||||
// eslint-disable-next-line prefer-const | ||||||||||||||||||||||
response = await next(config); | ||||||||||||||||||||||
logger.log('End request', config); | ||||||||||||||||||||||
return response; | ||||||||||||||||||||||
Comment on lines
+8
to
+13
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { HttpException } from '@nestjs/common'; | ||
import { Middleware } from '../interfaces'; | ||
import { extractErrorBody } from '../common'; | ||
|
||
export const notOkError = (): Middleware => async (config, next) => { | ||
const response = await next(config); | ||
if (!response?.ok) { | ||
const errorBody = await extractErrorBody(response); | ||
throw new HttpException(errorBody, response.status); | ||
} | ||
return response; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can't find this middleware