Skip to content

Commit

Permalink
Merge pull request #31 from paltalabs/zephyr-serverless-function
Browse files Browse the repository at this point in the history
add callServerlessFunction
  • Loading branch information
chopan123 authored Jun 6, 2024
2 parents cf0ca37 + 88c650b commit 9eb0f00
Showing 1 changed file with 61 additions and 15 deletions.
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,
});
}
}

0 comments on commit 9eb0f00

Please sign in to comment.