Skip to content

Commit

Permalink
CAMS-282 updated uriPath functions to properly handle query params (#892
Browse files Browse the repository at this point in the history
)

Jira ticket: CAMS-282

Co-authored-by: Fritz Madden <[email protected]>
Co-authored-by: James Brooks <[email protected]>
  • Loading branch information
3 people authored Sep 12, 2024
1 parent 9c46e49 commit daebc2d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 12 deletions.
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

0 comments on commit daebc2d

Please sign in to comment.