Skip to content

Commit

Permalink
Remove ember-ajax and use ember-fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
SmitGala committed Jan 6, 2025
1 parent 974855a commit 048a75b
Show file tree
Hide file tree
Showing 89 changed files with 1,316 additions and 710 deletions.
8 changes: 2 additions & 6 deletions app/adapters/file-report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import { ModelSchema } from 'ember-data';
import commondrf from './commondrf';
import Store, { Snapshot } from '@ember-data/store';
import ModelRegistry from 'ember-data/types/registries/model';
import NetworkService from 'irene/services/network';
import { inject as service } from '@ember/service';
import { FileReportScanType } from 'irene/models/file-report';

interface FileReportQuery {
Expand All @@ -18,8 +16,6 @@ enum REPORT_TYPE_ENDPOINT {
}

export default class FileReport extends commondrf {
@service declare network: NetworkService;

filesBaseUrl = this.buildURLFromBase(`${this.namespace_v2}/files`);
reportsBaseUrl = this.buildURLFromBase(`${this.namespace_v2}/reports`);

Expand Down Expand Up @@ -54,11 +50,11 @@ export default class FileReport extends commondrf {
const reportTypeEndpoint = REPORT_TYPE_ENDPOINT[type];
const url = `${this.reportsBaseUrl}/${reportId}/${reportTypeEndpoint}`;

const response = await this.network.request(url, {
const response = await this.ajax(url, 'GET', {
headers: this.headers,
});

return response.json() as { url?: string };
return response as { url?: string };
}
}

Expand Down
3 changes: 2 additions & 1 deletion app/authenticators/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import RouterService from '@ember/routing/router-service';
import ENV from 'irene/config/environment';
import OidcService from 'irene/services/oidc';
import FreshdeskService from 'irene/services/freshdesk';
import type IreneAjaxService from 'irene/services/ajax';

export interface LoginSuccessDataProps {
token: string;
Expand Down Expand Up @@ -35,7 +36,7 @@ export const processData = (data: LoginSuccessDataProps) => {
};

export default class BaseAuthenticator extends Base {
@service declare ajax: any;
@service declare ajax: IreneAjaxService;
@service declare session: any;
@service declare router: RouterService;
@service('notifications') declare notify: NotificationService;
Expand Down
2 changes: 1 addition & 1 deletion app/authenticators/irene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class IreneAuthenticator extends BaseAuthenticator {

const url = ENV['ember-simple-auth']['loginEndPoint'];

return this.ajax.post(url, { data }).then((data: LoginSuccessDataProps) => {
return this.ajax.post<LoginSuccessDataProps>(url, { data }).then((data) => {
this.restoreLastTransition(data.user_id);

return processData(data);
Expand Down
37 changes: 20 additions & 17 deletions app/authenticators/saml2.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,34 @@
import ENV from 'irene/config/environment';
import BaseAuthenticator, { LoginSuccessDataProps, processData } from './base';
import type { AjaxError } from 'irene/services/ajax';

export default class Saml2Auth extends BaseAuthenticator {
authenticate(ssotoken: string) {
return new Promise((resolve, reject) => {
const url = ENV['endpoints']['saml2Login'];
const url = ENV['endpoints']['saml2Login'] as string;

this.ajax.post(url, { data: { token: ssotoken } }).then(
(data: LoginSuccessDataProps) => {
this.restoreLastTransition(data.user_id);
this.ajax
.post<LoginSuccessDataProps>(url, { data: { token: ssotoken } })
.then(
(data) => {
this.restoreLastTransition(data.user_id);

data = processData(data);
resolve(data);
},
(error: AjaxError) => {
let msg = 'Login failed';
data = processData(data);
resolve(data);
},
(error: AjaxError) => {
let msg = 'Login failed';

if (error.payload.message) {
msg = 'Login failed: ' + error.payload.message;
}
if (error.payload.message) {
msg = 'Login failed: ' + error.payload.message;
}

this.notify.error(msg);
this.router.transitionTo('login');
this.notify.error(msg);
this.router.transitionTo('login');

return reject(msg);
}
);
return reject(msg);
}
);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import IntlService from 'ember-intl/services/intl';
import ENV from 'irene/config/environment';

import PersonaltokenModel from 'irene/models/personaltoken';
import type IreneAjaxService from 'irene/services/ajax';
import type { AjaxError } from 'irene/services/ajax';

export interface AccountSettingsDeveloperSettingsPersonaltokenListDeleteTokenSignature {
Args: {
Expand All @@ -17,7 +19,7 @@ export interface AccountSettingsDeveloperSettingsPersonaltokenListDeleteTokenSig

export default class AccountSettingsDeveloperSettingsPersonaltokenListDeleteTokenComponent extends Component<AccountSettingsDeveloperSettingsPersonaltokenListDeleteTokenSignature> {
@service declare intl: IntlService;
@service declare ajax: any;
@service declare ajax: IreneAjaxService;
@service('notifications') declare notify: NotificationService;

@tracked showRevokePersonalTokenConfirmBox = false;
Expand Down Expand Up @@ -50,7 +52,7 @@ export default class AccountSettingsDeveloperSettingsPersonaltokenListDeleteToke
this.notify.success(tTokenRevoked);
} catch (error) {
if (!this.isDestroyed) {
this.notify.error((error as AdapterError).payload.message);
this.notify.error((error as AjaxError).payload.message);
}
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ import { task } from 'ember-concurrency';
import { query } from 'ember-data-resources';

import PersonaltokenModel from 'irene/models/personaltoken';
import type IreneAjaxService from 'irene/services/ajax';
import type { AjaxError } from 'irene/services/ajax';

export default class AccountSettingsDeveloperSettingsPersonaltokenListComponent extends Component {
@service declare intl: IntlService;
@service declare ajax: any;
@service declare ajax: IreneAjaxService;
@service declare store: Store;
@service('notifications') declare notify: NotificationService;

Expand Down Expand Up @@ -78,7 +80,7 @@ export default class AccountSettingsDeveloperSettingsPersonaltokenListComponent
name: this.tokenName,
};

await this.ajax.post(ENV.endpoints['personaltokens'], {
await this.ajax.post(ENV.endpoints['personaltokens'] as string, {
data,
});

Expand All @@ -91,7 +93,7 @@ export default class AccountSettingsDeveloperSettingsPersonaltokenListComponent
this.notify.success(tTokenCreated);
} catch (error) {
if (!this.isDestroyed) {
this.notify.error((error as AdapterError).payload.message);
this.notify.error((error as AjaxError).payload.message);
}
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import type Store from '@ember-data/store';
import ENV from 'irene/config/environment';
import parseError from 'irene/utils/parse-error';
import type DatetimeService from 'irene/services/datetime';
import type IreneAjaxService from 'irene/services/ajax';
import type { AjaxError } from 'irene/services/ajax';

const localeStrings = {
en: 'English',
Expand All @@ -19,7 +21,7 @@ type LocaleKeys = keyof typeof localeStrings;

export default class AccountSettingsGeneralSelectLanguageComponent extends Component {
@service declare intl: IntlService;
@service declare ajax: any;
@service declare ajax: IreneAjaxService;
@service declare datetime: DatetimeService;
@service declare session: any;
@service declare store: Store;
Expand Down Expand Up @@ -65,13 +67,14 @@ export default class AccountSettingsGeneralSelectLanguageComponent extends Compo
};

try {
await this.ajax.post(ENV.endpoints['lang'], { data });
await this.ajax.post(ENV.endpoints['lang'] as string, { data });

if (!this.isDestroyed) {
window.location.reload();
}
} catch (err) {
const error = err as AdapterError;
const error = err as AjaxError;

this.notify.error(error.payload.message);
}
});
Expand Down
45 changes: 27 additions & 18 deletions app/components/account-settings/security/multi-factor-auth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,21 @@ import ENUMS from 'irene/enums';
import MeService from 'irene/services/me';
import MfaModel from 'irene/models/mfa';
import UserModel from 'irene/models/user';
import type IreneAjaxService from 'irene/services/ajax';
import type { AjaxError } from 'irene/services/ajax';

type MfaConfirmEventData = { cancel: boolean; otp?: string };

type TokenData = {
token: string;
secret: string;
};

export default class AccountSettingsSecurityMultiFactorAuthComponent extends Component.extend(
Evented
) {
@service declare intl: IntlService;
@service declare ajax: any;
@service declare ajax: IreneAjaxService;
@service('notifications') declare notify: NotificationService;
@service declare me: MeService;
@service declare store: Store;
Expand Down Expand Up @@ -169,7 +176,7 @@ export default class AccountSettingsSecurityMultiFactorAuthComponent extends Com
});

getMFAEnableEmailToken = task(async () => {
return await this.ajax.post(this.mfaEndpoint, {
return await this.ajax.post<TokenData>(this.mfaEndpoint, {
data: {
method: ENUMS.MFA_METHOD.HOTP,
},
Expand All @@ -188,7 +195,7 @@ export default class AccountSettingsSecurityMultiFactorAuthComponent extends Com

return true;
} catch (error) {
const errorObj = (error as AdapterError).payload || {};
const errorObj = (error as AjaxError).payload || {};
const otpMsg = errorObj.otp && errorObj.otp[0];

if (otpMsg) {
Expand Down Expand Up @@ -302,7 +309,7 @@ export default class AccountSettingsSecurityMultiFactorAuthComponent extends Com
});

getMFAEnableAppToken = task(async () => {
return await this.ajax.post(this.mfaEndpoint, {
return await this.ajax.post<TokenData>(this.mfaEndpoint, {
data: {
method: ENUMS.MFA_METHOD.TOTP,
},
Expand Down Expand Up @@ -331,7 +338,7 @@ export default class AccountSettingsSecurityMultiFactorAuthComponent extends Com

return true;
} catch (error) {
const errorObj = (error as AdapterError).payload || {};
const errorObj = (error as AjaxError).payload || {};
const otpMsg = errorObj.otp && errorObj.otp[0];

if (otpMsg) {
Expand Down Expand Up @@ -448,14 +455,14 @@ export default class AccountSettingsSecurityMultiFactorAuthComponent extends Com

verifySwitchToEmailAppOTP = task(async (otp) => {
try {
return await this.ajax.post(this.mfaEndpoint, {
return await this.ajax.post<TokenData>(this.mfaEndpoint, {
data: {
method: ENUMS.MFA_METHOD.HOTP,
otp: otp || '',
},
});
} catch (error) {
const errorObj = (error as AdapterError).payload || {};
const errorObj = (error as AjaxError).payload || {};
const otpMsg = errorObj.otp && errorObj.otp[0];
if (otpMsg) {
this.notify.error(this.tInvalidOTP);
Expand All @@ -481,7 +488,7 @@ export default class AccountSettingsSecurityMultiFactorAuthComponent extends Com

return true;
} catch (error) {
const errorObj = (error as AdapterError).payload || {};
const errorObj = (error as AjaxError).payload || {};
const otpMsg = errorObj.otp && errorObj.otp[0];

if (otpMsg) {
Expand Down Expand Up @@ -517,7 +524,7 @@ export default class AccountSettingsSecurityMultiFactorAuthComponent extends Com
appOTPNotConfirmed = !(tokenData || {}).token;
} while (appOTPNotConfirmed);

debug('SwitchTOEmail: App OTP Token Data ' + tokenData.token);
debug('SwitchTOEmail: App OTP Token Data ' + tokenData?.token);

while (true) {
debug('SwitchTOEmail: In Email OTP Loop');
Expand All @@ -529,7 +536,7 @@ export default class AccountSettingsSecurityMultiFactorAuthComponent extends Com

const confirmed = await this.verifySwitchToEmailEmailOTP.perform(
emailOTPData.otp,
tokenData.token
tokenData?.token
);

if (confirmed) {
Expand Down Expand Up @@ -651,7 +658,7 @@ export default class AccountSettingsSecurityMultiFactorAuthComponent extends Com
},
});
} catch (error) {
const errorObj = (error as AdapterError).payload || {};
const errorObj = (error as AjaxError).payload || {};
const otpMsg = errorObj.otp && errorObj.otp[0];

if (otpMsg) {
Expand All @@ -672,14 +679,14 @@ export default class AccountSettingsSecurityMultiFactorAuthComponent extends Com
}

try {
return await this.ajax.post(this.mfaEndpoint, {
return await this.ajax.post<TokenData>(this.mfaEndpoint, {
data: {
method: ENUMS.MFA_METHOD.TOTP,
otp: otp,
},
});
} catch (error) {
const errorObj = (error as AdapterError).payload || {};
const errorObj = (error as AjaxError).payload || {};
const otpMsg = errorObj.otp && errorObj.otp[0];

if (otpMsg) {
Expand Down Expand Up @@ -712,7 +719,7 @@ export default class AccountSettingsSecurityMultiFactorAuthComponent extends Com

return true;
} catch (error) {
const errorObj = (error as AdapterError).payload || {};
const errorObj = (error as AjaxError).payload || {};
const otpMsg = errorObj.otp && errorObj.otp[0];

if (otpMsg) {
Expand All @@ -733,7 +740,7 @@ export default class AccountSettingsSecurityMultiFactorAuthComponent extends Com
}

let emailOTPNotConfirmed;
let tokenData;
let tokenData: TokenData;

await this.staInitialEmail.perform();

Expand All @@ -745,7 +752,9 @@ export default class AccountSettingsSecurityMultiFactorAuthComponent extends Com
return;
}

tokenData = await this.staVerifyEmailOTP.perform(emailOTPData.otp);
tokenData = (await this.staVerifyEmailOTP.perform(
emailOTPData.otp
)) as TokenData;

emailOTPNotConfirmed = !(tokenData || {}).token;
} while (emailOTPNotConfirmed);
Expand Down Expand Up @@ -878,7 +887,7 @@ export default class AccountSettingsSecurityMultiFactorAuthComponent extends Com
data,
});
} catch (error) {
const payload = (error as AdapterError).payload || {};
const payload = (error as AjaxError).payload || {};

if (payload.otp && payload.otp.length) {
return;
Expand All @@ -901,7 +910,7 @@ export default class AccountSettingsSecurityMultiFactorAuthComponent extends Com

return true;
} catch (error) {
const errorObj = (error as AdapterError).payload || {};
const errorObj = (error as AjaxError).payload || {};
const otpMsg = errorObj.otp && errorObj.otp[0];

if (otpMsg) {
Expand Down
Loading

0 comments on commit 048a75b

Please sign in to comment.