@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.
Files changed (79) hide show
  1. package/build/index.d.ts +735 -0
  2. package/build/index.js +3200 -0
  3. package/build/index.mjs +3031 -0
  4. package/package.json +15 -10
  5. package/LICENSE.txt +0 -21
  6. package/src/ApiProvider.test.tsx +0 -60
  7. package/src/ApiProvider.tsx +0 -80
  8. package/src/LegacyContext.test.tsx +0 -52
  9. package/src/LegacyContext.tsx +0 -75
  10. package/src/admin.test.ts +0 -55
  11. package/src/admin.ts +0 -42
  12. package/src/alerts.ts +0 -107
  13. package/src/annotations.ts +0 -45
  14. package/src/apiKeys.ts +0 -82
  15. package/src/apiclient.test.ts +0 -126
  16. package/src/apiclient.ts +0 -336
  17. package/src/base.test.ts +0 -238
  18. package/src/base.ts +0 -120
  19. package/src/branches.test.ts +0 -234
  20. package/src/branches.ts +0 -201
  21. package/src/changesets.test.ts +0 -178
  22. package/src/changesets.ts +0 -82
  23. package/src/compare.ts +0 -90
  24. package/src/config.test.ts +0 -118
  25. package/src/config.ts +0 -60
  26. package/src/configLink.test.ts +0 -169
  27. package/src/configLink.ts +0 -89
  28. package/src/contentType.ts +0 -58
  29. package/src/diff.test.ts +0 -234
  30. package/src/diff.ts +0 -93
  31. package/src/errors.test.ts +0 -59
  32. package/src/errors.ts +0 -133
  33. package/src/fileContent.ts +0 -33
  34. package/src/groupSuggestions.ts +0 -32
  35. package/src/groups.test.ts +0 -241
  36. package/src/groups.ts +0 -141
  37. package/src/help/search/modal.de.ts +0 -33
  38. package/src/help/search/modal.en.ts +0 -33
  39. package/src/help/search/syntax.de.ts +0 -148
  40. package/src/help/search/syntax.en.ts +0 -145
  41. package/src/history.ts +0 -62
  42. package/src/import.ts +0 -114
  43. package/src/index.ts +0 -72
  44. package/src/keys.ts +0 -48
  45. package/src/links.test.ts +0 -99
  46. package/src/links.ts +0 -54
  47. package/src/login.test.ts +0 -240
  48. package/src/login.ts +0 -128
  49. package/src/loginInfo.ts +0 -43
  50. package/src/namespaceSuggestions.ts +0 -32
  51. package/src/namespaces.test.ts +0 -95
  52. package/src/namespaces.ts +0 -43
  53. package/src/notifications.ts +0 -183
  54. package/src/permissions.test.ts +0 -345
  55. package/src/permissions.ts +0 -202
  56. package/src/plugins.test.ts +0 -336
  57. package/src/plugins.ts +0 -259
  58. package/src/publicKeys.ts +0 -83
  59. package/src/repositories.test.ts +0 -508
  60. package/src/repositories.ts +0 -375
  61. package/src/repository-roles.test.ts +0 -229
  62. package/src/repository-roles.ts +0 -142
  63. package/src/reset.ts +0 -36
  64. package/src/search.ts +0 -153
  65. package/src/sources.test.ts +0 -233
  66. package/src/sources.ts +0 -136
  67. package/src/suggestions.ts +0 -47
  68. package/src/tags.test.ts +0 -272
  69. package/src/tags.ts +0 -118
  70. package/src/tests/createInfiniteCachingClient.ts +0 -37
  71. package/src/tests/createWrapper.tsx +0 -37
  72. package/src/tests/indexLinks.ts +0 -43
  73. package/src/urls.test.ts +0 -104
  74. package/src/urls.ts +0 -118
  75. package/src/usePluginCenterAuthInfo.ts +0 -91
  76. package/src/userSuggestions.ts +0 -32
  77. package/src/users.test.ts +0 -310
  78. package/src/users.ts +0 -240
  79. package/src/utils.ts +0 -29
