aboutsummaryrefslogtreecommitdiffstats
path: root/src/models/Recipe.ts
blob: e8f11df5fd4cc9c8f383c6f3c283c6eb782beef1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import { join } from 'node:path';
import { pathExistsSync } from 'fs-extra';
import semver from 'semver';
import { DEFAULT_SERVICE_SETTINGS } from '../config';
import { ifUndefined } from '../jsUtils';

interface RecipeData {
  id: string;
  name: string;
  version: string;
  aliases?: string[];
  path: string;
  config: {
    serviceURL?: string;
    hasDirectMessages?: boolean;
    hasIndirectMessages?: boolean;
    hasNotificationSound?: boolean;
    hasTeamId?: boolean;
    hasCustomUrl?: boolean;
    hasHostedOption?: boolean;
    urlInputPrefix?: string;
    urlInputSuffix?: string;
    disablewebsecurity?: boolean;
    partition?: string;
    local?: boolean;
    message?: string;
    allowFavoritesDelineationInUnreadCount?: boolean;
  };
}

export interface IRecipe {
  id: string;
  name: string;
  description: string;
  version: string;
  aliases: string[];
  serviceURL: string;
  hasDirectMessages: boolean;
  hasIndirectMessages: boolean;
  hasNotificationSound: boolean;
  hasTeamId: boolean;
  hasCustomUrl: boolean;
  hasHostedOption: boolean;
  urlInputPrefix: string;
  urlInputSuffix: string;
  message: string;
  allowFavoritesDelineationInUnreadCount: boolean;
  disablewebsecurity: boolean;
  path: string;
  partition: string;
  local: boolean;

  // eslint-disable-next-line @typescript-eslint/ban-types
  readonly overrideUserAgent?: null | Function;
  // eslint-disable-next-line @typescript-eslint/ban-types
  readonly buildUrl?: null | Function;
  // eslint-disable-next-line @typescript-eslint/ban-types
  readonly modifyRequestHeaders?: null | Function;
  // eslint-disable-next-line @typescript-eslint/ban-types
  readonly knownCertificateHosts?: null | Function;
  readonly events?: null | ((key: string) => string);

  // TODO: [TS DEBT] Need to check if below properties are needed and where is inherited / implemented from
  author?: string[];
  hasDarkMode?: boolean;
  validateUrl?: (url: string) => boolean;
  icons?: any;
}

export default class Recipe implements IRecipe {
  id = '';

  name = '';

  description = '';

  version = '';

  // Removing this specific type will cause a typescript error
  // even while it's the exact same as the interface
  aliases: string[] = [];

  serviceURL = '';

  hasDirectMessages = DEFAULT_SERVICE_SETTINGS.hasDirectMessages;

  hasIndirectMessages = DEFAULT_SERVICE_SETTINGS.hasIndirectMessages;

  hasNotificationSound = DEFAULT_SERVICE_SETTINGS.hasNotificationSound;

  hasTeamId = DEFAULT_SERVICE_SETTINGS.hasTeamId;

  hasCustomUrl = DEFAULT_SERVICE_SETTINGS.hasCustomUrl;

  hasHostedOption = DEFAULT_SERVICE_SETTINGS.hasHostedOption;

  urlInputPrefix = '';

  urlInputSuffix = '';

  message = '';

  allowFavoritesDelineationInUnreadCount =
    DEFAULT_SERVICE_SETTINGS.allowFavoritesDelineationInUnreadCount;

  disablewebsecurity = DEFAULT_SERVICE_SETTINGS.disablewebsecurity;

  path = '';

  partition = '';

  // TODO: Is this being used?
  local = false;

  // TODO: [TS DEBT] introduced to address missing function but need to check how validateUrl is inherited / implemented in recipe
  validateUrl?: (url: string) => boolean;

  // TODO: Need to reconcile which of these are optional/mandatory
  constructor(data: RecipeData) {
    if (!data) {
      throw new Error('Recipe config not valid');
    }

    if (!data.id) {
      // Ferdium 4 recipes do not have an Id
      throw new Error(`Recipe '${data.name}' requires Id`);
    }

    if (!semver.valid(data.version)) {
      throw new Error(
        `Version ${data.version} of recipe '${data.name}' is not a valid semver version`,
      );
    }

    // from the recipe
    this.id = ifUndefined<string>(data.id, this.id);
    this.name = ifUndefined<string>(data.name, this.name);
    this.version = ifUndefined<string>(data.version, this.version);
    this.aliases = ifUndefined<string[]>(data.aliases, this.aliases);
    this.serviceURL = ifUndefined<string>(
      data.config.serviceURL,
      this.serviceURL,
    );
    this.hasDirectMessages = ifUndefined<boolean>(
      data.config.hasDirectMessages,
      this.hasDirectMessages,
    );
    this.hasIndirectMessages = ifUndefined<boolean>(
      data.config.hasIndirectMessages,
      this.hasIndirectMessages,
    );
    this.hasNotificationSound = ifUndefined<boolean>(
      data.config.hasNotificationSound,
      this.hasNotificationSound,
    );
    this.hasTeamId = ifUndefined<boolean>(
      data.config.hasTeamId,
      this.hasTeamId,
    );
    this.hasCustomUrl = ifUndefined<boolean>(
      data.config.hasCustomUrl,
      this.hasCustomUrl,
    );
    this.hasHostedOption = ifUndefined<boolean>(
      data.config.hasHostedOption,
      this.hasHostedOption,
    );
    this.urlInputPrefix = ifUndefined<string>(
      data.config.urlInputPrefix,
      this.urlInputPrefix,
    );
    this.urlInputSuffix = ifUndefined<string>(
      data.config.urlInputSuffix,
      this.urlInputSuffix,
    );
    this.disablewebsecurity = ifUndefined<boolean>(
      data.config.disablewebsecurity,
      this.disablewebsecurity,
    );
    this.local = ifUndefined<boolean>(data.config.local, this.local);
    this.message = ifUndefined<string>(data.config.message, this.message);
    this.allowFavoritesDelineationInUnreadCount = ifUndefined<boolean>(
      data.config.allowFavoritesDelineationInUnreadCount,
      this.allowFavoritesDelineationInUnreadCount,
    );

    // computed
    this.path = data.path;
    this.partition = ifUndefined<string>(data.config.partition, this.partition);
  }

  // TODO: Need to remove this if its not used anywhere
  get author(): string[] {
    return [];
  }

  get hasDarkMode(): boolean {
    return pathExistsSync(join(this.path, 'darkmode.css'));
  }
}