From 8c28767352e9a7de855a52680b4f54ca29823e4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E0=A4=95=E0=A4=BE=E0=A4=B0=E0=A4=A4=E0=A5=8B=E0=A4=AB?= =?UTF-8?q?=E0=A5=8D=E0=A4=AB=E0=A5=87=E0=A4=B2=E0=A4=B8=E0=A5=8D=E0=A4=95?= =?UTF-8?q?=E0=A5=8D=E0=A4=B0=E0=A4=BF=E0=A4=AA=E0=A5=8D=E0=A4=9F=E2=84=A2?= Date: Fri, 21 Apr 2023 15:07:40 +0200 Subject: [PATCH] fix some issues --- packages/@n8n_io/client-oauth2/src/ClientOAuth2.ts | 9 +-------- .../@n8n_io/client-oauth2/src/ClientOAuth2Token.ts | 6 ++---- packages/@n8n_io/client-oauth2/src/CodeFlow.ts | 7 +++---- .../@n8n_io/client-oauth2/src/CredentialsFlow.ts | 8 +++----- packages/core/src/NodeExecuteFunctions.ts | 13 ++++++++----- 5 files changed, 17 insertions(+), 26 deletions(-) diff --git a/packages/@n8n_io/client-oauth2/src/ClientOAuth2.ts b/packages/@n8n_io/client-oauth2/src/ClientOAuth2.ts index 6b9a1db281483..8900666a84159 100644 --- a/packages/@n8n_io/client-oauth2/src/ClientOAuth2.ts +++ b/packages/@n8n_io/client-oauth2/src/ClientOAuth2.ts @@ -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), }); } diff --git a/packages/@n8n_io/client-oauth2/src/ClientOAuth2Token.ts b/packages/@n8n_io/client-oauth2/src/ClientOAuth2Token.ts index d2a26b134c336..9f39bd1ef0f81 100644 --- a/packages/@n8n_io/client-oauth2/src/ClientOAuth2Token.ts +++ b/packages/@n8n_io/client-oauth2/src/ClientOAuth2Token.ts @@ -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(config); + return this.client.createToken({ ...this.data, ...responseData }); } /** diff --git a/packages/@n8n_io/client-oauth2/src/CodeFlow.ts b/packages/@n8n_io/client-oauth2/src/CodeFlow.ts index 2509947a16e7d..c0f09f03dfb19 100644 --- a/packages/@n8n_io/client-oauth2/src/CodeFlow.ts +++ b/packages/@n8n_io/client-oauth2/src/CodeFlow.ts @@ -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'; @@ -99,7 +99,7 @@ export class CodeFlow { body.client_id = options.clientId; } - const responseData = await this.client.request( + const responseData = await this.client.request( requestOptions( { url: options.accessTokenUri, @@ -110,8 +110,7 @@ export class CodeFlow { options, ), ); - // eslint-disable-next-line @typescript-eslint/ban-ts-comment - // @ts-ignore + return this.client.createToken(responseData); } } diff --git a/packages/@n8n_io/client-oauth2/src/CredentialsFlow.ts b/packages/@n8n_io/client-oauth2/src/CredentialsFlow.ts index 2dc4b1a06f89b..cd2ae70991172 100644 --- a/packages/@n8n_io/client-oauth2/src/CredentialsFlow.ts +++ b/packages/@n8n_io/client-oauth2/src/CredentialsFlow.ts @@ -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'; @@ -32,7 +32,7 @@ export class CredentialsFlow { body.scope = sanitizeScope(options.scopes); } - const data = await this.client.request( + const responseData = await this.client.request( requestOptions( { url: options.accessTokenUri, @@ -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); } } diff --git a/packages/core/src/NodeExecuteFunctions.ts b/packages/core/src/NodeExecuteFunctions.ts index ba43de3bd8848..1cfd3b692fb78 100644 --- a/packages/core/src/NodeExecuteFunctions.ts +++ b/packages/core/src/NodeExecuteFunctions.ts @@ -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.