Skip to content

Commit 45c4d7a

Browse files
committed
Rename refreshToken and retrieveAccessToken method; Auto-formatting
1 parent 48984e9 commit 45c4d7a

File tree

2 files changed

+59
-64
lines changed

2 files changed

+59
-64
lines changed

src/Oauth.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ export async function pollOauthSession(config: AuthenticationConfig, sessionId:
128128
}
129129
}
130130

131-
export async function retrieveAccessToken(
131+
export async function getAccessToken(
132132
config: AuthenticationConfig,
133133
code: string,
134134
codeVerifier: string,

src/index.ts

Lines changed: 58 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,29 @@ import {
55
Token,
66
computeAuthorizationUrl,
77
pollOauthSession,
8-
retrieveAccessToken,
9-
refreshToken,
10-
revokeToken,
8+
getAccessToken,
9+
getRefreshToken,
10+
revokeToken
1111
} from './Oauth';
1212

1313
const DOMAIN_WINDOW_DEFAULT_URL = 'https://dev.frontify.test/finder';
1414
const POPUP_DEFAULT_TITLE = 'Authorize Frontify';
1515
const POPUP_STATE = {
16-
open: false,
17-
};
16+
open: false
17+
}
1818

1919
let popup: Popup;
2020

2121
export async function authorize(
2222
configuration: AuthenticationConfig,
23-
popupConfiguration?: PopupConfiguration,
24-
): Promise<Token | void> {
23+
popupConfiguration?: PopupConfiguration
24+
): Promise<Token | void>{
25+
2526
if (POPUP_STATE.open) {
2627
logMessage('warning', {
2728
code: 'ERR_POPUP_OPEN',
28-
message: 'Popup already open!',
29-
});
29+
message: 'Popup already open!'
30+
})
3031
throw new Error('Popup already open!');
3132
}
3233

@@ -43,42 +44,38 @@ export async function authorize(
4344
POPUP_STATE.open = true;
4445

4546
if (!configuration.domain) {
46-
return openDomainPopUp(configuration, popup)
47-
.then((res) => {
48-
POPUP_STATE.open = false;
49-
if (res) {
50-
return res;
51-
}
52-
})
53-
.catch(() => {
54-
delete configuration.domain;
55-
logMessage('error', {
56-
code: 'ERR_AUTH_SKIPPED',
57-
message: 'Domain not inserted!',
58-
});
59-
throw new Error('Domain not inserted!');
47+
return openDomainPopUp(configuration, popup).then((res) => {
48+
POPUP_STATE.open = false;
49+
if (res) {
50+
return res;
51+
}
52+
}).catch(() => {
53+
delete(configuration.domain);
54+
logMessage('error', {
55+
code: 'ERR_AUTH_SKIPPED',
56+
message: 'Domain not inserted!'
6057
});
58+
throw new Error('Domain not inserted!');
59+
});
6160
} else {
62-
return authenticate(configuration, popup)
63-
.then((res) => {
64-
POPUP_STATE.open = false;
65-
if (res) {
66-
return res;
67-
}
68-
})
69-
.catch((error) => {
70-
POPUP_STATE.open = false;
71-
throw new Error(error);
72-
});
61+
return authenticate(configuration, popup).then((res) => {
62+
POPUP_STATE.open = false;
63+
if (res) {
64+
return res;
65+
}
66+
}).catch((error) => {
67+
POPUP_STATE.open = false;
68+
throw new Error(error);
69+
});
7370
}
7471
}
7572

7673
export async function refresh(token: Token): Promise<Token> {
77-
return refreshToken(token.bearerToken.domain, token.bearerToken.refreshToken, token.clientId, token.scopes);
74+
return getRefreshToken(token.bearerToken.domain, token.bearerToken.refreshToken, token.clientId, token.scopes);
7875
}
7976

8077
export async function revoke(token: Token): Promise<Token> {
81-
await revokeToken(token.bearerToken.domain, token.bearerToken.accessToken);
78+
await revokeToken(token.bearerToken.domain, token.bearerToken.accessToken)
8279
return token;
8380
}
8481

@@ -87,13 +84,13 @@ async function authenticate(configuration: AuthenticationConfig, popUp: Popup):
8784
const { authorizationUrl, codeVerifier, sessionId } = await computeAuthorizationUrl(configuration);
8885
await openAuthPopUp(authorizationUrl, popUp);
8986
const authorizationCode = await pollOauthSession(configuration, sessionId);
90-
return retrieveAccessToken(configuration, authorizationCode, codeVerifier);
87+
return getAccessToken(configuration, authorizationCode, codeVerifier);
9188
} catch (error) {
9289
const errorMessage = `Error generating session. Make sure that the inserted domain is a valid and secure Frontify instance.`;
93-
popUp.popUp?.postMessage({ domainError: errorMessage }, '*');
90+
popUp.popUp?.postMessage({domainError: errorMessage}, '*');
9491
logMessage('error', {
9592
code: 'ERR_AUTH_FAILED',
96-
message: errorMessage,
93+
message: errorMessage
9794
});
9895
throw new Error(errorMessage);
9996
}
@@ -104,7 +101,7 @@ function openDomainPopUp(configuration: AuthenticationConfig, popUp: Popup): Pro
104101

105102
logMessage('warning', {
106103
code: 'WARN_DOMAIN_OPEN',
107-
message: 'Popup window opened!',
104+
message: 'Popup window opened!'
108105
});
109106

110107
return new Promise((resolve, reject) => {
@@ -114,24 +111,22 @@ function openDomainPopUp(configuration: AuthenticationConfig, popUp: Popup): Pro
114111
reject();
115112
logMessage('error', {
116113
code: 'ERR_DOMAIN_TIMEOUT',
117-
message: 'Popup window timeout!',
118-
});
114+
message: 'Popup window timeout!'
115+
})
119116
}, 5 * 60 * 1000);
120117

