Skip to content

Commit

Permalink
Merge pull request #66 from CrowdStrike/return-data-from-connect
Browse files Browse the repository at this point in the history
feat: return data from connect()
  • Loading branch information
rhinchey-cs authored Sep 5, 2024
2 parents a9d6969 + 6dffedd commit 019b208
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/four-hats-allow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@crowdstrike/foundry-js': minor
---

return initial data from connect()
6 changes: 4 additions & 2 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,11 @@ export default class FalconApi<DATA extends LocalData = LocalData> {
*
* This establishes a connection to send messages between the extension and the Falcon Console. Only when established you will be able to call other APIs.
*/
public async connect(): Promise<void> {
public async connect(): Promise<{ origin: string; data?: DATA }> {
const response = await this.bridge.postMessage({ type: 'connect' });

if (response !== undefined) {
const { origin, data } = response;
const { data, origin } = response;

this.bridge.setOrigin(origin);
this.data = data;
Expand All @@ -107,6 +107,8 @@ export default class FalconApi<DATA extends LocalData = LocalData> {
}

this.resizeTracker = new ResizeTracker(this.bridge);

return response;
}

/**
Expand Down
45 changes: 44 additions & 1 deletion tests/connect.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import FalconApi from '../src';
import FalconApi, { type LocalData } from '../src';
import { afterEach, beforeEach, expect, test } from 'vitest';

import type {
Expand Down Expand Up @@ -46,6 +46,49 @@ test('it can connect to main thread', async () => {
expect(api.isConnected).toEqual(true);
});

test('it returns data when connected', async () => {
const testData: LocalData = {
app: {
id: 'testid',
},
user: {
uuid: '123',
username: 'johndoe',
},
theme: 'theme-light',
cid: 'xxx',
locale: 'en-us',
};

const testOrigin = 'www.example.com';

// simulate ready answer coming back from main thread
window.parent.addEventListener(
'message',
(message: MessageEvent<MessageEnvelope<ConnectRequestMessage>>) => {
const response: MessageEnvelope<ConnectResponseMessage> = {
message: {
type: 'connect',
payload: {
origin: testOrigin,
data: testData,
},
},
meta: {
messageId: message.data.meta.messageId,
version: 'current',
},
};

window.postMessage(response);
},
);

const { data, origin } = await api.connect();
expect(data).toEqual(testData);
expect(origin).toEqual(testOrigin);
});

test('failed connect attempt will time out', async () => {
await expect(api.connect()).rejects.toThrow();
});

0 comments on commit 019b208

Please sign in to comment.