diff --git a/.changeset/wise-friends-shave.md b/.changeset/wise-friends-shave.md new file mode 100644 index 0000000..d287935 --- /dev/null +++ b/.changeset/wise-friends-shave.md @@ -0,0 +1,5 @@ +--- +'@ssecd/ihs': patch +--- + +Allow requests to be made directly to the base URL diff --git a/README.md b/README.md index 8f7ba58..efc1543 100644 --- a/README.md +++ b/README.md @@ -241,6 +241,22 @@ if (result2.error) { } ``` +### `ihs.request({ ... })` + +Request ke RestAPI secara langsung menggunakan method `request()` dari instansi `IHS`. Sangat berguna saat API atau Endpoint belum diimplementasikan. Contoh: + +```ts +await ihs.request({ + type: 'base', + path: '/masterdata/v1/mastersaranaindex/mastersarana', + searchParams: { + limit: '10', + page: '1', + jenis_sarana: '121' + } +}); +``` + ## Konfigurasi Konfigurasi mengikuti interface berikut: diff --git a/src/ihs.spec.ts b/src/ihs.spec.ts index b377774..f2c29b0 100644 --- a/src/ihs.spec.ts +++ b/src/ihs.spec.ts @@ -1,6 +1,12 @@ -import { describe, expect, it } from 'vitest'; +import { beforeAll, describe, expect, it } from 'vitest'; import IHS, { AuthDetail, DefaultAuthStore, IHSConfig } from './ihs.js'; +let ihs: IHS; + +beforeAll(() => { + ihs = new IHS(); +}); + describe('ihs', () => { it('instance should be object and valid instance', () => { const ihs = new IHS(); @@ -70,8 +76,25 @@ describe('ihs', () => { expect(store.get()).toBeUndefined(); }); - it('get patient resource should be return ok', async () => { - const ihs = new IHS(); + it(`request with base type should be return 200`, async () => { + const response = await ihs.request({ + type: 'base', + path: '/masterdata/v1/mastersaranaindex/mastersarana', + searchParams: { + limit: '1', + page: '1', + jenis_sarana: '121' + } + }); + expect(response.ok).toBe(true); + + const result = await response.json(); + expect(result.status_code).toBe(200); + expect(result.page).toBe(1); + expect(result.data.length).toBe(1); + }); + + it('get patient resource should be valid resource type', async () => { const response = await ihs.fhir(`/Patient/${process.env.TEST_PATIENT_ID}`); expect(response.ok).toBe(true); diff --git a/src/ihs.ts b/src/ihs.ts index dd3f146..a7ae82b 100644 --- a/src/ihs.ts +++ b/src/ihs.ts @@ -44,7 +44,23 @@ export interface IHSConfig { type UserConfig = Partial | (() => MaybePromise>); type RequestConfig = { - type: Exclude; + /** + * Tipe dari RestAPI. Gunakan tipe `base` jika API belum diimplementasikan. + * Contoh: + * + * ```ts + * await ihs.request({ + * type: 'base', + * path: '/masterdata/v1/mastersaranaindex/mastersarana', // <- include endpoint '/masterdata/v1/' + * searchParams: { + * limit: '10', + * page: '1', + * jenis_sarana: '121', + * } + * }) + * ``` + */ + type: 'base' | Exclude; path: string; searchParams?: URLSearchParams | Record | [string, string][]; } & RequestInit; @@ -149,7 +165,8 @@ export default class IHS { async request(config: RequestConfig): Promise { const { mode } = await this.getConfig(); const { type, path, searchParams, ...init } = config; - const url = buildUrl(defaultEndpointUrls[mode][type], path); + const baseUrl = type == 'base' ? defaultBaseUrls[mode] : defaultEndpointUrls[mode][type]; + const url = buildUrl(baseUrl, path); url.search = searchParams ? new URLSearchParams(searchParams).toString() : url.search; const auth = await this.auth(); init.headers = { diff --git a/words.txt b/words.txt index 57edd41..a38c073 100644 --- a/words.txt +++ b/words.txt @@ -16,6 +16,9 @@ kbki kemkes kriptografi lkpp +masterdata +mastersarana +mastersaranaindex OAEP OPTIN OPTOUT @@ -26,6 +29,7 @@ rxterm Silampari simrs Sriwijaya +ssecd subkategori tkdn ucum