Skip to content

Commit b00c27a

Browse files
committed
feat: ability to instantiate http client without prefix
1 parent c435cc3 commit b00c27a

File tree

4 files changed

+25
-21
lines changed

4 files changed

+25
-21
lines changed

src/httpClient.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ export class HttpClient {
7272
try {
7373
response = await this
7474
.getAxios()
75-
.get(`sanctum/csrf-cookie`, {baseURL: Orion.getHost()});
75+
.get(`sanctum/csrf-cookie`, {baseURL: Orion.getBaseUrl()});
7676
} catch (error) {
7777
throw new Error(
7878
`Unable to retrieve XSRF token cookie due to network error. Please ensure that SANCTUM_STATEFUL_DOMAINS and SESSION_DOMAIN environment variables are configured correctly on the API side.`

src/orion.ts

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { AuthDriver } from './drivers/default/enums/authDriver';
2-
import { HttpClient } from './httpClient';
3-
import axios, { AxiosInstance, AxiosRequestConfig } from 'axios';
1+
import {AuthDriver} from './drivers/default/enums/authDriver';
2+
import {HttpClient} from './httpClient';
3+
import axios, {AxiosInstance, AxiosRequestConfig} from 'axios';
44

55
export class Orion {
6-
protected static host: string;
6+
protected static baseUrl: string;
77
protected static prefix: string;
88
protected static authDriver: AuthDriver;
99
protected static token: string | null = null;
@@ -12,12 +12,12 @@ export class Orion {
1212
protected static makeHttpClientCallback: (() => AxiosInstance) | null = null;
1313

1414
public static init(
15-
host: string,
15+
baseUrl: string,
1616
prefix: string = 'api',
1717
authDriver: AuthDriver = AuthDriver.Default,
1818
token?: string
1919
): void {
20-
Orion.setHost(host);
20+
Orion.setBaseUrl(baseUrl);
2121
if (token) {
2222
Orion.setToken(token);
2323
}
@@ -27,13 +27,13 @@ export class Orion {
2727
this.httpClientConfig = Orion.buildHttpClientConfig();
2828
}
2929

30-
public static setHost(apiUrl: string): Orion {
31-
Orion.host = apiUrl;
30+
public static setBaseUrl(baseUrl: string): Orion {
31+
Orion.baseUrl = baseUrl;
3232
return Orion;
3333
}
3434

35-
public static getHost(): string {
36-
return Orion.host.endsWith('/') ? Orion.host : `${Orion.host}/`;
35+
public static getBaseUrl(): string {
36+
return Orion.baseUrl.endsWith('/') ? Orion.baseUrl : `${Orion.baseUrl}/`;
3737
}
3838

3939
public static setPrefix(prefix: string): Orion {
@@ -57,7 +57,7 @@ export class Orion {
5757
}
5858

5959
public static getApiUrl(): string {
60-
return Orion.getHost() + Orion.getPrefix();
60+
return Orion.getBaseUrl() + Orion.getPrefix();
6161
}
6262

6363
public static setToken(token: string): Orion {
@@ -85,12 +85,16 @@ export class Orion {
8585
return Orion;
8686
}
8787

88-
public static makeHttpClient(baseUrl?: string): HttpClient {
88+
public static makeHttpClient(baseUrl?: string, withPrefix = true): HttpClient {
8989
const client: AxiosInstance = this.makeHttpClientCallback
9090
? this.makeHttpClientCallback()
9191
: axios.create();
9292

93-
return new HttpClient(baseUrl || Orion.getApiUrl(), client, this.getAuthDriver());
93+
if (!baseUrl) {
94+
baseUrl = withPrefix ? Orion.getApiUrl() : Orion.getBaseUrl()
95+
}
96+
97+
return new HttpClient(baseUrl, client, this.getAuthDriver());
9498
}
9599

96100
public static makeHttpClientUsing(callback: () => AxiosInstance): Orion {

tests/unit/model.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Orion } from '../../src/orion';
22
import Post from '../stubs/models/post';
33

4-
Orion.setHost('https://example.com/api');
4+
Orion.setBaseUrl('https://example.com/api');
55

66
describe('Model tests', () => {
77
test('setting and getting key name', () => {

tests/unit/orion.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@ describe('Orion tests', () => {
77
test('initialization', () => {
88
Orion.init('https://example.com', 'custom-prefix', AuthDriver.Passport, 'test-token');
99

10-
expect(Orion.getHost()).toBe('https://example.com/');
10+
expect(Orion.getBaseUrl()).toBe('https://example.com/');
1111
expect(Orion.getPrefix()).toBe('custom-prefix');
1212
expect(Orion.getAuthDriver()).toBe(AuthDriver.Passport);
1313
expect(Orion.getToken()).toBe('test-token');
1414
});
1515

1616
test('getting and setting host', () => {
17-
Orion.setHost('https://example.com/');
17+
Orion.setBaseUrl('https://example.com/');
1818

19-
expect(Orion.getHost()).toBe('https://example.com/');
19+
expect(Orion.getBaseUrl()).toBe('https://example.com/');
2020
});
2121

2222
test('getting and setting prefix', () => {
@@ -26,7 +26,7 @@ describe('Orion tests', () => {
2626
});
2727

2828
test('getting api url', () => {
29-
Orion.setHost('https://example.com/');
29+
Orion.setBaseUrl('https://example.com/');
3030
Orion.setPrefix('api');
3131

3232
expect(Orion.getApiUrl()).toBe('https://example.com/api');
@@ -46,9 +46,9 @@ describe('Orion tests', () => {
4646
});
4747

4848
test('appending slash to the end when getting api url', () => {
49-
Orion.setHost('https://example.com/api');
49+
Orion.setBaseUrl('https://example.com/api');
5050

51-
expect(Orion.getHost()).toBe('https://example.com/api/');
51+
expect(Orion.getBaseUrl()).toBe('https://example.com/api/');
5252
});
5353

5454
test('making http client using user-provided callback', () => {

0 commit comments

Comments
 (0)