121118
popUp.onDomain(() => {
122119
clearTimeout(timeout);
123120
configuration.domain = popup.getDomain();
124-
authenticate(configuration, popup)
125-
.then((result) => {
126-
resolve(result);
127-
})
128-
.catch((error) => {
129-
throw new Error(error ?? 'Could not verify instance!');
130-
});
121+
authenticate(configuration, popup).then((result) => {
122+
resolve(result);
123+
}).catch((error) => {
124+
throw new Error(error ?? 'Could not verify instance!');
125+
});
131126
logMessage('warning', {
132127
code: 'WARN_DOMAIN_SELECT',
133-
message: 'Domain input submitted!',
134-
});
128+
message: 'Domain input submitted!'
129+
})
135130
});
136131

137132
popUp.onAborted(() => {
@@ -141,8 +136,8 @@ function openDomainPopUp(configuration: AuthenticationConfig, popUp: Popup): Pro
141136
reject();
142137
logMessage('warning', {
143138
code: 'WARN_DOMAIN_CLOSED',
144-
message: 'Popup window closed!',
145-
});
139+
message: 'Popup window closed!'
140+
})
146141
});
147142
});
148143
}
@@ -152,7 +147,7 @@ function openAuthPopUp(url: string, popUp: Popup): Promise<void> {
152147

153148
logMessage('warning', {
154149
code: 'WARN_DOMAIN_OPEN',
155-
message: 'Popup window opened!',
150+
message: 'Popup window opened!'
156151
});
157152

158153
return new Promise((resolve, reject) => {
@@ -162,8 +157,8 @@ function openAuthPopUp(url: string, popUp: Popup): Promise<void> {
162157
reject();
163158
logMessage('error', {
164159
code: 'ERR_DOMAIN_TIMEOUT',
165-
message: 'Popup window timeout!',
166-
});
160+
message: 'Popup window timeout!'
161+
})
167162
}, 5 * 60 * 1000);
168163

169164
popUp.onAborted(() => {
@@ -173,8 +168,8 @@ function openAuthPopUp(url: string, popUp: Popup): Promise<void> {
173168
reject();
174169
logMessage('warning', {
175170
code: 'WARN_DOMAIN_CLOSED',
176-
message: 'Popup window closed!',
177-
});
171+
message: 'Popup window closed!'
172+
})
178173
});
179174

180175
popUp.onSuccess(() => {
@@ -184,8 +179,8 @@ function openAuthPopUp(url: string, popUp: Popup): Promise<void> {
184179
resolve();
185180
logMessage('warning', {
186181
code: 'WARN_AUTH_SUCCESS',
187-
message: 'Auth success!',
188-
});
182+
message: 'Auth success!'
183+
})
189184
});
190185

191186
popUp.onCancelled(() => {
@@ -195,12 +190,12 @@ function openAuthPopUp(url: string, popUp: Popup): Promise<void> {
195190
reject();
196191
logMessage('warning', {
197192
code: 'WARN_AUTH_CANCELLED',
198-
message: 'Auth cancelled!',
199-
});
193+
message: 'Auth cancelled!'
194+
})
200195
});
201196
});
202197
}
203198

204199
function createPopUp(configuration: PopupConfiguration): Popup {
205200
return new Popup(configuration ?? {});
206-
}
201+
}

0 commit comments

Comments
 (0)