chore: prettier
This commit is contained in:
parent
4797ea7be2
commit
3250edf166
214
src/account.ts
214
src/account.ts
|
@ -1,103 +1,133 @@
|
|||
import {
|
||||
LoginRequest, LoginResponse,
|
||||
OTPDisableRequest,
|
||||
OTPGenerateResponse,
|
||||
OTPValidateRequest,
|
||||
OTPVerifyRequest,
|
||||
PasswordResetVerifyRequest, postApiAuthPasswordResetRequest,
|
||||
RegisterRequest,
|
||||
VerifyEmailRequest,
|
||||
} from './account/generated/index.js';
|
||||
LoginRequest,
|
||||
LoginResponse,
|
||||
OTPDisableRequest,
|
||||
OTPGenerateResponse,
|
||||
OTPValidateRequest,
|
||||
OTPVerifyRequest,
|
||||
PasswordResetVerifyRequest,
|
||||
postApiAuthPasswordResetRequest,
|
||||
RegisterRequest,
|
||||
VerifyEmailRequest,
|
||||
} from "./account/generated/index.js";
|
||||
import {
|
||||
postApiAuthLogin,
|
||||
postApiAuthRegister,
|
||||
postApiAuthVerifyEmail,
|
||||
getApiAuthOtpGenerate,
|
||||
postApiAuthOtpVerify,
|
||||
postApiAuthOtpValidate,
|
||||
postApiAuthOtpDisable,
|
||||
PasswordResetRequest,
|
||||
postApiAuthPasswordResetConfirm,
|
||||
} from './account/generated/index.js';
|
||||
import { AxiosResponse } from 'axios';
|
||||
postApiAuthLogin,
|
||||
postApiAuthRegister,
|
||||
postApiAuthVerifyEmail,
|
||||
getApiAuthOtpGenerate,
|
||||
postApiAuthOtpVerify,
|
||||
postApiAuthOtpValidate,
|
||||
postApiAuthOtpDisable,
|
||||
PasswordResetRequest,
|
||||
postApiAuthPasswordResetConfirm,
|
||||
} from "./account/generated/index.js";
|
||||
import { AxiosResponse } from "axios";
|
||||
|
||||
export default class AccountApi {
|
||||
private apiUrl: string;
|
||||
private jwtToken?: string;
|
||||
private apiUrl: string;
|
||||
private jwtToken?: string;
|
||||
|
||||
constructor(apiUrl: string) {
|
||||
this.apiUrl = apiUrl;
|
||||
constructor(apiUrl: string) {
|
||||
this.apiUrl = apiUrl;
|
||||
}
|
||||
|
||||
public static create(apiUrl: string): AccountApi {
|
||||
return new AccountApi(apiUrl);
|
||||
}
|
||||
|
||||
public async login(loginRequest: LoginRequest): Promise<boolean> {
|
||||
let ret = this.checkSuccessVal<LoginResponse>(
|
||||
await postApiAuthLogin(loginRequest, { baseURL: this.apiUrl }),
|
||||
);
|
||||
if (ret) {
|
||||
this.jwtToken = (ret as LoginResponse).token;
|
||||
}
|
||||
|
||||
public static create(apiUrl: string): AccountApi {
|
||||
return new AccountApi(apiUrl);
|
||||
return false;
|
||||
}
|
||||
|
||||
public async register(registerRequest: RegisterRequest): Promise<boolean> {
|
||||
return this.checkSuccessBool(
|
||||
await postApiAuthRegister(registerRequest, this.buildOptions()),
|
||||
);
|
||||
}
|
||||
|
||||
public async verifyEmail(
|
||||
verifyEmailRequest: VerifyEmailRequest,
|
||||
): Promise<boolean> {
|
||||
return this.checkSuccessBool(
|
||||
await postApiAuthVerifyEmail(verifyEmailRequest, this.buildOptions()),
|
||||
);
|
||||
}
|
||||
|
||||
public async generateOtp(): Promise<boolean | OTPGenerateResponse> {
|
||||
return this.checkSuccessVal<OTPGenerateResponse>(
|
||||
await getApiAuthOtpGenerate(this.buildOptions()),
|
||||
);
|
||||
}
|
||||
|
||||
public async verifyOtp(otpVerifyRequest: OTPVerifyRequest): Promise<boolean> {
|
||||
return this.checkSuccessBool(
|
||||
await postApiAuthOtpVerify(otpVerifyRequest, this.buildOptions()),
|
||||
);
|
||||
}
|
||||
|
||||
public async validateOtp(
|
||||
otpValidateRequest: OTPValidateRequest,
|
||||
): Promise<boolean> {
|
||||
return this.checkSuccessBool(
|
||||
await postApiAuthOtpValidate(otpValidateRequest, this.buildOptions()),
|
||||
);
|
||||
}
|
||||
|
||||
public async disableOtp(
|
||||
otpDisableRequest: OTPDisableRequest,
|
||||
): Promise<boolean> {
|
||||
return this.checkSuccessBool(
|
||||
await postApiAuthOtpDisable(otpDisableRequest, this.buildOptions()),
|
||||
);
|
||||
}
|
||||
|
||||
public async requestPasswordReset(
|
||||
passwordResetRequest: PasswordResetRequest,
|
||||
): Promise<boolean> {
|
||||
return this.checkSuccessBool(
|
||||
await postApiAuthPasswordResetRequest(
|
||||
passwordResetRequest,
|
||||
this.buildOptions(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
public async confirmPasswordReset(
|
||||
passwordResetVerifyRequest: PasswordResetVerifyRequest,
|
||||
): Promise<boolean> {
|
||||
return this.checkSuccessBool(
|
||||
await postApiAuthPasswordResetConfirm(
|
||||
passwordResetVerifyRequest,
|
||||
this.buildOptions(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
private checkSuccessBool(ret: AxiosResponse<void>): boolean {
|
||||
return ret.status === 200;
|
||||
}
|
||||
|
||||
private checkSuccessVal<T>(ret: AxiosResponse<T>): T | boolean {
|
||||
if (ret.status === 200) {
|
||||
return ret.data as T;
|
||||
}
|
||||
|
||||
public async login(loginRequest: LoginRequest): Promise<boolean> {
|
||||
let ret = this.checkSuccessVal<LoginResponse>(await postApiAuthLogin(loginRequest, { baseURL: this.apiUrl }))
|
||||
if (ret) {
|
||||
this.jwtToken = (ret as LoginResponse).token;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public async register(registerRequest: RegisterRequest): Promise<boolean> {
|
||||
return this.checkSuccessBool(await postApiAuthRegister(registerRequest, this.buildOptions()));
|
||||
}
|
||||
|
||||
public async verifyEmail(verifyEmailRequest: VerifyEmailRequest): Promise<boolean> {
|
||||
return this.checkSuccessBool(await postApiAuthVerifyEmail(verifyEmailRequest, this.buildOptions()));
|
||||
}
|
||||
|
||||
|
||||
public async generateOtp(): Promise<boolean | OTPGenerateResponse> {
|
||||
return this.checkSuccessVal<OTPGenerateResponse>(await getApiAuthOtpGenerate( this.buildOptions()));
|
||||
}
|
||||
|
||||
public async verifyOtp(otpVerifyRequest: OTPVerifyRequest): Promise<boolean> {
|
||||
return this.checkSuccessBool(await postApiAuthOtpVerify(otpVerifyRequest, this.buildOptions()));
|
||||
}
|
||||
|
||||
public async validateOtp(otpValidateRequest: OTPValidateRequest): Promise<boolean> {
|
||||
return this.checkSuccessBool(await postApiAuthOtpValidate(otpValidateRequest, this.buildOptions()));
|
||||
}
|
||||
|
||||
|
||||
public async disableOtp(otpDisableRequest: OTPDisableRequest): Promise<boolean> {
|
||||
return this.checkSuccessBool(await postApiAuthOtpDisable(otpDisableRequest, this.buildOptions()));
|
||||
}
|
||||
|
||||
public async requestPasswordReset(
|
||||
passwordResetRequest: PasswordResetRequest,
|
||||
): Promise<boolean> {
|
||||
return this.checkSuccessBool(await postApiAuthPasswordResetRequest(passwordResetRequest, this.buildOptions()));
|
||||
}
|
||||
|
||||
public async confirmPasswordReset(
|
||||
passwordResetVerifyRequest: PasswordResetVerifyRequest,
|
||||
): Promise<boolean> {
|
||||
return this.checkSuccessBool(await postApiAuthPasswordResetConfirm(passwordResetVerifyRequest, this.buildOptions()));
|
||||
}
|
||||
|
||||
private checkSuccessBool(ret: AxiosResponse<void>): boolean {
|
||||
return ret.status === 200;
|
||||
}
|
||||
|
||||
private checkSuccessVal<T>(ret: AxiosResponse<T>): T | boolean {
|
||||
if (ret.status === 200) {
|
||||
return ret.data as T;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private buildOptions(): any {
|
||||
return {
|
||||
baseURL: this.apiUrl,
|
||||
headers: {
|
||||
Authorization: `Bearer ${this.jwtToken}`,
|
||||
},
|
||||
};
|
||||
}
|
||||
private buildOptions(): any {
|
||||
return {
|
||||
baseURL: this.apiUrl,
|
||||
headers: {
|
||||
Authorization: `Bearer ${this.jwtToken}`,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,210 +1,210 @@
|
|||
openapi: 3.0.0
|
||||
info:
|
||||
title: Account Management API
|
||||
version: "1.0"
|
||||
description: API for managing user accounts, including login, registration, OTP operations, and password resets.
|
||||
title: Account Management API
|
||||
version: "1.0"
|
||||
description: API for managing user accounts, including login, registration, OTP operations, and password resets.
|
||||
paths:
|
||||
/api/auth/login:
|
||||
post:
|
||||
summary: Login to the system
|
||||
requestBody:
|
||||
required: true
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/LoginRequest'
|
||||
responses:
|
||||
'200':
|
||||
description: Successfully logged in
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/LoginResponse'
|
||||
'401':
|
||||
description: Unauthorized
|
||||
/api/auth/register:
|
||||
post:
|
||||
summary: Register a new account
|
||||
requestBody:
|
||||
required: true
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/RegisterRequest'
|
||||
responses:
|
||||
'200':
|
||||
description: Successfully registered
|
||||
'400':
|
||||
description: Bad Request
|
||||
/api/auth/verify-email:
|
||||
post:
|
||||
summary: Verify email address
|
||||
requestBody:
|
||||
required: true
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/VerifyEmailRequest'
|
||||
responses:
|
||||
'200':
|
||||
description: Email verified successfully
|
||||
/api/auth/otp/generate:
|
||||
get:
|
||||
summary: Generate OTP for two-factor authentication
|
||||
responses:
|
||||
'200':
|
||||
description: OTP generated successfully
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/OTPGenerateResponse'
|
||||
/api/auth/otp/verify:
|
||||
post:
|
||||
summary: Verify OTP for enabling two-factor authentication
|
||||
requestBody:
|
||||
required: true
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/OTPVerifyRequest'
|
||||
responses:
|
||||
'200':
|
||||
description: OTP verified successfully
|
||||
/api/auth/otp/validate:
|
||||
post:
|
||||
summary: Validate OTP for two-factor authentication login
|
||||
requestBody:
|
||||
required: true
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/OTPValidateRequest'
|
||||
responses:
|
||||
'200':
|
||||
description: OTP validated successfully
|
||||
/api/auth/otp/disable:
|
||||
post:
|
||||
summary: Disable OTP for two-factor authentication
|
||||
requestBody:
|
||||
required: true
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/OTPDisableRequest'
|
||||
responses:
|
||||
'200':
|
||||
description: OTP disabled successfully
|
||||
/api/auth/password-reset/request:
|
||||
post:
|
||||
summary: Request a password reset
|
||||
requestBody:
|
||||
required: true
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/PasswordResetRequest'
|
||||
responses:
|
||||
'200':
|
||||
description: Password reset requested successfully
|
||||
/api/auth/password-reset/confirm:
|
||||
post:
|
||||
summary: Confirm a password reset
|
||||
requestBody:
|
||||
required: true
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/PasswordResetVerifyRequest'
|
||||
responses:
|
||||
'200':
|
||||
description: Password reset successfully
|
||||
/api/auth/login:
|
||||
post:
|
||||
summary: Login to the system
|
||||
requestBody:
|
||||
required: true
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: "#/components/schemas/LoginRequest"
|
||||
responses:
|
||||
"200":
|
||||
description: Successfully logged in
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: "#/components/schemas/LoginResponse"
|
||||
"401":
|
||||
description: Unauthorized
|
||||
/api/auth/register:
|
||||
post:
|
||||
summary: Register a new account
|
||||
requestBody:
|
||||
required: true
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: "#/components/schemas/RegisterRequest"
|
||||
responses:
|
||||
"200":
|
||||
description: Successfully registered
|
||||
"400":
|
||||
description: Bad Request
|
||||
/api/auth/verify-email:
|
||||
post:
|
||||
summary: Verify email address
|
||||
requestBody:
|
||||
required: true
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: "#/components/schemas/VerifyEmailRequest"
|
||||
responses:
|
||||
"200":
|
||||
description: Email verified successfully
|
||||
/api/auth/otp/generate:
|
||||
get:
|
||||
summary: Generate OTP for two-factor authentication
|
||||
responses:
|
||||
"200":
|
||||
description: OTP generated successfully
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: "#/components/schemas/OTPGenerateResponse"
|
||||
/api/auth/otp/verify:
|
||||
post:
|
||||
summary: Verify OTP for enabling two-factor authentication
|
||||
requestBody:
|
||||
required: true
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: "#/components/schemas/OTPVerifyRequest"
|
||||
responses:
|
||||
"200":
|
||||
description: OTP verified successfully
|
||||
/api/auth/otp/validate:
|
||||
post:
|
||||
summary: Validate OTP for two-factor authentication login
|
||||
requestBody:
|
||||
required: true
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: "#/components/schemas/OTPValidateRequest"
|
||||
responses:
|
||||
"200":
|
||||
description: OTP validated successfully
|
||||
/api/auth/otp/disable:
|
||||
post:
|
||||
summary: Disable OTP for two-factor authentication
|
||||
requestBody:
|
||||
required: true
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: "#/components/schemas/OTPDisableRequest"
|
||||
responses:
|
||||
"200":
|
||||
description: OTP disabled successfully
|
||||
/api/auth/password-reset/request:
|
||||
post:
|
||||
summary: Request a password reset
|
||||
requestBody:
|
||||
required: true
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: "#/components/schemas/PasswordResetRequest"
|
||||
responses:
|
||||
"200":
|
||||
description: Password reset requested successfully
|
||||
/api/auth/password-reset/confirm:
|
||||
post:
|
||||
summary: Confirm a password reset
|
||||
requestBody:
|
||||
required: true
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: "#/components/schemas/PasswordResetVerifyRequest"
|
||||
responses:
|
||||
"200":
|
||||
description: Password reset successfully
|
||||
|
||||
components:
|
||||
schemas:
|
||||
LoginRequest:
|
||||
type: object
|
||||
required:
|
||||
- email
|
||||
- password
|
||||
properties:
|
||||
email:
|
||||
type: string
|
||||
password:
|
||||
type: string
|
||||
LoginResponse:
|
||||
type: object
|
||||
properties:
|
||||
token:
|
||||
type: string
|
||||
RegisterRequest:
|
||||
type: object
|
||||
required:
|
||||
- firstName
|
||||
- lastName
|
||||
- email
|
||||
- password
|
||||
properties:
|
||||
firstName:
|
||||
type: string
|
||||
lastName:
|
||||
type: string
|
||||
email:
|
||||
type: string
|
||||
password:
|
||||
type: string
|
||||
VerifyEmailRequest:
|
||||
type: object
|
||||
required:
|
||||
- email
|
||||
- token
|
||||
properties:
|
||||
email:
|
||||
type: string
|
||||
token:
|
||||
type: string
|
||||
OTPGenerateResponse:
|
||||
type: object
|
||||
properties:
|
||||
OTP:
|
||||
type: string
|
||||
OTPVerifyRequest:
|
||||
type: object
|
||||
required:
|
||||
- OTP
|
||||
properties:
|
||||
OTP:
|
||||
type: string
|
||||
OTPValidateRequest:
|
||||
type: object
|
||||
required:
|
||||
- OTP
|
||||
properties:
|
||||
OTP:
|
||||
type: string
|
||||
OTPDisableRequest:
|
||||
type: object
|
||||
required:
|
||||
- password
|
||||
properties:
|
||||
password:
|
||||
type: string
|
||||
PasswordResetRequest:
|
||||
type: object
|
||||
required:
|
||||
- email
|
||||
properties:
|
||||
email:
|
||||
type: string
|
||||
PasswordResetVerifyRequest:
|
||||
type: object
|
||||
required:
|
||||
- email
|
||||
- token
|
||||
- password
|
||||
properties:
|
||||
email:
|
||||
type: string
|
||||
token:
|
||||
type: string
|
||||
password:
|
||||
type: string
|
||||
schemas:
|
||||
LoginRequest:
|
||||
type: object
|
||||
required:
|
||||
- email
|
||||
- password
|
||||
properties:
|
||||
email:
|
||||
type: string
|
||||
password:
|
||||
type: string
|
||||
LoginResponse:
|
||||
type: object
|
||||
properties:
|
||||
token:
|
||||
type: string
|
||||
RegisterRequest:
|
||||
type: object
|
||||
required:
|
||||
- firstName
|
||||
- lastName
|
||||
- email
|
||||
- password
|
||||
properties:
|
||||
firstName:
|
||||
type: string
|
||||
lastName:
|
||||
type: string
|
||||
email:
|
||||
type: string
|
||||
password:
|
||||
type: string
|
||||
VerifyEmailRequest:
|
||||
type: object
|
||||
required:
|
||||
- email
|
||||
- token
|
||||
properties:
|
||||
email:
|
||||
type: string
|
||||
token:
|
||||
type: string
|
||||
OTPGenerateResponse:
|
||||
type: object
|
||||
properties:
|
||||
OTP:
|
||||
type: string
|
||||
OTPVerifyRequest:
|
||||
type: object
|
||||
required:
|
||||
- OTP
|
||||
properties:
|
||||
OTP:
|
||||
type: string
|
||||
OTPValidateRequest:
|
||||
type: object
|
||||
required:
|
||||
- OTP
|
||||
properties:
|
||||
OTP:
|
||||
type: string
|
||||
OTPDisableRequest:
|
||||
type: object
|
||||
required:
|
||||
- password
|
||||
properties:
|
||||
password:
|
||||
type: string
|
||||
PasswordResetRequest:
|
||||
type: object
|
||||
required:
|
||||
- email
|
||||
properties:
|
||||
email:
|
||||
type: string
|
||||
PasswordResetVerifyRequest:
|
||||
type: object
|
||||
required:
|
||||
- email
|
||||
- token
|
||||
- password
|
||||
properties:
|
||||
email:
|
||||
type: string
|
||||
token:
|
||||
type: string
|
||||
password:
|
||||
type: string
|
||||
|
|
28
src/sdk.ts
28
src/sdk.ts
|
@ -1,21 +1,21 @@
|
|||
import AccountApi from './account.js';
|
||||
import AccountApi from "./account.js";
|
||||
|
||||
export class Sdk {
|
||||
private apiUrl: string;
|
||||
private accountApi?: AccountApi;
|
||||
private apiUrl: string;
|
||||
private accountApi?: AccountApi;
|
||||
|
||||
constructor(apiUrl: string) {
|
||||
this.apiUrl = apiUrl;
|
||||
}
|
||||
constructor(apiUrl: string) {
|
||||
this.apiUrl = apiUrl;
|
||||
}
|
||||
|
||||
public static create(apiUrl: string): Sdk {
|
||||
return new Sdk(apiUrl);
|
||||
}
|
||||
public static create(apiUrl: string): Sdk {
|
||||
return new Sdk(apiUrl);
|
||||
}
|
||||
|
||||
public account(): AccountApi {
|
||||
if (!this.accountApi) {
|
||||
this.accountApi = AccountApi.create(this.apiUrl);
|
||||
}
|
||||
return this.accountApi;
|
||||
public account(): AccountApi {
|
||||
if (!this.accountApi) {
|
||||
this.accountApi = AccountApi.create(this.apiUrl);
|
||||
}
|
||||
return this.accountApi;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue