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: fetch with esm #54

Open
wants to merge 1 commit 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
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ module.exports = {
transform: {
'^.+\\.ts?$': 'ts-jest',
},
moduleNameMapper: { '^node-fetch$': require.resolve('node-fetch') },
testEnvironment: 'node',
setupFiles: ['dotenv/config'],
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"build": "rollup -c",
"test": "jest --coverage --verbose",
"test:e2e": "jest --testRegex=.e2e-spec.ts",
"test:package": "./test.package.sh",
"test:package": "node --experimental-vm-modules node_modules/jest/bin/jest.js --verbose packages/fetch/test/fetch.spec.ts",
"test:watch": "jest --watch --coverage --verbose",
"lint": "eslint --ext ts .",
"typechain": "typechain --target=ethers-v5 --out-dir ./packages/contracts/src/generated ./packages/contracts/abi/*.json",
Expand Down
7 changes: 7 additions & 0 deletions packages/dynamic-esm/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"env": {
"es2021": true,
"node": true
},
"extends": ["eslint:recommended"]
}
21 changes: 21 additions & 0 deletions packages/dynamic-esm/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.
163 changes: 163 additions & 0 deletions packages/dynamic-esm/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
# Fetch

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/fetch
```

## Usage

### Basic usage

```ts
// Import
import { Module } from '@nestjs/common';
import { FetchModule } from '@lido-nestjs/fetch';
import { MyService } from './my.service';

@Module({
imports: [FetchModule.forFeature()],
providers: [MyService],
exports: [MyService],
})
export class MyModule {}

// Usage
import { FetchService } from '@lido-nestjs/fetch';

export class MyService {
constructor(private fetchService: FetchService) {}

async myFetch() {
return await this.fetchService.fetchJson('/url');
}
}
```

The `fetchService` provides 2 methods: `fetchJson` and `fetchText`, 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 { FetchModule } from '@lido-nestjs/fetch';

@Module({
imports: [FetchModule.forRoot()],
})
export class MyModule {}
```

### Async usage

```ts
import { Module } from '@nestjs/common';
import { FetchModule } from '@lido-nestjs/fetch';
import { ConfigModule, ConfigService } from './my.service';

@Module({
imports: [
ConfigModule,
FetchModule.forRootAsync({
async useFactory(configService: ConfigService) {
return { baseUrls: configService.baseUrls };
},
inject: [ConfigService],
}),
],
})
export class MyModule {}
```

### Module options

The `forRoot` and `forFeature` methods have the same options:

```ts
export interface FetchModuleOptions {
baseUrls?: string[];
retryPolicy?: RequestRetryPolicy;
}

export interface RequestRetryPolicy {
delay?: number;
attempts?: number;
}
```

| Option | Default | Desc |
| -------- | ------- | --------------------------------------- |
| baseUrls | [] | Array of base API URLs |
| delay | 1000 | Number of milliseconds between attempts |
| attempts | 0 | Number of times the query is retried |

#### Example

```ts
// Import
import { Module } from '@nestjs/common';
import { FetchModule } from '@lido-nestjs/fetch';
import { MyService } from './my.service';

@Module({
imports: [
FetchModule.forFeature({
baseUrls: ['https://my-api.com', 'https://my-fallback-api.com'],
retryPolicy: {
delay: 2000,
attempts: 3,
},
}),
],
providers: [MyService],
exports: [MyService],
})
export class MyModule {}

// Usage
import { FetchService } from '@lido-nestjs/fetch';

export class MyService {
constructor(private fetchService: FetchService) {}

async myFetch() {
return await this.fetchService.fetchJson('/foo');
}
}
```

If the provided API services are unavailable, the following happens:

- request to https://my-api.com/foo
- 2000 ms delay
- request to https://my-fallback-api.com/foo
- 2000 ms delay
- request to https://my-api.com/foo
- throw exception

### Local options

The `retryPolicy` for each query can be rewritten:

```ts
import { FetchService } from '@lido-nestjs/fetch';

export class MyService {
constructor(private fetchService: FetchService) {}

async myFetch() {
return await this.fetchService.fetchJson('/foo', {
retryPolicy: {
delay: 2000,
attempts: 3,
},
});
}
}
```
32 changes: 32 additions & 0 deletions packages/dynamic-esm/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"name": "@lido-nestjs/dynamic-esm",
"version": "0.0.0-semantic-release",
"main": "src/index.js",
"types": "src/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/dynamic-esm"
},
"bugs": {
"url": "https://github.com/lidofinance/lido-nestjs-modules/issues"
},
"sideEffects": false,
"keywords": [
"lido",
"lido-nestjs",
"lido-nestjs-modules",
"lidofinance",
"dynamic-esm",
"dynamic-esm-typescript"
],
"files": [
"src/*"
],
"publishConfig": {
"registry": "https://registry.npmjs.org/",
"access": "public"
}
}
21 changes: 21 additions & 0 deletions packages/dynamic-esm/src/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export interface MinimalNodeModule {
filename: string;
}

