Skip to content

Commit ccb929b

Browse files
committed
add instance index to config and option to update the config service
1 parent f4f7558 commit ccb929b

File tree

5 files changed

+29
-23
lines changed

5 files changed

+29
-23
lines changed

projects/fusio-sdk/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ngx-fusio-sdk",
3-
"version": "5.1.2",
3+
"version": "5.1.3",
44
"description": "SDK to integrate Fusio into an Angular app",
55
"keywords": [
66
"Fusio",

projects/fusio-sdk/src/lib/config/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {CommonMessage, ConsumerPlan, ConsumerUserAccount} from "fusio-sdk";
33
import {GroupItem, Items} from "../service/navigation.service";
44

55
export interface Config {
6+
instance?: number,
67
baseUrl: string,
78
title?: string,
89
version?: string,

projects/fusio-sdk/src/lib/service/api.service.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export abstract class ApiService<T extends ClientAbstract> {
1313
constructor(private config: ConfigService) {
1414
this.baseUrl = '';
1515
this.store = new SessionTokenStore();
16-
this.setBaseUrl(this.config.getBaseUrl());
16+
this.reload();
1717
}
1818

1919
public getClientWithCredentials(clientId: string, clientSecret: string): T {
@@ -53,15 +53,24 @@ export abstract class ApiService<T extends ClientAbstract> {
5353
return this.baseUrl;
5454
}
5555

56-
public setBaseUrl(baseUrl: string) {
57-
this.baseUrl = ApiService.normalizeBaseUrl(baseUrl);
58-
this.store = new SessionTokenStore('fusio_access_token_' + this.config.getInstanceCode());
59-
}
60-
6156
public getTokenStore(): TokenStoreInterface {
6257
return this.store;
6358
}
6459

60+
/**
61+
* Reloads all values from the config
62+
*/
63+
public reload() {
64+
this.baseUrl = ApiService.normalizeBaseUrl(this.config.getBaseUrl());
65+
66+
const instance = this.config.getInstance();
67+
if (instance !== undefined) {
68+
this.store = new SessionTokenStore('fusio_access_token_' + instance);
69+
} else {
70+
this.store = new SessionTokenStore('fusio_access_token');
71+
}
72+
}
73+
6574
public hasValidToken(): boolean {
6675
const token = this.store.get();
6776
if (!token) {

projects/fusio-sdk/src/lib/service/config.service.ts

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ export class ConfigService {
1313

1414
constructor(@Inject(FUSIO_CONFIG) private config: Config) { }
1515

16+
public getInstance(): number|undefined {
17+
return this.config.instance;
18+
}
19+
1620
public getBaseUrl(): string {
1721
return this.config.baseUrl;
1822
}
@@ -146,20 +150,7 @@ export class ConfigService {
146150
}
147151
}
148152

149-
/**
150-
* @see https://stackoverflow.com/questions/7616461/generate-a-hash-from-string-in-javascript
151-
*/
152-
public getInstanceCode(): number {
153-
let hash = 0;
154-
if (this.config.baseUrl.length === 0) {
155-
return hash;
156-
}
157-
158-
for (let i = 0; i < this.config.baseUrl.length; i++) {
159-
hash = ((hash << 5) - hash) + this.config.baseUrl.charCodeAt(i);
160-
hash |= 0;
161-
}
162-
163-
return hash;
153+
public update(config: Config): void {
154+
this.config = config;
164155
}
165156
}

projects/fusio-sdk/src/lib/service/user.service.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ export class UserService {
4646
}
4747

4848
private getKey(): string {
49-
return 'fusio_user_' + this.config.getInstanceCode();
49+
const instance = this.config.getInstance();
50+
if (instance !== undefined) {
51+
return 'fusio_user_' + instance;
52+
} else {
53+
return 'fusio_user';
54+
}
5055
}
5156
}

0 commit comments

Comments
 (0)