Skip to content

Commit 8d62a10

Browse files
authored
Enhance library to fully support modular architecture release (#5)
* Enhance library to fully support modular architecture release * 0.2.0 * 0.1.1 * Add global context to support constants
1 parent 8436764 commit 8d62a10

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

74 files changed

+4420
-677
lines changed

.eslintignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
package.json
2-
config
2+
config
3+
dist/

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# mod-arch-shared
1+
# Modular Architecture Shared Library
22

33
## Overview
44

__tests__/__mocks__/mockGenericResponse.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ type MockGenericResponse = {
44
description?: string;
55
};
66

7-
87
export const mockGenericResponse = ({
98
name = 'test',
109
description = 'test',

__tests__/__mocks__/utils.ts

Lines changed: 0 additions & 5 deletions
This file was deleted.

api/__tests__/errorUtils.spec.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ import { NotReadyError } from '~/utilities/useFetchState';
22
import { APIError } from '~/api/types';
33
import { handleRestFailures } from '~/api/errorUtils';
44
import { mockGenericResponse } from '~/__tests__/__mocks__/mockGenericResponse';
5-
import { mockBFFResponse } from '~/__tests__/__mocks__/utils';
5+
import { mockModArchResponse } from '~/api/';
66

77
describe('handleRestFailures', () => {
88
it('should successfully return registered models', async () => {
99
const modelRegistryMock = mockGenericResponse({});
10-
const result = await handleRestFailures(Promise.resolve(mockBFFResponse(modelRegistryMock)));
10+
const result = await handleRestFailures(
11+
Promise.resolve(mockModArchResponse(modelRegistryMock)),
12+
);
1113
expect(result.data).toStrictEqual(modelRegistryMock);
1214
});
1315

api/apiUtils.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { APIOptions } from '~/api/types';
22
import { EitherOrNone } from '~/types/typeHelpers';
3-
import { BFFBody } from '~/types';
4-
import { AUTH_HEADER, MOCK_AUTH } from '~/utilities/const';
3+
import { ModArchBody } from '~/types';
54

65
export const mergeRequestInit = (
76
opts: APIOptions = {},
@@ -65,14 +64,10 @@ const callRestJSON = <T>(
6564
requestData = JSON.stringify(data);
6665
}
6766

68-
// Workaround if we wanna force in a call to add the AUTH_HEADER
69-
const authHeader = Object.keys(otherOptions.headers || {}).some((key) => key === AUTH_HEADER);
70-
7167
return fetch(`${host}${path}${searchParams ? `?${searchParams}` : ''}`, {
7268
...otherOptions,
7369
headers: {
7470
...otherOptions.headers,
75-
...(MOCK_AUTH && !authHeader && { [AUTH_HEADER]: localStorage.getItem(AUTH_HEADER) }),
7671
...(contentType && { 'Content-Type': contentType }),
7772
},
7873
method,
@@ -177,15 +172,15 @@ export const restDELETE = <T>(
177172
parseJSON: options?.parseJSON,
178173
});
179174

180-
export const isModelRegistryResponse = <T>(response: unknown): response is BFFBody<T> => {
175+
export const isModArchResponse = <T>(response: unknown): response is ModArchBody<T> => {
181176
if (typeof response === 'object' && response !== null) {
182177
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
183-
const bffBody = response as { data?: T };
184-
return bffBody.data !== undefined;
178+
const modArchBody = response as { data?: T };
179+
return modArchBody.data !== undefined;
185180
}
186181
return false;
187182
};
188183

189-
export const assembleBFFBody = <T>(data: T): BFFBody<T> => ({
184+
export const assembleModArchBody = <T>(data: T): ModArchBody<T> => ({
190185
data,
191186
});

api/const.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { ModArchBody } from '~/types';
2+
3+
export const mockModArchResponse = <T>(data: T): ModArchBody<T> => ({
4+
data,
5+
});

api/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ export * from './apiUtils';
33
export * from './errorUtils';
44
export * from './types';
55
export * from './useAPIState';
6-
export * from './k8s';
6+
export * from './k8s';
7+
export * from './const';

api/k8s.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,34 @@
11
import { APIOptions } from '~/api/types';
22
import { handleRestFailures } from '~/api/errorUtils';
3-
import { isModelRegistryResponse, restGET } from '~/api/apiUtils';
4-
import { BFF_API_VERSION } from '~/utilities/const';
5-
import { URL_PREFIX } from '~/utilities/const';
3+
import { isModArchResponse, restGET } from '~/api/apiUtils';
64
import { Namespace, UserSettings } from '~/types';
75

6+
// Define a type for the config needed by these functions
7+
type K8sApiConfig = {
8+
BFF_API_VERSION: string;
9+
URL_PREFIX: string;
10+
};
811

12+
// Functions now accept config object
913
export const getUser =
10-
(hostPath: string) =>
14+
(hostPath: string, config: K8sApiConfig) =>
1115
(opts: APIOptions): Promise<UserSettings> =>
1216
handleRestFailures(
13-
restGET(hostPath, `${URL_PREFIX}/api/${BFF_API_VERSION}/user`, {}, opts),
17+
restGET(hostPath, `${config.URL_PREFIX}/api/${config.BFF_API_VERSION}/user`, {}, opts),
1418
).then((response) => {
15-
if (isModelRegistryResponse<UserSettings>(response)) {
19+
if (isModArchResponse<UserSettings>(response)) {
1620
return response.data;
1721
}
1822
throw new Error('Invalid response format');
1923
});
2024

2125
export const getNamespaces =
22-
(hostPath: string) =>
26+
(hostPath: string, config: K8sApiConfig) =>
2327
(opts: APIOptions): Promise<Namespace[]> =>
2428
handleRestFailures(
25-
restGET(hostPath, `${URL_PREFIX}/api/${BFF_API_VERSION}/namespaces`, {}, opts),
29+
restGET(hostPath, `${config.URL_PREFIX}/api/${config.BFF_API_VERSION}/namespaces`, {}, opts),
2630
).then((response) => {
27-
if (isModelRegistryResponse<Namespace[]>(response)) {
31+
if (isModArchResponse<Namespace[]>(response)) {
2832
return response.data;
2933
}
3034
throw new Error('Invalid response format');

api/useAPIState.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as React from 'react';
22
import { APIState } from '~/api/types';
33

4-
const useAPIState = <T>(
4+
export const useAPIState = <T>(
55
hostPath: string | null,
66
createAPI: (path: string) => T,
77
): [apiState: APIState<T>, refreshAPIState: () => void] => {
@@ -28,5 +28,3 @@ const useAPIState = <T>(
2828

2929
return [apiState, refreshAPIState];
3030
};
31-
32-
export default useAPIState;

0 commit comments

Comments
 (0)