@verdocs/js-sdk 4.1.15 → 4.1.16
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.mts +10 -3
- package/dist/index.d.ts +10 -3
- package/dist/index.js +246 -227
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +246 -227
- package/dist/index.mjs.map +1 -1
- package/dist/package.json +1 -1
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -906,7 +906,7 @@ type TSession = IUserSession | ISigningSession | null;
|
|
|
906
906
|
*/
|
|
907
907
|
type TActiveSession = IUserSession | ISigningSession;
|
|
908
908
|
type TEnvironment = "verdocs" | "verdocs-stage";
|
|
909
|
-
type TSessionChangedListener = (endpoint: VerdocsEndpoint, session: TSession) => void;
|
|
909
|
+
type TSessionChangedListener = (endpoint: VerdocsEndpoint, session: TSession, profile: IProfile | null) => void;
|
|
910
910
|
interface VerdocsEndpointOptions {
|
|
911
911
|
baseURL?: string;
|
|
912
912
|
baseURLv2?: string;
|
|
@@ -953,6 +953,13 @@ declare class VerdocsEndpoint {
|
|
|
953
953
|
* with Envelopes.
|
|
954
954
|
*/
|
|
955
955
|
session: TSession;
|
|
956
|
+
/**
|
|
957
|
+
* The current user's profile, if known. Note that while sessions are loaded and handled synchronously,
|
|
958
|
+
* profiles are loaded asynchronously and may not be available immediately after a session is loaded.
|
|
959
|
+
* To ensure both are available, developers should subscribe to the `onSessionChanged` event, which
|
|
960
|
+
* will not be fired until the profile is loaded and verified.
|
|
961
|
+
*/
|
|
962
|
+
profile: IProfile | null;
|
|
956
963
|
api: AxiosInstance;
|
|
957
964
|
/**
|
|
958
965
|
* Create a new VerdocsEndpoint to call Verdocs platform services.
|
|
@@ -1115,8 +1122,8 @@ declare class VerdocsEndpoint {
|
|
|
1115
1122
|
*/
|
|
1116
1123
|
onSessionChanged(listener: TSessionChangedListener): () => void;
|
|
1117
1124
|
/**
|
|
1118
|
-
* Load a persisted session from localStorage. Typically called once after the endpoint is configured
|
|
1119
|
-
* or component starts.
|
|
1125
|
+
* Load a persisted session from localStorage. Typically called once after the endpoint is configured
|
|
1126
|
+
* when the app or component starts.
|
|
1120
1127
|
*/
|
|
1121
1128
|
loadSession(): VerdocsEndpoint;
|
|
1122
1129
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -906,7 +906,7 @@ type TSession = IUserSession | ISigningSession | null;
|
|
|
906
906
|
*/
|
|
907
907
|
type TActiveSession = IUserSession | ISigningSession;
|
|
908
908
|
type TEnvironment = "verdocs" | "verdocs-stage";
|
|
909
|
-
type TSessionChangedListener = (endpoint: VerdocsEndpoint, session: TSession) => void;
|
|
909
|
+
type TSessionChangedListener = (endpoint: VerdocsEndpoint, session: TSession, profile: IProfile | null) => void;
|
|
910
910
|
interface VerdocsEndpointOptions {
|
|
911
911
|
baseURL?: string;
|
|
912
912
|
baseURLv2?: string;
|
|
@@ -953,6 +953,13 @@ declare class VerdocsEndpoint {
|
|
|
953
953
|
* with Envelopes.
|
|
954
954
|
*/
|
|
955
955
|
session: TSession;
|
|
956
|
+
/**
|
|
957
|
+
* The current user's profile, if known. Note that while sessions are loaded and handled synchronously,
|
|
958
|
+
* profiles are loaded asynchronously and may not be available immediately after a session is loaded.
|
|
959
|
+
* To ensure both are available, developers should subscribe to the `onSessionChanged` event, which
|
|
960
|
+
* will not be fired until the profile is loaded and verified.
|
|
961
|
+
*/
|
|
962
|
+
profile: IProfile | null;
|
|
956
963
|
api: AxiosInstance;
|
|
957
964
|
/**
|
|
958
965
|
* Create a new VerdocsEndpoint to call Verdocs platform services.
|
|
@@ -1115,8 +1122,8 @@ declare class VerdocsEndpoint {
|
|
|
1115
1122
|
*/
|
|
1116
1123
|
onSessionChanged(listener: TSessionChangedListener): () => void;
|
|
1117
1124
|
/**
|
|
1118
|
-
* Load a persisted session from localStorage. Typically called once after the endpoint is configured
|
|
1119
|
-
* or component starts.
|
|
1125
|
+
* Load a persisted session from localStorage. Typically called once after the endpoint is configured
|
|
1126
|
+
* when the app or component starts.
|
|
1120
1127
|
*/
|
|
1121
1128
|
loadSession(): VerdocsEndpoint;
|
|
1122
1129
|
}
|
package/dist/index.js
CHANGED
|
@@ -795,9 +795,231 @@ var globalThis_1 = (function () {
|
|
|
795
795
|
|
|
796
796
|
var globalThis$1 = /*@__PURE__*/getDefaultExportFromCjs(globalThis_1);
|
|
797
797
|
|
|
798
|
+
/**
|
|
799
|
+
* Authenticate to Verdocs.
|
|
800
|
+
*
|
|
801
|
+
* ```typescript
|
|
802
|
+
* import {authenticate, VerdocsEndpoint} from '@verdocs/js-sdk';
|
|
803
|
+
*
|
|
804
|
+
* // Client-side call, suitable for Web and mobile apps:
|
|
805
|
+
* const {access_token} = await Auth.authenticate({ username: 'test@test.com', password: 'PASSWORD', grant_type:'password' });
|
|
806
|
+
* VerdocsEndpoint.getDefault().setAuthToken(access_token);
|
|
807
|
+
*
|
|
808
|
+
* // Server-side call, suitable for server apps. NEVER EXPOSE client_secret IN FRONT-END CODE:
|
|
809
|
+
* const {access_token} = await Auth.authenticate({ client_id: '...', client_secret: '...', grant_type:'client_credentials' });
|
|
810
|
+
* VerdocsEndpoint.getDefault().setAuthToken(access_token);
|
|
811
|
+
* ```
|
|
812
|
+
*/
|
|
813
|
+
const authenticate = (endpoint, params) => endpoint.api //
|
|
814
|
+
.post('/v2/oauth2/token', params)
|
|
815
|
+
.then((r) => r.data);
|
|
816
|
+
/**
|
|
817
|
+
* If called before the session expires, this will refresh the caller's session and tokens.
|
|
818
|
+
*
|
|
819
|
+
* ```typescript
|
|
820
|
+
* import {Auth, VerdocsEndpoint} from '@verdocs/js-sdk';
|
|
821
|
+
*
|
|
822
|
+
* const {accessToken} = await Auth.refreshTokens();
|
|
823
|
+
* VerdocsEndpoint.setAuthToken(accessToken);
|
|
824
|
+
* ```
|
|
825
|
+
*/
|
|
826
|
+
const refreshToken = (endpoint, refreshToken) => authenticate(endpoint, { grant_type: 'refresh_token', refresh_token: refreshToken });
|
|
827
|
+
/**
|
|
828
|
+
* Update the caller's password when the old password is known (typically for logged-in users).
|
|
829
|
+
*
|
|
830
|
+
* ```typescript
|
|
831
|
+
* import {changePassword} from '@verdocs/js-sdk';
|
|
832
|
+
*
|
|
833
|
+
* const {status, message} = await changePassword({ email, oldPassword, newPassword });
|
|
834
|
+
* if (status !== 'OK') {
|
|
835
|
+
* window.alert(`Password reset error: ${message}`);
|
|
836
|
+
* }
|
|
837
|
+
* ```
|
|
838
|
+
*/
|
|
839
|
+
const changePassword = (endpoint, params) => endpoint.api //
|
|
840
|
+
.post('/v2/users/change-password', params)
|
|
841
|
+
.then((r) => r.data);
|
|
842
|
+
/**
|
|
843
|
+
* Request a password reset, when the old password is not known (typically in login forms).
|
|
844
|
+
*
|
|
845
|
+
* ```typescript
|
|
846
|
+
* import {resetPassword} from '@verdocs/js-sdk';
|
|
847
|
+
*
|
|
848
|
+
* const {success} = await resetPassword({ email });
|
|
849
|
+
* if (status !== 'OK') {
|
|
850
|
+
* window.alert(`Please check your email for instructions on how to reset your password.`);
|
|
851
|
+
* }
|
|
852
|
+
* ```
|
|
853
|
+
*/
|
|
854
|
+
const resetPassword = (endpoint, params) => endpoint.api //
|
|
855
|
+
.post('/v2/users/reset-password', params)
|
|
856
|
+
.then((r) => r.data);
|
|
857
|
+
/**
|
|
858
|
+
* Resend the email verification request. Note that to prevent certain forms of abuse, the email address is not
|
|
859
|
+
* a parameter here. Instead, the caller must be authenticated as the (unverified) user. To simplify this process,
|
|
860
|
+
* the access token to be used may be passed directly as a parameter here. This avoids the need to set it as the
|
|
861
|
+
* active token on an endpoint, which may be inconvenient in workflows where it is preferable to keep the user in
|
|
862
|
+
* "anonymous" mode while verification is being performed.
|
|
863
|
+
*
|
|
864
|
+
* ```typescript
|
|
865
|
+
* import {resendVerification} from '@verdocs/js-sdk';
|
|
866
|
+
*
|
|
867
|
+
* const result = await resendVerification();
|
|
868
|
+
* ```
|
|
869
|
+
*/
|
|
870
|
+
const verifyEmail = (endpoint, email, code) => endpoint.api //
|
|
871
|
+
.post('/v2/users/verify-email', { email, code })
|
|
872
|
+
.then((r) => r.data);
|
|
873
|
+
/**
|
|
874
|
+
* Resend the email verification request. Note that to prevent certain forms of abuse, the email address is not
|
|
875
|
+
* a parameter here. Instead, the caller must be authenticated as the (unverified) user. To simplify this process,
|
|
876
|
+
* the access token to be used may be passed directly as a parameter here. This avoids the need to set it as the
|
|
877
|
+
* active token on an endpoint, which may be inconvenient in workflows where it is preferable to keep the user in
|
|
878
|
+
* "anonymous" mode while verification is being performed.
|
|
879
|
+
*
|
|
880
|
+
* ```typescript
|
|
881
|
+
* import {resendVerification} from '@verdocs/js-sdk';
|
|
882
|
+
*
|
|
883
|
+
* const result = await resendVerification();
|
|
884
|
+
* ```
|
|
885
|
+
*/
|
|
886
|
+
const resendVerification = (endpoint, accessToken) => endpoint.api //
|
|
887
|
+
.post('/v2/users/resend-verification', {}, accessToken ? { headers: { Authorization: `Bearer ${accessToken}` } } : {})
|
|
888
|
+
.then((r) => r.data);
|
|
889
|
+
|
|
890
|
+
const getNotifications = async (endpoint) => endpoint.api //
|
|
891
|
+
.get('/v2/notifications')
|
|
892
|
+
.then((r) => r.data);
|
|
893
|
+
|
|
894
|
+
/**
|
|
895
|
+
* Get the user's available profiles. The current profile will be marked with `current: true`.
|
|
896
|
+
*
|
|
897
|
+
* ```typescript
|
|
898
|
+
* import {getProfiles} from '@verdocs/js-sdk';
|
|
899
|
+
*
|
|
900
|
+
* const profiles = await getProfiles();
|
|
901
|
+
* ```
|
|
902
|
+
*/
|
|
903
|
+
const getProfiles = (endpoint) => endpoint.api //
|
|
904
|
+
.get('/v2/profiles')
|
|
905
|
+
.then((r) => r.data);
|
|
906
|
+
/**
|
|
907
|
+
* Get the user's available profiles. The current profile will be marked with `current: true`.
|
|
908
|
+
*
|
|
909
|
+
* ```typescript
|
|
910
|
+
* import {getCurrentProfile} from '@verdocs/js-sdk';
|
|
911
|
+
*
|
|
912
|
+
* const profiles = await getCurrentProfile(VerdocsEndpoint.getDefault());
|
|
913
|
+
* ```
|
|
914
|
+
*/
|
|
915
|
+
const getCurrentProfile = (endpoint) => endpoint.api //
|
|
916
|
+
.get('/v2/profiles')
|
|
917
|
+
.then((r) => (r.data || []).find((profile) => profile.current));
|
|
918
|
+
/**
|
|
919
|
+
* Get a profile. The caller must have admin access to the given profile.
|
|
920
|
+
*
|
|
921
|
+
* ```typescript
|
|
922
|
+
* import {getProfile} from '@verdocs/js-sdk';
|
|
923
|
+
*
|
|
924
|
+
* const profile = await getProfile(VerdocsEndpoint.getDefault(), 'PROFILEID');
|
|
925
|
+
* ```
|
|
926
|
+
*/
|
|
927
|
+
const getProfile = (endpoint, profileId) => endpoint.api //
|
|
928
|
+
.get(`/v2/profiles/${profileId}`)
|
|
929
|
+
.then((r) => r.data);
|
|
930
|
+
/**
|
|
931
|
+
* Switch the caller's "current" profile. The current profile is used for permissions checking
|
|
932
|
+
* and profile_id field settings for most operations in Verdocs. It is important to select the
|
|
933
|
+
* appropropriate profile before calling other API functions.
|
|
934
|
+
*
|
|
935
|
+
* ```typescript
|
|
936
|
+
* import {switchProfile} from '@verdocs/js-sdk';
|
|
937
|
+
*
|
|
938
|
+
* const newProfile = await switchProfile(VerdocsEndpoint.getDefault(), 'PROFILEID');
|
|
939
|
+
* ```
|
|
940
|
+
*/
|
|
941
|
+
const switchProfile = (endpoint, profileId) => endpoint.api //
|
|
942
|
+
.post(`/v2/profiles/${profileId}/switch`)
|
|
943
|
+
.then((r) => r.data);
|
|
944
|
+
/**
|
|
945
|
+
* Update a profile. For future expansion, the profile ID to update is required, but currently
|
|
946
|
+
* this must also be the "current" profile for the caller.
|
|
947
|
+
*
|
|
948
|
+
* ```typescript
|
|
949
|
+
* import {updateProfile} from '@verdocs/js-sdk/Users';
|
|
950
|
+
*
|
|
951
|
+
* const newProfile = await updateProfile(VerdocsEndpoint.getDefault(), 'PROFILEID');
|
|
952
|
+
* ```
|
|
953
|
+
*/
|
|
954
|
+
const updateProfile = (endpoint, profileId, params) => endpoint.api //
|
|
955
|
+
.patch(`/v2/profiles/${profileId}`, params)
|
|
956
|
+
.then((r) => r.data);
|
|
957
|
+
/**
|
|
958
|
+
* Delete a profile. If the requested profile is the caller's curent profile, the next
|
|
959
|
+
* available profile will be selected.
|
|
960
|
+
*
|
|
961
|
+
* ```typescript
|
|
962
|
+
* import {deleteProfile} from '@verdocs/js-sdk';
|
|
963
|
+
*
|
|
964
|
+
* await deleteProfile(VerdocsEndpoint.getDefault(), 'PROFILEID');
|
|
965
|
+
* ```
|
|
966
|
+
*/
|
|
967
|
+
const deleteProfile = (endpoint, profileId) => endpoint.api //
|
|
968
|
+
.delete(`/v2/profiles/${profileId}`)
|
|
969
|
+
.then((r) => r.data);
|
|
970
|
+
/**
|
|
971
|
+
* Create a new user account. Note that there are two registration paths for creation:
|
|
972
|
+
* - Get invited to an organization, by an admin or owner of that org.
|
|
973
|
+
* - Created a new organization. The caller will become the first owner of the new org.
|
|
974
|
+
*
|
|
975
|
+
* This endpoint is for the second path, so an organization name is required. It is NOT
|
|
976
|
+
* required to be unique because it is very common for businesses to have the same names,
|
|
977
|
+
* without conflicting (e.g. "Delta" could be Delta Faucet or Delta Airlines).
|
|
978
|
+
*
|
|
979
|
+
* The new profile will automatically be set as the user's "current" profile, and new
|
|
980
|
+
* session tokens will be returned to the caller. However, the caller's email may not yet
|
|
981
|
+
* be verified. In that case, the caller will not yet be able to call other endpoints in
|
|
982
|
+
* the Verdocs API. The caller will need to check their email for a verification code,
|
|
983
|
+
* which should be submitted via the `verifyEmail` endpoint.
|
|
984
|
+
*
|
|
985
|
+
* ```typescript
|
|
986
|
+
* import {createProfile} from '@verdocs/js-sdk';
|
|
987
|
+
*
|
|
988
|
+
* const newSession = await createProfile(VerdocsEndpoint.getDefault(), {
|
|
989
|
+
* orgName: 'NEW ORG', email: 'a@b.com', password: '12345678', firstName: 'FIRST', lastName: 'LAST'
|
|
990
|
+
* });
|
|
991
|
+
* ```
|
|
992
|
+
*/
|
|
993
|
+
const createProfile = (endpoint, params) => endpoint.api //
|
|
994
|
+
.post('/v2/profiles', params)
|
|
995
|
+
.then((r) => r.data);
|
|
996
|
+
/**
|
|
997
|
+
* Update the caller's profile photo. This can only be called for the user's "current" profile.
|
|
998
|
+
*
|
|
999
|
+
* ```typescript
|
|
1000
|
+
* import {uploadProfilePhoto} from '@verdocs/js-sdk';
|
|
1001
|
+
*
|
|
1002
|
+
* await uploadProfilePhoto((VerdocsEndpoint.getDefault(), profileId, file);
|
|
1003
|
+
* ```
|
|
1004
|
+
*/
|
|
1005
|
+
const updateProfilePhoto = (endpoint, profileId, file, onUploadProgress) => {
|
|
1006
|
+
const formData = new FormData();
|
|
1007
|
+
formData.append('picture', file, file.name);
|
|
1008
|
+
return endpoint.api //
|
|
1009
|
+
.patch(`/v2/profiles/${profileId}`, formData, {
|
|
1010
|
+
timeout: 120000,
|
|
1011
|
+
onUploadProgress: (event) => {
|
|
1012
|
+
const total = event.total || 1;
|
|
1013
|
+
const loaded = event.loaded || 0;
|
|
1014
|
+
onUploadProgress?.(Math.floor((loaded * 100) / (total || 1)), loaded, total || 1);
|
|
1015
|
+
},
|
|
1016
|
+
})
|
|
1017
|
+
.then((r) => r.data);
|
|
1018
|
+
};
|
|
1019
|
+
|
|
798
1020
|
// @credit https://derickbailey.com/2016/03/09/creating-a-true-singleton-in-node-js-with-es6-symbols/
|
|
799
1021
|
// Also see globalThis for comments about why we're doing this in the first place.
|
|
800
|
-
const ENDPOINT_KEY = Symbol.for('
|
|
1022
|
+
const ENDPOINT_KEY = Symbol.for('verdocs-default-endpoint');
|
|
801
1023
|
const requestLogger = (r) => {
|
|
802
1024
|
// tslint:disable-next-line
|
|
803
1025
|
console.debug(`[JS-SDK] ${r.method.toUpperCase()} ${r.baseURL}${r.url}`, r.data ? JSON.stringify(r.data) : '');
|
|
@@ -845,6 +1067,13 @@ class VerdocsEndpoint {
|
|
|
845
1067
|
* with Envelopes.
|
|
846
1068
|
*/
|
|
847
1069
|
session = null;
|
|
1070
|
+
/**
|
|
1071
|
+
* The current user's profile, if known. Note that while sessions are loaded and handled synchronously,
|
|
1072
|
+
* profiles are loaded asynchronously and may not be available immediately after a session is loaded.
|
|
1073
|
+
* To ensure both are available, developers should subscribe to the `onSessionChanged` event, which
|
|
1074
|
+
* will not be fired until the profile is loaded and verified.
|
|
1075
|
+
*/
|
|
1076
|
+
profile = null;
|
|
848
1077
|
api;
|
|
849
1078
|
/**
|
|
850
1079
|
* Create a new VerdocsEndpoint to call Verdocs platform services.
|
|
@@ -1063,7 +1292,17 @@ class VerdocsEndpoint {
|
|
|
1063
1292
|
this.api.defaults.headers.common.signer = `Bearer ${token}`;
|
|
1064
1293
|
}
|
|
1065
1294
|
localStorage.setItem(this.sessionStorageKey(), token);
|
|
1066
|
-
this
|
|
1295
|
+
getCurrentProfile(this)
|
|
1296
|
+
.then((r) => {
|
|
1297
|
+
window?.console?.debug('[JS_SDK] Loaded profile', r);
|
|
1298
|
+
this.profile = r || null;
|
|
1299
|
+
this.notifySessionListeners();
|
|
1300
|
+
})
|
|
1301
|
+
.catch((e) => {
|
|
1302
|
+
this.profile = null;
|
|
1303
|
+
window?.console?.warn('Unable to load profile', e);
|
|
1304
|
+
this.notifySessionListeners();
|
|
1305
|
+
});
|
|
1067
1306
|
return this;
|
|
1068
1307
|
}
|
|
1069
1308
|
/**
|
|
@@ -1084,6 +1323,7 @@ class VerdocsEndpoint {
|
|
|
1084
1323
|
delete this.api.defaults.headers.common.Authorization;
|
|
1085
1324
|
delete this.api.defaults.headers.common.signer;
|
|
1086
1325
|
this.session = null;
|
|
1326
|
+
this.profile = null;
|
|
1087
1327
|
this.token = null;
|
|
1088
1328
|
this.notifySessionListeners();
|
|
1089
1329
|
return this;
|
|
@@ -1095,6 +1335,7 @@ class VerdocsEndpoint {
|
|
|
1095
1335
|
localStorage.removeItem(this.sessionStorageKey());
|
|
1096
1336
|
delete this.api.defaults.headers.common.Authorization;
|
|
1097
1337
|
this.session = null;
|
|
1338
|
+
this.profile = null;
|
|
1098
1339
|
this.token = null;
|
|
1099
1340
|
this.notifySessionListeners();
|
|
1100
1341
|
return this;
|
|
@@ -1102,7 +1343,7 @@ class VerdocsEndpoint {
|
|
|
1102
1343
|
notifySessionListeners() {
|
|
1103
1344
|
this.sessionListeners.forEach((listener) => {
|
|
1104
1345
|
try {
|
|
1105
|
-
listener(this, this.session);
|
|
1346
|
+
listener(this, this.session, this.profile);
|
|
1106
1347
|
}
|
|
1107
1348
|
catch (e) {
|
|
1108
1349
|
// NOOP
|
|
@@ -1122,8 +1363,8 @@ class VerdocsEndpoint {
|
|
|
1122
1363
|
};
|
|
1123
1364
|
}
|
|
1124
1365
|
/**
|
|
1125
|
-
* Load a persisted session from localStorage. Typically called once after the endpoint is configured
|
|
1126
|
-
* or component starts.
|
|
1366
|
+
* Load a persisted session from localStorage. Typically called once after the endpoint is configured
|
|
1367
|
+
* when the app or component starts.
|
|
1127
1368
|
*/
|
|
1128
1369
|
loadSession() {
|
|
1129
1370
|
const token = localStorage.getItem(this.sessionStorageKey());
|
|
@@ -2627,228 +2868,6 @@ const isValidRoleName = (value, roles) => roles.findIndex((role) => role.name ==
|
|
|
2627
2868
|
const TagRegEx = /^[a-zA-Z0-9-]{0,32}$/;
|
|
2628
2869
|
const isValidTag = (value, tags) => TagRegEx.test(value) || tags.findIndex((tag) => tag === value) !== -1;
|
|
2629
2870
|
|
|
2630
|
-
/**
|
|
2631
|
-
* Authenticate to Verdocs.
|
|
2632
|
-
*
|
|
2633
|
-
* ```typescript
|
|
2634
|
-
* import {authenticate, VerdocsEndpoint} from '@verdocs/js-sdk';
|
|
2635
|
-
*
|
|
2636
|
-
* // Client-side call, suitable for Web and mobile apps:
|
|
2637
|
-
* const {access_token} = await Auth.authenticate({ username: 'test@test.com', password: 'PASSWORD', grant_type:'password' });
|
|
2638
|
-
* VerdocsEndpoint.getDefault().setAuthToken(access_token);
|
|
2639
|
-
*
|
|
2640
|
-
* // Server-side call, suitable for server apps. NEVER EXPOSE client_secret IN FRONT-END CODE:
|
|
2641
|
-
* const {access_token} = await Auth.authenticate({ client_id: '...', client_secret: '...', grant_type:'client_credentials' });
|
|
2642
|
-
* VerdocsEndpoint.getDefault().setAuthToken(access_token);
|
|
2643
|
-
* ```
|
|
2644
|
-
*/
|
|
2645
|
-
const authenticate = (endpoint, params) => endpoint.api //
|
|
2646
|
-
.post('/v2/oauth2/token', params)
|
|
2647
|
-
.then((r) => r.data);
|
|
2648
|
-
/**
|
|
2649
|
-
* If called before the session expires, this will refresh the caller's session and tokens.
|
|
2650
|
-
*
|
|
2651
|
-
* ```typescript
|
|
2652
|
-
* import {Auth, VerdocsEndpoint} from '@verdocs/js-sdk';
|
|
2653
|
-
*
|
|
2654
|
-
* const {accessToken} = await Auth.refreshTokens();
|
|
2655
|
-
* VerdocsEndpoint.setAuthToken(accessToken);
|
|
2656
|
-
* ```
|
|
2657
|
-
*/
|
|
2658
|
-
const refreshToken = (endpoint, refreshToken) => authenticate(endpoint, { grant_type: 'refresh_token', refresh_token: refreshToken });
|
|
2659
|
-
/**
|
|
2660
|
-
* Update the caller's password when the old password is known (typically for logged-in users).
|
|
2661
|
-
*
|
|
2662
|
-
* ```typescript
|
|
2663
|
-
* import {changePassword} from '@verdocs/js-sdk';
|
|
2664
|
-
*
|
|
2665
|
-
* const {status, message} = await changePassword({ email, oldPassword, newPassword });
|
|
2666
|
-
* if (status !== 'OK') {
|
|
2667
|
-
* window.alert(`Password reset error: ${message}`);
|
|
2668
|
-
* }
|
|
2669
|
-
* ```
|
|
2670
|
-
*/
|
|
2671
|
-
const changePassword = (endpoint, params) => endpoint.api //
|
|
2672
|
-
.post('/v2/users/change-password', params)
|
|
2673
|
-
.then((r) => r.data);
|
|
2674
|
-
/**
|
|
2675
|
-
* Request a password reset, when the old password is not known (typically in login forms).
|
|
2676
|
-
*
|
|
2677
|
-
* ```typescript
|
|
2678
|
-
* import {resetPassword} from '@verdocs/js-sdk';
|
|
2679
|
-
*
|
|
2680
|
-
* const {success} = await resetPassword({ email });
|
|
2681
|
-
* if (status !== 'OK') {
|
|
2682
|
-
* window.alert(`Please check your email for instructions on how to reset your password.`);
|
|
2683
|
-
* }
|
|
2684
|
-
* ```
|
|
2685
|
-
*/
|
|
2686
|
-
const resetPassword = (endpoint, params) => endpoint.api //
|
|
2687
|
-
.post('/v2/users/reset-password', params)
|
|
2688
|
-
.then((r) => r.data);
|
|
2689
|
-
/**
|
|
2690
|
-
* Resend the email verification request. Note that to prevent certain forms of abuse, the email address is not
|
|
2691
|
-
* a parameter here. Instead, the caller must be authenticated as the (unverified) user. To simplify this process,
|
|
2692
|
-
* the access token to be used may be passed directly as a parameter here. This avoids the need to set it as the
|
|
2693
|
-
* active token on an endpoint, which may be inconvenient in workflows where it is preferable to keep the user in
|
|
2694
|
-
* "anonymous" mode while verification is being performed.
|
|
2695
|
-
*
|
|
2696
|
-
* ```typescript
|
|
2697
|
-
* import {resendVerification} from '@verdocs/js-sdk';
|
|
2698
|
-
*
|
|
2699
|
-
* const result = await resendVerification();
|
|
2700
|
-
* ```
|
|
2701
|
-
*/
|
|
2702
|
-
const verifyEmail = (endpoint, email, code) => endpoint.api //
|
|
2703
|
-
.post('/v2/users/verify-email', { email, code })
|
|
2704
|
-
.then((r) => r.data);
|
|
2705
|
-
/**
|
|
2706
|
-
* Resend the email verification request. Note that to prevent certain forms of abuse, the email address is not
|
|
2707
|
-
* a parameter here. Instead, the caller must be authenticated as the (unverified) user. To simplify this process,
|
|
2708
|
-
* the access token to be used may be passed directly as a parameter here. This avoids the need to set it as the
|
|
2709
|
-
* active token on an endpoint, which may be inconvenient in workflows where it is preferable to keep the user in
|
|
2710
|
-
* "anonymous" mode while verification is being performed.
|
|
2711
|
-
*
|
|
2712
|
-
* ```typescript
|
|
2713
|
-
* import {resendVerification} from '@verdocs/js-sdk';
|
|
2714
|
-
*
|
|
2715
|
-
* const result = await resendVerification();
|
|
2716
|
-
* ```
|
|
2717
|
-
*/
|
|
2718
|
-
const resendVerification = (endpoint, accessToken) => endpoint.api //
|
|
2719
|
-
.post('/v2/users/resend-verification', {}, accessToken ? { headers: { Authorization: `Bearer ${accessToken}` } } : {})
|
|
2720
|
-
.then((r) => r.data);
|
|
2721
|
-
|
|
2722
|
-
const getNotifications = async (endpoint) => endpoint.api //
|
|
2723
|
-
.get('/v2/notifications')
|
|
2724
|
-
.then((r) => r.data);
|
|
2725
|
-
|
|
2726
|
-
/**
|
|
2727
|
-
* Get the user's available profiles. The current profile will be marked with `current: true`.
|
|
2728
|
-
*
|
|
2729
|
-
* ```typescript
|
|
2730
|
-
* import {getProfiles} from '@verdocs/js-sdk';
|
|
2731
|
-
*
|
|
2732
|
-
* const profiles = await getProfiles();
|
|
2733
|
-
* ```
|
|
2734
|
-
*/
|
|
2735
|
-
const getProfiles = (endpoint) => endpoint.api //
|
|
2736
|
-
.get('/v2/profiles')
|
|
2737
|
-
.then((r) => r.data);
|
|
2738
|
-
/**
|
|
2739
|
-
* Get the user's available profiles. The current profile will be marked with `current: true`.
|
|
2740
|
-
*
|
|
2741
|
-
* ```typescript
|
|
2742
|
-
* import {getCurrentProfile} from '@verdocs/js-sdk';
|
|
2743
|
-
*
|
|
2744
|
-
* const profiles = await getCurrentProfile(VerdocsEndpoint.getDefault());
|
|
2745
|
-
* ```
|
|
2746
|
-
*/
|
|
2747
|
-
const getCurrentProfile = (endpoint) => endpoint.api //
|
|
2748
|
-
.get('/v2/profiles')
|
|
2749
|
-
.then((r) => (r.data || []).find((profile) => profile.current));
|
|
2750
|
-
/**
|
|
2751
|
-
* Get a profile. The caller must have admin access to the given profile.
|
|
2752
|
-
*
|
|
2753
|
-
* ```typescript
|
|
2754
|
-
* import {getProfile} from '@verdocs/js-sdk';
|
|
2755
|
-
*
|
|
2756
|
-
* const profile = await getProfile(VerdocsEndpoint.getDefault(), 'PROFILEID');
|
|
2757
|
-
* ```
|
|
2758
|
-
*/
|
|
2759
|
-
const getProfile = (endpoint, profileId) => endpoint.api //
|
|
2760
|
-
.get(`/v2/profiles/${profileId}`)
|
|
2761
|
-
.then((r) => r.data);
|
|
2762
|
-
/**
|
|
2763
|
-
* Switch the caller's "current" profile. The current profile is used for permissions checking
|
|
2764
|
-
* and profile_id field settings for most operations in Verdocs. It is important to select the
|
|
2765
|
-
* appropropriate profile before calling other API functions.
|
|
2766
|
-
*
|
|
2767
|
-
* ```typescript
|
|
2768
|
-
* import {switchProfile} from '@verdocs/js-sdk';
|
|
2769
|
-
*
|
|
2770
|
-
* const newProfile = await switchProfile(VerdocsEndpoint.getDefault(), 'PROFILEID');
|
|
2771
|
-
* ```
|
|
2772
|
-
*/
|
|
2773
|
-
const switchProfile = (endpoint, profileId) => endpoint.api //
|
|
2774
|
-
.post(`/v2/profiles/${profileId}/switch`)
|
|
2775
|
-
.then((r) => r.data);
|
|
2776
|
-
/**
|
|
2777
|
-
* Update a profile. For future expansion, the profile ID to update is required, but currently
|
|
2778
|
-
* this must also be the "current" profile for the caller.
|
|
2779
|
-
*
|
|
2780
|
-
* ```typescript
|
|
2781
|
-
* import {updateProfile} from '@verdocs/js-sdk/Users';
|
|
2782
|
-
*
|
|
2783
|
-
* const newProfile = await updateProfile(VerdocsEndpoint.getDefault(), 'PROFILEID');
|
|
2784
|
-
* ```
|
|
2785
|
-
*/
|
|
2786
|
-
const updateProfile = (endpoint, profileId, params) => endpoint.api //
|
|
2787
|
-
.patch(`/v2/profiles/${profileId}`, params)
|
|
2788
|
-
.then((r) => r.data);
|
|
2789
|
-
/**
|
|
2790
|
-
* Delete a profile. If the requested profile is the caller's curent profile, the next
|
|
2791
|
-
* available profile will be selected.
|
|
2792
|
-
*
|
|
2793
|
-
* ```typescript
|
|
2794
|
-
* import {deleteProfile} from '@verdocs/js-sdk';
|
|
2795
|
-
*
|
|
2796
|
-
* await deleteProfile(VerdocsEndpoint.getDefault(), 'PROFILEID');
|
|
2797
|
-
* ```
|
|
2798
|
-
*/
|
|
2799
|
-
const deleteProfile = (endpoint, profileId) => endpoint.api //
|
|
2800
|
-
.delete(`/v2/profiles/${profileId}`)
|
|
2801
|
-
.then((r) => r.data);
|
|
2802
|
-
/**
|
|
2803
|
-
* Create a new user account. Note that there are two registration paths for creation:
|
|
2804
|
-
* - Get invited to an organization, by an admin or owner of that org.
|
|
2805
|
-
* - Created a new organization. The caller will become the first owner of the new org.
|
|
2806
|
-
*
|
|
2807
|
-
* This endpoint is for the second path, so an organization name is required. It is NOT
|
|
2808
|
-
* required to be unique because it is very common for businesses to have the same names,
|
|
2809
|
-
* without conflicting (e.g. "Delta" could be Delta Faucet or Delta Airlines).
|
|
2810
|
-
*
|
|
2811
|
-
* The new profile will automatically be set as the user's "current" profile, and new
|
|
2812
|
-
* session tokens will be returned to the caller. However, the caller's email may not yet
|
|
2813
|
-
* be verified. In that case, the caller will not yet be able to call other endpoints in
|
|
2814
|
-
* the Verdocs API. The caller will need to check their email for a verification code,
|
|
2815
|
-
* which should be submitted via the `verifyEmail` endpoint.
|
|
2816
|
-
*
|
|
2817
|
-
* ```typescript
|
|
2818
|
-
* import {createProfile} from '@verdocs/js-sdk';
|
|
2819
|
-
*
|
|
2820
|
-
* const newSession = await createProfile(VerdocsEndpoint.getDefault(), {
|
|
2821
|
-
* orgName: 'NEW ORG', email: 'a@b.com', password: '12345678', firstName: 'FIRST', lastName: 'LAST'
|
|
2822
|
-
* });
|
|
2823
|
-
* ```
|
|
2824
|
-
*/
|
|
2825
|
-
const createProfile = (endpoint, params) => endpoint.api //
|
|
2826
|
-
.post('/v2/profiles', params)
|
|
2827
|
-
.then((r) => r.data);
|
|
2828
|
-
/**
|
|
2829
|
-
* Update the caller's profile photo. This can only be called for the user's "current" profile.
|
|
2830
|
-
*
|
|
2831
|
-
* ```typescript
|
|
2832
|
-
* import {uploadProfilePhoto} from '@verdocs/js-sdk';
|
|
2833
|
-
*
|
|
2834
|
-
* await uploadProfilePhoto((VerdocsEndpoint.getDefault(), profileId, file);
|
|
2835
|
-
* ```
|
|
2836
|
-
*/
|
|
2837
|
-
const updateProfilePhoto = (endpoint, profileId, file, onUploadProgress) => {
|
|
2838
|
-
const formData = new FormData();
|
|
2839
|
-
formData.append('picture', file, file.name);
|
|
2840
|
-
return endpoint.api //
|
|
2841
|
-
.patch(`/v2/profiles/${profileId}`, formData, {
|
|
2842
|
-
timeout: 120000,
|
|
2843
|
-
onUploadProgress: (event) => {
|
|
2844
|
-
const total = event.total || 1;
|
|
2845
|
-
const loaded = event.loaded || 0;
|
|
2846
|
-
onUploadProgress?.(Math.floor((loaded * 100) / (total || 1)), loaded, total || 1);
|
|
2847
|
-
},
|
|
2848
|
-
})
|
|
2849
|
-
.then((r) => r.data);
|
|
2850
|
-
};
|
|
2851
|
-
|
|
2852
2871
|
exports.AtoB = AtoB;
|
|
2853
2872
|
exports.Countries = Countries;
|
|
2854
2873
|
exports.VerdocsEndpoint = VerdocsEndpoint;
|