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

Add base url config #66

Merged
merged 6 commits into from
Oct 25, 2023
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
5 changes: 5 additions & 0 deletions .changeset/wise-berries-yawn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@ssecd/jkn': minor
---

add custom base url config
22 changes: 20 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,15 @@ function persistSep(sep: VClaimResponse<'sep', 'insertV2'>) {

Konfigurasi mengikuti interface berikut:

```ts
````ts
interface Config {
/**
* Kode PPK yang diberikan BPJS.
*
* Diperlukan untuk melakukan proses encryption
* pada web service eRekam Medis.
*
* @default process.env.JKN_PPK_CODE
*/
ppkCode: string;

Expand Down Expand Up @@ -214,8 +216,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<Record<Type, Record<Mode, string>>>;
}
```
````

## API Tersedia

Expand Down
46 changes: 34 additions & 12 deletions src/fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ export interface Config {
*
* Diperlukan untuk melakukan proses encryption
* pada web service eRekam Medis.
*
* @default process.env.JKN_PPK_CODE
mustofa-id marked this conversation as resolved.
Show resolved Hide resolved
*/
ppkCode: string;

Expand Down Expand Up @@ -95,6 +97,22 @@ export 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<Record<Type, Record<Mode, string>>>;
}

export interface SendOption {
Expand Down Expand Up @@ -140,7 +158,7 @@ export type SendResponse<T> = {
rekamMedis: LowerResponse<T, string>;
};

const api_base_urls: Record<Type, Record<Mode, string>> = {
const defaultBaseUrls: Record<Type, Record<Mode, string>> = {
vclaim: {
development: 'https://apijkn-dev.bpjs-kesehatan.go.id/vclaim-rest-dev',
production: 'https://apijkn.bpjs-kesehatan.go.id/vclaim-rest'
Expand Down Expand Up @@ -181,7 +199,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<Config> | (() => MaybePromise<Partial<Config>>)) {}
Expand All @@ -190,16 +209,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) {
Expand All @@ -209,6 +222,12 @@ export class Fetcher {
this.configured = true;
}

private mergeConfig(target: Config, source: Partial<Config>): Config {
// simple object merge strategy because only baseUrls is typeof object for now
const baseUrls = { ...target.baseUrls, ...source.baseUrls };
return { ...target, ...source, baseUrls };
}

private get userKeyMap(): Record<Type, string | undefined> {
return {
vclaim: this.config.vclaimUserKey,
Expand Down Expand Up @@ -260,7 +279,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 ?? {}) };

Expand Down
25 changes: 25 additions & 0 deletions test/fetcher.test.ts
Original file line number Diff line number Diff line change
@@ -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'
);
});
});