/**
* Asynchronously import a native ESM module from within a `.ts` file compiled to CommonJS.
*
* Usage:
*
* ```
* await dynamicImport(module, 'native-esm-module');
* ```
*
* @param module node `module` object for the importing file
*/
export function dynamicImport(
importSpecifier: string,
module: MinimalNodeModule,
): Promise<any>;

export { dynamicImport as importEsm };
25 changes: 25 additions & 0 deletions packages/dynamic-esm/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const Module = require('module');
const { isAbsolute } = require('path');

exports.dynamicImport = importEsm;
exports.importEsm = importEsm;

async function importEsm(specifier, module) {
if (isAbsolute) {
return await import(specifier);
}
let resolvedPath;
try {
const req = Module.createRequire(module.filename);
try {
resolvedPath = req.resolve(Path.posix.join(specifier, 'package.json'));
} catch {
resolvedPath = req.resolve(specifier);
}
} catch {
throw new Error(
`Unable to locate module "${specifier}" relative to "${module?.filename}" using the CommonJS resolver. Consider passing an absolute path to the target module.`,
);
}
return import(resolvedPath);
}
5 changes: 3 additions & 2 deletions packages/fetch/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@
"access": "public"
},
"dependencies": {
"@lido-nestjs/dynamic-esm": "workspace:*",
"@lido-nestjs/middleware": "workspace:*",
"@types/node-fetch": "^2.5.12",
"node-fetch": "^2.6.7"
"@types/node-fetch": "^2.6.2",
"node-fetch": "^3.2.10"
},
"peerDependencies": {
"@nestjs/common": "^8.2.5",
Expand Down
24 changes: 16 additions & 8 deletions packages/fetch/src/fetch.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import fetch, { Response } from 'node-fetch';
import type * as Fetch from 'node-fetch';

import { HttpException, Inject, Injectable, Optional } from '@nestjs/common';
import { MiddlewareService } from '@lido-nestjs/middleware';
import {
Expand All @@ -12,6 +13,13 @@ import {
RequestInit,
FetchModuleOptions,
} from './interfaces/fetch.interface';
import { dynamicImport } from '@lido-nestjs/dynamic-esm';

const fetchCall = async (url: RequestInfo, init?: RequestInit) => {
const mod = (await dynamicImport('node-fetch', module))
.default as typeof Fetch.default;
return mod(url, init);
};

@Injectable()
export class FetchService {
Expand All @@ -20,7 +28,7 @@ export class FetchService {
@Inject(FETCH_GLOBAL_OPTIONS_TOKEN)
public options: FetchModuleOptions | null,

private middlewareService: MiddlewareService<Promise<Response>>,
private middlewareService: MiddlewareService<Promise<Fetch.Response>>,
) {
this.options?.middlewares?.forEach((middleware) => {
middlewareService.use(middleware);
Expand All @@ -29,7 +37,7 @@ export class FetchService {

public async fetchJson<T>(url: RequestInfo, init?: RequestInit): Promise<T> {
const response = await this.wrappedRequest(url, init);
return await response.json();
return (await response.json()) as T;
}

public async fetchText(
Expand All @@ -43,21 +51,21 @@ export class FetchService {
protected async wrappedRequest(
url: RequestInfo,
init?: RequestInit,
): Promise<Response> {
): Promise<Fetch.Response> {
return await this.middlewareService.go(() => this.request(url, init));
}

protected async request(
url: RequestInfo,
init?: RequestInit,
attempt = 0,
): Promise<Response> {
): Promise<Fetch.Response> {
attempt++;

try {
const baseUrl = this.getBaseUrl(attempt);
const fullUrl = this.getUrl(baseUrl, url);
const response = await fetch(fullUrl, init);
const response = await fetchCall(fullUrl, init);

if (!response.ok) {
const errorBody = await this.extractErrorBody(response);
Expand All @@ -81,10 +89,10 @@ export class FetchService {
}

protected async extractErrorBody(
response: Response,
response: Fetch.Response,
): Promise<string | Record<string, unknown>> {
try {
return await response.json();
return (await response.json()) as string | Record<string, unknown>;
} catch (error) {
return response.statusText;
}
Expand Down
Loading