Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CAMS-282 updated uriPath functions to properly handle query params #892

Merged
merged 1 commit into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions user-interface/src/lib/models/api2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,21 +57,21 @@ describe('extractPathFromUri', () => {
const api = addAuthHeaderToApi();
api.host = `https://some-domain.gov`;
const expectedPath = '/this/is/a/path';
const uri = `${api.host}${expectedPath}?these=are;the=params`;
const uri = `${api.host}${expectedPath}?these=are&the=params`;

const actualPath = extractPathFromUri(uri, api);
const { uriOrPathSubstring } = extractPathFromUri(uri, api);

expect(actualPath).toEqual(expectedPath);
expect(uriOrPathSubstring).toEqual(expectedPath);
});

test('should return path when given only a path', () => {
const api = addAuthHeaderToApi();
api.host = '';
const expectedPath = '/this/is/a/path';

const actualPath = extractPathFromUri(expectedPath, api);
const { uriOrPathSubstring } = extractPathFromUri(expectedPath, api);

expect(actualPath).toEqual(expectedPath);
expect(uriOrPathSubstring).toEqual(expectedPath);
});
});

Expand Down
33 changes: 26 additions & 7 deletions user-interface/src/lib/models/api2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,18 @@ export function extractPathFromUri(uriOrPath: string, api: ApiClient) {
}

const paramsIndex = uriOrPath.search(/\?.*=/);
const queryParams: ObjectKeyVal = {};
let uriOrPathSubstring: string = '' + uriOrPath;
if (paramsIndex >= 0) {
uriOrPath = uriOrPath.substring(0, paramsIndex);
uriOrPathSubstring = uriOrPath.substring(0, paramsIndex);
const queryParamString = uriOrPath.substring(paramsIndex + 1).split('&');
queryParamString.forEach((param) => {
const splitParams = param.split('=');
queryParams[splitParams[0]] = splitParams[1];
});
}

return uriOrPath;
return { uriOrPathSubstring, queryParams };
}

export function addAuthHeaderToApi(): ApiClient {
Expand All @@ -91,21 +98,28 @@ export function addAuthHeaderToApi(): ApiClient {
export function useGenericApi(): GenericApiClient {
const api = addAuthHeaderToApi();

function justThePath(uriOrPath: string): string {
function justThePath(uriOrPath: string): {
uriOrPathSubstring: string;
queryParams: ObjectKeyVal;
} {
return extractPathFromUri(uriOrPath, api);
}

return {
async get<T = object>(path: string, options?: ObjectKeyVal): Promise<ResponseBody<T>> {
const body = await api.get(justThePath(path), options);
const { uriOrPathSubstring, queryParams } = justThePath(path);
options = { ...options, ...queryParams };
const body = await api.get(uriOrPathSubstring, options);
return body as ResponseBody<T>;
},
async patch<T = object>(
path: string,
body: object,
options?: ObjectKeyVal,
): Promise<ResponseBody<T> | void> {
const responseBody = await api.patch(justThePath(path), body, options);
const { uriOrPathSubstring, queryParams } = justThePath(path);
options = { ...options, ...queryParams };
const responseBody = await api.patch(uriOrPathSubstring, body, options);
if (!responseBody) {
return;
}
Expand All @@ -116,7 +130,9 @@ export function useGenericApi(): GenericApiClient {
body: object,
options?: ObjectKeyVal,
): Promise<ResponseBody<T> | void> {
const responseBody = await api.post(justThePath(path), body, options);
const { uriOrPathSubstring, queryParams } = justThePath(path);
options = { ...options, ...queryParams };
const responseBody = await api.post(uriOrPathSubstring, body, options);
if (!responseBody) {
return;
}
Expand All @@ -127,11 +143,14 @@ export function useGenericApi(): GenericApiClient {
body: object,
options?: ObjectKeyVal,
): Promise<ResponseBody<T>> {
const responseBody = await api.put(justThePath(path), body, options);
const { uriOrPathSubstring, queryParams } = justThePath(path);
options = { ...options, ...queryParams };
const responseBody = await api.put(uriOrPathSubstring, body, options);
return responseBody as ResponseBody<T>;
},
};
}

const api = useGenericApi;

async function getAttorneys() {
Expand Down
Loading