Skip to content

Commit

Permalink
(Refactor)O3-2815: Replace usages of '/ws/rest/v1' with restBaseUrl
Browse files Browse the repository at this point in the history
  • Loading branch information
jwnasambu authored and denniskigen committed Feb 24, 2024
1 parent dbc0a88 commit 8a89f07
Show file tree
Hide file tree
Showing 57 changed files with 295 additions and 254 deletions.
2 changes: 1 addition & 1 deletion e2e/fixtures/api.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { APIRequestContext, PlaywrightWorkerArgs, WorkerFixture } from '@playwright/test';
import { type APIRequestContext, type PlaywrightWorkerArgs, type WorkerFixture } from '@playwright/test';

/**
* A fixture which initializes an [`APIRequestContext`](https://playwright.dev/docs/api/class-apirequestcontext)
Expand Down
4 changes: 2 additions & 2 deletions packages/esm-form-engine-app/src/hooks/useFormSchema.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import useSWR from 'swr';

import { openmrsFetch } from '@openmrs/esm-framework';
import { openmrsFetch, restBaseUrl } from '@openmrs/esm-framework';
import { type OHRIFormSchema } from '@openmrs/openmrs-form-engine-lib';

/**
Expand All @@ -10,7 +10,7 @@ import { type OHRIFormSchema } from '@openmrs/openmrs-form-engine-lib';
* @returns An object containing the form schema, error, and loading state
*/
const useFormSchema = (formUuid: string) => {
const url = formUuid ? `/ws/rest/v1/o3/forms/${formUuid}` : null;
const url = formUuid ? `${restBaseUrl}/o3/forms/${formUuid}` : null;

const { data, error, isLoading } = useSWR<{ data: OHRIFormSchema }>(url, openmrsFetch);

Expand Down
11 changes: 6 additions & 5 deletions packages/esm-form-entry-app/src/app/offline/caching.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
messageOmrsServiceWorker,
omrsOfflineCachingStrategyHttpHeaderName,
openmrsFetch,
restBaseUrl,
setupDynamicOfflineDataHandler,
subscribePrecacheStaticDependencies,
} from '@openmrs/esm-framework';
Expand All @@ -12,8 +13,8 @@ import type { FormSchema } from '../types';
export function setupStaticDataOfflinePrecaching() {
subscribePrecacheStaticDependencies(async () => {
const urlsToCache = [
'/ws/rest/v1/location?q=&v=custom:(uuid,display)',
'/ws/rest/v1/provider?q=&v=custom:(uuid,display,person:(uuid))',
`${restBaseUrl}/location?q=&v=custom:(uuid,display)`,
`${restBaseUrl}/provider?q=&v=custom:(uuid,display,person:(uuid))`,
];

await Promise.all(
Expand Down Expand Up @@ -66,7 +67,7 @@ export function setupDynamicOfflineFormDataHandler() {
}

async function getCacheableFormUrls(formUuid: string) {
const getFormRes = await openmrsFetch<FormSchema>(`/ws/rest/v1/o3/forms/${formUuid}`);
const getFormRes = await openmrsFetch<FormSchema>(`${restBaseUrl}/o3/forms/${formUuid}`);
const form = getFormRes.data;

if (!form) {
Expand All @@ -78,11 +79,11 @@ async function getCacheableFormUrls(formUuid: string) {
// - https://github.com/openmrs/openmrs-esm-patient-chart/blob/415790e1ad9b8bdbd1201958d21a06fa93ec7237/packages/esm-form-entry-app/src/app/openmrs-api/form-resource.service.ts#L21
// - https://github.com/openmrs/openmrs-esm-patient-chart/blob/415790e1ad9b8bdbd1201958d21a06fa93ec7237/packages/esm-form-entry-app/src/app/form-schema/form-schema.service.ts#L31
// - https://github.com/openmrs/openmrs-esm-patient-chart/blob/415790e1ad9b8bdbd1201958d21a06fa93ec7237/packages/esm-form-entry-app/src/app/form-schema/form-schema.service.ts#L164
`/ws/rest/v1/form/${formUuid}?v=full`,
`${restBaseUrl}/form/${formUuid}?v=full`,

// Required by:
// - https://github.com/openmrs/openmrs-esm-patient-chart/blob/415790e1ad9b8bdbd1201958d21a06fa93ec7237/packages/esm-form-entry-app/src/app/openmrs-api/form-resource.service.ts#L10
// - https://github.com/openmrs/openmrs-esm-patient-chart/blob/415790e1ad9b8bdbd1201958d21a06fa93ec7237/packages/esm-form-entry-app/src/app/form-schema/form-schema.service.ts#L167
`/ws/rest/v1/o3/forms/${formUuid}`,
`${restBaseUrl}/o3/forms/${formUuid}`,
].filter(Boolean);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { TestBed } from '@angular/core/testing';
import { EncounterResourceService } from './encounter-resource.service';
import { LocalStorageService } from '../local-storage/local-storage.service';
import { OpenmrsApiModule } from './openmrs-api.module';
import { restBaseUrl } from '@openmrs/esm-framework';

describe('EncounterResourceService', () => {
let httpMock: HttpTestingController;
Expand Down Expand Up @@ -97,7 +98,7 @@ describe('EncounterResourceService', () => {
links: [
{
rel: 'self',
uri: 'https://amrs.ampath.or.ke:8443/amrs/ws/rest/v1/location',
uri: `https://amrs.ampath.or.ke:8443/amrs${restBaseUrl}/location`,
},
],
},
Expand All @@ -107,7 +108,7 @@ describe('EncounterResourceService', () => {
links: [
{
rel: 'self',
uri: 'https://amrs.ampath.or.ke:8443/amrs/ws/rest/v1/encountertype',
uri: `https://amrs.ampath.or.ke:8443/amrs${restBaseUrl}/encountertype`,
},
],
},
Expand Down Expand Up @@ -196,7 +197,7 @@ describe('EncounterResourceService', () => {
display: '',
links: [
{
uri: 'https://test1.ampath.or.ke:8443/amrs/ws/rest/v1/',
uri: `https://test1.ampath.or.ke:8443/amrs${restBaseUrl}`,
rel: 'self',
},
],
Expand All @@ -206,7 +207,7 @@ describe('EncounterResourceService', () => {
display: 'Location-5',
links: [
{
uri: 'https://test1.ampath.or.ke:8443/amrs/ws/rest/v1',
uri: `https://test1.ampath.or.ke:8443/amrs${restBaseUrl}`,
rel: 'self',
},
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ describe('FormResourceService Unit Tests', () => {

const req = httpMock.expectOne(winRef.openmrsRestBase.trim() + 'form/' + uuid + '?v=full');
expect(req.request.method).toBe('GET');
expect(req.request.urlWithParams).toContain('/ws/rest/v1/form/form-uuid?v=full');
expect(req.request.urlWithParams).toContain(`/ws/rest/v1/form/form-uuid?v=full`);
}));

it('should make API call with correct URL when getFormMetaDataByUuid is invoked with v', fakeAsync(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { HttpClient } from '@angular/common/http';
import { WindowRef } from '../window-ref';
import { Form } from '@openmrs/ngx-formentry';
import { MetaData } from '../types';
import { parseDate, showSnackbar } from '@openmrs/esm-framework';
import { parseDate, restBaseUrl, showSnackbar } from '@openmrs/esm-framework';
import { SingleSpaPropsService } from '../single-spa-props/single-spa-props.service';
import { EncounterResourceService } from './encounter-resource.service';
import dayjs from 'dayjs';
Expand All @@ -19,7 +19,7 @@ export class ProgramResourceService {
) {}

private programEnrollmentUrl(): string {
return `${this.windowRef.nativeWindow.openmrsBase}/ws/rest/v1/programenrollment`;
return `${this.windowRef.nativeWindow.openmrsBase}${restBaseUrl}/programenrollment`;
}

public handlePatientCareProgram(form: Form, encounterUuid: string): void {
Expand Down
3 changes: 2 additions & 1 deletion packages/esm-form-entry-app/src/app/window-ref.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Injectable } from '@angular/core';
import { restBaseUrl } from '@openmrs/esm-framework';

function _window() {
// return the global native browser window object
Expand All @@ -14,6 +15,6 @@ export class WindowRef {
}

get openmrsRestBase(): string {
return this.nativeWindow.openmrsBase + '/ws/rest/v1/';
return this.nativeWindow.openmrsBase + restBaseUrl;
}
}
18 changes: 9 additions & 9 deletions packages/esm-form-entry-app/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'reflect-metadata';
import 'zone.js';
import { defineConfigSchema, messageOmrsServiceWorker } from '@openmrs/esm-framework';
import { defineConfigSchema, fhirBaseUrl, messageOmrsServiceWorker, restBaseUrl } from '@openmrs/esm-framework';
import { setupDynamicOfflineFormDataHandler, setupStaticDataOfflinePrecaching } from './app/offline/caching';
import { configSchema } from './config-schema';

Expand All @@ -22,42 +22,42 @@ export function startupApp() {

messageOmrsServiceWorker({
type: 'registerDynamicRoute',
pattern: '.+/ws/fhir2/R4/Observation.+',
pattern: `.+${fhirBaseUrl}/Observation.+`,
});

messageOmrsServiceWorker({
type: 'registerDynamicRoute',
pattern: '.+/ws/rest/v1/obs.+',
pattern: `.+${restBaseUrl}/obs.+`,
});

messageOmrsServiceWorker({
type: 'registerDynamicRoute',
pattern: '.+/ws/rest/v1/session.*',
pattern: `.+${restBaseUrl}/session.*`,
});

messageOmrsServiceWorker({
type: 'registerDynamicRoute',
pattern: '.+/ws/rest/v1/provider.*',
pattern: `.+${restBaseUrl}/provider.*`,
});

messageOmrsServiceWorker({
type: 'registerDynamicRoute',
pattern: '.+/ws/rest/v1/location.*',
pattern: `.+${restBaseUrl}/location.*`,
});

messageOmrsServiceWorker({
type: 'registerDynamicRoute',
pattern: '.+/ws/rest/v1/person.*',
pattern: `.+${restBaseUrl}/person.*`,
});

messageOmrsServiceWorker({
type: 'registerDynamicRoute',
pattern: '.+/ws/rest/v1/form.*',
pattern: `.+${restBaseUrl}/form.*`,
});

messageOmrsServiceWorker({
type: 'registerDynamicRoute',
pattern: '.+/ws/rest/v1/o3/forms.*',
pattern: `.+${restBaseUrl}/o3/forms.*`,
});

defineConfigSchema(moduleName, configSchema);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import useSWRImmutable from 'swr/immutable';
import { openmrsFetch, useConfig } from '@openmrs/esm-framework';
import { openmrsFetch, restBaseUrl, useConfig } from '@openmrs/esm-framework';
import type { OpenMRSResource } from '../../types';
import { AllergenType } from '../../types';
import useSWR from 'swr';
Expand Down Expand Up @@ -54,7 +54,7 @@ export interface AllergicReaction {
export function useAllergicReactions() {
const allergyReactionUuid = useConfig().concepts.allergyReactionUuid;
const { data: allergicReactionsData, isLoading } = useSWRImmutable<{ data: ConceptFetchResponse }>(
`/ws/rest/v1/concept/${allergyReactionUuid}?v=full`,
`${restBaseUrl}/concept/${allergyReactionUuid}?v=full`,
openmrsFetch,
);

Expand All @@ -74,15 +74,15 @@ export function useAllergens() {
} = useConfig();

const { data: drugAllergenData } = useSWRImmutable<{ data: ConceptFetchResponse }>(
`/ws/rest/v1/concept/${drugAllergenUuid}?v=full`,
`${restBaseUrl}/concept/${drugAllergenUuid}?v=full`,
openmrsFetch,
);
const { data: environmentalAllergenData } = useSWRImmutable<{ data: ConceptFetchResponse }>(
`/ws/rest/v1/concept/${environmentalAllergenUuid}?v=full`,
`${restBaseUrl}/concept/${environmentalAllergenUuid}?v=full`,
openmrsFetch,
);
const { data: foodAllergenData } = useSWRImmutable<{ data: ConceptFetchResponse }>(
`/ws/rest/v1/concept/${foodAllergenUuid}?v=full`,
`${restBaseUrl}/concept/${foodAllergenUuid}?v=full`,
openmrsFetch,
);

Expand Down Expand Up @@ -119,7 +119,7 @@ export function useAllergens() {
export function useAllergenSearch(allergenToLookup: string) {
const allergenConceptClassUuid = useConfig()?.concepts.allergenUuid;

const searchURL = `/ws/rest/v1/conceptsearch?conceptClasses=${allergenConceptClassUuid}&q=${allergenToLookup}`;
const searchURL = `${restBaseUrl}/conceptsearch?conceptClasses=${allergenConceptClassUuid}&q=${allergenToLookup}`;

const { data, error, isLoading } = useSWR<{ data: { results: Array<CodedAllergen> } }, Error>(
allergenToLookup ? searchURL : null,
Expand All @@ -134,7 +134,7 @@ export function useAllergenSearch(allergenToLookup: string) {
}

export function saveAllergy(payload: NewAllergy, patientUuid: string, abortController: AbortController) {
return openmrsFetch(`/ws/rest/v1/patient/${patientUuid}/allergy`, {
return openmrsFetch(`${restBaseUrl}/patient/${patientUuid}/allergy`, {
headers: {
'Content-Type': 'application/json',
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import useSWR from 'swr';
import { map } from 'rxjs/operators';
import capitalize from 'lodash-es/capitalize';
import { fhirBaseUrl, openmrsFetch, openmrsObservableFetch } from '@openmrs/esm-framework';
import { fhirBaseUrl, openmrsFetch, openmrsObservableFetch, restBaseUrl } from '@openmrs/esm-framework';
import { type FHIRAllergy, type FHIRAllergyResponse, type ReactionSeverity } from '../types';

export type Allergy = {
Expand Down Expand Up @@ -85,7 +85,7 @@ export function saveAllergy(patientAllergy: any, patientUuid: string, abortContr
};
});

return openmrsFetch(`/ws/rest/v1/patient/${patientUuid}/allergy`, {
return openmrsFetch(`${restBaseUrl}/patient/${patientUuid}/allergy`, {
headers: {
'Content-Type': 'application/json',
},
Expand Down Expand Up @@ -121,7 +121,7 @@ export function updatePatientAllergy(
};
});

return openmrsFetch(`/ws/rest/v1/patient/${patientUuid}/allergy/${allergyUuid.allergyUuid}`, {
return openmrsFetch(`${restBaseUrl}/patient/${patientUuid}/allergy/${allergyUuid.allergyUuid}`, {
headers: {
'Content-Type': 'application/json',
},
Expand All @@ -144,7 +144,7 @@ export function updatePatientAllergy(
}

export function deletePatientAllergy(patientUuid: string, allergyUuid: any, abortController: AbortController) {
return openmrsFetch(`/ws/rest/v1/patient/${patientUuid}/allergy/${allergyUuid.allergyUuid}`, {
return openmrsFetch(`${restBaseUrl}/patient/${patientUuid}/allergy/${allergyUuid.allergyUuid}`, {
method: 'DELETE',
signal: abortController.signal,
});
Expand Down
5 changes: 3 additions & 2 deletions packages/esm-patient-allergies-app/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
getAsyncLifecycle,
getSyncLifecycle,
messageOmrsServiceWorker,
restBaseUrl,
translateFrom,
} from '@openmrs/esm-framework';
import { createDashboardLink, registerWorkspace } from '@openmrs/esm-patient-common-lib';
Expand All @@ -24,12 +25,12 @@ export const importTranslation = require.context('../translations', false, /.jso
export function startupApp() {
messageOmrsServiceWorker({
type: 'registerDynamicRoute',
pattern: '.+/ws/rest/v1/concept.+',
pattern: `.+${restBaseUrl}/concept.+`,
});

messageOmrsServiceWorker({
type: 'registerDynamicRoute',
pattern: '.+/ws/rest/v1/patient/.+/allergy.+',
pattern: `.+${restBaseUrl}/patient/.+/allergy.+`,
});

messageOmrsServiceWorker({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import dayjs from 'dayjs';
import useSWR from 'swr';
import { openmrsFetch } from '@openmrs/esm-framework';
import { openmrsFetch, restBaseUrl } from '@openmrs/esm-framework';
import { type AppointmentsFetchResponse } from '../types';
import isToday from 'dayjs/plugin/isToday';
dayjs.extend(isToday);

const appointmentsSearchUrl = `/ws/rest/v1/appointments/search`;
const appointmentsSearchUrl = `${restBaseUrl}/appointments/search`;

export function useAppointments(patientUuid: string, startDate: string, abortController: AbortController) {
/*
Expand Down Expand Up @@ -62,7 +62,7 @@ export const cancelAppointment = async (toStatus: string, appointmentUuid: strin
const omrsDateFormat = 'YYYY-MM-DDTHH:mm:ss.SSSZZ';
const timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;
const statusChangeTime = dayjs(new Date()).format(omrsDateFormat);
const url = `/ws/rest/v1/appointments/${appointmentUuid}/status-change`;
const url = `${restBaseUrl}/appointments/${appointmentUuid}/status-change`;
return await openmrsFetch(url, {
body: { toStatus, onDate: statusChangeTime, timeZone: timeZone },
method: 'POST',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import useSWRImmutable from 'swr/immutable';
import { openmrsFetch } from '@openmrs/esm-framework';
import { openmrsFetch, restBaseUrl } from '@openmrs/esm-framework';

export interface GlobalProperty {
uuid: string;
Expand All @@ -15,7 +15,7 @@ export const useAllowedExtensions = () => {
// Global property from the attachments backend module https://github.com/openmrs/openmrs-module-attachments/blob/master/api/src/main/java/org/openmrs/module/attachments/AttachmentsConstants.java that contains the allowed file extensions
const allowedExtensionsGlobalProperty: string = 'attachments.allowedFileExtensions';
const customRepresentation = 'custom:(value)';
const url = `/ws/rest/v1/systemsetting?&v=${customRepresentation}&q=${allowedExtensionsGlobalProperty}`;
const url = `${restBaseUrl}/systemsetting?&v=${customRepresentation}&q=${allowedExtensionsGlobalProperty}`;

const { data, error, isLoading } = useSWRImmutable<{ data: { results: Array<GlobalProperty> } }>(url, openmrsFetch);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { useMemo } from 'react';
import useSWR from 'swr';
import { type FetchResponse, openmrsFetch } from '@openmrs/esm-framework';
import { type FetchResponse, openmrsFetch, restBaseUrl } from '@openmrs/esm-framework';

const customRepresentation =
'custom:(display,uuid,personA:(uuid,age,display),personB:(uuid,age,display),relationshipType:(uuid,display,description,aIsToB,bIsToA))';

export function useRelationships(patientUuid: string) {
const url = patientUuid ? `/ws/rest/v1/relationship?person=${patientUuid}&v=${customRepresentation}` : null;
const url = patientUuid ? `${restBaseUrl}/relationship?person=${patientUuid}&v=${customRepresentation}` : null;
const { data, error, isLoading, isValidating } = useSWR<FetchResponse<RelationshipsResponse>, Error>(
url,
openmrsFetch,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { openmrsFetch, useConfig } from '@openmrs/esm-framework';
import { openmrsFetch, restBaseUrl, useConfig } from '@openmrs/esm-framework';
import useSWRImmutable from 'swr/immutable';
import { type ConfigObject } from '../config-schema';
import { type Patient } from '../types';
Expand All @@ -15,7 +15,7 @@ const customRepresentation =
*/
export const usePatientAttributes = (patientUuid: string | null) => {
const { data, error, isLoading } = useSWRImmutable<{ data: Patient }>(
patientUuid ? `/ws/rest/v1/patient/${patientUuid}?v=${customRepresentation}` : null,
patientUuid ? `${restBaseUrl}/patient/${patientUuid}?v=${customRepresentation}` : null,
openmrsFetch,
);

Expand Down
Loading

0 comments on commit 8a89f07

Please sign in to comment.