@@ -1,142 +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
- import { ApiResult, useRequiredIndexLink } from "./base";
25
- import { RepositoryRole, RepositoryRoleCollection, RepositoryRoleCreation } from "@scm-manager/ui-types";
26
- import { useMutation, useQuery, useQueryClient } from "react-query";
27
- import { apiClient } from "./apiclient";
28
- import * as urls from "./urls";
29
- import { createQueryString } from "./utils";
30
- import { requiredLink } from "./links";
31
-
32
- export type UseRepositoryRolesRequest = {
33
- page?: number | string;
34
- };
35
-
36
- export const useRepositoryRoles = (request?: UseRepositoryRolesRequest): ApiResult<RepositoryRoleCollection> => {
37
- const queryClient = useQueryClient();
38
- const indexLink = useRequiredIndexLink("repositoryRoles");
39
-
40
- const queryParams: Record<string, string> = {};
41
- if (request?.page) {
42
- queryParams.page = request.page.toString();
43
- }
44
-
45
- return useQuery<RepositoryRoleCollection, Error>(
46
- ["repositoryRoles", request?.page || 0],
47
- () => apiClient.get(`${indexLink}?${createQueryString(queryParams)}`).then((response) => response.json()),
48
- {
49
- onSuccess: (repositoryRoles: RepositoryRoleCollection) => {
50
- repositoryRoles._embedded?.repositoryRoles.forEach((repositoryRole: RepositoryRole) =>
51
- queryClient.setQueryData(["repositoryRole", repositoryRole.name], repositoryRole)
52
- );
53
- },
54
- }
55
- );
56
- };
57
-
58
- export const useRepositoryRole = (name: string): ApiResult<RepositoryRole> => {
59
- const indexLink = useRequiredIndexLink("repositoryRoles");
60
- return useQuery<RepositoryRole, Error>(["repositoryRole", name], () =>
61
- apiClient.get(urls.concat(indexLink, name)).then((response) => response.json())
62
- );
63
- };
64
-
65
- const createRepositoryRole = (link: string) => {
66
- return (repositoryRole: RepositoryRoleCreation) => {
67
- return apiClient
68
- .post(link, repositoryRole, "application/vnd.scmm-repositoryRole+json;v=2")
69
- .then((response) => {
70
- const location = response.headers.get("Location");
71
- if (!location) {
72
- throw new Error("Server does not return required Location header");
73
- }
74
- return apiClient.get(location);
75
- })
76
- .then((response) => response.json());
77
- };
78
- };
79
-
80
- export const useCreateRepositoryRole = () => {
81
- const queryClient = useQueryClient();
82
- const link = useRequiredIndexLink("repositoryRoles");
83
- const { mutate, data, isLoading, error } = useMutation<RepositoryRole, Error, RepositoryRoleCreation>(
84
- createRepositoryRole(link),
85
- {
86
- onSuccess: (repositoryRole) => {
87
- queryClient.setQueryData(["repositoryRole", repositoryRole.name], repositoryRole);
88
- return queryClient.invalidateQueries(["repositoryRoles"]);
89
- },
90
- }
91
- );
92
- return {
93
- create: (repositoryRole: RepositoryRoleCreation) => mutate(repositoryRole),
94
- isLoading,
95
- error,
96
- repositoryRole: data,
97
- };
98
- };
99
-
100
- export const useUpdateRepositoryRole = () => {
101
- const queryClient = useQueryClient();
102
- const { mutate, isLoading, error, data } = useMutation<unknown, Error, RepositoryRole>(
103
- (repositoryRole) => {
104
- const updateUrl = requiredLink(repositoryRole, "update");
105
- return apiClient.put(updateUrl, repositoryRole, "application/vnd.scmm-repositoryRole+json;v=2");
106
- },
107
- {
108
- onSuccess: async (_, repositoryRole) => {
109
- await queryClient.invalidateQueries(["repositoryRole", repositoryRole.name]);
110
- await queryClient.invalidateQueries(["repositoryRoles"]);
111
- },
112
- }
113
- );
114
- return {
115
- update: (repositoryRole: RepositoryRole) => mutate(repositoryRole),
116
- isLoading,
117
- error,
118
- isUpdated: !!data,
119
- };
120
- };
121
-
122
- export const useDeleteRepositoryRole = () => {
123
- const queryClient = useQueryClient();
124
- const { mutate, isLoading, error, data } = useMutation<unknown, Error, RepositoryRole>(
125
- (repositoryRole) => {
126
- const deleteUrl = requiredLink(repositoryRole, "delete");
127
- return apiClient.delete(deleteUrl);
128
- },
129
- {
130
- onSuccess: async (_, name) => {
131
- await queryClient.removeQueries(["repositoryRole", name]);
132
- await queryClient.invalidateQueries(["repositoryRoles"]);
133
- },
134
- }
135
- );
136
- return {
137
- remove: (repositoryRole: RepositoryRole) => mutate(repositoryRole),
138
- isLoading,
139
- error,
140
- isDeleted: !!data,
141
- };
142
- };
package/src/reset.ts DELETED
@@ -1,36 +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
- import { QueryClient, useQueryClient } from "react-query";
25
-
26
- export const reset = (queryClient: QueryClient) => {
27
- queryClient.removeQueries({
28
- predicate: ({ queryKey }) => queryKey !== "index",
29
- });
30
- return queryClient.invalidateQueries("index");
31
- };
32
-
33
- export const useReset = () => {
34
- const queryClient = useQueryClient();
35
- return () => reset(queryClient);
36
- };
package/src/search.ts DELETED
@@ -1,153 +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 { ApiResult, ApiResultWithFetching, useIndexJsonResource, useIndexLinks } from "./base";
26
- import { Link, QueryResult, SearchableType } from "@scm-manager/ui-types";
27
- import { apiClient } from "./apiclient";
28
- import { createQueryString } from "./utils";
29
- import { useQueries, useQuery } from "react-query";
30
- import { useEffect, useState } from "react";
31
-
32
- export type SearchOptions = {
33
- type: string;
34
- page?: number;
35
- pageSize?: number;
36
- };
37
-
38
- const defaultSearchOptions: SearchOptions = {
39
- type: "repository",
40
- };
41
-
42
- const isString = (str: string | undefined): str is string => !!str;
43
-
44
- export const useSearchTypes = () => {
45
- return useSearchLinks()
46
- .map((link) => link.name)
47
- .filter(isString);
48
- };
49
-
50
- export const useSearchableTypes = () => useIndexJsonResource<SearchableType[]>("searchableTypes");
51
-
52
- export const useSearchCounts = (types: string[], query: string) => {
53
- const searchLinks = useSearchLinks();
54
- const queries = useQueries(
55
- types.map((type) => ({
56
- queryKey: ["search", type, query, "count"],
57
- queryFn: () =>
58
- apiClient.get(`${findLink(searchLinks, type)}?q=${query}&countOnly=true`).then((response) => response.json()),
59
- }))
60
- );
61
- const result: { [type: string]: ApiResultWithFetching<number> } = {};
62
- queries.forEach((q, i) => {
63
- result[types[i]] = {
64
- isLoading: q.isLoading,
65
- isFetching: q.isFetching,
66
- error: q.error as Error,
67
- data: (q.data as QueryResult)?.totalHits,
68
- };
69
- });
70
- return result;
71
- };
72
-
73
- const findLink = (links: Link[], name: string) => {
74
- for (const l of links) {
75
- if (l.name === name) {
76
- return l.href;
77
- }
78
- }
79
- throw new Error(`could not find search link for ${name}`);
80
- };
81
-
82
- const useSearchLinks = () => {
83
- const links = useIndexLinks();
84
- const searchLinks = links["search"];
85
- if (!searchLinks) {
86
- throw new Error("could not find search links in index");
87
- }
88
-
89
- if (!Array.isArray(searchLinks)) {
90
- throw new Error("search links returned in wrong format, array is expected");
91
- }
92
- return searchLinks as Link[];
93
- };
94
-
95
- const useSearchLink = (name: string) => {
96
- const searchLinks = useSearchLinks();
97
- return findLink(searchLinks, name);
98
- };
99
-
100
- export const useSearch = (query: string, optionParam = defaultSearchOptions): ApiResult<QueryResult> => {
101
- const options = { ...defaultSearchOptions, ...optionParam };
102
- const link = useSearchLink(options.type);
103
-
104
- const queryParams: Record<string, string> = {};
105
- queryParams.q = query;
106
- if (options.page) {
107
- queryParams.page = options.page.toString();
108
- }
109
- if (options.pageSize) {
110
- queryParams.pageSize = options.pageSize.toString();
111
- }
112
- return useQuery<QueryResult, Error>(
113
- ["search", options.type, queryParams],
114
- () => apiClient.get(`${link}?${createQueryString(queryParams)}`).then((response) => response.json()),
115
- {
116
- enabled: query?.length > 1,
117
- }
118
- );
119
- };
120
-
121
- const useObserveAsync = <D extends any[], R, E = Error>(fn: (...args: D) => Promise<R>, deps: D) => {
122
- const [data, setData] = useState<R>();
123
- const [isLoading, setLoading] = useState(false);
124
- const [error, setError] = useState<E>();
125
- useEffect(() => {
126
- setLoading(true);
127
- fn(...deps)
128
- .then(setData)
129
- .catch(setError)
130
- .finally(() => setLoading(false));
131
- }, deps);
132
- return { data, isLoading, error };
133
- };
134
-
135
- const supportedLanguages = ["de", "en"];
136
-
137
- const pickLang = (language: string) => {
138
- if (!supportedLanguages.includes(language)) {
139
- return "en";
140
- }
141
- return language;
142
- };
143
-
144
- export const useSearchHelpContent = (language: string) =>
145
- useObserveAsync(
146
- (lang) => import(`./help/search/modal.${pickLang(lang)}`).then((module) => module.default),
147
- [language]
148
- );
149
- export const useSearchSyntaxContent = (language: string) =>
150
- useObserveAsync(
151
- (lang) => import(`./help/search/syntax.${pickLang(lang)}`).then((module) => module.default),
152
- [language]
153
- );
@@ -1,233 +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
- import { File, Repository } from "@scm-manager/ui-types";
25
- import { useSources } from "./sources";
26
- import fetchMock from "fetch-mock";
27
- import createInfiniteCachingClient from "./tests/createInfiniteCachingClient";
28
- import { act, renderHook } from "@testing-library/react-hooks";
29
- import createWrapper from "./tests/createWrapper";
30
-
31
- describe("Test sources hooks", () => {
32
- const puzzle42: Repository = {
33
- namespace: "puzzles",
34
- name: "42",
35
- type: "git",
36
- _links: {
37
- sources: {
38
- href: "/src",
39
- },
40
- },
41
- };
42
-
43
- const readmeMd: File = {
44
- name: "README.md",
45
- path: "README.md",
46
- directory: false,
47
- revision: "abc",
48
- length: 21,
49
- description: "Awesome readme",
50
- _links: {},
51
- _embedded: {
52
- children: [],
53
- },
54
- };
55
-
56
- const rootDirectory: File = {
57
- name: "",
58
- path: "",
59
- directory: true,
60
- revision: "abc",
61
- _links: {},
62
- _embedded: {
63
- children: [readmeMd],
64
- },
65
- };
66
-
67
- const sepecialMd: File = {
68
- name: "special.md",
69
- path: "main/special.md",
70
- directory: false,
71
- revision: "abc",
72
- length: 42,
73
- description: "Awesome special file",
74
- _links: {},
75
- _embedded: {
76
- children: [],
77
- },
78
- };
79
-
80
- const sepecialMdPartial: File = {
81
- ...sepecialMd,
82
- partialResult: true,
83
- computationAborted: false,
84
- };
85
-
86
- const sepecialMdComputationAborted: File = {
87
- ...sepecialMd,
88
- partialResult: true,
89
- computationAborted: true,
90
- };
91
-
92
- const mainDirectoryTruncated: File = {
93
- name: "main",
94
- path: "main",
95
- directory: true,
96
- revision: "abc",
97
- truncated: true,
98
- _links: {
99
- proceed: {
100
- href: "src/2",
101
- },
102
- },
103
- _embedded: {
104
- children: [],
105
- },
106
- };
107
-
108
- const mainDirectory: File = {
109
- ...mainDirectoryTruncated,
110
- truncated: false,
111
- _embedded: {
112
- children: [sepecialMd],
113
- },
114
- };
115
-
116
- beforeEach(() => {
117
- fetchMock.reset();
118
- });
119
-
120
- const firstChild = (directory?: File) => {
121
- if (directory?._embedded?.children && directory._embedded.children.length > 0) {
122
- return directory._embedded.children[0];
123
- }
124
- };
125
-
126
- describe("useSources tests", () => {
127
- it("should return root directory", async () => {
128
- const queryClient = createInfiniteCachingClient();
129
- fetchMock.getOnce("/api/v2/src?collapse=true", rootDirectory);
130
- const { result, waitFor } = renderHook(() => useSources(puzzle42), {
131
- wrapper: createWrapper(undefined, queryClient),
132
- });
133
- await waitFor(() => !!result.current.data);
134
- expect(result.current.data).toEqual(rootDirectory);
135
- });
136
-
137
- it("should return file from url with revision and path", async () => {
138
- const queryClient = createInfiniteCachingClient();
139
- fetchMock.getOnce("/api/v2/src/abc/README.md?collapse=true", readmeMd);
140
- const { result, waitFor } = renderHook(() => useSources(puzzle42, { revision: "abc", path: "README.md" }), {
141
- wrapper: createWrapper(undefined, queryClient),
142
- });
143
- await waitFor(() => !!result.current.data);
144
- expect(result.current.data).toEqual(readmeMd);
145
- });
146
-
147
- it("should fetch next page", async () => {
148
- const queryClient = createInfiniteCachingClient();
149
- fetchMock.getOnce("/api/v2/src?collapse=true", mainDirectoryTruncated);
150
- fetchMock.getOnce("/api/v2/src/2", mainDirectory);
151
- const { result, waitFor, waitForNextUpdate } = renderHook(() => useSources(puzzle42), {
152
- wrapper: createWrapper(undefined, queryClient),
153
- });
154
- await waitFor(() => !!result.current.data);
155
-
156
- expect(result.current.data).toEqual(mainDirectoryTruncated);
157
-
158
- await act(() => {
159
- const { fetchNextPage } = result.current;
160
- fetchNextPage();
161
- return waitForNextUpdate();
162
- });
163
- await waitFor(() => !result.current.isFetchingNextPage);
164
-
165
- expect(result.current.data).toEqual(mainDirectory);
166
- });
167
-
168
- it("should refetch if partial files exists", async () => {
169
- const queryClient = createInfiniteCachingClient();
170
- fetchMock.get(
171
- "/api/v2/src?collapse=true",
172
- {
173
- ...mainDirectory,
174
- _embedded: {
175
- children: [sepecialMdPartial],
176
- },
177
- },
178
- {
179
- repeat: 1,
180
- }
181
- );
182
- fetchMock.get(
183
- "/api/v2/src?collapse=true",
184
- {
185
- ...mainDirectory,
186
- _embedded: {
187
- children: [sepecialMd],
188
- },
189
- },
190
- {
191
- repeat: 1,
192
- overwriteRoutes: false,
193
- }
194
- );
195
-
196
- const { result, waitFor } = renderHook(() => useSources(puzzle42, { refetchPartialInterval: 100 }), {
197
- wrapper: createWrapper(undefined, queryClient),
198
- });
199
-
200
- await waitFor(() => !!firstChild(result.current.data));
201
- expect(firstChild(result.current.data)?.partialResult).toBe(true);
202
-
203
- await waitFor(() => !firstChild(result.current.data)?.partialResult);
204
- expect(firstChild(result.current.data)?.partialResult).toBeFalsy();
205
- });
206
-
207
- it("should not refetch if computation is aborted", async () => {
208
- const queryClient = createInfiniteCachingClient();
209
- fetchMock.getOnce("/api/v2/src/abc/main/special.md?collapse=true", sepecialMdComputationAborted, { repeat: 1 });
210
- // should never be called
211
- fetchMock.getOnce("/api/v2/src/abc/main/special.md?collapse=true", sepecialMd, {
212
- repeat: 1,
213
- overwriteRoutes: false,
214
- });
215
- const { result, waitFor } = renderHook(
216
- () =>
217
- useSources(puzzle42, {
218
- revision: "abc",
219
- path: "main/special.md",
220
- refetchPartialInterval: 100,
221
- }),
222
- {
223
- wrapper: createWrapper(undefined, queryClient),
224
- }
225
- );
226
- await waitFor(() => !!result.current.data);
227
- expect(result.current.data).toEqual(sepecialMdComputationAborted);
228
-
229
- await new Promise((r) => setTimeout(r, 200));
230
- expect(result.current.data).toEqual(sepecialMdComputationAborted);
231
- });
232
- });
233
- });
package/src/sources.ts DELETED
@@ -1,136 +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
- import { File, Link, Repository } from "@scm-manager/ui-types";
25
- import { requiredLink } from "./links";
26
- import { apiClient } from "./apiclient";
27
- import * as urls from "./urls";
28
- import { useInfiniteQuery } from "react-query";
29
- import { repoQueryKey } from "./keys";
30
- import { useEffect } from "react";
31
- import { createQueryString } from "./utils";
32
-
33
- export type UseSourcesOptions = {
34
- revision?: string;
35
- path?: string;
36
- refetchPartialInterval?: number;
37
- enabled?: boolean;
38
- collapse?: boolean;
39
- };
40
-
41
- const UseSourcesDefaultOptions: UseSourcesOptions = {
42
- enabled: true,
43
- refetchPartialInterval: 3000,
44
- collapse: true
45
- };
46
-
47
- export const useSources = (repository: Repository, opts: UseSourcesOptions = UseSourcesDefaultOptions) => {
48
- const options = {
49
- ...UseSourcesDefaultOptions,
50
- ...opts,
51
- };
52
- const link = createSourcesLink(repository, options);
53
- const { isLoading, error, data, isFetchingNextPage, fetchNextPage, refetch } = useInfiniteQuery<File, Error, File>(
54
- repoQueryKey(repository, "sources", options.revision || "", options.path || "", options.collapse ? "collapse" : ""),
55
- ({ pageParam }) => {
56
- return apiClient.get(pageParam || link).then((response) => response.json());
57
- },
58
- {
59
- enabled: options.enabled,
60
- getNextPageParam: (lastPage) => {
61
- return (lastPage._links.proceed as Link)?.href;
62
- },
63
- }
64
- );
65
-
66
- const file = merge(data?.pages);
67
- useEffect(() => {
68
- const intervalId = setInterval(() => {
69
- if (isPartial(file)) {
70
- refetch({
71
- throwOnError: true,
72
- });
73
- }
74
- }, options.refetchPartialInterval);
75
- return () => clearInterval(intervalId);
76
- }, [options.refetchPartialInterval, file]);
77
-
78
- return {
79
- isLoading,
80
- error,
81
- data: file,
82
- isFetchingNextPage,
83
- fetchNextPage: () => {
84
- // wrapped because we do not want to leak react-query types in our api
85
- fetchNextPage();
86
- },
87
- };
88
- };
89
-
90
- const createSourcesLink = (repository: Repository, options: UseSourcesOptions) => {
91
- let link = requiredLink(repository, "sources");
92
- if (options.revision) {
93
- link = urls.concat(link, encodeURIComponent(options.revision));
94
-
95
- if (options.path) {
96
- link = urls.concat(link, options.path);
97
- }
98
- }
99
- if (options.collapse) {
100
- return `${link}?${createQueryString({ collapse: "true" })}`;
101
- }
102
- return link;
103
- };
104
-
105
- const merge = (files?: File[]): File | undefined => {
106
- if (!files || files.length === 0) {
107
- return;
108
- }
109
- const children = [];
110
- for (const page of files) {
111
- children.push(...(page._embedded?.children || []));
112
- }
113
- const lastPage = files[files.length - 1];
114
- return {
115
- ...lastPage,
116
- _embedded: {
117
- ...lastPage._embedded,
118
- children,
119
- },
120
- };
121
- };
122
-
123
- const isFilePartial = (f: File) => {
124
- return f.partialResult && !f.computationAborted;
125
- };
126
-
127
- const isPartial = (file?: File) => {
128
- if (!file) {
129
- return false;
130
- }
131
- if (isFilePartial(file)) {
132
- return true;
133
- }
134
-
135
- return file._embedded?.children?.some(isFilePartial);
136
- };