Skip to content

Commit 5666cac

Browse files
committed
feat: allow updating storeId after initialization
1 parent 06dc955 commit 5666cac

File tree

6 files changed

+55
-21
lines changed

6 files changed

+55
-21
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
## v0.0.1
44

5-
### [0.0.1](https://github.com/openfga/js-sdk/releases/tag/v0.0.1) (2022-06-09)
5+
### [0.0.1](https://github.com/openfga/js-sdk/releases/tag/v0.0.1) (2022-06-15)
66

77
Initial Release

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ You need your store id to call the OpenFGA API (unless it is to create a store o
116116
```javascript
117117
const { stores } = await openFga.listStores();
118118

119-
// stores = [{ "id": "1uHxCSuTP0VKPYSnkq1pbb1jeZw", "name": "OpenFGA Demo Store", "created_at": "2022-01-01T00:00:00.000Z", "updated_at": "2022-01-01T00:00:00.000Z" }]
119+
// stores = [{ "id": "01FQH7V8BEG3GPQW93KTRFR8JB", "name": "OpenFGA Demo Store", "created_at": "2022-01-01T00:00:00.000Z", "updated_at": "2022-01-01T00:00:00.000Z" }]
120120
```
121121

122122
#### Create Store
@@ -128,7 +128,7 @@ const { id: storeId } = await openFga.createStore({
128128
name: "OpenFGA Demo Store",
129129
});
130130

131-
// storeId = "1uHxCSuTP0VKPYSnkq1pbb1jeZw"
131+
// storeId = "01FQH7V8BEG3GPQW93KTRFR8JB"
132132
```
133133

134134
#### Get Store
@@ -142,7 +142,7 @@ const store = await openFga.getStore({
142142
name: "OpenFGA Demo Store",
143143
});
144144

145-
// stores = { "id": "1uHxCSuTP0VKPYSnkq1pbb1jeZw", "name": "OpenFGA Demo Store", "created_at": "2022-01-01T00:00:00.000Z", "updated_at": "2022-01-01T00:00:00.000Z" }
145+
// store = { "id": "01FQH7V8BEG3GPQW93KTRFR8JB", "name": "OpenFGA Demo Store", "created_at": "2022-01-01T00:00:00.000Z", "updated_at": "2022-01-01T00:00:00.000Z" }
146146
```
147147

148148
#### Delete Store

base.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,12 @@ export class BaseAPI {
4646

4747
this.credentials = Credentials.init(this.configuration);
4848
}
49+
50+
public get storeId() {
51+
return this.configuration.storeId;
52+
}
53+
54+
public set storeId(storeId: string) {
55+
this.configuration.storeId = storeId;
56+
}
4957
}

configuration.ts

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -111,15 +111,15 @@ export class Configuration {
111111
this.apiHost = params.apiHost!;
112112
this.storeId = params.storeId!;
113113

114-
const credentialOpts = params.credentials;
114+
const credentialParams = params.credentials;
115115

116-
if (credentialOpts) {
117-
switch (credentialOpts?.method) {
116+
if (credentialParams) {
117+
switch (credentialParams?.method) {
118118
case CredentialsMethod.ApiToken:
119119
this.credentials = {
120-
method: credentialOpts.method,
120+
method: credentialParams.method,
121121
config: {
122-
token: credentialOpts.config.token!,
122+
token: credentialParams.config.token!,
123123
headerName: "Authorization",
124124
headerValuePrefix: "Bearer",
125125
}
@@ -129,11 +129,11 @@ export class Configuration {
129129
this.credentials = {
130130
method: CredentialsMethod.ClientCredentials,
131131
config: {
132-
// We are enforcing that they exist here, but validating that they set later in isValid
133-
clientId: credentialOpts.config.clientId,
134-
clientSecret: credentialOpts.config.clientSecret,
135-
apiAudience: credentialOpts.config.apiAudience,
136-
apiTokenIssuer: credentialOpts.config.apiTokenIssuer,
132+
// We are only copying them from the passed in params here. We will be validating that they are valid in the Credentials constructor
133+
clientId: credentialParams.config.clientId,
134+
clientSecret: credentialParams.config.clientSecret,
135+
apiAudience: credentialParams.config.apiAudience,
136+
apiTokenIssuer: credentialParams.config.apiTokenIssuer,
137137
}
138138
};
139139
break;
@@ -156,10 +156,11 @@ export class Configuration {
156156
}
157157

158158
/**
159-
* Ensures that the Configuration is valid
160-
* @return boolean
159+
*
160+
* @return {boolean}
161+
* @throws {FgaValidationError}
161162
*/
162-
public isValid() {
163+
public isValid(): boolean {
163164
assertParamExists("Configuration", "apiScheme", this.apiScheme);
164165
assertParamExists("Configuration", "apiHost", this.apiHost);
165166

credentials.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,11 @@ export class Credentials {
119119
}
120120
}
121121

122-
123-
public isValid() {
122+
/**
123+
*
124+
* @throws {FgaValidationError}
125+
*/
126+
public isValid(): void {
124127
const { authConfig } = this;
125128
switch (authConfig?.method) {
126129
case CredentialsMethod.None:

tests/index.test.ts

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ describe("OpenFga SDK", function () {
219219
).toThrowError();
220220
});
221221

222-
it("should not require clientId, clientSecret, apiTokenIssuer and apiAudience in configuration when not needed", () => {
222+
it("should not require credentials in configuration when not needed", () => {
223223
expect(
224224
() =>
225225
new OpenFgaApi({
@@ -229,7 +229,20 @@ describe("OpenFga SDK", function () {
229229
).not.toThrowError();
230230
});
231231

232-
it("should require clientId, clientSecret, apiTokenIssuer and apiAudience in configuration when needed", () => {
232+
it("should require apiToken credentials in configuration in api_token flow", () => {
233+
expect(
234+
() =>
235+
new OpenFgaApi({
236+
storeId: baseConfig.storeId!,
237+
apiHost: baseConfig.apiHost,
238+
credentials: {
239+
method: CredentialsMethod.ApiToken as any
240+
}
241+
})
242+
).toThrowError();
243+
});
244+
245+
it("should require clientId, clientSecret, apiTokenIssuer and apiAudience credentials in configuration in client_credentials flow", () => {
233246
expect(
234247
() =>
235248
new OpenFgaApi({
@@ -332,6 +345,15 @@ describe("OpenFga SDK", function () {
332345
const configuration = new Configuration(baseConfig);
333346
expect(() => new OpenFgaApi(configuration)).not.toThrowError();
334347
});
348+
349+
it("should allow updating the storeId after initialization", async () => {
350+
const openFgaApi = new OpenFgaApi({
351+
apiHost: OPENFGA_API_HOST
352+
});
353+
expect(openFgaApi.storeId).toBe(undefined);
354+
openFgaApi.storeId = OPENFGA_STORE_ID;
355+
expect(openFgaApi.storeId).toBe(OPENFGA_STORE_ID);
356+
});
335357
});
336358

337359
describe("error handling", () => {

0 commit comments

Comments
 (0)