Skip to content
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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions packages/request/LICENSE.txt
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.
127 changes: 127 additions & 0 deletions packages/request/README.md
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,
Copy link
Contributor

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

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' });
}
}
```
51 changes: 51 additions & 0 deletions packages/request/package.json
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"
}
}
26 changes: 26 additions & 0 deletions packages/request/src/common/clone.ts
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) => {
Copy link
Contributor

Choose a reason for hiding this comment

The 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) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why reduce and not map? did you copy deepClone from some package?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perhaps we should take the cloneDeep from lodash?

Copy link
Member Author

Choose a reason for hiding this comment

The 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pls, don't use @ts-ignore

newObj[key] = deepClone(obj[key]);
return newObj;
}, {});
}
};
13 changes: 13 additions & 0 deletions packages/request/src/common/http.ts
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);
3 changes: 3 additions & 0 deletions packages/request/src/common/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './clone';
export * from './http';
export * from './url';
17 changes: 17 additions & 0 deletions packages/request/src/common/url.ts
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());
};
26 changes: 26 additions & 0 deletions packages/request/src/compose/compose.ts
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[]) {
Copy link
Contributor

Choose a reason for hiding this comment

The 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

Copy link
Member Author

Choose a reason for hiding this comment

The 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.
And I think we should use this way in other modules in this repo. For example, middleware service looks very difficult but if we remove Nestjs abstraction from middleware code all ideas of that plugin will be looks like one line of code.

return (requestConfig: RequestConfig) => {
// copy object bcs we can mutate it
const internalConfig = deepClone(requestConfig) as InternalConfig;
internalConfig.attempt = 0;
Copy link
Contributor

Choose a reason for hiding this comment

The 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

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean? Can I pass this variable to the context scope?


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);
};
}
1 change: 1 addition & 0 deletions packages/request/src/compose/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './compose';
4 changes: 4 additions & 0 deletions packages/request/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from './interfaces';

export * from './middlewares';
export * from './request';
15 changes: 15 additions & 0 deletions packages/request/src/interfaces/compose.interface.ts
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>;
2 changes: 2 additions & 0 deletions packages/request/src/interfaces/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './compose.interface';
export * from './request.interface';
14 changes: 14 additions & 0 deletions packages/request/src/interfaces/request.interface.ts
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[];
}
4 changes: 4 additions & 0 deletions packages/request/src/middlewares/index.ts
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';
14 changes: 14 additions & 0 deletions packages/request/src/middlewares/logger.ts
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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;
logger.log('Start request', config);
const response = await next(config);
logger.log('End request', config);
return response;

};
12 changes: 12 additions & 0 deletions packages/request/src/middlewares/not-ok-error.ts
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;
};
Loading