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

add callServerlessFunction #31

Merged
merged 1 commit into from
Jun 6, 2024
Merged
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
76 changes: 61 additions & 15 deletions src/Mercury.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
GetAllContractEventSubscriptionsResponse,
GetAllFullAccountSubscriptionsResponse,
ContractEntriesResponse,
SubscribeToMultipleLedgerEntriesArgs
SubscribeToMultipleLedgerEntriesArgs,
} from "./types";
import { SubscribeToContractEventsArgs } from "./types/subscriptions";
import { toSnakeCase } from "./utils";
Expand Down Expand Up @@ -209,13 +209,16 @@ export class Mercury {
maxSingleSize: this._defaultMaxSingleSize,
durability: args.durability,
keyXdr: args.keyXdr,
hydrate: args.hydrate?? true,
hydrate: args.hydrate ?? true,
});
const response = await this._backendRequest({ method: "POST", url: "/entry", body })
.catch((error: string)=>{
console.error(error)
})
return response
const response = await this._backendRequest({
method: "POST",
url: "/entry",
body,
}).catch((error: string) => {
console.error(error);
});
return response;
}

/**
Expand All @@ -224,19 +227,25 @@ export class Mercury {
* @param args - The arguments for subscribing to multiple ledger entries.
* @returns An array of results for each subscribed ledger entry.
*/
public async subscribeToMultipleLedgerEntries(args: SubscribeToMultipleLedgerEntriesArgs) {
const results = []
for(let i = 0; i < args.contractId.length; i++){
public async subscribeToMultipleLedgerEntries(
args: SubscribeToMultipleLedgerEntriesArgs
) {
const results = [];
for (let i = 0; i < args.contractId.length; i++) {
const body = this._createRequestBody({
contractId: args.contractId[i],
keyXdr: args.keyXdr,
durability: args.durability,
hydrate: args.hydrate?? true,
hydrate: args.hydrate ?? true,
});
const response = await this._backendRequest({ method: "POST", url: "/entry", body })
results.push(response)
const response = await this._backendRequest({
method: "POST",
url: "/entry",
body,
});
results.push(response);
}
return results
return results;
}

/**
Expand Down Expand Up @@ -422,12 +431,49 @@ export class Mercury {
* @param args - The query request and optional variables.
* @returns A promise that resolves to the result of the query.
*/
public async getCustomQuery(args: { request: string; variables?: any; }) {
public async getCustomQuery(args: { request: string; variables?: any }) {
return this._graphqlRequest({
body: {
request: QUERIES.getCustomQuery(args.request),
variables: args.variables,
},
});
}

/**
* Calls a specific serverless function with the provided arguments.
* @param args Object containing the following properties:
* - functionName: Name of the serverless function to call.
* - arguments: Arguments to be passed to the function in object format.
* @returns ApiResponse with data or error information.
*/
public async callServerlessFunction(args: {
functionName: string;
arguments: Object;
}) {
try {
JSON.stringify(args.arguments);
} catch (error) {
return {
ok: false,
data: null,
error: "Invalid arguments",
};
}

const body = {
mode: {
Function: {
fname: args.functionName,
arguments: JSON.stringify(args.arguments),
},
},
};

return this._backendRequest({
method: "POST",
url: "/zephyr/execute",
body,
});
}
}