feat: initial version

This commit is contained in:
Derrick Hammer 2024-03-06 16:51:23 -05:00
parent 4088e152fa
commit 5de87a9b3f
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
12 changed files with 17080 additions and 1 deletions

11
.changeset/config.json Normal file
View File

@ -0,0 +1,11 @@
{
"$schema": "https://unpkg.com/@changesets/config@3.0.0/schema.json",
"changelog": "@changesets/cli/changelog",
"commit": false,
"fixed": [],
"linked": [],
"access": "public",
"baseBranch": "master",
"updateInternalDependencies": "patch",
"ignore": []
}

37
.gitignore vendored Normal file
View File

@ -0,0 +1,37 @@
# preset generated files
/.eslintrc.json
/.husky
/.jestrc.json
/.gitignore
/.lintstagedrc.json
/.npmignore
/.prettierrc.json
/tsconfig.json
/tsconfig.build.json
# dependency
node_modules
# output
/lib
/generated
# coverage
/coverage
*.lcov
# diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
# log
/log
/logs
*.log
*-debug.log*
.*-debug.log*
# cache
.DS_Store
.cache
.npm
.pnpm*
*.tsbuildinfo
# configuration
.env*
!.env*.example
src/account/generated

18
.presetterrc.json Normal file
View File

@ -0,0 +1,18 @@
{
"preset": [
"presetter-preset-esm"
],
"config": {
"prettier": {
"singleQuote": false
},
"tsconfig.build": {
"include": {
"0": "src"
},
"compilerOptions": {
"rootDir": "src"
}
}
}
}

View File

@ -1,6 +1,6 @@
MIT License
Copyright (c) 2024 LumeWeb
Copyright (c) 2024 Hammer Technologies LLC
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

16601
npm-shrinkwrap.json generated Normal file

File diff suppressed because it is too large Load Diff

18
orval.config.ts Normal file
View File

@ -0,0 +1,18 @@
import { defineConfig } from 'orval';
export default defineConfig({
account: {
input: './src/account/swagger.yaml',
output: {
mode: 'split',
workspace: "./src/account/generated",
target: 'openapi.ts',
override: {
mutator: {
path: '../axios.ts',
name: 'customInstance',
},
},
},
},
});

34
package.json Normal file
View File

@ -0,0 +1,34 @@
{
"name": "@lumeweb/portal-sdk",
"version": "0.0.0",
"description": "",
"main": "lib/index.js",
"files": [
"lib"
],
"scripts": {
"prepare": "presetter bootstrap",
"build": "run build",
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "gitea@git.lumeweb.com:LumeWeb/portal-sdk.git"
},
"private": false,
"devDependencies": {
"@changesets/cli": "^2.27.1",
"presetter": "^4.7.0",
"presetter-preset-esm": "^4.7.0"
},
"readme": "ERROR: No README data found!",
"_id": "@lumeweb/portal-sdk@0.0.0",
"dependencies": {
"axios": "^1.6.7",
"memize": "^2.1.0",
"orval": "^6.25.0"
},
"publishConfig": {
"access": "public"
}
}

103
src/account.ts Normal file
View File

@ -0,0 +1,103 @@
import {
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';
export default class AccountApi {
private apiUrl: string;
private jwtToken?: string;
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;
}
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}`,
},
};
}
}

25
src/account/axios.ts Normal file
View File

@ -0,0 +1,25 @@
import Axios, { AxiosRequestConfig, AxiosResponse } from "axios";
import memize from "memize";
const axiosCreate = memize(Axios.create);
export const customInstance = <T>(
config: AxiosRequestConfig,
options?: AxiosRequestConfig,
): Promise<AxiosResponse<T>> => {
const source = Axios.CancelToken.source();
const instance = axiosCreate({ baseURL: options?.baseURL });
const promise = instance({
...config,
...options,
cancelToken: source.token,
}).then(({ data }) => data);
// @ts-ignore
promise.cancel = () => {
source.cancel("Query was cancelled");
};
return promise;
};

210
src/account/swagger.yaml Normal file
View File

@ -0,0 +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.
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
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

1
src/index.ts Normal file
View File

@ -0,0 +1 @@
export * from "./sdk.js";

21
src/sdk.ts Normal file
View File

@ -0,0 +1,21 @@
import AccountApi from 'src/account.js';
export default class Sdk {
private apiUrl: string;
private accountApi?: AccountApi;
constructor(apiUrl: string) {
this.apiUrl = 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;
}
}