Skip to content

Commit

Permalink
Merge pull request #15 from Gateway-DAO/feat/default-handlers
Browse files Browse the repository at this point in the history
feat: added default values for all getters very imp!
  • Loading branch information
Siddharth9890 authored Jan 9, 2024
2 parents cc6dc10 + c6bf58d commit 5cc5105
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 40 deletions.
15 changes: 9 additions & 6 deletions src/data-model/data-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
dataModels_queryQueryVariables,
FilterDataModelInput,
} from '../../.mesh';
import { DEFAULT_TAKE_LIMIT } from '../utils/constants';
import { errorHandler } from '../utils/errorHandler';
import { isUUIDValid, validateObjectProperties } from '../utils/validators';

Expand Down Expand Up @@ -61,12 +62,14 @@ export class DataModel {
* call.
*/

async getDataModels({
filter,
order,
skip,
take,
}: dataModels_queryQueryVariables = {}) {
async getDataModels(
{ filter, order, skip, take }: dataModels_queryQueryVariables = {
filter: {},
order: {},
skip: 0,
take: DEFAULT_TAKE_LIMIT,
},
) {
try {
const data = await this.sdk.dataModels_query({
filter,
Expand Down
28 changes: 16 additions & 12 deletions src/dataRequestsTemplate/dataRequestsTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,19 @@ export class DataRequestTemplate {
* @returns the result of the `dataRequestTemplates_query` method call, which is a Promise
* that resolves to the data request templates.
*/
async getDataRequestTemplates({
filter,
order,
skip,
take,
}: {
filter?: FilterDataRequestTemplateInput;
order?: JSON;
skip?: number;
take?: number;
} = {}) {
async getDataRequestTemplates(
{
filter,
order,
skip,
take,
}: {
filter?: FilterDataRequestTemplateInput;
order?: JSON;
skip?: number;
take?: number;
} = { filter: {}, order: {} as JSON, skip: 0, take: 10 },
) {
try {
return await this.sdk.dataRequestTemplates_query({
filter,
Expand All @@ -87,7 +89,9 @@ export class DataRequestTemplate {
* `FilterDataRequestTemplateInput`.
* @returns the count of data request templates.
*/
async getDataRequestsTemplateCount(filter?: FilterDataRequestTemplateInput) {
async getDataRequestsTemplateCount(
filter: FilterDataRequestTemplateInput = {},
) {
try {
return (await this.sdk.dataRequestTemplatesCount_query({ filter }))
.dataRequestTemplatesCount;
Expand Down
21 changes: 12 additions & 9 deletions src/organization/organization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
UpdateOrganizationInput,
} from '../../.mesh';
import { OrganizationIdentifierType } from '../types';
import { DEFAULT_TAKE_LIMIT } from '../utils/constants';
import { errorHandler } from '../utils/errorHandler';
import { isStringValid, validateObjectProperties } from '../utils/validators';

Expand Down Expand Up @@ -135,15 +136,17 @@ export class Organization {
* to filter the organizations based on certain criteria.
* @returns the result of the `organizations_query` method call from the `sdk` object.
*/
async getOrganizations({
filter,
skip,
take,
}: {
filter?: FilterOrganizationInput;
skip?: number;
take?: number;
} = {}) {
async getOrganizations(
{
filter,
skip,
take,
}: {
filter?: FilterOrganizationInput;
skip?: number;
take?: number;
} = { filter: {}, skip: 0, take: DEFAULT_TAKE_LIMIT },
) {
try {
return await this.sdk.organizations_query({ filter, skip, take });
} catch (error) {
Expand Down
26 changes: 21 additions & 5 deletions src/pda/pda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
UpdatePDAInput,
} from '../../.mesh';
import { PDAFilter, PDAStatus } from '../types';
import { DEFAULT_TAKE_LIMIT } from '../utils/constants';
import { errorHandler } from '../utils/errorHandler';
import { isUUIDValid, validateObjectProperties } from '../utils/validators';

Expand Down Expand Up @@ -39,7 +40,7 @@ export class PDA {
* of type `FilterPDAInput`.
* @returns a Promise that resolves to a number.
*/
async getPDACount(filter?: FilterPDAInput) {
async getPDACount(filter: FilterPDAInput = {}) {
try {
return (await this.sdk.PDACount_query({ filter })).PDACount;
} catch (error) {
Expand All @@ -53,10 +54,18 @@ export class PDA {
* @param {PDAFilter} - - `filter`: An object that contains filter criteria for the query.
* @returns a Promise that resolves to a value of type PDAs_queryQuery.
*/
async getPDAs({ filter, order, skip, take }: PDAFilter = {}) {
async getPDAs(
{ filter, order, skip, take }: PDAFilter = {
filter: {},
order: {} as JSON,
skip: 0,
take: DEFAULT_TAKE_LIMIT,
},
) {
try {
return await this.sdk.PDAs_query({ filter, order, skip, take });
} catch (error) {
console.log(error);
throw new Error(errorHandler(error));
}
}
Expand All @@ -68,7 +77,14 @@ export class PDA {
* used to specify conditions that the returned PDAs must meet.
* @returns a Promise that resolves to an object of type `issuedPDAs_queryQuery`.
*/
async getIssuedPDAs({ filter, order, skip, take }: PDAFilter = {}) {
async getIssuedPDAs(
{ filter, order, skip, take }: PDAFilter = {
filter: {},
order: {} as JSON,
skip: 0,
take: DEFAULT_TAKE_LIMIT,
},
) {
try {
return await this.sdk.issuedPDAs_query({ filter, order, skip, take });
} catch (error) {
Expand All @@ -83,7 +99,7 @@ export class PDA {
* specify criteria for filtering the issued PDAs. It is of type `FilterPDAInput`.
* @returns a Promise that resolves to a number.
*/
async getIssuedPDAsCount(filter?: FilterPDAInput) {
async getIssuedPDAsCount(filter: FilterPDAInput = {}) {
try {
return (await this.sdk.issuedPDAsCount_query({ filter })).issuedPDAsCount;
} catch (error) {
Expand All @@ -98,7 +114,7 @@ export class PDA {
* filter the results of the query. It is of type `FilterPDAInput`.
* @returns a Promise that resolves to a number.
*/
async myPDACount(filter?: FilterPDAInput) {
async myPDACount(filter: FilterPDAInput = {}) {
try {
return (await this.sdk.myPDACount_query({ filter })).myPDACount;
} catch (error) {
Expand Down
18 changes: 14 additions & 4 deletions src/user/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
UpdateUserInput,
} from '../../.mesh';
import { PDAFilter, UserIdentifierType } from '../types';
import { DEFAULT_TAKE_LIMIT } from '../utils/constants';
import { errorHandler } from '../utils/errorHandler';
import {
isEmailValid,
Expand Down Expand Up @@ -63,7 +64,7 @@ export class User {
* of type `FilterPDAInput`.
* @returns a Promise that resolves to a number.
*/
async myPDACount(filter?: FilterPDAInput) {
async myPDACount(filter: FilterPDAInput = {}) {
try {
return (await this.sdk.myPDACount_query({ filter })).myPDACount;
} catch (error) {
Expand All @@ -77,7 +78,14 @@ export class User {
* @param {PDAFilter} - - `filter`: An object that contains filter criteria for the query.
* @returns a Promise that resolves to a value of type `myPDAs_queryQuery`.
*/
async myPDAs({ filter, order, skip, take }: PDAFilter = {}) {
async myPDAs(
{ filter, order, skip, take }: PDAFilter = {
filter: {},
order: {} as JSON,
skip: 0,
take: DEFAULT_TAKE_LIMIT,
},
) {
try {
return await this.sdk.myPDAs_query({ filter, order, skip, take });
} catch (error) {
Expand All @@ -94,7 +102,7 @@ export class User {
* logical operators like AND and OR to combine multiple conditions.
* @returns the count of data models that match the provided filter.
*/
async myDataModelsCount(filter?: FilterDataModelInput) {
async myDataModelsCount(filter: FilterDataModelInput = {}) {
try {
return (await this.sdk.dataModelsCount_query({ filter })).dataModelsCount;
} catch (error) {
Expand All @@ -111,7 +119,9 @@ export class User {
* specific conditions such as template name, creator, or any other relevant attributes.
* @returns the count of myDataRequestTemplates that match the provided filter.
*/
async myDataRequestTemplatesCount(filter?: FilterDataRequestTemplateInput) {
async myDataRequestTemplatesCount(
filter: FilterDataRequestTemplateInput = {},
) {
try {
return (await this.sdk.myDataRequestTemplatesCount_query({ filter }))
.myDataRequestTemplatesCount;
Expand Down
1 change: 1 addition & 0 deletions src/utils/constants.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export const STRING_VALIDATION_LENGTH = 3;
export const TEST_DEFAULT_TIMEOUT = 10000;
export const DEFAULT_TAKE_LIMIT = 15;
16 changes: 12 additions & 4 deletions src/utils/errorHandler.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
export const errorHandler = (error: any): string => {
if (typeof error === 'object' && error !== null && 'message' in error) {
return error.message;
} else {
return 'Something went wrong!';
if (typeof error === 'object' && error !== null) {
if (
Array.isArray(error.errors) &&
error.errors.length > 0 &&
'message' in error.errors[0]
) {
return error.errors[0].message;
} else if ('message' in error) {
return error.message;
}
}

return 'Something went wrong!';
};

0 comments on commit 5cc5105

Please sign in to comment.