Skip to content

Commit

Permalink
feat: added pda v3 class,tests,mocks,stubs
Browse files Browse the repository at this point in the history
  • Loading branch information
Siddharth9890 committed May 2, 2024
1 parent ba421fe commit a0e4ced
Show file tree
Hide file tree
Showing 21 changed files with 442 additions and 29 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ dist/
*.tgz
coverage/
.DS_STORE
gatewaySdk/
gatewaySdk/
src/testing.ts
2 changes: 1 addition & 1 deletion __mocks__/auth.mock.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Auth } from '../src/auth/auth';
import { authStub } from '../test/stubs/auth.stub';
import { pdaStub } from '../test/stubs/pda.stub';
import { pdaStub } from '../test/stubs/pda/pda.stub';

export const AuthMockService = (auth: Auth) => ({
checkUsernameAvailabilityMock: jest
Expand Down
4 changes: 2 additions & 2 deletions __mocks__/pda.mock.ts → __mocks__/pda/pda.mock.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { PDA } from '../src/pda/pda';
import { pdaStub } from '../test/stubs/pda.stub';
import { PDA } from '../../src/pda/pda';
import { pdaStub } from '../../test/stubs/pda/pda.stub';

export const PDAMockService = (pda: PDA) => ({
createPDAMock: jest.spyOn(pda.sdk, 'createPDA_mutation').mockResolvedValue({
Expand Down
31 changes: 31 additions & 0 deletions __mocks__/pda/pdav3.mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { PDA } from '../../src/pda/pdav3';
import { pdaStub } from '../../test/stubs/pda/pdav3.stub';

export const PDAMockService = (pda: PDA) => ({
// createPDAMock: jest.spyOn(pda.sdk, 'createPDA_mutation').mockResolvedValue({
// createPDA: pdaStub(),
// }),
// changePDAStatusMock: jest
// .spyOn(pda.sdk, 'changePDAStatus_mutation')
// .mockResolvedValue({
// changePDAStatus: pdaStub({ status: 'Suspended' }),
// }),
// pdaCountMock: jest
// .spyOn(pda.sdk, 'PDACount_query')
// .mockResolvedValue({ PDACount: 10 }),
getPDAMock: jest.spyOn(pda.sdk, 'PDA_query').mockResolvedValue({
PDA: pdaStub(),
}),
// pdasMock: jest
// .spyOn(pda.sdk, 'PDAs_query')
// .mockResolvedValue({ PDAs: [pdaStub()] }),
// issuedCountPDAMock: jest
// .spyOn(pda.sdk, 'issuedPDAsCount_query')
// .mockResolvedValue({ issuedPDAsCount: 10 }),
// issuedPDAMock: jest
// .spyOn(pda.sdk, 'issuedPDAs_query')
// .mockResolvedValue({ issuedPDAs: [pdaStub()] }),
// updatePDAMock: jest
// .spyOn(pda.sdk, 'updatePDA_mutation')
// .mockResolvedValue({ updatePDA: pdaStub() }),
});
12 changes: 6 additions & 6 deletions src/Gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ import { Request } from './request/request';
import { DataModel } from './data-model/data-model';
import { User } from './user/user';
import { Transaction } from './transaction/transaction';
import { checkVersion, clientTimingWrapper, urlsChecker } from './utils/helper';
import {
checkVersion,
clientTimingWrapper,
parameterChecker,
} from './utils/helper';

export {
AuthType,
Expand Down Expand Up @@ -43,11 +47,7 @@ export class Gateway {
url: string;
logging?: boolean;
}) {
if (!apiKey) throw new Error('No apikey found!');
if (!token) throw new Error('No token found!');
if (!url) throw new Error('No url found!.Enter either testnet or prod');

urlsChecker(url);
parameterChecker(apiKey, token, url);

checkVersion();

Expand Down
33 changes: 33 additions & 0 deletions src/GatewayV3.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { GraphQLClient } from 'graphql-request';
import { Sdk, getSdk } from '../gatewaySdk/sources/GatewayV3';
import {
checkVersion,
clientTimingWrapper,
parameterChecker,
} from './utils/helper';

export class GatewayV3 {
private sdk: Sdk;

constructor({
apiKey,
token,
url,
logging = false,
}: {
apiKey: string;
token: string;
url: string;
logging?: boolean;
}) {
parameterChecker(apiKey, token, url);

checkVersion();

const client = new GraphQLClient(url, {
headers: { Authorization: `Bearer ${token}`, 'X-Api-Key': apiKey },
});

this.sdk = getSdk(client, logging ? clientTimingWrapper : undefined);
}
}
144 changes: 144 additions & 0 deletions src/pda/pdav3.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
import {
CreatePDAInput,
FilterPDAInput,
issuedPDAs_queryQueryVariables,
PDACount_queryQueryVariables,
PDAs_queryQueryVariables,
PDAStatus,
Sdk,
UpdatePDAInput,
} from '../../gatewaySdk/sources/GatewayV3';
import { errorHandler } from '../utils/errorHandler';
import { isUUIDValid, validateObjectProperties } from '../utils/validators';

export class PDA {
public sdk: Sdk;

constructor(sdk: Sdk) {
this.sdk = sdk;
}

/**
* The function `getPDA` is an asynchronous function that takes an `id` parameter and returns a
* Promise that resolves to a `PDA_queryQuery` object.
* @param {string} id - A string representing the ID of the PDA that you
* want to query.
* @returns The function `getPda` is returning a Promise that resolves to a `PDA_queryQuery` object.
*/
async getPDA(id: string) {
try {
isUUIDValid(id);
return await this.sdk.PDA_query({ id });
} catch (error) {
throw new Error(errorHandler(error));
}
}

/**
* The function `getPDACount` is an asynchronous function that retrieves the count of PDAs
* based on an optional filter.
* @param {FilterPDAInput} [filter] - The `filter` parameter is an optional input that allows you to
* specify criteria for filtering the PDAs before counting them. It is
* of type `FilterPDAInput`.
* @returns a Promise that resolves to a number.
*/
async getPDACount(filter?: PDACount_queryQueryVariables) {
try {
return (await this.sdk.PDACount_query(filter)).PDACount;
} catch (error) {
throw new Error(errorHandler(error));
}
}

/**
* The function `getPDAs` retrieves PDAs based on the provided filter, order, skip, and take
* parameters.
* @param {PDAs_queryQueryVariables} - - `filter`: An object that contains filter criteria for the query.
* @returns a Promise that resolves to a value of type PDAs_queryQuery.
*/
async getPDAs(variables?: PDAs_queryQueryVariables) {
try {
return await this.sdk.PDAs_query(variables);
} catch (error) {
throw new Error(errorHandler(error));
}
}

/**
* The function `getIssuedPDAs` retrieves issued PDAs based on the provided filter, order, skip, and
* take parameters.
* @param {issuedPDAs_queryQueryVariables} - - `filter`: An object that contains filter criteria for the query. It is
* used to specify conditions that the returned PDAs must meet.
* @returns a Promise that resolves to an object of type `issuedPDAs_queryQuery`.
*/
async getIssuedPDAs(variables?: issuedPDAs_queryQueryVariables) {
try {
return await this.sdk.issuedPDAs_query(variables);
} catch (error) {
throw new Error(errorHandler(error));
}
}

/**
* The function `getIssuedPDAsCount` is an asynchronous function that retrieves the count of issued
* PDAs based on an optional filter.
* @param {FilterPDAInput} [filter] - The `filter` parameter is an optional input that allows you to
* specify criteria for filtering the issued PDAs. It is of type `FilterPDAInput`.
* @returns a Promise that resolves to a number.
*/
async getIssuedPDAsCount(filter?: FilterPDAInput) {
try {
return (await this.sdk.issuedPDAsCount_query({ filter })).issuedPDAsCount;
} catch (error) {
throw new Error(errorHandler(error));
}
}

/**
* The function `changePDAStatus` is an asynchronous function that takes an `id` and a `status` as
* parameters and returns a Promise that resolves to a `changePDAStatus_mutationMutation` object.
* @param - - `id`: The ID of the PDA whose status needs to be changed.
* @returns a Promise that resolves to a `changePDAStatus_mutationMutation` object.
*/
async changePDAStatus({ id, status }: { id: string; status: PDAStatus }) {
try {
isUUIDValid(id);
return await this.sdk.changePDAStatus_mutation({ input: { id, status } });
} catch (error) {
throw new Error(errorHandler(error));
}
}

/**
* The function creates a PDA using the provided input and returns the result.
* @param {CreatePDAInput} pdaInput - The `pdaInput` parameter is an object that contains the input
* data for creating a PDA . It is of type `CreatePDAInput`.
* @returns the result of the `createPDA_mutation` method call, which is a Promise.
*/
async createPDA(pdaInput: CreatePDAInput) {
try {
validateObjectProperties(pdaInput);
return await this.sdk.createPDA_mutation({ input: pdaInput });
} catch (error) {
throw new Error(errorHandler(error));
}
}

/**
* The function `updatePDA` updates a PDA using the provided input and returns
* the result of the mutation.
* @param {UpdatePDAInput} updatedPDA - The parameter `updatedPDA` is of type `UpdatePDAInput`. It is
* an input object that contains the data to update a PDA. The specific
* properties and their types within `UpdatePDAInput` would depend on the implementation of the
* `updatePDA_m
* @returns a Promise that resolves to an object of type `updatePDA_mutationMutation`.
*/
async updatePDA(updatedPDA: UpdatePDAInput) {
try {
validateObjectProperties(updatedPDA);
return await this.sdk.updatePDA_mutation({ input: updatedPDA });
} catch (error) {
throw new Error(errorHandler(error));
}
}
}
3 changes: 2 additions & 1 deletion src/testing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ import { Gateway } from './Gateway';
const testMutationsAndQueries = async () => {
const gatewayInstance = new Gateway({
apiKey: 'm9Y5ntNcTlwQ2LbRpYr6K_VhxJXuZJ6Q',
token: '',
token:
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJwcm90b2NvbF9pZCI6IjNmOGUxZDUwLWQ2OWYtNGFkNS1iYjI2LWNiZDI3YmQ1M2UxOCIsInByb3RvY29sX2hhc2giOiI0MDIyMDU4MThjYWMxMDljZWJkNzE4ZDZlZTE5YzcyMWViNWU2N2JlMmNkODg2NTljMzZiMjYwMzMwNzhlNzI3ZjhmZjcwYzNkOTU5YjI3ZWNkZWRmNTA2NzI3NjZlODciLCJwcm90b2NvbF93YWxsZXQiOiI2NjE3NmYyMTQ1MzMwMjQyODZhMDdjNjIiLCJwcm90b2NvbF9zaWduZXIiOiJkOWM4NWJmMi1iZTMyLTRmZDQtYjU4MS1hZWM3YmIyYmU2ZmEiLCJhdXRoIjp7ImlkIjoiMzkyMWU5ZWMtNDRmZS00NDhmLWFjNWMtNjdjZWQxNGFjMjI0IiwidHlwZSI6IldBTExFVCJ9LCJjcnlwdG8iOnsicHVibGljUGVtIjoiLS0tLS1CRUdJTiBQVUJMSUMgS0VZLS0tLS1cclxuTUlJQ0lqQU5CZ2txaGtpRzl3MEJBUUVGQUFPQ0FnOEFNSUlDQ2dLQ0FnRUFwTWErMXZmblhCa2owNmdiVUk3clxyXG5IeW1kS3FiSEJwa0RQbUx4SHlHL0VWWEx4N3dKaVA2OHpyOGJlcjloMGdWMWQ4ZGVIdlpBRVJGWXZXbTdIakVrXHJcblNpdE9RdEI0Vm1RajBrTjFWa2VldGN1VU90ZURFVEFlRjhZa3F2cVpLS2hQN1RZV3BCQkQyRVpybHIyWUhINlVcclxuY2FZbmhuRGtnWkZhQnZ3bFRUOTNOTkJmVGwrSlJIb25OaFNhNGdneFdUT296Nmh4aVA1K25GMGJpTHpRa1hNU1xyXG5PMmNHd1NIdCtFVlFMNDViOEd4MDE2QzY4MGJkSEdXcEE0bGQrVmtEZkVmNWRSMDZuK1ZaWWxBZFcrZURrWURyXHJcbmFWMjdIZWxYSXhiWFRidW90aXlsbU5SYzJZU24rMWpaWFBreXkyTDk3Q3hjQTlsRjBvS255WFFKSEpOS0NtcFRcclxubDVYOHovdjV0eGYzM3NOMHRoZ1d4QjU4aFJTcUFtQnExbnRJZVkvNUpna24zelhLREFQM3FnNGtFdXRDNWRDTVxyXG5QZ1J6MTBVMmtJVHRVbHdxTXpLbUszaG5mbHF2MVljaTZyb010a1ZqVGM3M2FOTWswYUxwVTVhejBGd3d2R3F4XHJcbmF1S1lqcXFlTG5sdGduVTZiL0pvd2Yvckx3WitEQ0FMTmRGYzBmaFlJUG9Lc3dHOGdwMHVwNG5OV2diMUMrZ2JcclxuWEQ2T1piTnFacDFlMUhWdGVSU21nT3BWQ2huTm4rVW5rK0Rta3c0cVNoamVnQkpnaEM0d2YrT25IU1Jqek9iK1xyXG5pQ25ObnhFK21xdDlDcmw2NjIxbmFzSmpTWTZaazVLeVBQRk9YaUJkVkR6c3Jobkw4Ym5WbXRCWGtpcFZXMDZvXHJcblQ2M1pyVjBtajJKWEZxNTh1dFhlemtVQ0F3RUFBUT09XHJcbi0tLS0tRU5EIFBVQkxJQyBLRVktLS0tLVxyXG4ifSwidHlwZSI6ImF1dGhfdG9rZW4iLCJpYXQiOjE3MTQwNTQ4MDV9.Lce0NCBvjuLD3uYy1O7tkP0cuSvNzFdWdTccIVP6IFQ',
url: 'https://develop.protocol.mygateway.xyz/graphql',
logging: true,
});
Expand Down
12 changes: 10 additions & 2 deletions src/utils/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,15 @@ export const checkVersion = async () => {
}
};

export const urlsChecker = (url: string): boolean => {
export const parameterChecker = (
apiKey: string,
token: string,
url: string,
): boolean => {
if (!apiKey) throw new Error('No apikey found!');
if (!token) throw new Error('No token found!');
if (!url)
throw new Error('No url found!.Use either sandbox or production url');
const urls = [
'https://protocol.mygateway.xyz/graphql',
'https://sandbox.protocol.mygateway.xyz/graphql',
Expand All @@ -51,5 +59,5 @@ export const urlsChecker = (url: string): boolean => {
];

if (urls.includes(url)) return true;
else throw new Error('No valid url found!');
else throw new Error('No valid url found!. Use sandbox or production url');
};
2 changes: 1 addition & 1 deletion test/auth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { AuthMockService } from '../__mocks__/auth.mock';
import { authStub } from './stubs/auth.stub';
import { AuthType } from '../src/types';
import { GraphQLClient } from 'graphql-request';
import { getSdk } from '../gatewaySdk';
import { getSdk } from '../gatewaySdk/sources/GatewayV2';

let auth: Auth;

Expand Down
2 changes: 1 addition & 1 deletion test/data-model.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getSdk } from '../gatewaySdk';
import { getSdk } from '../gatewaySdk/sources/GatewayV2';
import { DataModel } from '../src/data-model/data-model';
import {
dataModelCreateStub,
Expand Down
2 changes: 1 addition & 1 deletion test/dataRequestsTemplate.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { GraphQLClient } from 'graphql-request';
import { getSdk } from '../gatewaySdk';
import { getSdk } from '../gatewaySdk/sources/GatewayV2';
import { DataRequestTemplateMockService } from '../__mocks__/dataRequestTemplate.mock';
import { DataRequestTemplate } from '../src/dataRequestsTemplate/dataRequestsTemplate';
import {
Expand Down
2 changes: 1 addition & 1 deletion test/organization.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { GraphQLClient } from 'graphql-request';
import { getSdk } from '../gatewaySdk';
import { getSdk } from '../gatewaySdk/sources/GatewayV2';
import { OrganizationMockService } from '../__mocks__/organization.mock';
import { Organization } from '../src/organization/organization';
import {
Expand Down
11 changes: 6 additions & 5 deletions test/pda.test.ts → test/pda/pda.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { PDAStatus } from '../src/types';
import { PDA } from '../src/pda/pda';
import { getSdk } from '../gatewaySdk';
import { pdaCreateStub, pdaStub } from './stubs/pda.stub';
import { PDAMockService } from '../__mocks__/pda.mock';
import { GraphQLClient } from 'graphql-request';

import { PDAStatus } from '../../src/types';
import { PDA } from '../../src/pda/pda';
import { getSdk } from '../../gatewaySdk/sources/GatewayV2';
import { pdaCreateStub, pdaStub } from '../stubs/pda/pda.stub';
import { PDAMockService } from '../../__mocks__/pda/pda.mock';

let pda: PDA;

beforeAll(() => {
Expand Down
Loading

0 comments on commit a0e4ced

Please sign in to comment.