@singularlogic/coreplatts 0.0.7 → 0.0.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { AxiosInstance } from 'axios';
1
+ import { AxiosInstance, AxiosRequestConfig } from 'axios';
2
2
 
3
3
  interface Updated {
4
4
  by?: string;
@@ -135,6 +135,7 @@ interface Bucket {
135
135
  creation_date?: Date;
136
136
  }
137
137
 
138
+ type TokenProvider = () => string | Promise<string>;
138
139
  interface IClient$1 {
139
140
  login(email: string, password: string): Promise<OidcToken>;
140
141
  myUser(token?: string): Promise<User$1>;
@@ -142,33 +143,69 @@ interface IClient$1 {
142
143
  myOrganizations(token?: string): Promise<Group$1[]>;
143
144
  organizationByID(id: string, token?: string): Promise<Group$1>;
144
145
  organizationByName(name: string, token?: string): Promise<Group$1>;
146
+ addToOrganization(organization: string, users: UserRoles, token?: string): Promise<void>;
147
+ register(email: string, password: string, password_confirm: string, firstName: string, lastName: string): Promise<void>;
145
148
  createOrganization(name: string, token?: string): Promise<Bucket>;
146
- addToOrganization(organization: string, users: UserRoles, token?: string): void;
149
+ removeOrganizationByID(organization: string, token?: string): Promise<Bucket>;
147
150
  getFolderByName(name: string, token?: string): Promise<Folder>;
148
151
  getFolderByID(id: string, token?: string): Promise<Folder>;
149
- removeOrganizationByID(organization: string, token?: string): Promise<Bucket>;
150
- register(email: string, password: string, password_confirm: string, firstName: string, lastName: string): void;
151
152
  }
153
+ /**
154
+ * Client supports:
155
+ * - Keycloak mode: setTokenProvider(() => auth.getValidToken())
156
+ * - Legacy mode: login(email, pass) -> stores tokens in sessionStorage
157
+ *
158
+ * For SPAs with Keycloak: DO NOT store tokens. Use setTokenProvider.
159
+ */
152
160
  declare class Client implements IClient$1 {
153
161
  private _userConsumer;
154
162
  private _groupConsumer;
155
163
  private _bucketConsumer;
156
164
  private _folderConsumer;
157
- constructor(_managementURL: string, _accountURL: string);
165
+ private _axiosAccount;
166
+ private _axiosManagement;
167
+ private _tokenProvider?;
168
+ constructor(managementURL: string, accountURL: string);
169
+ /**
170
+ * Set a token provider (Keycloak-friendly).
171
+ * Example from Angular:
172
+ * client.setTokenProvider(() => auth.getValidToken())
173
+ */
174
+ setTokenProvider(provider: TokenProvider): void;
175
+ /**
176
+ * Optional legacy mode.
177
+ * If you call login(), it stores tokens in sessionStorage
178
+ * and the default token provider will read them.
179
+ *
180
+ * For Keycloak SPA: do NOT use this.
181
+ */
182
+ login(email: string, password: string): Promise<OidcToken>;
158
183
  setSession(session: OidcToken): void;
159
184
  getSessionVariable(key: string): string | null;
160
- login(email: string, password: string): Promise<OidcToken>;
185
+ private _attachAuthInterceptor;
186
+ /**
187
+ * If your consumers already accept `token?: string` and set headers inside,
188
+ * you can leave them as-is and just pass token through.
189
+ *
190
+ * But the best pattern is: do not pass token in SPA and let interceptor handle it.
191
+ */
161
192
  myUser(token?: string): Promise<User$1>;
162
193
  allOrganizations(token?: string): Promise<Group$1[]>;
163
194
  myOrganizations(token?: string): Promise<Group$1[]>;
164
195
  organizationByID(id: string, token?: string): Promise<Group$1>;
165
196
  organizationByName(name: string, token?: string): Promise<Group$1>;
197
+ addToOrganization(organization: string, users: UserRoles, token?: string): Promise<void>;
198
+ register(email: string, password: string, password_confirm: string, firstName: string, lastName: string): Promise<void>;
166
199
  createOrganization(name: string, token?: string): Promise<Bucket>;
167
200
  removeOrganizationByID(organization: string, token?: string): Promise<Bucket>;
168
- addToOrganization(organization: string, users: UserRoles, token?: string): void;
169
- register(email: string, password: string, password_confirm: string, firstName: string, lastName: string): Promise<any>;
170
201
  getFolderByName(name: string, token?: string): Promise<Folder>;
171
202
  getFolderByID(id: string, token?: string): Promise<Folder>;
203
+ get accountAxios(): AxiosInstance;
204
+ get managementAxios(): AxiosInstance;
205
+ /**
206
+ * Make an axios request using an explicit token without passing it through every method:
207
+ */
208
+ requestWithToken<T>(instance: AxiosInstance, token: string, config: AxiosRequestConfig): Promise<T>;
172
209
  }
173
210
 
174
211
  type User = User$1;
package/dist/index.js CHANGED
@@ -1,7 +1,9 @@
1
1
  "use strict";
2
2
  var __create = Object.create;
3
3
  var __defProp = Object.defineProperty;
4
+ var __defProps = Object.defineProperties;
4
5
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
5
7
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
8
  var __getOwnPropSymbols = Object.getOwnPropertySymbols;
7
9
  var __getProtoOf = Object.getPrototypeOf;
@@ -19,6 +21,7 @@ var __spreadValues = (a, b) => {
19
21
  }
20
22
  return a;
21
23
  };
24
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
22
25
  var __export = (target, all) => {
23
26
  for (var name in all)
24
27
  __defProp(target, name, { get: all[name], enumerable: true });
@@ -783,65 +786,87 @@ var FolderConsumer = class extends BaseConsumer {
783
786
 
784
787
  // src/client.ts
785
788
  var Client = class {
786
- constructor(_managementURL, _accountURL) {
787
- const configManagement = {
788
- baseURL: _managementURL
789
- };
790
- const configAccount = {
791
- baseURL: _accountURL
792
- };
793
- const axiosManagement = import_axios.default.create(configManagement);
794
- const axiosAccount = import_axios.default.create(configAccount);
789
+ constructor(managementURL, accountURL) {
790
+ const axiosManagement = import_axios.default.create({ baseURL: managementURL });
791
+ const axiosAccount = import_axios.default.create({ baseURL: accountURL });
792
+ this._axiosManagement = axiosManagement;
793
+ this._axiosAccount = axiosAccount;
794
+ this._attachAuthInterceptor(this._axiosAccount);
795
+ this._attachAuthInterceptor(this._axiosManagement);
795
796
  this._userConsumer = new UserConsumer(axiosAccount);
796
797
  this._groupConsumer = new GroupConsumer(axiosAccount);
797
798
  this._bucketConsumer = new BucketConsumer(axiosManagement);
798
799
  this._folderConsumer = new FolderConsumer(axiosManagement);
799
- this.addToOrganization = withValidToken(this.addToOrganization.bind(this), _accountURL);
800
- this.myUser = withValidToken(this.myUser.bind(this), _accountURL);
801
- this.removeOrganizationByID = withValidToken(this.removeOrganizationByID.bind(this), _accountURL);
802
- this.organizationByName = withValidToken(this.organizationByName.bind(this), _accountURL);
803
- this.allOrganizations = withValidToken(this.allOrganizations.bind(this), _accountURL);
804
- this.myOrganizations = withValidToken(this.myOrganizations.bind(this), _accountURL);
805
- this.organizationByID = withValidToken(this.organizationByID.bind(this), _accountURL);
806
- this.createOrganization = withValidToken(this.createOrganization.bind(this), _accountURL);
807
- this.getFolderByName = withValidToken(this.getFolderByName.bind(this), _accountURL);
808
- this.getFolderByID = withValidToken(this.getFolderByID.bind(this), _accountURL);
809
800
  }
801
+ /**
802
+ * Set a token provider (Keycloak-friendly).
803
+ * Example from Angular:
804
+ * client.setTokenProvider(() => auth.getValidToken())
805
+ */
806
+ setTokenProvider(provider) {
807
+ this._tokenProvider = provider;
808
+ }
809
+ /**
810
+ * Optional legacy mode.
811
+ * If you call login(), it stores tokens in sessionStorage
812
+ * and the default token provider will read them.
813
+ *
814
+ * For Keycloak SPA: do NOT use this.
815
+ */
816
+ login(email, password) {
817
+ return __async(this, null, function* () {
818
+ const resp = yield this._userConsumer.login(email, password);
819
+ this.setSession(resp);
820
+ if (!this._tokenProvider) {
821
+ this._tokenProvider = () => {
822
+ var _a;
823
+ return (_a = this.getSessionVariable("access_token")) != null ? _a : "";
824
+ };
825
+ }
826
+ return resp;
827
+ });
828
+ }
829
+ // -----------------------------
830
+ // Token / session helpers
831
+ // -----------------------------
810
832
  setSession(session) {
811
- const storage = typeof window !== "undefined" ? sessionStorage : global.sessionStorage;
833
+ if (typeof window === "undefined" || !window.sessionStorage) return;
812
834
  const now = Math.floor(Date.now() / 1e3);
813
- if (storage) {
814
- storage.setItem("access_token", session.access_token);
815
- storage.setItem("refresh_token", session.refresh_token);
816
- storage.setItem("refresh_expires_in", String(now + session.expires_in));
817
- storage.setItem("refresh_exp", String(now + session.refresh_expires_in));
818
- } else {
819
- console.warn("Session storage is not available.");
820
- }
835
+ sessionStorage.setItem("access_token", session.access_token);
836
+ sessionStorage.setItem("refresh_token", session.refresh_token);
837
+ sessionStorage.setItem("exp", String(now + session.expires_in));
838
+ sessionStorage.setItem("refresh_exp", String(now + session.refresh_expires_in));
821
839
  }
822
840
  getSessionVariable(key) {
823
- const storage = typeof window !== "undefined" ? sessionStorage : global.sessionStorage;
824
- return storage ? storage.getItem(key) : null;
825
- }
826
- // private _setSession(session: OidcToken): void {
827
- // const now = Math.floor(Date.now() / 1000);
828
- // if (typeof localStorage !== "undefined") {
829
- // localStorage.setItem('token', session.access_token);
830
- // localStorage.setItem('refresh_token', session.refresh_token);
831
- // localStorage.setItem('exp', String(now + session.expires_in));
832
- // localStorage.setItem('refresh_exp', String(now + session.refresh_expires_in));
833
- // } else {
834
- // console.warn("⚠️ Local Storage not available. You will need to pass tokens manually.");
835
- // }
836
- // }
837
- login(email, password) {
838
- return this._userConsumer.login(email, password).then(
839
- (resp) => {
840
- this.setSession(resp);
841
- return resp;
841
+ if (typeof window === "undefined" || !window.sessionStorage) return null;
842
+ return sessionStorage.getItem(key);
843
+ }
844
+ _attachAuthInterceptor(instance) {
845
+ instance.interceptors.request.use((config) => __async(this, null, function* () {
846
+ var _a, _b, _c, _d, _e, _f;
847
+ const anyConfig = config;
848
+ if (anyConfig._token && typeof anyConfig._token === "string") {
849
+ (_c = (_b = (_a = config.headers) == null ? void 0 : _a.set) == null ? void 0 : _b.call(_a, "Authorization", `Bearer ${anyConfig._token}`)) != null ? _c : config.headers["Authorization"] = `Bearer ${anyConfig._token}`;
850
+ return config;
842
851
  }
843
- );
844
- }
852
+ if (this._tokenProvider) {
853
+ const token = yield this._tokenProvider();
854
+ if (token) {
855
+ (_f = (_e = (_d = config.headers) == null ? void 0 : _d.set) == null ? void 0 : _e.call(_d, "Authorization", `Bearer ${token}`)) != null ? _f : config.headers["Authorization"] = `Bearer ${token}`;
856
+ }
857
+ }
858
+ return config;
859
+ }));
860
+ }
861
+ /**
862
+ * If your consumers already accept `token?: string` and set headers inside,
863
+ * you can leave them as-is and just pass token through.
864
+ *
865
+ * But the best pattern is: do not pass token in SPA and let interceptor handle it.
866
+ */
867
+ // -----------------------------
868
+ // Account APIs
869
+ // -----------------------------
845
870
  myUser(token) {
846
871
  return this._userConsumer.getUser(token);
847
872
  }
@@ -857,35 +882,33 @@ var Client = class {
857
882
  organizationByName(name, token) {
858
883
  return this._groupConsumer.getGroupByName(name, token);
859
884
  }
860
- createOrganization(name, token) {
861
- return this._groupConsumer.postGroup(name, token).then(
862
- (group) => {
863
- return this._bucketConsumer.postBucket(group.id, name, token);
864
- }
865
- );
866
- }
867
- removeOrganizationByID(organization, token) {
868
- return this._groupConsumer.deleteGroup(organization, token).then(
869
- () => {
870
- return this._bucketConsumer.deleteBucket(organization, token).then(
871
- (bucket) => {
872
- if (!bucket) {
873
- throw new Error(`Bucket with ID ${organization} could not be deleted.`);
874
- }
875
- return bucket;
876
- }
877
- );
878
- }
879
- );
880
- }
881
885
  addToOrganization(organization, users, token) {
882
- return this._groupConsumer.addUsers(organization, users, token);
886
+ return __async(this, null, function* () {
887
+ yield this._groupConsumer.addUsers(organization, users, token);
888
+ });
883
889
  }
884
890
  register(email, password, password_confirm, firstName, lastName) {
885
- if (password != password_confirm) {
886
- throw new Error("Passwords do not match");
887
- }
888
- return this._userConsumer.register(email, password, firstName, lastName);
891
+ return __async(this, null, function* () {
892
+ if (password !== password_confirm) throw new Error("Passwords do not match");
893
+ yield this._userConsumer.register(email, password, firstName, lastName);
894
+ });
895
+ }
896
+ // -----------------------------
897
+ // Management APIs
898
+ // -----------------------------
899
+ createOrganization(name, token) {
900
+ return __async(this, null, function* () {
901
+ const group = yield this._groupConsumer.postGroup(name, token);
902
+ return this._bucketConsumer.postBucket(group.id, name, token);
903
+ });
904
+ }
905
+ removeOrganizationByID(organization, token) {
906
+ return __async(this, null, function* () {
907
+ yield this._groupConsumer.deleteGroup(organization, token);
908
+ const bucket = yield this._bucketConsumer.deleteBucket(organization, token);
909
+ if (!bucket) throw new Error(`Bucket with ID ${organization} could not be deleted.`);
910
+ return bucket;
911
+ });
889
912
  }
890
913
  getFolderByName(name, token) {
891
914
  return this._folderConsumer.getFolderByName(name, token);
@@ -893,6 +916,26 @@ var Client = class {
893
916
  getFolderByID(id, token) {
894
917
  return this._folderConsumer.getFolderByID(id, token);
895
918
  }
919
+ // ---------------------------------------------------
920
+ // OPTIONAL helpers for “explicit token per call”
921
+ // without changing all your consumers:
922
+ // ---------------------------------------------------
923
+ get accountAxios() {
924
+ return this._axiosAccount;
925
+ }
926
+ get managementAxios() {
927
+ return this._axiosManagement;
928
+ }
929
+ /**
930
+ * Make an axios request using an explicit token without passing it through every method:
931
+ */
932
+ requestWithToken(instance, token, config) {
933
+ return __async(this, null, function* () {
934
+ const cfg = __spreadProps(__spreadValues({}, config), { _token: token });
935
+ const res = yield instance.request(cfg);
936
+ return res.data;
937
+ });
938
+ }
896
939
  };
897
940
  // Annotate the CommonJS export names for ESM import in node:
898
941
  0 && (module.exports = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@singularlogic/coreplatts",
3
- "version": "0.0.7",
3
+ "version": "0.0.10",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "files": [