Skip to content
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

feat: useCollectionObject #29

Merged
merged 2 commits into from
Mar 18, 2025
Merged
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
53 changes: 28 additions & 25 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ name: Build

on:
push:
branches: [ main ]
branches: [main]
pull_request:
branches: [ main ]
branches: [main]

jobs:
build:
Expand All @@ -16,26 +16,29 @@ jobs:
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/

steps:
- uses: actions/checkout@v4

- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'

- name: Install dependencies
run: npm ci

- name: Build
run: npm run build

- name: Cache build artifacts
uses: actions/cache@v4
with:
path: |
dist
node_modules
key: ${{ runner.os }}-build-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-build-
- uses: actions/checkout@v4

- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
cache: "npm"

- name: Install dependencies
run: npm ci

- name: Build
run: npm run build

- name: Test
run: npm test
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the change, all others are whitespace to fix indention under steps.


- name: Cache build artifacts
uses: actions/cache@v4
with:
path: |
dist
node_modules
key: ${{ runner.os }}-build-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-build-
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,25 @@ return (

> [!IMPORTANT]
> Note that the `data` object returned by `useFoundry()` is a React state object, which the Foundry provider correctly updates via a `falcon.events.on('data')` event handler. Do not use the `falcon.data` object, since it will not correctly re-render your UI when a data event occurs.

### `useCollectionObject`

Query a collection object. This hook is useful to look up a well known value when a component renders, for example a configuration value.

```javascript
const [config, configReady, configError] = useCollectionObject(
"config",
"default"
);
useEffect(() => {
if (!configReady) return;
if (configError) return; // TODO: handle error
// TODO: use config value
}, [configReady]);
```

### Types

Use these types to perform type assertions on responses from foundry-js and safely interact with those responses (rather than asserting them as `any`). See the documentation for each type for more details.

- `CollectionReadResponse` - Returned from `falcon.collection().read()`.
19 changes: 19 additions & 0 deletions __mocks__/@crowdstrike/foundry-js.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { jest } from "@jest/globals";

/**
* Stub that enables `jest.mock("@crowdstrike/foundry-js")` since we won't be able to interact with
* Foundry from a local test. Mock specific function calls in individual tests, for example:
*
* ```
* const mockReadResponse = { special_key: "special_value" };
* const mockCollection = {
* read: jest
* .fn<() => Promise<CollectionReadResponse>>()
* .mockResolvedValue(mockReadResponse),
* };
* const mockFalcon = {
* collection: jest.fn().mockReturnValue(mockCollection),
* };
* ```
*/
export const FalconApi = jest.fn();
15 changes: 15 additions & 0 deletions __mocks__/foundry-context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { jest } from "@jest/globals";

/**
* Stub that enables `jest.mock("./foundry-context")` since we won't have an actual `FoundryProvider`
* in a local test. Return a mock `FalconApi` (see `FalconApi` mock docs):

*
* ```javascript
* (useFoundry as jest.Mock).mockReturnValue({
* isInitialized: true,
* falcon: mockFalcon,
* });
* ```
*/
export const useFoundry = jest.fn();
8 changes: 8 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/** @type {import('ts-jest').JestConfigWithTsJest} **/
export default {
preset: "ts-jest",
testEnvironment: "jsdom",
transform: {
"^.+.tsx?$": ["ts-jest", {}],
},
};
Loading