@@ -15,7 +15,7 @@ if (!pryv.Connection.prototype.apiOne) {
1515 * @param {string } [resultKey] - if given, returns the value or throws an error if not present
1616 * @throws {Error } if .error is present the response
1717 */
18- pryv . Connection . prototype . apiOne = async function ( method , params = { } , expectedKey ) {
18+ pryv . Connection . prototype . apiOne = async function apiOne ( method , params = { } , expectedKey ) {
1919 const result = await this . api ( [ { method, params } ] ) ;
2020 if ( result [ 0 ] == null || result [ 0 ] . error || ( expectedKey != null && result [ 0 ] [ expectedKey ] == null ) ) {
2121 const innerObject = result [ 0 ] ?. error || result ;
@@ -26,6 +26,35 @@ if (!pryv.Connection.prototype.apiOne) {
2626 if ( expectedKey != null ) return result [ 0 ] [ expectedKey ] ;
2727 return result [ 0 ] ;
2828 } ;
29+
30+ /**
31+ * Revoke : Delete the accessId
32+ * - Do not thow error if access is already revoked, just return null;
33+ * @param {boolean } [throwOnFail = true] - if set to false do not throw Error on failure
34+ * @param {Connection } [usingConnection] - specify which connection issues the revoke, might be necessary when selfRovke
35+ */
36+ pryv . Connection . prototype . revoke = async function revoke ( throwOnFail = true , usingConnection ) {
37+ usingConnection = usingConnection || this ;
38+ let accessInfo = null ;
39+ // get accessId
40+ try {
41+ accessInfo = await this . accessInfo ( ) ;
42+ } catch ( e ) {
43+ if ( e . response ?. body ?. error ?. id === 'invalid-access-token' ) {
44+ return null ; // Already revoked OK..
45+ }
46+ if ( throwOnFail ) throw e ;
47+ return null ;
48+ }
49+ // delete access
50+ try {
51+ const result = usingConnection . apiOne ( 'accesses.delete' , { id : accessInfo . id } ) ;
52+ return result ;
53+ } catch ( e ) {
54+ if ( throwOnFail ) throw e ;
55+ return null ;
56+ }
57+ } ;
2958}
3059
3160module . exports = pryv ;
0 commit comments