@liberstudio/cloudflare-list 2.0.4 → 2.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -39,3 +39,25 @@ import { CloudflareAttacksModule } from "@liberstudio/cloudflare-list";
39
39
  })
40
40
  export class AppModule {}
41
41
  ```
42
+
43
+ ## Versione Asincrona con ConfigModule
44
+
45
+ ```typescript
46
+ import { CloudflareAttacksModule } from "@liberstudio/cloudflare-list";
47
+
48
+ @Module({
49
+ imports: [
50
+ CloudflareAttacksModule.forRootAsync({
51
+ imports: [ConfigModule],
52
+ inject: [ConfigService],
53
+ useFactory: (config: ConfigService) => ({
54
+ apiToken: config.getOrThrow<string>('CLOUDFLARE_API_TOKEN'),
55
+ accountId: config.getOrThrow<string>('CLOUDFLARE_ACCOUNT_ID'),
56
+ listId: config.getOrThrow<string>('CLOUDFLARE_LIST_ID'),
57
+ comment: config.get<string>('CLOUDFLARE_LIST_COMMENT') || 'Blocked'
58
+ })
59
+ }),
60
+ ],
61
+ })
62
+ export class AppModule {}
63
+ ```
@@ -1,10 +1,10 @@
1
- import { HttpService } from '@nestjs/axios';
2
- import type { CloudflareAttacksOptions, CloudflareResponse } from './interfaces';
1
+ import { HttpService } from "@nestjs/axios";
2
+ import type { CloudflareAttacksOptions, CloudflareResponse } from "./interfaces";
3
3
  export declare class AttacksService {
4
4
  private readonly httpService;
5
5
  private readonly options;
6
6
  private readonly logger;
7
7
  private readonly API_URL;
8
8
  constructor(httpService: HttpService, options: CloudflareAttacksOptions);
9
- updateIpList(ip: string): Promise<CloudflareResponse>;
9
+ updateIpList(ip: string): Promise<CloudflareResponse | undefined>;
10
10
  }
@@ -25,20 +25,25 @@ let AttacksService = class AttacksService {
25
25
  constructor(httpService, options) {
26
26
  this.httpService = httpService;
27
27
  this.options = options;
28
- this.logger = new common_1.Logger('AttackLogger');
28
+ this.logger = new common_1.Logger("AttackLogger");
29
29
  this.API_URL = `https://api.cloudflare.com/client/v4/accounts`;
30
30
  }
31
31
  async updateIpList(ip) {
32
+ this.logger.debug(`Options al momento della chiamata: ${JSON.stringify(this.options)}`);
33
+ if (!this.options || !this.options.apiToken) {
34
+ this.logger.error("Sincronizzazione fallita: Le opzioni Cloudflare non sono ancora pronte.");
35
+ return;
36
+ }
32
37
  const { accountId, listId, apiToken, comment } = this.options;
33
38
  const url = `${this.API_URL}/${accountId}/rules/lists/${listId}/items`;
34
39
  this.logger.error(`Aggiungo IP ${ip} alla lista Cloudflare`);
35
- const formattedIp = ip.includes('/') ? ip : `${ip}/32`;
40
+ const formattedIp = ip.includes("/") ? ip : `${ip}/32`;
36
41
  const body = [{ ip: formattedIp, comment }];
37
42
  try {
38
43
  const response = await (0, rxjs_1.firstValueFrom)(this.httpService.post(url, body, {
39
44
  headers: {
40
45
  Authorization: `Bearer ${apiToken}`,
41
- 'Content-Type': 'application/json',
46
+ "Content-Type": "application/json",
42
47
  },
43
48
  }));
44
49
  return response.data;
@@ -46,17 +51,17 @@ let AttacksService = class AttacksService {
46
51
  catch (error) {
47
52
  if (axios_2.default.isAxiosError(error)) {
48
53
  const data = error.response?.data;
49
- const message = data?.errors?.[0]?.message || 'Errore Cloudflare API';
54
+ const message = data?.errors?.[0]?.message || "Errore Cloudflare API";
50
55
  const status = error.response?.status || 500;
51
56
  throw new common_1.HttpException(message, status);
52
57
  }
53
- throw new common_1.HttpException('Internal Server Error', 500);
58
+ throw new common_1.HttpException("Internal Server Error", 500);
54
59
  }
55
60
  }
56
61
  };
57
62
  exports.AttacksService = AttacksService;
58
63
  exports.AttacksService = AttacksService = __decorate([
59
64
  (0, common_1.Injectable)(),
60
- __param(1, (0, common_1.Inject)('CLOUDFLARE_OPTIONS')),
65
+ __param(1, (0, common_1.Inject)("CLOUDFLARE_OPTIONS")),
61
66
  __metadata("design:paramtypes", [axios_1.HttpService, Object])
62
67
  ], AttacksService);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@liberstudio/cloudflare-list",
3
- "version": "2.0.4",
3
+ "version": "2.0.6",
4
4
  "description": "Modulo NestJS per gestione IP List Cloudflare",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",