• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

teableio / teable / 8421654220

25 Mar 2024 02:22PM CUT coverage: 79.934% (+53.8%) from 26.087%
8421654220

Pull #495

github

web-flow
Merge 4faeebea5 into 1869c986d
Pull Request #495: chore: add licenses for non-NPM packages

3256 of 3853 branches covered (84.51%)

25152 of 31466 relevant lines covered (79.93%)

1188.29 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

27.84
/apps/nestjs-backend/src/features/automation/actions/webhook/webhook.ts
1
import { Injectable, Logger, Scope } from '@nestjs/common';
2✔
2
import type { Almanac, Event, RuleResult } from 'json-rules-engine';
2✔
3
import { isEmpty, get } from 'lodash';
2✔
4
import fetch from 'node-fetch';
2✔
5
import type { IActionResponse, ITemplateSchema, IConstSchema, IObjectSchema } from '../action-core';
2✔
6
import { ActionCore, actionConst, ActionResponseStatus } from '../action-core';
2✔
7

2✔
8
export interface IWebhookSchema extends Record<string, unknown> {
2✔
9
  url: ITemplateSchema;
2✔
10
  method: IConstSchema;
2✔
11
  headers?: IObjectSchema;
2✔
12
  body?: ITemplateSchema;
2✔
13
  timeout?: IConstSchema;
2✔
14
  responseParams?: IObjectSchema;
2✔
15
}
2✔
16

2✔
17
export interface IWebhookOptions {
2✔
18
  url: string;
2✔
19
  method: string;
2✔
20
  headers?: Record<string, string>;
2✔
21
  body?: string;
2✔
22
  timeout?: number;
2✔
23
  responseParams?: Record<string, string>;
2✔
24
}
2✔
25

2✔
26
@Injectable({ scope: Scope.REQUEST })
2✔
27
export class Webhook extends ActionCore {
2✔
28
  private logger = new Logger(Webhook.name);
×
29

×
30
  constructor() {
×
31
    super();
×
32
  }
×
33

×
34
  bindParams(id: string, params: IWebhookSchema, priority?: number): this {
×
35
    return this.setName(id).setEvent({ type: id, params: params }).setPriority(priority);
×
36
  }
×
37

×
38
  onSuccess = async (event: Event, almanac: Almanac, _ruleResult: RuleResult): Promise<void> => {
×
39
    const {
×
40
      url,
×
41
      method,
×
42
      headers,
×
43
      body,
×
44
      timeout = 60000,
×
45
      responseParams,
×
46
    } = await this.parseInputSchema<IWebhookOptions>(event.params as IWebhookSchema, almanac);
×
47

×
48
    let outPut: IActionResponse<unknown>;
×
49

×
50
    await fetch(url, {
×
51
      method,
×
52
      headers,
×
53
      body,
×
54
      timeout,
×
55
    })
×
56
      .then((response) => response.json())
×
57
      .then((resultJson) => {
×
58
        const responseData = this.responseDataWrapper(
×
59
          resultJson as Record<string, unknown>,
×
60
          responseParams
×
61
        );
×
62
        outPut = { data: responseData, status: ActionResponseStatus.OK };
×
63
      })
×
64
      .catch((error) => {
×
65
        this.logger.error(error.message, error?.stack);
×
66
        outPut = {
×
67
          error: error.message,
×
68
          data: '',
×
69
          status: ActionResponseStatus.InternalServerError,
×
70
        };
×
71
      })
×
72
      .finally(() => {
×
73
        almanac.addRuntimeFact(`${actionConst.OutPutFlag}${this.name}`, outPut);
×
74
      });
×
75
  };
×
76

×
77
  private responseDataWrapper(
×
78
    json: Record<string, unknown>,
×
79
    responseParams?: Record<string, string>
×
80
  ) {
×
81
    let responseData: Record<string, unknown>;
×
82
    if (responseParams && !isEmpty(responseParams)) {
×
83
      // When the 'responseParams' parameter is defined, it means that custom response results are constructed.
×
84
      // The format and number of custom parameters depend on the user-defined parameter data.
×
85
      responseData = Object.entries(responseParams).reduce(
×
86
        (pre, [key, value]) => {
×
87
          pre[key] = get(json, value);
×
88
          return pre;
×
89
        },
×
90
        {} as Record<string, unknown>
×
91
      );
×
92
    } else {
×
93
      responseData = json;
×
94
    }
×
95
    return responseData;
×
96
  }
×
97
}
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc