@scm-manager/ui-api 2.36.1 → 2.36.2-20220522-120507
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/build/index.d.ts +735 -0
- package/build/index.js +3200 -0
- package/build/index.mjs +3031 -0
- package/package.json +15 -10
- package/LICENSE.txt +0 -21
- package/src/ApiProvider.test.tsx +0 -60
- package/src/ApiProvider.tsx +0 -80
- package/src/LegacyContext.test.tsx +0 -52
- package/src/LegacyContext.tsx +0 -75
- package/src/admin.test.ts +0 -55
- package/src/admin.ts +0 -42
- package/src/alerts.ts +0 -107
- package/src/annotations.ts +0 -45
- package/src/apiKeys.ts +0 -82
- package/src/apiclient.test.ts +0 -126
- package/src/apiclient.ts +0 -336
- package/src/base.test.ts +0 -238
- package/src/base.ts +0 -120
- package/src/branches.test.ts +0 -234
- package/src/branches.ts +0 -201
- package/src/changesets.test.ts +0 -178
- package/src/changesets.ts +0 -82
- package/src/compare.ts +0 -90
- package/src/config.test.ts +0 -118
- package/src/config.ts +0 -60
- package/src/configLink.test.ts +0 -169
- package/src/configLink.ts +0 -89
- package/src/contentType.ts +0 -58
- package/src/diff.test.ts +0 -234
- package/src/diff.ts +0 -93
- package/src/errors.test.ts +0 -59
- package/src/errors.ts +0 -133
- package/src/fileContent.ts +0 -33
- package/src/groupSuggestions.ts +0 -32
- package/src/groups.test.ts +0 -241
- package/src/groups.ts +0 -141
- package/src/help/search/modal.de.ts +0 -33
- package/src/help/search/modal.en.ts +0 -33
- package/src/help/search/syntax.de.ts +0 -148
- package/src/help/search/syntax.en.ts +0 -145
- package/src/history.ts +0 -62
- package/src/import.ts +0 -114
- package/src/index.ts +0 -72
- package/src/keys.ts +0 -48
- package/src/links.test.ts +0 -99
- package/src/links.ts +0 -54
- package/src/login.test.ts +0 -240
- package/src/login.ts +0 -128
- package/src/loginInfo.ts +0 -43
- package/src/namespaceSuggestions.ts +0 -32
- package/src/namespaces.test.ts +0 -95
- package/src/namespaces.ts +0 -43
- package/src/notifications.ts +0 -183
- package/src/permissions.test.ts +0 -345
- package/src/permissions.ts +0 -202
- package/src/plugins.test.ts +0 -336
- package/src/plugins.ts +0 -259
- package/src/publicKeys.ts +0 -83
- package/src/repositories.test.ts +0 -508
- package/src/repositories.ts +0 -375
- package/src/repository-roles.test.ts +0 -229
- package/src/repository-roles.ts +0 -142
- package/src/reset.ts +0 -36
- package/src/search.ts +0 -153
- package/src/sources.test.ts +0 -233
- package/src/sources.ts +0 -136
- package/src/suggestions.ts +0 -47
- package/src/tags.test.ts +0 -272
- package/src/tags.ts +0 -118
- package/src/tests/createInfiniteCachingClient.ts +0 -37
- package/src/tests/createWrapper.tsx +0 -37
- package/src/tests/indexLinks.ts +0 -43
- package/src/urls.test.ts +0 -104
- package/src/urls.ts +0 -118
- package/src/usePluginCenterAuthInfo.ts +0 -91
- package/src/userSuggestions.ts +0 -32
- package/src/users.test.ts +0 -310
- package/src/users.ts +0 -240
- package/src/utils.ts +0 -29
package/src/apiclient.test.ts
DELETED
|
@@ -1,126 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* MIT License
|
|
3
|
-
*
|
|
4
|
-
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
|
|
5
|
-
*
|
|
6
|
-
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
-
* of this software and associated documentation files (the "Software"), to deal
|
|
8
|
-
* in the Software without restriction, including without limitation the rights
|
|
9
|
-
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
-
* copies of the Software, and to permit persons to whom the Software is
|
|
11
|
-
* furnished to do so, subject to the following conditions:
|
|
12
|
-
*
|
|
13
|
-
* The above copyright notice and this permission notice shall be included in all
|
|
14
|
-
* copies or substantial portions of the Software.
|
|
15
|
-
*
|
|
16
|
-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
-
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
-
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
-
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
-
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
-
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
-
* SOFTWARE.
|
|
23
|
-
*/
|
|
24
|
-
|
|
25
|
-
import { apiClient, createUrl, extractXsrfTokenFromCookie } from "./apiclient";
|
|
26
|
-
import fetchMock from "fetch-mock";
|
|
27
|
-
import { BackendError, BadGatewayError } from "./errors";
|
|
28
|
-
|
|
29
|
-
describe("create url", () => {
|
|
30
|
-
it("should not change absolute urls", () => {
|
|
31
|
-
expect(createUrl("https://www.scm-manager.org")).toBe("https://www.scm-manager.org");
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
it("should add prefix for api", () => {
|
|
35
|
-
expect(createUrl("/users")).toBe("/api/v2/users");
|
|
36
|
-
expect(createUrl("users")).toBe("/api/v2/users");
|
|
37
|
-
});
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
describe("error handling tests", () => {
|
|
41
|
-
const earthNotFoundError = {
|
|
42
|
-
transactionId: "42t",
|
|
43
|
-
errorCode: "42e",
|
|
44
|
-
message: "earth not found",
|
|
45
|
-
context: [
|
|
46
|
-
{
|
|
47
|
-
type: "planet",
|
|
48
|
-
id: "earth"
|
|
49
|
-
}
|
|
50
|
-
]
|
|
51
|
-
};
|
|
52
|
-
|
|
53
|
-
afterEach(() => {
|
|
54
|
-
fetchMock.reset();
|
|
55
|
-
fetchMock.restore();
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
it("should create a normal error, if the content type is not scmm-error", done => {
|
|
59
|
-
fetchMock.getOnce("/api/v2/error", {
|
|
60
|
-
status: 404
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
apiClient.get("/error").catch((err: Error) => {
|
|
64
|
-
expect(err.name).toEqual("Error");
|
|
65
|
-
expect(err.message).toContain("404");
|
|
66
|
-
done();
|
|
67
|
-
});
|
|
68
|
-
});
|
|
69
|
-
|
|
70
|
-
it("should create a bad gateway error", done => {
|
|
71
|
-
fetchMock.getOnce("/api/v2/error", {
|
|
72
|
-
status: 502
|
|
73
|
-
});
|
|
74
|
-
|
|
75
|
-
apiClient.get("/error").catch((err: Error) => {
|
|
76
|
-
expect(err).toBeInstanceOf(BadGatewayError);
|
|
77
|
-
done();
|
|
78
|
-
});
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
it("should create an backend error, if the content type is scmm-error", done => {
|
|
82
|
-
fetchMock.getOnce("/api/v2/error", {
|
|
83
|
-
status: 404,
|
|
84
|
-
headers: {
|
|
85
|
-
"Content-Type": "application/vnd.scmm-error+json;v=2"
|
|
86
|
-
},
|
|
87
|
-
body: earthNotFoundError
|
|
88
|
-
});
|
|
89
|
-
|
|
90
|
-
apiClient.get("/error").catch((err: BackendError) => {
|
|
91
|
-
expect(err).toBeInstanceOf(BackendError);
|
|
92
|
-
|
|
93
|
-
expect(err.message).toEqual("earth not found");
|
|
94
|
-
expect(err.statusCode).toBe(404);
|
|
95
|
-
|
|
96
|
-
expect(err.transactionId).toEqual("42t");
|
|
97
|
-
expect(err.errorCode).toEqual("42e");
|
|
98
|
-
expect(err.context).toEqual([
|
|
99
|
-
{
|
|
100
|
-
type: "planet",
|
|
101
|
-
id: "earth"
|
|
102
|
-
}
|
|
103
|
-
]);
|
|
104
|
-
done();
|
|
105
|
-
});
|
|
106
|
-
});
|
|
107
|
-
});
|
|
108
|
-
|
|
109
|
-
describe("extract xsrf token", () => {
|
|
110
|
-
it("should return undefined if no cookie exists", () => {
|
|
111
|
-
const token = extractXsrfTokenFromCookie(undefined);
|
|
112
|
-
expect(token).toBeUndefined();
|
|
113
|
-
});
|
|
114
|
-
|
|
115
|
-
it("should return undefined without X-Bearer-Token exists", () => {
|
|
116
|
-
const token = extractXsrfTokenFromCookie("a=b; c=d; e=f");
|
|
117
|
-
expect(token).toBeUndefined();
|
|
118
|
-
});
|
|
119
|
-
|
|
120
|
-
it("should return xsrf token", () => {
|
|
121
|
-
const cookie =
|
|
122
|
-
"a=b; X-Bearer-Token=eyJhbGciOiJIUzI1NiJ9.eyJ4c3JmIjoiYjE0NDRmNWEtOWI5Mi00ZDA0LWFkMzMtMTAxYjY3MWQ1YTc0Iiwic3ViIjoic2NtYWRtaW4iLCJqdGkiOiI2RFJpQVphNWwxIiwiaWF0IjoxNTc0MDcyNDQ4LCJleHAiOjE1NzQwNzYwNDgsInNjbS1tYW5hZ2VyLnJlZnJlc2hFeHBpcmF0aW9uIjoxNTc0MTE1NjQ4OTU5LCJzY20tbWFuYWdlci5wYXJlbnRUb2tlbklkIjoiNkRSaUFaYTVsMSJ9.VUJtKeWUn3xtHCEbG51r7ceXZ8CF3cmN8J-eb9EDY_U; c=d";
|
|
123
|
-
const token = extractXsrfTokenFromCookie(cookie);
|
|
124
|
-
expect(token).toBe("b1444f5a-9b92-4d04-ad33-101b671d5a74");
|
|
125
|
-
});
|
|
126
|
-
});
|
package/src/apiclient.ts
DELETED
|
@@ -1,336 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* MIT License
|
|
3
|
-
*
|
|
4
|
-
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
|
|
5
|
-
*
|
|
6
|
-
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
-
* of this software and associated documentation files (the "Software"), to deal
|
|
8
|
-
* in the Software without restriction, including without limitation the rights
|
|
9
|
-
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
-
* copies of the Software, and to permit persons to whom the Software is
|
|
11
|
-
* furnished to do so, subject to the following conditions:
|
|
12
|
-
*
|
|
13
|
-
* The above copyright notice and this permission notice shall be included in all
|
|
14
|
-
* copies or substantial portions of the Software.
|
|
15
|
-
*
|
|
16
|
-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
-
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
-
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
-
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
-
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
-
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
-
* SOFTWARE.
|
|
23
|
-
*/
|
|
24
|
-
|
|
25
|
-
import { contextPath } from "./urls";
|
|
26
|
-
import {
|
|
27
|
-
BackendErrorContent,
|
|
28
|
-
BadGatewayError,
|
|
29
|
-
createBackendError,
|
|
30
|
-
ForbiddenError,
|
|
31
|
-
isBackendError,
|
|
32
|
-
TOKEN_EXPIRED_ERROR_CODE,
|
|
33
|
-
TokenExpiredError,
|
|
34
|
-
UnauthorizedError
|
|
35
|
-
} from "./errors";
|
|
36
|
-
|
|
37
|
-
type SubscriptionEvent = {
|
|
38
|
-
type: string;
|
|
39
|
-
};
|
|
40
|
-
|
|
41
|
-
type OpenEvent = SubscriptionEvent;
|
|
42
|
-
|
|
43
|
-
type ErrorEvent = SubscriptionEvent & {
|
|
44
|
-
error: Error;
|
|
45
|
-
};
|
|
46
|
-
|
|
47
|
-
type MessageEvent = SubscriptionEvent & {
|
|
48
|
-
data: string;
|
|
49
|
-
lastEventId?: string;
|
|
50
|
-
};
|
|
51
|
-
|
|
52
|
-
type MessageListeners = {
|
|
53
|
-
[eventType: string]: (event: MessageEvent) => void;
|
|
54
|
-
};
|
|
55
|
-
|
|
56
|
-
type SubscriptionContext = {
|
|
57
|
-
onOpen?: OpenEvent;
|
|
58
|
-
onMessage: MessageListeners;
|
|
59
|
-
onError?: ErrorEvent;
|
|
60
|
-
};
|
|
61
|
-
|
|
62
|
-
type SubscriptionArgument = MessageListeners | SubscriptionContext;
|
|
63
|
-
|
|
64
|
-
type Cancel = () => void;
|
|
65
|
-
|
|
66
|
-
const sessionId = (
|
|
67
|
-
Date.now().toString(36) +
|
|
68
|
-
Math.random()
|
|
69
|
-
.toString(36)
|
|
70
|
-
.substr(2, 5)
|
|
71
|
-
).toUpperCase();
|
|
72
|
-
|
|
73
|
-
const extractXsrfTokenFromJwt = (jwt: string) => {
|
|
74
|
-
const parts = jwt.split(".");
|
|
75
|
-
if (parts.length === 3) {
|
|
76
|
-
return JSON.parse(atob(parts[1])).xsrf;
|
|
77
|
-
}
|
|
78
|
-
};
|
|
79
|
-
|
|
80
|
-
// @VisibleForTesting
|
|
81
|
-
export const extractXsrfTokenFromCookie = (cookieString?: string) => {
|
|
82
|
-
if (cookieString) {
|
|
83
|
-
const cookies = cookieString.split(";");
|
|
84
|
-
for (const c of cookies) {
|
|
85
|
-
const parts = c.trim().split("=");
|
|
86
|
-
if (parts[0] === "X-Bearer-Token" || parts[0] === "X-SCM-Init-Token") {
|
|
87
|
-
return extractXsrfTokenFromJwt(parts[1]);
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
};
|
|
92
|
-
|
|
93
|
-
const extractXsrfToken = () => {
|
|
94
|
-
return extractXsrfTokenFromCookie(document.cookie);
|
|
95
|
-
};
|
|
96
|
-
|
|
97
|
-
const createRequestHeaders = () => {
|
|
98
|
-
const headers: { [key: string]: string } = {
|
|
99
|
-
// disable caching for now
|
|
100
|
-
Cache: "no-cache",
|
|
101
|
-
// identify the request as ajax request
|
|
102
|
-
"X-Requested-With": "XMLHttpRequest",
|
|
103
|
-
// identify the web interface
|
|
104
|
-
"X-SCM-Client": "WUI",
|
|
105
|
-
// identify the window session
|
|
106
|
-
"X-SCM-Session-ID": sessionId
|
|
107
|
-
};
|
|
108
|
-
|
|
109
|
-
const xsrf = extractXsrfToken();
|
|
110
|
-
if (xsrf) {
|
|
111
|
-
headers["X-XSRF-Token"] = xsrf;
|
|
112
|
-
}
|
|
113
|
-
return headers;
|
|
114
|
-
};
|
|
115
|
-
|
|
116
|
-
const applyFetchOptions: (p: RequestInit) => RequestInit = o => {
|
|
117
|
-
if (o.headers) {
|
|
118
|
-
o.headers = {
|
|
119
|
-
...createRequestHeaders()
|
|
120
|
-
};
|
|
121
|
-
} else {
|
|
122
|
-
o.headers = createRequestHeaders();
|
|
123
|
-
}
|
|
124
|
-
o.credentials = "same-origin";
|
|
125
|
-
return o;
|
|
126
|
-
};
|
|
127
|
-
|
|
128
|
-
function handleFailure(response: Response) {
|
|
129
|
-
if (!response.ok) {
|
|
130
|
-
if (response.status === 401) {
|
|
131
|
-
if (isBackendError(response)) {
|
|
132
|
-
return response.json().then((content: BackendErrorContent) => {
|
|
133
|
-
if (content.errorCode === TOKEN_EXPIRED_ERROR_CODE) {
|
|
134
|
-
throw new TokenExpiredError("Token expired", 401);
|
|
135
|
-
} else {
|
|
136
|
-
throw new UnauthorizedError("Unauthorized", 401);
|
|
137
|
-
}
|
|
138
|
-
});
|
|
139
|
-
}
|
|
140
|
-
throw new UnauthorizedError("Unauthorized", 401);
|
|
141
|
-
} else if (response.status === 403) {
|
|
142
|
-
throw new ForbiddenError("Forbidden", 403);
|
|
143
|
-
} else if (response.status === 502) {
|
|
144
|
-
throw new BadGatewayError("Bad Gateway", 502);
|
|
145
|
-
} else if (isBackendError(response)) {
|
|
146
|
-
return response.json().then((content: BackendErrorContent) => {
|
|
147
|
-
throw createBackendError(content, response.status);
|
|
148
|
-
});
|
|
149
|
-
} else {
|
|
150
|
-
throw new Error("server returned status code " + response.status);
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
return response;
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
export function createUrl(url: string) {
|
|
157
|
-
if (url.includes("://")) {
|
|
158
|
-
return url;
|
|
159
|
-
}
|
|
160
|
-
let urlWithStartingSlash = url;
|
|
161
|
-
if (url.indexOf("/") !== 0) {
|
|
162
|
-
urlWithStartingSlash = "/" + urlWithStartingSlash;
|
|
163
|
-
}
|
|
164
|
-
return `${contextPath}/api/v2${urlWithStartingSlash}`;
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
export function createUrlWithIdentifiers(url: string): string {
|
|
168
|
-
return createUrl(url) + "?X-SCM-Client=WUI&X-SCM-Session-ID=" + sessionId;
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
type ErrorListener = (error: Error) => void;
|
|
172
|
-
|
|
173
|
-
type RequestListener = (url: string, options?: RequestInit) => void;
|
|
174
|
-
|
|
175
|
-
class ApiClient {
|
|
176
|
-
errorListeners: ErrorListener[] = [];
|
|
177
|
-
requestListeners: RequestListener[] = [];
|
|
178
|
-
|
|
179
|
-
get = (url: string): Promise<Response> => {
|
|
180
|
-
return this.request(url, applyFetchOptions({}))
|
|
181
|
-
.then(handleFailure)
|
|
182
|
-
.catch(this.notifyAndRethrow);
|
|
183
|
-
};
|
|
184
|
-
|
|
185
|
-
post = (
|
|
186
|
-
url: string,
|
|
187
|
-
payload?: any,
|
|
188
|
-
contentType = "application/json",
|
|
189
|
-
additionalHeaders: Record<string, string> = {}
|
|
190
|
-
) => {
|
|
191
|
-
return this.httpRequestWithJSONBody("POST", url, contentType, additionalHeaders, payload);
|
|
192
|
-
};
|
|
193
|
-
|
|
194
|
-
postText = (url: string, payload: string, additionalHeaders: Record<string, string> = {}) => {
|
|
195
|
-
return this.httpRequestWithTextBody("POST", url, additionalHeaders, payload);
|
|
196
|
-
};
|
|
197
|
-
|
|
198
|
-
putText = (url: string, payload: string, additionalHeaders: Record<string, string> = {}) => {
|
|
199
|
-
return this.httpRequestWithTextBody("PUT", url, additionalHeaders, payload);
|
|
200
|
-
};
|
|
201
|
-
|
|
202
|
-
postBinary = (url: string, fileAppender: (p: FormData) => void, additionalHeaders: Record<string, string> = {}) => {
|
|
203
|
-
const formData = new FormData();
|
|
204
|
-
fileAppender(formData);
|
|
205
|
-
|
|
206
|
-
const options: RequestInit = {
|
|
207
|
-
method: "POST",
|
|
208
|
-
body: formData,
|
|
209
|
-
headers: additionalHeaders
|
|
210
|
-
};
|
|
211
|
-
return this.httpRequestWithBinaryBody(options, url);
|
|
212
|
-
};
|
|
213
|
-
|
|
214
|
-
put(url: string, payload: any, contentType = "application/json", additionalHeaders: Record<string, string> = {}) {
|
|
215
|
-
return this.httpRequestWithJSONBody("PUT", url, contentType, additionalHeaders, payload);
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
head = (url: string) => {
|
|
219
|
-
let options: RequestInit = {
|
|
220
|
-
method: "HEAD"
|
|
221
|
-
};
|
|
222
|
-
options = applyFetchOptions(options);
|
|
223
|
-
return this.request(url, options)
|
|
224
|
-
.then(handleFailure)
|
|
225
|
-
.catch(this.notifyAndRethrow);
|
|
226
|
-
};
|
|
227
|
-
|
|
228
|
-
delete = (url: string): Promise<Response> => {
|
|
229
|
-
let options: RequestInit = {
|
|
230
|
-
method: "DELETE"
|
|
231
|
-
};
|
|
232
|
-
options = applyFetchOptions(options);
|
|
233
|
-
return this.request(url, options)
|
|
234
|
-
.then(handleFailure)
|
|
235
|
-
.catch(this.notifyAndRethrow);
|
|
236
|
-
};
|
|
237
|
-
|
|
238
|
-
httpRequestWithJSONBody = (
|
|
239
|
-
method: string,
|
|
240
|
-
url: string,
|
|
241
|
-
contentType: string,
|
|
242
|
-
additionalHeaders: Record<string, string>,
|
|
243
|
-
payload?: any
|
|
244
|
-
): Promise<Response> => {
|
|
245
|
-
const options: RequestInit = {
|
|
246
|
-
method: method,
|
|
247
|
-
headers: additionalHeaders
|
|
248
|
-
};
|
|
249
|
-
if (payload) {
|
|
250
|
-
options.body = JSON.stringify(payload);
|
|
251
|
-
}
|
|
252
|
-
return this.httpRequestWithBinaryBody(options, url, contentType);
|
|
253
|
-
};
|
|
254
|
-
|
|
255
|
-
httpRequestWithTextBody = (
|
|
256
|
-
method: string,
|
|
257
|
-
url: string,
|
|
258
|
-
additionalHeaders: Record<string, string> = {},
|
|
259
|
-
payload: string
|
|
260
|
-
) => {
|
|
261
|
-
const options: RequestInit = {
|
|
262
|
-
method: method,
|
|
263
|
-
headers: additionalHeaders
|
|
264
|
-
};
|
|
265
|
-
options.body = payload;
|
|
266
|
-
return this.httpRequestWithBinaryBody(options, url, "text/plain");
|
|
267
|
-
};
|
|
268
|
-
|
|
269
|
-
httpRequestWithBinaryBody = (options: RequestInit, url: string, contentType?: string) => {
|
|
270
|
-
options = applyFetchOptions(options);
|
|
271
|
-
if (contentType) {
|
|
272
|
-
if (!options.headers) {
|
|
273
|
-
options.headers = {};
|
|
274
|
-
}
|
|
275
|
-
// @ts-ignore We are sure that here we only get headers of type {[name:string]: string}
|
|
276
|
-
options.headers["Content-Type"] = contentType;
|
|
277
|
-
}
|
|
278
|
-
|
|
279
|
-
return this.request(url, options)
|
|
280
|
-
.then(handleFailure)
|
|
281
|
-
.catch(this.notifyAndRethrow);
|
|
282
|
-
};
|
|
283
|
-
|
|
284
|
-
subscribe(url: string, argument: SubscriptionArgument): Cancel {
|
|
285
|
-
const es = new EventSource(createUrlWithIdentifiers(url), {
|
|
286
|
-
withCredentials: true
|
|
287
|
-
});
|
|
288
|
-
|
|
289
|
-
let listeners: MessageListeners;
|
|
290
|
-
// type guard, to identify that argument is of type SubscriptionContext
|
|
291
|
-
if ("onMessage" in argument) {
|
|
292
|
-
listeners = (argument as SubscriptionContext).onMessage;
|
|
293
|
-
if (argument.onError) {
|
|
294
|
-
// @ts-ignore typing of EventSource is weird
|
|
295
|
-
es.onerror = argument.onError;
|
|
296
|
-
}
|
|
297
|
-
if (argument.onOpen) {
|
|
298
|
-
// @ts-ignore typing of EventSource is weird
|
|
299
|
-
es.onopen = argument.onOpen;
|
|
300
|
-
}
|
|
301
|
-
} else {
|
|
302
|
-
listeners = argument;
|
|
303
|
-
}
|
|
304
|
-
|
|
305
|
-
for (const type in listeners) {
|
|
306
|
-
// @ts-ignore typing of EventSource is weird
|
|
307
|
-
es.addEventListener(type, listeners[type]);
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
return () => es.close();
|
|
311
|
-
}
|
|
312
|
-
|
|
313
|
-
onRequest = (requestListener: RequestListener) => {
|
|
314
|
-
this.requestListeners.push(requestListener);
|
|
315
|
-
};
|
|
316
|
-
|
|
317
|
-
onError = (errorListener: ErrorListener) => {
|
|
318
|
-
this.errorListeners.push(errorListener);
|
|
319
|
-
};
|
|
320
|
-
|
|
321
|
-
private request = (url: string, options: RequestInit) => {
|
|
322
|
-
this.notifyRequestListeners(url, options);
|
|
323
|
-
return fetch(createUrl(url), options);
|
|
324
|
-
};
|
|
325
|
-
|
|
326
|
-
private notifyRequestListeners = (url: string, options: RequestInit) => {
|
|
327
|
-
this.requestListeners.forEach(requestListener => requestListener(url, options));
|
|
328
|
-
};
|
|
329
|
-
|
|
330
|
-
private notifyAndRethrow = (error: Error): never => {
|
|
331
|
-
this.errorListeners.forEach(errorListener => errorListener(error));
|
|
332
|
-
throw error;
|
|
333
|
-
};
|
|
334
|
-
}
|
|
335
|
-
|
|
336
|
-
export const apiClient = new ApiClient();
|
package/src/base.test.ts
DELETED
|
@@ -1,238 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* MIT License
|
|
3
|
-
*
|
|
4
|
-
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
|
|
5
|
-
*
|
|
6
|
-
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
-
* of this software and associated documentation files (the "Software"), to deal
|
|
8
|
-
* in the Software without restriction, including without limitation the rights
|
|
9
|
-
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
-
* copies of the Software, and to permit persons to whom the Software is
|
|
11
|
-
* furnished to do so, subject to the following conditions:
|
|
12
|
-
*
|
|
13
|
-
* The above copyright notice and this permission notice shall be included in all
|
|
14
|
-
* copies or substantial portions of the Software.
|
|
15
|
-
*
|
|
16
|
-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
-
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
-
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
-
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
-
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
-
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
-
* SOFTWARE.
|
|
23
|
-
*/
|
|
24
|
-
|
|
25
|
-
import fetchMock from "fetch-mock-jest";
|
|
26
|
-
import { useIndex, useIndexJsonResource, useIndexLink, useIndexLinks, useRequiredIndexLink, useVersion } from "./base";
|
|
27
|
-
import { renderHook } from "@testing-library/react-hooks";
|
|
28
|
-
import { LegacyContext } from "./LegacyContext";
|
|
29
|
-
import { IndexResources, Link } from "@scm-manager/ui-types";
|
|
30
|
-
import createWrapper from "./tests/createWrapper";
|
|
31
|
-
import { QueryClient } from "react-query";
|
|
32
|
-
|
|
33
|
-
describe("Test base api hooks", () => {
|
|
34
|
-
describe("useIndex tests", () => {
|
|
35
|
-
fetchMock.get("/api/v2/", {
|
|
36
|
-
version: "x.y.z",
|
|
37
|
-
_links: {}
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
it("should return index", async () => {
|
|
41
|
-
const { result, waitFor } = renderHook(() => useIndex(), { wrapper: createWrapper() });
|
|
42
|
-
await waitFor(() => {
|
|
43
|
-
return !!result.current.data;
|
|
44
|
-
});
|
|
45
|
-
expect(result.current?.data?.version).toEqual("x.y.z");
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
it("should call onIndexFetched of LegacyContext", async () => {
|
|
49
|
-
let index: IndexResources;
|
|
50
|
-
const context: LegacyContext = {
|
|
51
|
-
onIndexFetched: fetchedIndex => {
|
|
52
|
-
index = fetchedIndex;
|
|
53
|
-
},
|
|
54
|
-
initialize: () => null
|
|
55
|
-
};
|
|
56
|
-
const { result, waitFor } = renderHook(() => useIndex(), { wrapper: createWrapper(context) });
|
|
57
|
-
await waitFor(() => {
|
|
58
|
-
return !!result.current.data;
|
|
59
|
-
});
|
|
60
|
-
expect(index!.version).toEqual("x.y.z");
|
|
61
|
-
});
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
describe("useIndexLink tests", () => {
|
|
65
|
-
it("should throw an error if index is not available", () => {
|
|
66
|
-
const { result } = renderHook(() => useIndexLink("spaceships"), { wrapper: createWrapper() });
|
|
67
|
-
expect(result.error).toBeDefined();
|
|
68
|
-
});
|
|
69
|
-
|
|
70
|
-
it("should return undefined for unknown link", () => {
|
|
71
|
-
const queryClient = new QueryClient();
|
|
72
|
-
queryClient.setQueryData("index", {
|
|
73
|
-
version: "x.y.z",
|
|
74
|
-
_links: {}
|
|
75
|
-
});
|
|
76
|
-
const { result } = renderHook(() => useIndexLink("spaceships"), {
|
|
77
|
-
wrapper: createWrapper(undefined, queryClient)
|
|
78
|
-
});
|
|
79
|
-
expect(result.current).toBeUndefined();
|
|
80
|
-
});
|
|
81
|
-
|
|
82
|
-
it("should return undefined for link array", () => {
|
|
83
|
-
const queryClient = new QueryClient();
|
|
84
|
-
queryClient.setQueryData("index", {
|
|
85
|
-
version: "x.y.z",
|
|
86
|
-
_links: {
|
|
87
|
-
spaceships: [
|
|
88
|
-
{
|
|
89
|
-
name: "heartOfGold",
|
|
90
|
-
href: "/spaceships/heartOfGold"
|
|
91
|
-
},
|
|
92
|
-
{
|
|
93
|
-
name: "razorCrest",
|
|
94
|
-
href: "/spaceships/razorCrest"
|
|
95
|
-
}
|
|
96
|
-
]
|
|
97
|
-
}
|
|
98
|
-
});
|
|
99
|
-
const { result } = renderHook(() => useIndexLink("spaceships"), {
|
|
100
|
-
wrapper: createWrapper(undefined, queryClient)
|
|
101
|
-
});
|
|
102
|
-
expect(result.current).toBeUndefined();
|
|
103
|
-
});
|
|
104
|
-
|
|
105
|
-
it("should return link", () => {
|
|
106
|
-
const queryClient = new QueryClient();
|
|
107
|
-
queryClient.setQueryData("index", {
|
|
108
|
-
version: "x.y.z",
|
|
109
|
-
_links: {
|
|
110
|
-
spaceships: {
|
|
111
|
-
href: "/api/spaceships"
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
});
|
|
115
|
-
const { result } = renderHook(() => useIndexLink("spaceships"), {
|
|
116
|
-
wrapper: createWrapper(undefined, queryClient)
|
|
117
|
-
});
|
|
118
|
-
expect(result.current).toBe("/api/spaceships");
|
|
119
|
-
});
|
|
120
|
-
});
|
|
121
|
-
|
|
122
|
-
describe("useIndexLinks tests", () => {
|
|
123
|
-
it("should throw an error if index is not available", async () => {
|
|
124
|
-
const { result } = renderHook(() => useIndexLinks(), { wrapper: createWrapper() });
|
|
125
|
-
expect(result.error).toBeDefined();
|
|
126
|
-
});
|
|
127
|
-
|
|
128
|
-
it("should return links", () => {
|
|
129
|
-
const queryClient = new QueryClient();
|
|
130
|
-
queryClient.setQueryData("index", {
|
|
131
|
-
version: "x.y.z",
|
|
132
|
-
_links: {
|
|
133
|
-
spaceships: {
|
|
134
|
-
href: "/api/spaceships"
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
});
|
|
138
|
-
const { result } = renderHook(() => useIndexLinks(), {
|
|
139
|
-
wrapper: createWrapper(undefined, queryClient)
|
|
140
|
-
});
|
|
141
|
-
expect((result.current!.spaceships as Link).href).toBe("/api/spaceships");
|
|
142
|
-
});
|
|
143
|
-
});
|
|
144
|
-
|
|
145
|
-
describe("useVersion tests", () => {
|
|
146
|
-
it("should throw an error if version is not available", async () => {
|
|
147
|
-
const { result } = renderHook(() => useVersion(), { wrapper: createWrapper() });
|
|
148
|
-
expect(result.error).toBeDefined();
|
|
149
|
-
});
|
|
150
|
-
|
|
151
|
-
it("should return version", () => {
|
|
152
|
-
const queryClient = new QueryClient();
|
|
153
|
-
queryClient.setQueryData("index", {
|
|
154
|
-
version: "x.y.z"
|
|
155
|
-
});
|
|
156
|
-
const { result } = renderHook(() => useVersion(), {
|
|
157
|
-
wrapper: createWrapper(undefined, queryClient)
|
|
158
|
-
});
|
|
159
|
-
expect(result.current).toBe("x.y.z");
|
|
160
|
-
});
|
|
161
|
-
});
|
|
162
|
-
|
|
163
|
-
describe("useRequiredIndexLink tests", () => {
|
|
164
|
-
it("should throw error for undefined link", () => {
|
|
165
|
-
const queryClient = new QueryClient();
|
|
166
|
-
queryClient.setQueryData("index", {
|
|
167
|
-
version: "x.y.z",
|
|
168
|
-
_links: {}
|
|
169
|
-
});
|
|
170
|
-
const { result } = renderHook(() => useRequiredIndexLink("spaceships"), {
|
|
171
|
-
wrapper: createWrapper(undefined, queryClient)
|
|
172
|
-
});
|
|
173
|
-
expect(result.error).toBeDefined();
|
|
174
|
-
});
|
|
175
|
-
|
|
176
|
-
it("should return link", () => {
|
|
177
|
-
const queryClient = new QueryClient();
|
|
178
|
-
queryClient.setQueryData("index", {
|
|
179
|
-
version: "x.y.z",
|
|
180
|
-
_links: {
|
|
181
|
-
spaceships: {
|
|
182
|
-
href: "/api/spaceships"
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
});
|
|
186
|
-
const { result } = renderHook(() => useRequiredIndexLink("spaceships"), {
|
|
187
|
-
wrapper: createWrapper(undefined, queryClient)
|
|
188
|
-
});
|
|
189
|
-
expect(result.current).toBe("/api/spaceships");
|
|
190
|
-
});
|
|
191
|
-
});
|
|
192
|
-
|
|
193
|
-
describe("useIndexJsonResource tests", () => {
|
|
194
|
-
it("should return json resource from link", async () => {
|
|
195
|
-
const queryClient = new QueryClient();
|
|
196
|
-
queryClient.setQueryData("index", {
|
|
197
|
-
version: "x.y.z",
|
|
198
|
-
_links: {
|
|
199
|
-
spaceships: {
|
|
200
|
-
href: "/spaceships"
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
|
-
});
|
|
204
|
-
|
|
205
|
-
const spaceship = {
|
|
206
|
-
name: "heartOfGold"
|
|
207
|
-
};
|
|
208
|
-
|
|
209
|
-
fetchMock.get("/api/v2/spaceships", spaceship);
|
|
210
|
-
|
|
211
|
-
const { result, waitFor } = renderHook(() => useIndexJsonResource<typeof spaceship>("spaceships"), {
|
|
212
|
-
wrapper: createWrapper(undefined, queryClient)
|
|
213
|
-
});
|
|
214
|
-
|
|
215
|
-
await waitFor(() => {
|
|
216
|
-
return !!result.current.data;
|
|
217
|
-
});
|
|
218
|
-
|
|
219
|
-
expect(result.current.data!.name).toBe("heartOfGold");
|
|
220
|
-
});
|
|
221
|
-
});
|
|
222
|
-
|
|
223
|
-
it("should return nothing if link is not available", () => {
|
|
224
|
-
const queryClient = new QueryClient();
|
|
225
|
-
queryClient.setQueryData("index", {
|
|
226
|
-
version: "x.y.z",
|
|
227
|
-
_links: {}
|
|
228
|
-
});
|
|
229
|
-
|
|
230
|
-
const { result } = renderHook(() => useIndexJsonResource<{}>("spaceships"), {
|
|
231
|
-
wrapper: createWrapper(undefined, queryClient)
|
|
232
|
-
});
|
|
233
|
-
|
|
234
|
-
expect(result.current.isLoading).toBe(false);
|
|
235
|
-
expect(result.current.error).toBeFalsy();
|
|
236
|
-
expect(result.current.data).toBeFalsy();
|
|
237
|
-
});
|
|
238
|
-
});
|