Skip to content

Commit c5998ff

Browse files
Scott-Meyerclaude
andcommitted
refactor(apps): follow @datadog/apps-backend runtime entry-point split
The SDK renamed its build-tool contract from a single @datadog/apps-backend/internal entry (createBackendFrom$/setBackend) to @datadog/apps-backend/runtime (setBackend) paired with a runtime factory at @datadog/apps-backend/runtime/jsFunctionWithActions (buildRuntimeFromJsFunctionWithActions). Update the generated virtual entry to import and call the new pair. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
1 parent 8f0b64f commit c5998ff

4 files changed

Lines changed: 26 additions & 24 deletions

File tree

packages/plugins/apps/src/backend/shared.test.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,28 @@ import {
99

1010
describe('SET_BACKEND_CONTEXT_SNIPPET', () => {
1111
test('registers the backend runtime context', () => {
12-
const backendHandle = { opaque: true };
13-
const createBackendFrom$ = jest.fn().mockReturnValue(backendHandle);
12+
const runtimeHandle = { opaque: true };
13+
const buildRuntimeFromJsFunctionWithActions = jest.fn().mockReturnValue(runtimeHandle);
1414
const setBackend = jest.fn();
1515
const context = { Source: { initiator: { id: 'user-id' } } };
1616

1717
// eslint-disable-next-line no-new-func
1818
const run = new Function(
19-
'createBackendFrom$',
19+
'buildRuntimeFromJsFunctionWithActions',
2020
'setBackend',
2121
'$',
2222
SET_BACKEND_CONTEXT_SNIPPET,
2323
);
24-
run(createBackendFrom$, setBackend, context);
24+
run(buildRuntimeFromJsFunctionWithActions, setBackend, context);
2525

26-
expect(createBackendFrom$).toHaveBeenCalledWith(context);
27-
expect(setBackend).toHaveBeenCalledWith(backendHandle);
26+
expect(buildRuntimeFromJsFunctionWithActions).toHaveBeenCalledWith(context);
27+
expect(setBackend).toHaveBeenCalledWith(runtimeHandle);
2828
});
2929

30-
test('does not throw when createBackendFrom$/setBackend are unavailable', () => {
30+
test('does not throw when buildRuntimeFromJsFunctionWithActions/setBackend are unavailable', () => {
3131
// eslint-disable-next-line no-new-func
3232
const run = new Function(
33-
'createBackendFrom$',
33+
'buildRuntimeFromJsFunctionWithActions',
3434
'setBackend',
3535
'$',
3636
SET_BACKEND_CONTEXT_SNIPPET,

packages/plugins/apps/src/backend/shared.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,24 @@ export function isActionCatalogInstalled(fromDir: string): boolean {
2020
return isPackageExportInstalled('@datadog/action-catalog/action-execution', fromDir);
2121
}
2222

23-
/** Check if the backend-internal @datadog/apps-backend runtime entry point is installed. */
23+
/** Check if the @datadog/apps-backend "JS Function with Actions" runtime factory is installed. */
2424
export function isDatadogAppsInstalled(fromDir: string): boolean {
25-
return isPackageExportInstalled('@datadog/apps-backend/internal', fromDir);
25+
return isPackageExportInstalled('@datadog/apps-backend/runtime/jsFunctionWithActions', fromDir);
2626
}
2727

2828
/** The import line to pull action-catalog's setExecuteActionImplementation into bundles. */
2929
export const ACTION_CATALOG_IMPORT =
3030
"import { setExecuteActionImplementation } from '@datadog/action-catalog/action-execution';";
3131

3232
/** The import line that exposes @datadog/apps-backend backend context initialization. */
33-
export const DATADOG_APPS_IMPORT =
34-
"import { createBackendFrom$, setBackend } from '@datadog/apps-backend/internal';";
33+
export const DATADOG_APPS_IMPORT = `\
34+
import { buildRuntimeFromJsFunctionWithActions } from '@datadog/apps-backend/runtime/jsFunctionWithActions';
35+
import { setBackend } from '@datadog/apps-backend/runtime';`;
3536

3637
/** Script snippet that supplies the backend runtime context to @datadog/apps. */
3738
export const SET_BACKEND_CONTEXT_SNIPPET = `\
38-
if (typeof createBackendFrom$ === 'function' && typeof setBackend === 'function') {
39-
setBackend(createBackendFrom$($));
39+
if (typeof buildRuntimeFromJsFunctionWithActions === 'function' && typeof setBackend === 'function') {
40+
setBackend(buildRuntimeFromJsFunctionWithActions($));
4041
}`;
4142

4243
/** Script snippet that registers the $.Actions-based executeAction implementation at runtime. */

packages/plugins/apps/src/backend/virtual-entry.test.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@ describe('Backend Functions - generateVirtualEntryContent', () => {
9797
'/src/handler.ts',
9898
PROJECT_ROOT,
9999
);
100-
expect(result).not.toContain('@datadog/apps-backend/internal');
101-
expect(result).not.toContain('setBackend(createBackendFrom$($))');
100+
expect(result).not.toContain('@datadog/apps-backend/runtime');
101+
expect(result).not.toContain('setBackend(buildRuntimeFromJsFunctionWithActions($))');
102102
});
103103
});
104104

@@ -143,13 +143,14 @@ describe('Backend Functions - generateVirtualEntryContent', () => {
143143
);
144144

145145
expect(result).toContain(
146-
"import { createBackendFrom$, setBackend } from '@datadog/apps-backend/internal'",
147-
);
148-
expect(result.indexOf('setBackend(createBackendFrom$($))')).toBeGreaterThan(
149-
result.indexOf('globalThis.$ = $'),
146+
"import { buildRuntimeFromJsFunctionWithActions } from '@datadog/apps-backend/runtime/jsFunctionWithActions'",
150147
);
148+
expect(result).toContain("import { setBackend } from '@datadog/apps-backend/runtime'");
149+
expect(
150+
result.indexOf('setBackend(buildRuntimeFromJsFunctionWithActions($))'),
151+
).toBeGreaterThan(result.indexOf('globalThis.$ = $'));
151152
expect(result.indexOf('await myHandler(...args)')).toBeGreaterThan(
152-
result.indexOf('setBackend(createBackendFrom$($))'),
153+
result.indexOf('setBackend(buildRuntimeFromJsFunctionWithActions($))'),
153154
);
154155
});
155156
});

packages/plugins/apps/src/backend/virtual-entry.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ export function generateVirtualEntryContent(
2121
projectRoot: string,
2222
): string {
2323
const lines: string[] = [];
24-
const hasDatadogAppsBackendInternal = isDatadogAppsInstalled(projectRoot);
24+
const hasDatadogAppsBackendRuntime = isDatadogAppsInstalled(projectRoot);
2525

2626
lines.push(`import { ${functionName} } from ${JSON.stringify(entryPath)};`);
2727

28-
if (hasDatadogAppsBackendInternal) {
28+
if (hasDatadogAppsBackendRuntime) {
2929
lines.push(DATADOG_APPS_IMPORT);
3030
}
3131

@@ -38,7 +38,7 @@ export function generateVirtualEntryContent(
3838
lines.push('export async function main($) {');
3939
lines.push(' globalThis.$ = $;');
4040
lines.push('');
41-
if (hasDatadogAppsBackendInternal) {
41+
if (hasDatadogAppsBackendRuntime) {
4242
lines.push(' // Supply the backend runtime context');
4343
lines.push(SET_BACKEND_CONTEXT_SNIPPET);
4444
lines.push('');

0 commit comments

Comments
 (0)