context_drop 0.1.1 → 0.1.3
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.
- checksums.yaml +4 -4
- data/README.md +68 -11
- data/app/assets/javascripts/context_drop/context_drop.js +1263 -616
- data/app/controllers/context_drop/configs_controller.rb +3 -1
- data/app/javascript/context_drop/__tests__/api-client.test.ts +310 -0
- data/app/javascript/context_drop/__tests__/widget.test.ts +158 -3
- data/app/javascript/context_drop/api-client.ts +153 -48
- data/app/javascript/context_drop/element-picker.ts +894 -485
- data/app/javascript/context_drop/icons.ts +2 -0
- data/app/javascript/context_drop/screenshot.ts +7 -1
- data/app/javascript/context_drop/styles.ts +88 -39
- data/app/javascript/context_drop/types.ts +1 -0
- data/app/javascript/context_drop/widget.ts +150 -61
- data/lib/context_drop/version.rb +1 -1
- metadata +2 -1
|
@@ -6,82 +6,187 @@ import type {
|
|
|
6
6
|
|
|
7
7
|
export class ContextDropApiClient {
|
|
8
8
|
private baseUrl: string;
|
|
9
|
+
private cachedCsrfToken: string | null = null;
|
|
10
|
+
private inFlightConfigPromise: Promise<WidgetConfig> | null = null;
|
|
9
11
|
|
|
10
12
|
constructor(baseUrl: string = "/context_drop") {
|
|
11
13
|
this.baseUrl = baseUrl.replace(/\/+$/, "") || "/context_drop";
|
|
12
14
|
}
|
|
13
15
|
|
|
14
16
|
public getCsrfToken(): string | null {
|
|
17
|
+
const metaToken = this.getMetaCsrfToken();
|
|
18
|
+
if (metaToken) {
|
|
19
|
+
return metaToken;
|
|
20
|
+
}
|
|
21
|
+
return this.cachedCsrfToken;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
public setCsrfToken(token: string | null): void {
|
|
25
|
+
this.cachedCsrfToken = token ? token.trim() : null;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
private getMetaCsrfToken(): string | null {
|
|
15
29
|
if (typeof document === "undefined") return null;
|
|
16
30
|
const meta = document.querySelector('meta[name="csrf-token"]');
|
|
17
|
-
|
|
31
|
+
const content = meta ? meta.getAttribute("content") : null;
|
|
32
|
+
return content && content.trim().length > 0 ? content.trim() : null;
|
|
18
33
|
}
|
|
19
34
|
|
|
20
35
|
public async fetchConfig(): Promise<WidgetConfig> {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
36
|
+
if (this.inFlightConfigPromise) {
|
|
37
|
+
return this.inFlightConfigPromise;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
this.inFlightConfigPromise = (async () => {
|
|
41
|
+
try {
|
|
42
|
+
const response = await fetch(`${this.baseUrl}/config`, {
|
|
43
|
+
method: "GET",
|
|
44
|
+
credentials: "same-origin",
|
|
45
|
+
headers: {
|
|
46
|
+
Accept: "application/json"
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
if (!response.ok) {
|
|
51
|
+
throw new Error(`Failed to fetch ContextDrop config (${response.status})`);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const config = (await response.json()) as WidgetConfig;
|
|
55
|
+
if (config && typeof config.csrf_token === "string" && config.csrf_token.trim().length > 0) {
|
|
56
|
+
this.cachedCsrfToken = config.csrf_token.trim();
|
|
57
|
+
if (typeof document !== "undefined") {
|
|
58
|
+
const meta = document.querySelector('meta[name="csrf-token"]');
|
|
59
|
+
if (meta) {
|
|
60
|
+
meta.setAttribute("content", this.cachedCsrfToken);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
return config;
|
|
65
|
+
} finally {
|
|
66
|
+
this.inFlightConfigPromise = null;
|
|
26
67
|
}
|
|
27
|
-
});
|
|
68
|
+
})();
|
|
28
69
|
|
|
29
|
-
|
|
30
|
-
|
|
70
|
+
return this.inFlightConfigPromise;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
public async resolveCsrfToken(forceRefresh = false): Promise<string | null> {
|
|
74
|
+
if (!forceRefresh) {
|
|
75
|
+
const metaToken = this.getMetaCsrfToken();
|
|
76
|
+
if (metaToken) {
|
|
77
|
+
return metaToken;
|
|
78
|
+
}
|
|
79
|
+
if (this.cachedCsrfToken) {
|
|
80
|
+
return this.cachedCsrfToken;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
try {
|
|
85
|
+
const config = await this.fetchConfig();
|
|
86
|
+
if (config && config.csrf_token) {
|
|
87
|
+
return config.csrf_token.trim();
|
|
88
|
+
}
|
|
89
|
+
} catch {
|
|
90
|
+
// Continue and fall back to current token or null
|
|
31
91
|
}
|
|
32
92
|
|
|
33
|
-
return
|
|
93
|
+
return this.cachedCsrfToken || this.getMetaCsrfToken();
|
|
34
94
|
}
|
|
35
95
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
96
|
+
private isCsrfError(status: number, message: string): boolean {
|
|
97
|
+
if (status !== 422) return false;
|
|
98
|
+
return /csrf|authenticity/i.test(message);
|
|
99
|
+
}
|
|
39
100
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
101
|
+
private async executeWithCsrfRetry<T>(
|
|
102
|
+
requestFn: (csrfToken: string | null) => Promise<Response>,
|
|
103
|
+
defaultErrorMessage: string
|
|
104
|
+
): Promise<T> {
|
|
105
|
+
let csrfToken = await this.resolveCsrfToken(false);
|
|
106
|
+
let response = await requestFn(csrfToken);
|
|
107
|
+
|
|
108
|
+
if (response.ok) {
|
|
109
|
+
return (await response.json()) as T;
|
|
46
110
|
}
|
|
47
111
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
112
|
+
if (response.status === 422) {
|
|
113
|
+
const rawText = await response.text();
|
|
114
|
+
let parsedError: string | null = null;
|
|
115
|
+
try {
|
|
116
|
+
const json = JSON.parse(rawText) as { error?: string };
|
|
117
|
+
if (json && typeof json.error === "string") {
|
|
118
|
+
parsedError = json.error;
|
|
119
|
+
}
|
|
120
|
+
} catch {
|
|
121
|
+
// Not JSON
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const errorMessage = parsedError || rawText || "";
|
|
125
|
+
const shouldRetryCsrf =
|
|
126
|
+
this.isCsrfError(response.status, errorMessage) || (!csrfToken && response.status === 422);
|
|
127
|
+
|
|
128
|
+
if (shouldRetryCsrf) {
|
|
129
|
+
csrfToken = await this.resolveCsrfToken(true);
|
|
130
|
+
response = await requestFn(csrfToken);
|
|
131
|
+
if (response.ok) {
|
|
132
|
+
return (await response.json()) as T;
|
|
133
|
+
}
|
|
134
|
+
const retryErrBody = await response.json().catch(() => ({}));
|
|
135
|
+
throw new Error(
|
|
136
|
+
(retryErrBody as { error?: string }).error || `${defaultErrorMessage} (${response.status})`
|
|
137
|
+
);
|
|
138
|
+
}
|
|
54
139
|
|
|
55
|
-
|
|
56
|
-
const errBody = await response.json().catch(() => ({}));
|
|
57
|
-
throw new Error((errBody as { error?: string }).error || `Upload failed (${response.status})`);
|
|
140
|
+
throw new Error(parsedError || errorMessage || `${defaultErrorMessage} (${response.status})`);
|
|
58
141
|
}
|
|
59
142
|
|
|
60
|
-
|
|
143
|
+
const errBody = await response.json().catch(() => ({}));
|
|
144
|
+
throw new Error(
|
|
145
|
+
(errBody as { error?: string }).error || `${defaultErrorMessage} (${response.status})`
|
|
146
|
+
);
|
|
61
147
|
}
|
|
62
148
|
|
|
63
|
-
public async
|
|
64
|
-
const
|
|
65
|
-
|
|
66
|
-
|
|
149
|
+
public async uploadAttachment(file: Blob, filename: string): Promise<{ signed_blob_id: string }> {
|
|
150
|
+
const makeRequest = async (csrfToken: string | null) => {
|
|
151
|
+
const formData = new FormData();
|
|
152
|
+
formData.append("file", file, filename);
|
|
153
|
+
|
|
154
|
+
const headers: Record<string, string> = {
|
|
155
|
+
Accept: "application/json"
|
|
156
|
+
};
|
|
157
|
+
if (csrfToken) {
|
|
158
|
+
headers["X-CSRF-Token"] = csrfToken;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
return fetch(`${this.baseUrl}/attachments`, {
|
|
162
|
+
method: "POST",
|
|
163
|
+
credentials: "same-origin",
|
|
164
|
+
headers,
|
|
165
|
+
body: formData
|
|
166
|
+
});
|
|
67
167
|
};
|
|
68
|
-
const csrf = this.getCsrfToken();
|
|
69
|
-
if (csrf) {
|
|
70
|
-
headers["X-CSRF-Token"] = csrf;
|
|
71
|
-
}
|
|
72
168
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
credentials: "same-origin",
|
|
76
|
-
headers,
|
|
77
|
-
body: JSON.stringify({ feedback: payload })
|
|
78
|
-
});
|
|
169
|
+
return this.executeWithCsrfRetry(makeRequest, "Upload failed");
|
|
170
|
+
}
|
|
79
171
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
172
|
+
public async submitFeedback(payload: FeedbackSubmissionPayload): Promise<FeedbackSubmissionResponse> {
|
|
173
|
+
const makeRequest = async (csrfToken: string | null) => {
|
|
174
|
+
const headers: Record<string, string> = {
|
|
175
|
+
"Content-Type": "application/json",
|
|
176
|
+
Accept: "application/json"
|
|
177
|
+
};
|
|
178
|
+
if (csrfToken) {
|
|
179
|
+
headers["X-CSRF-Token"] = csrfToken;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
return fetch(`${this.baseUrl}/feedback`, {
|
|
183
|
+
method: "POST",
|
|
184
|
+
credentials: "same-origin",
|
|
185
|
+
headers,
|
|
186
|
+
body: JSON.stringify({ feedback: payload })
|
|
187
|
+
});
|
|
188
|
+
};
|
|
84
189
|
|
|
85
|
-
return
|
|
190
|
+
return this.executeWithCsrfRetry(makeRequest, "Submission failed");
|
|
86
191
|
}
|
|
87
192
|
}
|