-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbase-storage.ts
More file actions
101 lines (86 loc) · 3.5 KB
/
base-storage.ts
File metadata and controls
101 lines (86 loc) · 3.5 KB
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
import {BadRequestError, InternalServerError} from 'errors/apps-sdk-error';
import {RequestOptions} from 'types/fetch';
import {Token} from 'types/general';
import {isDefined} from 'types/guards';
import {Options} from 'types/storage';
import {fetchWrapper} from 'utils/fetch-wrapper';
import {Logger} from 'utils/logger';
export abstract class BaseStorage {
protected logger: Logger;
constructor(private readonly token: Token) {
this.logger = new Logger('Storage', { mondayInternal: true });
}
protected async storageFetchV2<T>(url: string, options: RequestOptions) {
const { method, body } = options;
const stringifiedBody = JSON.stringify(body);
if (!isDefined(method)) {
throw new InternalServerError('An error occurred');
}
const headers = {
Authorization: this.token,
'Content-Type': 'application/json',
'User-Agent': 'monday-apps-sdk'
};
let response: T | undefined;
try {
response = await fetchWrapper<T>(url, {
method,
headers,
...(body && { body: stringifiedBody })
});
} catch (error: unknown) {
this.logger.error('[storageFetch] Unexpected error occurred while communicating with storage', { error: error as Error });
throw new InternalServerError('An issue occurred while accessing storage');
}
return response as T;
}
protected async storageFetch<T>(key: string, options: RequestOptions, externalOptions?: Options) {
const { method, body } = options;
const stringifiedBody = JSON.stringify(body);
const url = this.generateCrudPath(key, externalOptions);
if (!isDefined(method)) {
throw new InternalServerError('An error occurred');
}
const headers = {
Authorization: this.token,
'Content-Type': 'application/json',
'User-Agent': 'monday-apps-sdk'
};
let response: T | undefined;
try {
response = await fetchWrapper<T>(url, {
method,
headers,
...(body && { body: stringifiedBody })
});
} catch (error: unknown) {
this.logger.error('[storageFetch] Unexpected error occurred while communicating with storage', { error: error as Error });
throw new InternalServerError('An issue occurred while accessing storage');
}
return response as T;
}
private getStorageUrl () {
const url = process.env.STORAGE_URL || 'https://apps-storage.monday.com/app_storage_api/v2';
return url;
}
private getStorageUrlV2 () {
const url = process.env.STORAGE_URL || 'https://apps-storage.monday.com/api/v2';
return url;
}
private generateCrudPath (key: string, options?: Options) {
if (!isDefined(key)) {
throw new BadRequestError('Missing key');
}
const shareGlobally = options?.shared ?? false;
const storageUrl = this.getStorageUrl();
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
const fullPath = `${storageUrl}/${key}?shareGlobally=${shareGlobally}`;
return fullPath;
}
public counterUrl () {
const storageUrl = this.getStorageUrlV2();
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
const fullPath = `${storageUrl}/operations`;
return fullPath;
}
}