Skip to content

Added support for action callbacks in ensemble.invokeAPI #1052

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
6 changes: 2 additions & 4 deletions apps/kitchen-sink/src/ensemble/screens/home.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,10 @@ View:
console.log('>>> secret variable >>>', ensemble.secrets.dummyOauthSecret)
ensemble.storage.set('products', []);
ensemble.invokeAPI('getDummyProducts').then((res) => ensemble.storage.set('products', (res?.body?.users || []).map((i) => ({ ...i, name: i.firstName + ' ' + i.lastName }))));
const res = await ensemble.invokeAPI('getDummyNumbers')
await new Promise((resolve) => setTimeout(resolve, 5000))
return res
ensemble.invokeAPI('getDummyNumbers')
onComplete:
executeCode: |
console.log('API triggered', result)
console.log('API triggered')

header:
title:
Expand Down
51 changes: 29 additions & 22 deletions packages/framework/src/api/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,31 +50,38 @@ export const invokeAPI = async (
const useMockResponse =
has(api, "mockResponse") && isUsingMockResponse(screenContext.app?.id);

const response = await queryClient.fetchQuery({
queryKey: [hash],
queryFn: () =>
DataFetcher.fetch(
api,
{ ...apiInputs, ...context },
{
mockResponse: mockResponse(
evaluatedMockResponse ?? api.mockResponse,
try {
const response = await queryClient.fetchQuery({
queryKey: [hash],
queryFn: () =>
DataFetcher.fetch(
api,
{ ...apiInputs, ...context },
{
mockResponse: mockResponse(
evaluatedMockResponse ?? api.mockResponse,
useMockResponse,
),
useMockResponse,
),
useMockResponse,
},
),
staleTime:
api.cacheExpirySeconds && !options?.bypassCache
? api.cacheExpirySeconds * 1000
: 0,
});
},
),
staleTime:
api.cacheExpirySeconds && !options?.bypassCache
? api.cacheExpirySeconds * 1000
: 0,
});

if (setter) {
set(update, api.name, response);
setter(screenDataAtom, { ...update });
if (setter) {
set(update, api.name, response);
setter(screenDataAtom, { ...update });
}

api.onResponseAction?.callback({ ...context, response });

return response;
} catch (err) {
api.onErrorAction?.callback({ ...context, error: err });
}
return response;
};

export const handleConnectSocket = (
Expand Down
4 changes: 3 additions & 1 deletion packages/framework/src/shared/models.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { CSSProperties } from "react";
import type { EnsembleAction } from "./actions";
import type { EnsembleAction, EnsembleActionHookResult } from "./actions";
import type { EnsembleConfigYAML } from "./dto";

/**
Expand Down Expand Up @@ -74,6 +74,8 @@ export interface EnsembleAPIModel {
body?: string | object;
onResponse?: EnsembleAction;
onError?: EnsembleAction;
onResponseAction?: EnsembleActionHookResult;
onErrorAction?: EnsembleActionHookResult;
mockResponse?: EnsembleMockResponse | string;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ test("call ensemble.invokeAPI with bypassCache", async () => {

expect(withoutForceInitialResult).toBe(withoutForceResult);
expect(withForceResult).not.toBe(withoutForceResult);
});
}, 10000);

test.todo("populates application invokables");

Expand Down
Loading