Skip to content

Commit

Permalink
DPLT-1044 Add context.fetchFromSocialApi (#134)
Browse files Browse the repository at this point in the history
  • Loading branch information
morgsmccauley authored Jul 19, 2023
1 parent 828a83f commit db7cc70
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
15 changes: 15 additions & 0 deletions indexer-js-queue-handler/__snapshots__/indexer.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Indexer unit tests Indexer.buildImperativeContextForFunction() can fetch from the near social api 1`] = `
[
[
"https://api.near.social/index",
{
"body": "{"action":"post","key":"main","options":{"limit":1,"order":"desc"}}",
"headers": {
"Content-Type": "application/json",
},
"method": "POST",
},
],
]
`;

exports[`Indexer unit tests Indexer.runFunctions() allows imperative execution of GraphQL operations 1`] = `
[
[
Expand Down
7 changes: 4 additions & 3 deletions indexer-js-queue-handler/indexer.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ export default class Indexer {
vm.freeze(blockWithHelpers, 'block');
vm.freeze(context, 'context');
vm.freeze(context, 'console'); // provide console.log via context.log
vm.freeze(fetch, 'fetch');
vm.freeze(mutationsReturnValue, 'mutationsReturnValue'); // this still allows context.set to modify it

const modifiedFunction = this.transformIndexerFunction(indexerFunction.code);
Expand Down Expand Up @@ -221,8 +220,7 @@ export default class Indexer {
},
log: async (...log) => { // starting with imperative logging for both imperative and functional contexts
return await this.writeLog(functionName, block_height, ...log);
}

},
};
}

Expand Down Expand Up @@ -266,6 +264,9 @@ export default class Indexer {
value
);
},
fetchFromSocialApi: async (path, options) => {
return this.deps.fetch(`https://api.near.social${path}`, options);
}
};
}

Expand Down
24 changes: 24 additions & 0 deletions indexer-js-queue-handler/indexer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,30 @@ mutation _1 { set(functionName: "buildnear.testnet/test", key: "foo2", data: "in
]);
});

test('Indexer.buildImperativeContextForFunction() can fetch from the near social api', async () => {
const mockFetch = jest.fn();
const indexer = new Indexer('mainnet', { fetch: mockFetch, awsXray: mockAwsXray, metrics: mockMetrics });

const context = indexer.buildImperativeContextForFunction();

await context.fetchFromSocialApi('/index', {
method: 'POST',
headers: {
['Content-Type']: 'application/json',
},
body: JSON.stringify({
action: 'post',
key: 'main',
options: {
limit: 1,
order: 'desc'
}
})
});

expect(mockFetch.mock.calls).toMatchSnapshot();
});

test('Indexer.buildImperativeContextForFunction() throws when a GraphQL response contains errors', async () => {
const mockFetch = jest.fn()
.mockResolvedValue({
Expand Down

0 comments on commit db7cc70

Please sign in to comment.