From 336d81998b58def4ec1412c62daecba07b26dbe8 Mon Sep 17 00:00:00 2001 From: mustofa-id Date: Wed, 25 Oct 2023 09:29:06 +0700 Subject: [PATCH 1/6] add baseUrls config --- src/fetcher.ts | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/src/fetcher.ts b/src/fetcher.ts index e7eb74f..f210038 100644 --- a/src/fetcher.ts +++ b/src/fetcher.ts @@ -95,6 +95,8 @@ export interface Config { * @default false */ throw: boolean; + + baseUrls: Partial>>; } export interface SendOption { @@ -140,7 +142,7 @@ export type SendResponse = { rekamMedis: LowerResponse; }; -const api_base_urls: Record> = { +const defaultBaseUrls: Record> = { vclaim: { development: 'https://apijkn-dev.bpjs-kesehatan.go.id/vclaim-rest-dev', production: 'https://apijkn.bpjs-kesehatan.go.id/vclaim-rest' @@ -181,7 +183,8 @@ export class Fetcher { pcareUserKey: process.env.JKN_PCARE_USER_KEY ?? '', icareUserKey: process.env.JKN_ICARE_USER_KEY ?? '', rekamMedisUserKey: process.env.JKN_REKAM_MEDIS_USER_KEY ?? '', - throw: false + throw: false, + baseUrls: defaultBaseUrls }; constructor(private userConfig?: Partial | (() => MaybePromise>)) {} @@ -190,16 +193,10 @@ export class Fetcher { if (!this.userConfig || this.configured) return; if (typeof this.userConfig === 'object') { - this.config = { - ...this.config, - ...this.userConfig - }; + this.config = this.mergeConfig(this.config, this.userConfig); } else if (typeof this.userConfig === 'function') { - const config = await this.userConfig(); - this.config = { - ...this.config, - ...config - }; + const userConfig = await this.userConfig(); + this.config = this.mergeConfig(this.config, userConfig); } if (!this.config.consId || !this.config.consSecret) { @@ -209,6 +206,11 @@ export class Fetcher { this.configured = true; } + private mergeConfig(target: Config, source: Partial): Config { + const baseUrls = { ...target.baseUrls, ...source.baseUrls }; + return { ...target, ...source, baseUrls }; + } + private get userKeyMap(): Record { return { vclaim: this.config.vclaimUserKey, @@ -260,7 +262,10 @@ export class Fetcher { let response = ''; try { - const url = new URL(api_base_urls[type][this.config.mode] + option.path); + const baseUrl = this.config.baseUrls[type]; + if (!baseUrl) throw new Error(`base url of type "${type}" is invalid`); + + const url = new URL(baseUrl[this.config.mode] + option.path); const init: RequestInit = { method: option.method ?? 'GET' }; const headers = { ...this.getDefaultHeaders(type), ...(option.headers ?? {}) }; From f027d7132e56a02d10965a7344ce71450b6de850 Mon Sep 17 00:00:00 2001 From: mustofa-id Date: Wed, 25 Oct 2023 09:29:27 +0700 Subject: [PATCH 2/6] add test --- test/fetcher.test.ts | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 test/fetcher.test.ts diff --git a/test/fetcher.test.ts b/test/fetcher.test.ts new file mode 100644 index 0000000..ef28fef --- /dev/null +++ b/test/fetcher.test.ts @@ -0,0 +1,25 @@ +import { describe, expect, it } from 'vitest'; +import { Fetcher } from '../src/fetcher'; + +describe('Fetcher', () => { + it.concurrent('validate config', async () => { + const userConfig = { + ppkCode: '12345', + baseUrls: { + pcare: { + development: 'http://dev.example.com', + production: 'http://example.com' + } + } + }; + const fetcher = new Fetcher(userConfig); + const config = await fetcher.getConfig(); + + expect(config.ppkCode).toBe(userConfig.ppkCode); + expect(config.baseUrls.pcare).toEqual(userConfig.baseUrls.pcare); + expect(config.baseUrls.pcare?.development).toBe(userConfig.baseUrls.pcare.development); + expect(config.baseUrls.vclaim?.development).toBe( + 'https://apijkn-dev.bpjs-kesehatan.go.id/vclaim-rest-dev' + ); + }); +}); From c296ad8866b27dff1ae4d0c2940e6ec9f50a797a Mon Sep 17 00:00:00 2001 From: mustofa-id Date: Wed, 25 Oct 2023 09:37:47 +0700 Subject: [PATCH 3/6] add docs --- README.md | 20 ++++++++++++++++++-- src/fetcher.ts | 17 +++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 74bbd6e..ac486f9 100644 --- a/README.md +++ b/README.md @@ -125,7 +125,7 @@ function persistSep(sep: VClaimResponse<'sep', 'insertV2'>) { Konfigurasi mengikuti interface berikut: -```ts +````ts interface Config { /** * Kode PPK yang diberikan BPJS. @@ -214,8 +214,24 @@ interface Config { * @default false */ throw: boolean; + + /** + * Base URL web service dari BPJS. Secara default sudah diatur + * berdasarkan base url yang ada di TrustMark. Nilai dapat diatur + * secara partial, misalnya: + * + * ``` + * baseUrls: { + * vclaim: { + * development: 'http://dev.example.com', + * production: 'http://prod.example.com' + * } + * } + * ``` + */ + baseUrls: Partial>>; } -``` +```` ## API Tersedia diff --git a/src/fetcher.ts b/src/fetcher.ts index f210038..64ff391 100644 --- a/src/fetcher.ts +++ b/src/fetcher.ts @@ -13,6 +13,8 @@ export interface Config { * * Diperlukan untuk melakukan proses encryption * pada web service eRekam Medis. + * + * @default process.env.JKN_PPK_CODE */ ppkCode: string; @@ -96,6 +98,20 @@ export interface Config { */ throw: boolean; + /** + * Base URL web service dari BPJS. Secara default sudah diatur + * berdasarkan base url yang ada di TrustMark. Nilai dapat diatur + * secara partial, misalnya: + * + * ``` + * baseUrls: { + * vclaim: { + * development: 'http://dev.example.com', + * production: 'http://prod.example.com' + * } + * } + * ``` + */ baseUrls: Partial>>; } @@ -207,6 +223,7 @@ export class Fetcher { } private mergeConfig(target: Config, source: Partial): Config { + // simple object merge strategy because only baseUrls is typeof object for now const baseUrls = { ...target.baseUrls, ...source.baseUrls }; return { ...target, ...source, baseUrls }; } From 8f7681d4cf7a9c8fb12027604b92fbe6d3db9722 Mon Sep 17 00:00:00 2001 From: mustofa-id Date: Wed, 25 Oct 2023 09:39:21 +0700 Subject: [PATCH 4/6] add changeset --- .changeset/wise-berries-yawn.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/wise-berries-yawn.md diff --git a/.changeset/wise-berries-yawn.md b/.changeset/wise-berries-yawn.md new file mode 100644 index 0000000..c1d6bf7 --- /dev/null +++ b/.changeset/wise-berries-yawn.md @@ -0,0 +1,5 @@ +--- +'@ssecd/jkn': minor +--- + +add custom base url config From 3dda1b0a197fece2c5504abb16b7aa77c3d97e5e Mon Sep 17 00:00:00 2001 From: mustofa-id Date: Wed, 25 Oct 2023 09:47:31 +0700 Subject: [PATCH 5/6] update config docs --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index ac486f9..40f1206 100644 --- a/README.md +++ b/README.md @@ -132,6 +132,8 @@ interface Config { * * Diperlukan untuk melakukan proses encryption * pada web service eRekam Medis. + * + * @default process.env.JKN_PPK_CODE */ ppkCode: string; From e5162c19f5463f78ac8512fbe869ec850a288c2c Mon Sep 17 00:00:00 2001 From: mustofa-id Date: Wed, 25 Oct 2023 09:48:22 +0700 Subject: [PATCH 6/6] format docs --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 40f1206..ba2542f 100644 --- a/README.md +++ b/README.md @@ -132,7 +132,7 @@ interface Config { * * Diperlukan untuk melakukan proses encryption * pada web service eRekam Medis. - * + * * @default process.env.JKN_PPK_CODE */ ppkCode: string;