Merge pull request #342 from automatisch/refactor/use-http-client-for-typeform-auth
refactor: Use http client to authenticate typeform
This commit is contained in:
@@ -5,14 +5,12 @@ import type {
|
|||||||
IJSONObject,
|
IJSONObject,
|
||||||
} from '@automatisch/types';
|
} from '@automatisch/types';
|
||||||
import { URLSearchParams } from 'url';
|
import { URLSearchParams } from 'url';
|
||||||
import axios, { AxiosInstance } from 'axios';
|
import HttpClient from '../../helpers/http-client';
|
||||||
|
|
||||||
export default class Authentication implements IAuthentication {
|
export default class Authentication implements IAuthentication {
|
||||||
appData: IApp;
|
appData: IApp;
|
||||||
connectionData: IJSONObject;
|
connectionData: IJSONObject;
|
||||||
client: AxiosInstance = axios.create({
|
client: HttpClient;
|
||||||
baseURL: 'https://api.typeform.com',
|
|
||||||
});
|
|
||||||
|
|
||||||
scope: string[] = [
|
scope: string[] = [
|
||||||
'forms:read',
|
'forms:read',
|
||||||
@@ -27,6 +25,7 @@ export default class Authentication implements IAuthentication {
|
|||||||
constructor(appData: IApp, connectionData: IJSONObject) {
|
constructor(appData: IApp, connectionData: IJSONObject) {
|
||||||
this.connectionData = connectionData;
|
this.connectionData = connectionData;
|
||||||
this.appData = appData;
|
this.appData = appData;
|
||||||
|
this.client = new HttpClient({ baseURL: 'https://api.typeform.com' });
|
||||||
}
|
}
|
||||||
|
|
||||||
get oauthRedirectUrl() {
|
get oauthRedirectUrl() {
|
||||||
|
@@ -1,5 +1,6 @@
|
|||||||
import Context from '../../types/express/context';
|
import Context from '../../types/express/context';
|
||||||
import App from '../../models/app';
|
import App from '../../models/app';
|
||||||
|
import axios from 'axios';
|
||||||
|
|
||||||
type Params = {
|
type Params = {
|
||||||
input: {
|
input: {
|
||||||
@@ -22,11 +23,19 @@ const createAuthData = async (
|
|||||||
const appClass = (await import(`../../apps/${connection.key}`)).default;
|
const appClass = (await import(`../../apps/${connection.key}`)).default;
|
||||||
const appData = App.findOneByKey(connection.key);
|
const appData = App.findOneByKey(connection.key);
|
||||||
|
|
||||||
if (!connection.formattedData) { return null; }
|
if (!connection.formattedData) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
const appInstance = new appClass(appData, connection.formattedData);
|
const appInstance = new appClass(appData, connection.formattedData);
|
||||||
const authLink = await appInstance.authenticationClient.createAuthData();
|
const authLink = await appInstance.authenticationClient.createAuthData();
|
||||||
|
|
||||||
|
try {
|
||||||
|
await axios.get(authLink.url);
|
||||||
|
} catch (error) {
|
||||||
|
throw new Error('Error occured while creating authorization URL!');
|
||||||
|
}
|
||||||
|
|
||||||
await connection.$query().patch({
|
await connection.$query().patch({
|
||||||
formattedData: {
|
formattedData: {
|
||||||
...connection.formattedData,
|
...connection.formattedData,
|
||||||
|
Reference in New Issue
Block a user