Skip to content

Commit

Permalink
fix some issues
Browse files Browse the repository at this point in the history
  • Loading branch information
netroy committed May 2, 2023
1 parent 32b89e4 commit 8c28767
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 26 deletions.
9 changes: 1 addition & 8 deletions packages/@n8n_io/client-oauth2/src/ClientOAuth2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,9 @@ export class ClientOAuth2 {
/**
* Create a new token from existing data.
*/
createToken(
access: string,
refresh: string,
type?: string,
data?: ClientOAuth2TokenData,
): ClientOAuth2Token {
createToken(data: ClientOAuth2TokenData, type?: string): ClientOAuth2Token {
return new ClientOAuth2Token(this, {
...data,
access_token: access,
refresh_token: refresh,
...(typeof type === 'string' ? { token_type: type } : type),
});
}
Expand Down
6 changes: 2 additions & 4 deletions packages/@n8n_io/client-oauth2/src/ClientOAuth2Token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,8 @@ export class ClientOAuth2Token {
{},
);

const data = await this.client.request(config);
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
return this.client.createToken({ ...this.data, ...data });
const responseData = await this.client.request<ClientOAuth2TokenData>(config);
return this.client.createToken({ ...this.data, ...responseData });
}

/**
Expand Down
7 changes: 3 additions & 4 deletions packages/@n8n_io/client-oauth2/src/CodeFlow.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as qs from 'querystring';
import type { ClientOAuth2, ClientOAuth2Options } from './ClientOAuth2';
import type { ClientOAuth2Token } from './ClientOAuth2Token';
import type { ClientOAuth2Token, ClientOAuth2TokenData } from './ClientOAuth2Token';
import { DEFAULT_HEADERS, DEFAULT_URL_BASE } from './constants';
import { auth, expects, getAuthError, requestOptions, sanitizeScope } from './utils';

Expand Down Expand Up @@ -99,7 +99,7 @@ export class CodeFlow {
body.client_id = options.clientId;
}

const responseData = await this.client.request(
const responseData = await this.client.request<ClientOAuth2TokenData>(
requestOptions(
{
url: options.accessTokenUri,
Expand All @@ -110,8 +110,7 @@ export class CodeFlow {
options,
),
);
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore

return this.client.createToken(responseData);
}
}
8 changes: 3 additions & 5 deletions packages/@n8n_io/client-oauth2/src/CredentialsFlow.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { ClientOAuth2, ClientOAuth2Options } from './ClientOAuth2';
import type { ClientOAuth2Token } from './ClientOAuth2Token';
import type { ClientOAuth2Token, ClientOAuth2TokenData } from './ClientOAuth2Token';
import { DEFAULT_HEADERS } from './constants';
import { auth, expects, requestOptions, sanitizeScope } from './utils';

Expand Down Expand Up @@ -32,7 +32,7 @@ export class CredentialsFlow {
body.scope = sanitizeScope(options.scopes);
}

const data = await this.client.request(
const responseData = await this.client.request<ClientOAuth2TokenData>(
requestOptions(
{
url: options.accessTokenUri,
Expand All @@ -47,8 +47,6 @@ export class CredentialsFlow {
options,
),
);
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
return this.client.createToken(data);
return this.client.createToken(responseData);
}
}
13 changes: 8 additions & 5 deletions packages/core/src/NodeExecuteFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1121,13 +1121,16 @@ export async function requestOAuth2(
oauthTokenData = data;
}

const accessToken =
get(oauthTokenData, oAuth2Options?.property as string) || oauthTokenData.accessToken;
const refreshToken = oauthTokenData.refreshToken;
const token = oAuthClient.createToken(
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
get(oauthTokenData, oAuth2Options?.property as string) || oauthTokenData.accessToken,
oauthTokenData.refreshToken,
{
...oauthTokenData,
...(accessToken ? { access_token: accessToken } : {}),
...(refreshToken ? { refresh_token: refreshToken } : {}),
},
oAuth2Options?.tokenType || oauthTokenData.tokenType,
oauthTokenData,
);
// Signs the request by adding authorization headers or query parameters depending
// on the token-type used.
Expand Down

0 comments on commit 8c28767

Please sign in to comment.