Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #71

Merged
merged 4 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 29 additions & 22 deletions src/modules/account/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,29 @@ import {
import { paths } from '../../api';
import { GTWError } from '../../helpers/custom-error';
import { CryptoService } from '../../services/crypto-service';
import { Wallet } from './wallet';

export class Account {
private client: OpenAPIClient<paths, MediaType>;
private cryptoService: CryptoService;
protected wallet: Wallet;

constructor(client: OpenAPIClient<paths, MediaType>) {
this.client = client;
this.cryptoService = new CryptoService();
this.wallet = new Wallet(client);
}

/**
* The `createAccount` function in TypeScript asynchronously creates a new account by verifying a
* message, sending a POST request to an endpoint, and returning a token upon success.
* @param {AccountCreateRequest} - The `createAccount` function takes in an `AccountCreateRequest`
* object with the following parameters:
* @returns The `createAccount` function returns a token from the `data` object after successfully
* verifying the message signature and creating an account with the provided username and wallet
* address.
* The `create` function in TypeScript asynchronously verifies a message, then sends a POST request to
* create an account and returns the generated token.
* @param {AccountCreateRequest} - The `create` method takes in an `AccountCreateRequest` object with
* the following parameters:
* @returns The `create` function is returning a token from the `data` object after successfully
* verifying the message signature and creating a new account with the provided information (message,
* signature, username, wallet address).
*/
async createAccount({
async create({
message,
signature,
username,
Expand All @@ -46,13 +49,13 @@ export class Account {
}

/**
* This async function retrieves account information by making a GET request to '/accounts/me' and
* handles errors by throwing a custom GTWError if any occur.
* @returns The `getAccountInfo` function is returning the `data` object fetched from the
* `/accounts/me` endpoint. If there is an error during the API call, a `GTWError` is thrown with the
* error and response details.
* This async function retrieves the account information for the currently authenticated user.
* @returns The `getMe` function is returning a Promise that resolves to a `MyAccountResponse` object.
* This object is obtained by making a GET request to the '/accounts/me' endpoint using
* `this.client.GET('/accounts/me')`. If there is an error during the request, a `GTWError` is thrown
* with the error and response details. Otherwise, the function returns the data obtained from
*/
async getAccountInfo(): Promise<MyAccountResponse> {
async getMe(): Promise<MyAccountResponse> {
const { data, response, error } = await this.client.GET('/accounts/me');

if (error) {
Expand All @@ -63,15 +66,19 @@ export class Account {
}

/**
* The function `updateAccount` asynchronously updates the profile picture and username of the current
* account using a PATCH request.
* @param - The `updateAccount` function takes in an object with two properties: `profile_picture` and
* `username`, both of type string. These values are used to update the user's account information by
* making a PATCH request to the `/accounts/me` endpoint with the provided data. If there is an error
* @returns The `updateAccount` function is returning the `data` object after making a PATCH request to
* update the account information (profile picture and username).
* This TypeScript function updates the profile picture and username of the current user's account
* using a PATCH request.
* @param {string} [profile_picture] - The `profile_picture` parameter in the `updateMe` function is
* used to update the profile picture of the current user's account. It is an optional parameter,
* meaning you can choose to provide a new profile picture URL or leave it empty to not update the
* profile picture.
* @param {string} [username] - The `username` parameter in the `updateMe` function is used to update
* the username of the account. If a new `username` value is provided when calling this function, it
* will be used to update the username associated with the account.
* @returns The `updateMe` function is returning the `data` object after making a PATCH request to
* update the user's profile picture and username.
*/
async updateAccount(profile_picture?: string, username?: string) {
async updateMe(profile_picture?: string, username?: string) {
const { data, error, response } = await this.client.PATCH('/accounts/me', {
body: { profile_picture, username },
});
Expand Down
67 changes: 67 additions & 0 deletions src/modules/account/wallet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { MediaType } from 'openapi-typescript-helpers';
import { OpenAPIClient, MyAccountResponse } from '../../common/types';
import { paths } from '../../api';
import { GTWError } from '../../helpers/custom-error';
import { ValidationService } from '../../services/validator-service';

export class Wallet {
private client: OpenAPIClient<paths, `${string}/${string}`>;
private validationService: ValidationService;

constructor(client: OpenAPIClient<paths, MediaType>) {
this.client = client;
this.validationService = new ValidationService();
}

/**
* The `add` function in TypeScript adds a new wallet address to the user's account asynchronously.
* @param {string} address - The `add` function in the code snippet you provided is an asynchronous
* function that takes a `string` parameter called `address`. The function first checks if the
* `address` string is empty using a validation service method `isEmptyString(address)`. Then, it makes
* a POST request to a specific endpoint
* @returns The `add` function is returning a `Promise` that resolves to a `MyAccountResponse` object.
*/

async add(address: string): Promise<MyAccountResponse> {
this.validationService.isEmptyString(address);
const { data, error, response } = await this.client.POST(
'/accounts/me/wallets',
{
body: { address: address },
},
);

if (error) {
throw new GTWError(error, response);
}

return data;
}

/**
* This TypeScript function asynchronously removes a wallet associated with a specific address from the
* user's account.
* @param {string} address - The `remove` function is an asynchronous function that takes a `string`
* parameter called `address`. This function sends a DELETE request to the endpoint
* `/accounts/me/wallets/{address}` with the provided `address` parameter as part of the path. It then
* returns a `Promise` that resolves
* @returns The `remove` function is returning a `Promise` that resolves to a `MyAccountResponse`
* object.
*/
async remove(address: string): Promise<MyAccountResponse> {
this.validationService.isEmptyString(address);

const { data, error, response } = await this.client.DELETE(
'/accounts/me/wallets/{address}',
{
params: { path: { address: address } },
},
);

if (error) {
throw new GTWError(error, response);

Check warning on line 62 in src/modules/account/wallet.ts

View check run for this annotation

Codecov / codecov/patch

src/modules/account/wallet.ts#L62

Added line #L62 was not covered by tests
}

return data;
}
}
101 changes: 41 additions & 60 deletions src/modules/data-model/data-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,20 @@ export class DataModel {
this.client = client;
this.validationService = validationService;
}

/**
* This TypeScript function asynchronously fetches data models from a server using GET request with
* optional pagination parameters.
* @param {number} [page=1] - The `page` parameter is used to specify the page number of the data
* models to retrieve. By default, it is set to 1, meaning that the function will retrieve the data
* models from the first page.
* @param {number} [page_size=10] - The `page_size` parameter in the `getDataModels` function
* specifies the number of data models to be retrieved per page. By default, it is set to 10, meaning
* that when the function is called without providing a specific `page_size` value, it will retrieve
* 10 data models per
* @returns The `getDataModels` function is returning the data fetched from the API endpoint
* `/data-models` with the specified pagination parameters `page` and `page_size`. If there is an
* error during the API request, a `GTWError` is thrown with the error and response details. If there
* is no error, the function returns the fetched data.
* This async function retrieves a paginated list of data models from a server using GET request.
* @param {number} [page=1] - The `page` parameter in the `getAll` function is used to specify the page
* number of the data to retrieve. By default, it is set to 1 if not provided when calling the
* function.
* @param {number} [page_size=10] - The `page_size` parameter in the `getAll` function specifies the
* number of items to be displayed per page when fetching data from the `/data-models` endpoint. By
* default, it is set to 10, meaning that the API will return a maximum of 10 data items per page
* unless
* @returns The `getAll` function is returning a `HelperPaginatedResponse` object containing data of
* type `DataModelType`.
*/
async getDataModels(page: number = 1, page_size: number = 10) {

async getAll(page: number = 1, page_size: number = 10) {
const { data, error, response } = await this.client.GET('/data-models', {
params: { query: { page, page_size } },
});
Expand All @@ -48,17 +45,16 @@ export class DataModel {
}

/**
* This TypeScript function creates a data model by sending a POST request to a specified endpoint.
* @param {DataModelRequest} dataModelInput - The `dataModelInput` parameter in the `createDataModel`
* function is of type `DataModelRequest`. It is the input data that will be used to create a new
* data model. This input likely contains information such as the name, fields, and other properties
* of the data model that will
* @returns The `createDataModel` function is returning the `data` object after making a POST request
* to create a data model.
* This TypeScript function creates a new data model by sending a POST request to a specified endpoint.
* @param {DataModelRequest} dataModelInput - The `dataModelInput` parameter in the `create` function
* is of type `DataModelRequest`. This parameter likely contains the data needed to create a new data
* model, such as the attributes and properties of the data model.
* @returns The `create` method is returning a Promise that resolves to a `DataModelType` object. The
* method makes a POST request to the '/data-models' endpoint with the `dataModelInput` as the request
* body. If there is an error during the POST request, a `GTWError` is thrown with the error and
* response details. Otherwise, the method returns the `data`
*/
async createDataModel(
dataModelInput: DataModelRequest,
): Promise<DataModelType> {
async create(dataModelInput: DataModelRequest): Promise<DataModelType> {
const { data, error, response } = await this.client.POST('/data-models', {
body: dataModelInput,
});
Expand All @@ -69,25 +65,14 @@ export class DataModel {
return data!;
}

/**
* This TypeScript function updates a data model using a PUT request with error handling.
* @param {number} dataModelId - The `dataModelId` parameter is the unique identifier of the data
* model that you want to update. It is a number that specifies which data model in the system you
* are targeting for the update operation.
* @param {DataModelRequest} dataModelInput - The `dataModelInput` parameter in the `updateDataModel`
* function is of type `DataModelRequest`. It is the data that will be used to update the data model
* with the specified `dataModelId`.
* @returns The `updateDataModel` function is returning the updated data model after making a PUT
* request to the server with the provided `dataModelInput` for the specified `dataModelId`.
*/
// async updateDataModel(
// async update(
// dataModelId: number,
// dataModelInput: DataModelRequest,
// ): Promise<DataModelType> {
// const { data, error, response } = await this.client.PUT(
// '/data-models/{id}',
// {
// body: {},
// body: { ...dataModelInput },
// params: { path: { id: dataModelId } },
// },
// );
Expand All @@ -100,17 +85,16 @@ export class DataModel {
// }

/**
* This TypeScript function asynchronously fetches a data model by its ID using a GET request.
* This function asynchronously retrieves a data model by its ID using a GET request.
* @param {number} dataModelId - The `dataModelId` parameter is a number that represents the unique
* identifier of a data model. This function `getDataModelById` is an asynchronous function that
* retrieves a data model by its ID using an HTTP GET request to a specific endpoint. It uses the
* `dataModelId` parameter to specify
* @returns The `getDataModelById` function is returning the data fetched from the API endpoint for
* the specified `dataModelId`. If there is an error during the API request, it will throw a
* `GTWError` with the error and response details. If there is no error, it will return the retrieved
* data.
* identifier of a data model. This identifier is used to retrieve a specific data model from the
* server.
* @returns The `getById` function is returning a Promise that resolves to a `DataModelType` object.
* The function makes an asynchronous GET request to retrieve a data model by its ID, and if
* successful, it returns the data model. If there is an error during the request, it throws a
* `GTWError` with the error and response details.
*/
async getDataModelById(dataModelId: number): Promise<DataModelType> {
async getById(dataModelId: number): Promise<DataModelType> {
const { data, error, response } = await this.client.GET(
'/data-models/{id}',
{
Expand All @@ -125,20 +109,17 @@ export class DataModel {
}

/**
* This TypeScript function retrieves data models specific to the current user with optional
* pagination parameters.
* @param {number} [page=1] - The `page` parameter in the `getMyDataModels` function is used to
* specify the page number of the data models to retrieve. By default, it is set to 1 if not
* provided.
* @param {number} [page_size=10] - The `page_size` parameter in the `getMyDataModels` function
* specifies the number of data models to be retrieved per page. By default, it is set to 10, meaning
* that the function will retrieve 10 data models per page unless specified otherwise.
* @returns The `getMyDataModels` function returns the data fetched from the API endpoint
* `/data-models/me` based on the provided `page` and `page_size` parameters. If there is an error
* during the API request, a `GTWError` is thrown with the error and response details. If the request
* is successful, the function returns the retrieved data.
* This TypeScript function asynchronously retrieves paginated data models specific to the current
* user.
* @param {number} [page=1] - The `page` parameter in the `getMy` function is used to specify the page
* number of the data to retrieve. By default, it is set to 1 if not provided.
* @param {number} [page_size=10] - The `page_size` parameter in the `getMy` function specifies the
* number of items to be displayed per page when fetching data from the endpoint `/data-models/me`. By
* default, if not provided, the `page_size` is set to 10. This means that the API will return
* @returns The `getMy` function returns a `HelperPaginatedResponse` object containing data of type
* `DataModelType`.
*/
async getMyDataModels(page: number = 1, page_size: number = 10) {
async getMy(page: number = 1, page_size: number = 10) {
const { data, response, error } = await this.client.GET('/data-models/me', {
params: { query: { page, page_size } },
});
Expand Down
19 changes: 19 additions & 0 deletions src/services/validator-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,25 @@ export class ValidationService {
return true;
}

/**
* The function checks if a string is empty or contains only whitespace.
* @param {string} value - The `value` parameter in the `isEmptyString` function is a string that you
* want to check for emptiness or containing only whitespace characters. The function will return
* `true` if the string is not empty or does not contain only whitespace characters, otherwise it will
* throw an error indicating that the string
* @returns The `isEmptyString` function is returning a boolean value, either `true` if the provided
* string is not empty or contains only whitespace, or it will throw an error if the string is empty or
* contains only whitespace.
*/
public isEmptyString(value: string): boolean {
if (value.trim().length === 0) {
throw new Error(
'The provided string is empty or contains only whitespace',
);
}
return true;
}

/**
* The function `validateString` checks if a given string meets a minimum length requirement and
* returns a boolean value accordingly.
Expand Down
12 changes: 6 additions & 6 deletions test/account.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe('Account', () => {
it('should return with new account with given credentials', async () => {
mockPost.mockResolvedValue(successMessage({ data: { token: 'test' } }));

const result = await account.createAccount(
const result = await account.create(
authDetails({ username: 'testuser' }),
);

Expand All @@ -42,7 +42,7 @@ describe('Account', () => {
mockPost.mockResolvedValue(errorMessage());

await expect(
account.createAccount(authDetails({ username: 'testuser' })),
account.create(authDetails({ username: 'testuser' })),
).rejects.toThrow(GTWError);
expect(mockPost).toHaveBeenCalledWith(routes.CreateAccount, {
body: authDetails({ username: 'testuser' }),
Expand All @@ -62,7 +62,7 @@ describe('Account', () => {
error: null,
});

const result = await account.getAccountInfo();
const result = await account.getMe();

expect(result).toEqual(mockData);
expect(mockClient.GET).toHaveBeenCalledWith(routes.GetMyAccount);
Expand All @@ -71,7 +71,7 @@ describe('Account', () => {
it('should throw GTWError when API call fails', async () => {
mockGet.mockResolvedValue(errorMessage());

await expect(account.getAccountInfo()).rejects.toThrow(GTWError);
await expect(account.getMe()).rejects.toThrow(GTWError);
expect(mockClient.GET).toHaveBeenCalledWith(routes.GetMyAccount);
});
});
Expand All @@ -95,7 +95,7 @@ describe('Account', () => {
error: null,
});

const result = await account.updateAccount(
const result = await account.updateMe(
'https://example.com/profile-picture.png',
);

Expand All @@ -117,7 +117,7 @@ describe('Account', () => {
error: { error: 'Unauthorized' },
});

await expect(account.updateAccount()).rejects.toThrow(GTWError);
await expect(account.updateMe()).rejects.toThrow(GTWError);
expect(mockClient.PATCH).toHaveBeenCalledWith(routes.UpdateAccount, {
body: {
profile_picture: 'https://example.com/profile-picture.png',
Expand Down
Loading
Loading