diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index a0e0000..0ad33f0 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -1,4 +1,4 @@ -name: Build PR +name: Run Eslint & Test cases on: pull_request: @@ -9,7 +9,7 @@ jobs: runs-on: ubuntu-latest steps: - name: Checkout Project - uses: actions/checkout@v2 + uses: actions/checkout@v3 - name: Install pnpm run: npm install -g pnpm@7.33.5 - name: Install Project Dependencies @@ -20,3 +20,7 @@ jobs: run: pnpm mesh build - name: Test project run: pnpm test + env: + DATAMODEL_ID: ${{ secrets.DATAMODEL_ID }} + BEARER_TOKEN: ${{ secrets.BEARER_TOKEN }} + API_KEY: ${{ secrets.API_KEY }} diff --git a/.gitignore b/.gitignore index 2549b93..26d4624 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ node_modules/ -.mesh/ \ No newline at end of file +.mesh/ +.vscode/ +.env \ No newline at end of file diff --git a/.meshrc.yml b/.meshrc.yml index cf60044..a332591 100644 --- a/.meshrc.yml +++ b/.meshrc.yml @@ -1,10 +1,14 @@ sources: - - name: MyGraphQLApi + - name: GatewaySDK handler: graphql: endpoint: https://develop.protocol.mygateway.xyz/v1/graphql - method: 'POST' - useGETForQueries: false + transforms: + - filterSchema: + mode: bare + filters: + - Query.!transaction + - Query.!transactions customFetch: ./custom-fetch.ts diff --git a/custom-fetch-backup.ts b/custom-fetch-backup.ts deleted file mode 100644 index 8112ccd..0000000 --- a/custom-fetch-backup.ts +++ /dev/null @@ -1,16 +0,0 @@ -import { fetch } from '@whatwg-node/fetch'; -import { MeshContext } from '@graphql-mesh/runtime'; - -type ContextModified = MeshContext & { - token: string; - apiKey: string; -}; - -export default function patchedFetch( - url: string, - init: RequestInit, - context: ContextModified, -) { - context.apiKey; - return fetch(url, init); -} diff --git a/package.json b/package.json index 0cae277..94cdbb2 100644 --- a/package.json +++ b/package.json @@ -17,6 +17,7 @@ "@graphql-mesh/cli": "^0.87.14", "@graphql-mesh/graphql": "^0.95.7", "@graphql-mesh/runtime": "^0.96.11", + "@graphql-mesh/transform-filter-schema": "^0.96.0", "@types/node": "^20.8.4", "@whatwg-node/fetch": "^0.9.13", "graphql": "^16.8.1" @@ -24,6 +25,7 @@ "devDependencies": { "@types/jest": "^29.5.7", "@typescript-eslint/eslint-plugin": "^6.4.0", + "dotenv": "^16.3.1", "eslint": "^8.53.0", "eslint-config-prettier": "^9.0.0", "eslint-config-standard-with-typescript": "^39.1.1", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 470cd6b..dbcf463 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -4,10 +4,12 @@ specifiers: '@graphql-mesh/cli': ^0.87.14 '@graphql-mesh/graphql': ^0.95.7 '@graphql-mesh/runtime': ^0.96.11 + '@graphql-mesh/transform-filter-schema': ^0.96.0 '@types/jest': ^29.5.7 '@types/node': ^20.8.4 '@typescript-eslint/eslint-plugin': ^6.4.0 '@whatwg-node/fetch': ^0.9.13 + dotenv: ^16.3.1 eslint: ^8.53.0 eslint-config-prettier: ^9.0.0 eslint-config-standard-with-typescript: ^39.1.1 @@ -26,6 +28,7 @@ dependencies: '@graphql-mesh/cli': 0.87.16_vwgveer3jlgyiirf436qk2ic7e '@graphql-mesh/graphql': 0.95.8_vwgveer3jlgyiirf436qk2ic7e '@graphql-mesh/runtime': 0.96.13_graphql@16.8.1 + '@graphql-mesh/transform-filter-schema': 0.96.0_graphql@16.8.1 '@types/node': 20.8.10 '@whatwg-node/fetch': 0.9.14 graphql: 16.8.1 @@ -33,6 +36,7 @@ dependencies: devDependencies: '@types/jest': 29.5.7 '@typescript-eslint/eslint-plugin': 6.9.1_5iy6f2d3forlnf27wdbuobyvmm + dotenv: 16.3.1 eslint: 8.53.0 eslint-config-prettier: 9.0.0_eslint@8.53.0 eslint-config-standard-with-typescript: 39.1.1_yemh74rzfn7vdu6a2wimsryixq @@ -1407,7 +1411,7 @@ packages: tsconfig-paths: 4.2.0 tslib: 2.6.2 typescript: 5.2.2 - uWebSockets.js: github.com/uNetworking/uWebSockets.js/c10b47c350cc97c299c6b4a07a98abb628704c71 + uWebSockets.js: github.com/uNetworking/uWebSockets.js/fca27c05066342b01951782248640c8013b37196 yargs: 17.7.2 transitivePeerDependencies: - '@swc/core' @@ -1696,6 +1700,25 @@ packages: tslib: 2.6.2 dev: false + /@graphql-mesh/transform-filter-schema/0.96.0_graphql@16.8.1: + resolution: + { + integrity: sha512-nQUZc58/vF3nptINp7Y5yEcJlEbEmZXIeY1IM7u8620cpeZvmo7Keugemmj5dSmskgRBpdb/+pJc2ETxZCnHVQ==, + } + engines: { node: '>=16.0.0' } + peerDependencies: + '@graphql-mesh/types': ^0.96.0 + '@graphql-mesh/utils': ^0.96.0 + '@graphql-tools/utils': ^9.2.1 || ^10.0.0 + graphql: '*' + tslib: ^2.4.0 + dependencies: + '@graphql-tools/delegate': 10.0.3_graphql@16.8.1 + '@graphql-tools/wrap': 10.0.1_graphql@16.8.1 + graphql: 16.8.1 + minimatch: 9.0.3 + dev: false + /@graphql-mesh/types/0.95.8_wnznsj3m6w55sxihavtcyzsb5q: resolution: { @@ -4354,7 +4377,6 @@ packages: integrity: sha512-IPzF4w4/Rd94bA9imS68tZBaYyBWSCE47V1RGuMrB94iyTOIEwRmVL2x/4An+6mETpLrKJ5hQkB8W4kFAadeIQ==, } engines: { node: '>=12' } - dev: false /dset/3.1.3: resolution: @@ -9403,11 +9425,11 @@ packages: dev: false optional: true - github.com/uNetworking/uWebSockets.js/c10b47c350cc97c299c6b4a07a98abb628704c71: + github.com/uNetworking/uWebSockets.js/fca27c05066342b01951782248640c8013b37196: resolution: { - tarball: https://codeload.github.com/uNetworking/uWebSockets.js/tar.gz/c10b47c350cc97c299c6b4a07a98abb628704c71, + tarball: https://codeload.github.com/uNetworking/uWebSockets.js/tar.gz/fca27c05066342b01951782248640c8013b37196, } name: uWebSockets.js - version: 20.33.0 + version: 20.34.0 dev: false diff --git a/src/Auth.ts b/src/Auth.ts deleted file mode 100644 index 7301f9e..0000000 --- a/src/Auth.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { createWalletNonce_mutationMutation } from '../.mesh'; - -export class Auth { - private sdk: any; - - constructor(sdk: any) { - this.sdk = sdk; - } - - async createWalletNounce( - wallet: string, - ): Promise { - return await this.sdk.createWalletNonce_mutation({ input: { wallet } }); - } -} diff --git a/src/Gateway.ts b/src/Gateway.ts index c9f7145..6063432 100644 --- a/src/Gateway.ts +++ b/src/Gateway.ts @@ -2,7 +2,6 @@ import { getMeshSDK, Sdk } from '../.mesh'; import { PDA } from './pda/pda'; export class Gateway { - // public auth: Auth; public pda: PDA; private sdk: Sdk; @@ -12,7 +11,6 @@ export class Gateway { apiKey, token, }); - // this.auth = new Auth(); this.pda = new PDA(this.sdk); } } diff --git a/src/pda/PDA.ts b/src/pda/PDA.ts deleted file mode 100644 index 5fc01ba..0000000 --- a/src/pda/PDA.ts +++ /dev/null @@ -1,28 +0,0 @@ -import { CreatePDAInput, Sdk, createPDA_mutationMutation } from '../../.mesh'; - -export class PDA { - private sdk: Sdk; - - constructor(sdk: Sdk) { - this.sdk = sdk; - } - - async createPDA( - pdaInput: CreatePDAInput, - ): Promise { - try { - return await this.sdk.createPDA_mutation({ input: pdaInput }); - } catch (error: any) { - console.log(error); - throw new Error(error); - } - } - - async getPDA(id: string) { - try { - return await this.sdk.PDA_query({ id }); - } catch (error: any) { - throw new Error(error); - } - } -} diff --git a/src/pda/pda.ts b/src/pda/pda.ts new file mode 100644 index 0000000..6d193c4 --- /dev/null +++ b/src/pda/pda.ts @@ -0,0 +1,151 @@ +import { + CreatePDAInput, + Sdk, + FilterPDAInput, + UpdatePDAInput, +} from '../../.mesh'; +import { PDAFilter, PDAStatus } from '../types'; +import { errorHandler } from '../utils/errorHandler'; + +export class PDA { + private 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 { + 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?: FilterPDAInput) { + 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 {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) { + try { + return await this.sdk.PDAs_query({ filter, order, skip, take }); + } catch (error) { + throw new Error(errorHandler(error)); + } + } + + /** + * The function `getIssuedPDAs` retrieves issued PDAs based on the provided filter, order, skip, and + * take parameters. + * @param {PDAFilter} - - `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({ filter, order, skip, take }: PDAFilter) { + try { + return await this.sdk.issuedPDAs_query({ filter, order, skip, take }); + } 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 `myPDACount` is an asynchronous function that returns the count of myPDAs based on an + * optional filter. + * @param {FilterPDAInput} [filter] - The `filter` parameter is an optional input that can be used to + * filter the results of the query. It is of type `FilterPDAInput`. + * @returns a Promise that resolves to a number. + */ + async myPDACount(filter?: FilterPDAInput) { + try { + return (await this.sdk.myPDACount_query({ filter })).myPDACount; + } 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 { + 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 { + 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 { + return await this.sdk.updatePDA_mutation({ input: updatedPDA }); + } catch (error) { + throw new Error(errorHandler(error)); + } + } +} diff --git a/src/types.ts b/src/types.ts index 9c5c601..71f61ba 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,3 +1,5 @@ +import { FilterPDAInput } from '../.mesh'; + export enum UserIdentifierType { EMAIL = 'EMAIL', EVM = 'EVM', @@ -5,3 +7,17 @@ export enum UserIdentifierType { SOLANA = 'SOLANA', USER_ID = 'USER_ID', } + +export enum PDAStatus { + 'Expired' = 'Expired', + 'Revoked' = 'Revoked', + 'Suspended' = 'Suspended', + 'Valid' = 'Valid', +} + +export type PDAFilter = { + filter?: FilterPDAInput; + order?: JSON; + skip?: number; + take?: number; +}; diff --git a/src/utils/errorHandler.ts b/src/utils/errorHandler.ts new file mode 100644 index 0000000..66a43dc --- /dev/null +++ b/src/utils/errorHandler.ts @@ -0,0 +1,7 @@ +export const errorHandler = (error: any): string => { + if (typeof error === 'object' && error !== null && 'message' in error) { + return error.message; + } else { + return 'Something went wrong!'; + } +}; diff --git a/test/pda.test.ts b/test/pda.test.ts index 70c6316..76c0158 100644 --- a/test/pda.test.ts +++ b/test/pda.test.ts @@ -1,20 +1,74 @@ +import dotenv from 'dotenv'; import { Gateway } from '../src/Gateway'; +import { PDAStatus, UserIdentifierType } from '../src/types'; +dotenv.config(); +const DEFAULT_TIMEOUT = 10000; let api: Gateway; beforeAll(() => { api = new Gateway({ - apiKey: 'm9Y5ntNcTlwQ2LbRpYr6K_VhxJXuZJ6Q', - token: - 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJwcm90b2NvbF9pZCI6IjkwNzcwODA2LWFlMGMtNGExNS05MjYzLWU5MWJhYWY1ZDkzZiIsImF1dGgiOnsiaWQiOiJlMjJiOWZkNi05NjgzLTRjZDgtOGZlOS1lZWU0YzFmYTJjZjciLCJ0eXBlIjoiV0FMTEVUIn0sImNyeXB0byI6eyJwdWJsaWNQZW0iOiItLS0tLUJFR0lOIFBVQkxJQyBLRVktLS0tLVxyXG5NSUlDSWpBTkJna3Foa2lHOXcwQkFRRUZBQU9DQWc4QU1JSUNDZ0tDQWdFQXpUdTJCWVVSS3VvM2V2MGx0MjUzXHJcblR3SDlGQkhUbU90dlRXYlQraTExSEF5WW5vUGx4UWV3M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: process.env.API_KEY!, + token: process.env.BEARER_TOKEN!, }); }); -describe('pda test', () => { - it('find pda', async () => { - const { PDA } = await api.pda.getPDA( - '6bc1a11f-f91d-4361-9a22-5df8d1bf4dc8', - ); - expect(PDA?.dataAsset?.title).toEqual('test'); - }); +describe('PDA SERVICE TESTING', () => { + it( + 'pda crud', + async () => { + let obj = { + dataModelId: process.env.DATAMODEL_ID!, + description: 'test', + title: 'test', + claim: { + gatewayUse: 'test', + }, + owner: { + type: UserIdentifierType.GATEWAY_ID, + value: 'sid', + }, + }; + const { createPDA } = await api.pda.createPDA(obj); + const { changePDAStatus } = await api.pda.changePDAStatus({ + id: createPDA.id, + status: PDAStatus.Suspended, + }); + expect(changePDAStatus.status).toEqual(PDAStatus.Suspended); + const { PDA } = await api.pda.getPDA(createPDA.id); + expect(PDA?.dataAsset?.title).toEqual('test'); + }, + DEFAULT_TIMEOUT, + ); + + it( + 'pda count', + async () => { + const count = await api.pda.getPDACount(); + expect(count).toBeGreaterThanOrEqual(0); + }, + DEFAULT_TIMEOUT, + ); + + it( + 'pdas', + async () => { + const { PDAs } = await api.pda.getPDAs({ + filter: { dataModelIds: [process.env.DATAMODEL_ID!] }, + skip: 0, + take: 10, + }); + expect(PDAs.length).toBeGreaterThanOrEqual(0); + }, + DEFAULT_TIMEOUT, + ); + + it( + 'issued pdas count', + async () => { + const count = await api.pda.getIssuedPDAsCount(); + expect(count).toBeGreaterThanOrEqual(0); + }, + DEFAULT_TIMEOUT, + ); }); diff --git a/tsconfig.json b/tsconfig.json index c29e012..6901cc3 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,5 +1,5 @@ { - "include": ["src/**/*.ts", "custom-fetch.ts", "custom-fetch-backup.ts"], + "include": ["src/**/*.ts", "custom-fetch.ts"], "exclude": ["jest.config.ts", "test", "**/*.test.ts"], "compilerOptions": { "target": "es2016" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */,