-
Notifications
You must be signed in to change notification settings - Fork 24
feat: Add raw request method for calling arbitrary API endpoints #298
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
base: main
Are you sure you want to change the base?
Conversation
|
Important Review skippedAuto incremental reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the You can disable this status message by setting the WalkthroughThe pull request adds a raw HTTP request capability across the OpenFGA TypeScript SDK, enabling developers to make arbitrary HTTP calls to OpenFGA endpoints. A new Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related issues
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (4)
tests/rawRequest.test.ts (3)
100-119: Consider using a more meaningful assertion.The test relies on the implicit behavior that nock will fail if the header is not matched. While this works, the
expect(true).toBe(true)assertion on line 118 is a code smell. Consider adding an explicit assertion that provides more value.🔎 Suggested improvement
- // If we get here without error, the Content-Type header was correctly applied - expect(true).toBe(true); + // Nock would have thrown if the Content-Type header was missing + expect(nock.isDone()).toBe(true);
191-195: Same pattern of trivial assertion.This applies to the custom headers test as well. Consider using
expect(nock.isDone()).toBe(true)for a more meaningful assertion.
246-250: Same pattern of trivial assertion in authentication test.Consider using
expect(nock.isDone()).toBe(true)here as well for consistency.api.ts (1)
833-836: Consider also handling DELETE with body.While less common, HTTP DELETE requests can also have a body. The current implementation only sets
Content-Typefor POST/PUT/PATCH. If a user passes a body with DELETE, it will be serialized (line 841-843) but without theContent-Typeheader.🔎 Suggested fix to include DELETE with body support
// Set Content-Type for requests with body - if (body !== undefined && (method === "POST" || method === "PUT" || method === "PATCH")) { + if (body !== undefined) { localVarHeaderParameter["Content-Type"] = "application/json"; }Alternatively, if you want to keep the current behavior to avoid unexpected Content-Type headers on bodyless requests, the current implementation is acceptable since this is an escape hatch and users can manually set headers via
options.headers.
📜 Review details
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (3)
api.tsclient.tstests/rawRequest.test.ts
🧰 Additional context used
🧬 Code graph analysis (2)
client.ts (2)
apiModel.ts (3)
CheckRequest(277-314)TupleKey(1432-1457)CheckRequestTupleKey(322-341)common.ts (1)
PromiseResult(116-116)
api.ts (2)
common.ts (8)
setSearchParams(51-66)DUMMY_BASE_URL(23-23)serializeDataIfNeeded(88-96)toPathString(102-104)RequestArgs(30-33)PromiseResult(116-116)createRequestFunction(280-347)CallResult(112-114)base.ts (1)
RequestArgs(15-18)
🔇 Additional comments (12)
tests/rawRequest.test.ts (4)
1-30: Test setup and imports look correct.The test file follows the existing test patterns in the codebase, properly setting up nock mocking and disabling network connections.
32-80: GET request tests are comprehensive and well-structured.The tests cover successful GET requests, query parameters, and $response access pattern. Good coverage.
140-153: Good test coverage for DELETE with 204 No Content response.The test correctly handles the 204 status code scenario and validates via
$response.status.
198-231: Error handling tests properly validate SDK error mapping.Tests correctly verify that 404 responses map to
FgaApiNotFoundErrorand 400 responses map toFgaApiValidationError, ensuring the rawRequest inherits SDK error handling.client.ts (3)
261-278: Well-documented type definitions for rawRequest.The
HttpMethodtype andClientRawRequestParamsinterface are clearly defined with JSDoc comments. This follows the existing patterns in the codebase.
967-1013: rawRequest method is well-implemented with excellent documentation.The method:
- Has comprehensive JSDoc with examples showing different use cases
- Properly delegates to the underlying API layer
- Uses
PromiseResult<any>which is appropriate for an escape hatch that can return arbitrary response data- Follows the existing patterns of other client methods
122-124: Minor formatting changes - no behavioral impact.These are just formatting adjustments to the existing type definitions.
api.ts (5)
811-849: rawRequest implementation follows SDK patterns correctly.The implementation:
- Uses the same URL construction pattern as other endpoints
- Properly handles query parameters via the existing
setSearchParamsutility- Only sets
Content-Type: application/jsonfor methods with body (POST/PUT/PATCH)- Uses
serializeDataIfNeededfor body serialization, consistent with other endpoints
1126-1143: OpenFgaApiFp rawRequest properly integrates with telemetry.The method correctly uses
createRequestFunctionwhich provides authentication, retry logic, and telemetry tracking. The telemetry attributeFgaClientRequestMethod: "RawRequest"is appropriately set for observability.
1344-1358: OpenFgaApiFactory rawRequest follows established patterns.The factory implementation correctly delegates to the functional programming interface.
1593-1608: OpenFgaApi class rawRequest method is well-documented.The public method has comprehensive JSDoc documentation and correctly delegates through the layers.
811-812: Path parameter is passed directly without validation.The
pathparameter is used directly in URL construction without validation. This is intentional for an "escape hatch" method, but consider documenting that:
- The path should start with
/- Path traversal characters could lead to unexpected behavior
This is acceptable given the method's purpose as an escape hatch for advanced users who know what they're doing.
Ensure the documentation or README mentions that users are responsible for constructing valid paths. The inline JSDoc example
'/stores/{store_id}/my-endpoint'provides good guidance.
|
Reposting my issue comment here:
|
SoulPancake
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR @Abishek-Newar
Can you add a section documenting this too
similar to the go-sdk https://github.com/openfga/go-sdk?tab=readme-ov-file#calling-other-endpoints
Examples should be nice to have
|
@SoulPancake I have made the reviewed changes |
|
Hi @SoulPancake , I’m seeing test case failures in the CI environment, but everything passes locally on my machine. |
|
@Abishek-Newar |
|
Hi @SoulPancake , I’m trying to diagnose why the tests are failing in GitHub Actions, even though they’re passing locally. I’d really appreciate it if you could share any hints or guidance when you have a moment. Thank you! |
|
@Abishek-Newar It seems to be a known issue with nocks in CI. Once we investigate it, we will let you know. |
|
@SoulPancake ok sure |
|
Hi @SoulPancake , is it possible to go for nocks@13, until this issue with nocks and @mswjs/interceptors is resolved ? |
Description
This PR adds a rawRequest method to the SDK.
It allows developers to send any HTTP request to any OpenFGA API endpoint, while still using the SDK’s built-in features like authentication, configuration, retries, and error handling.
This is useful as an “escape hatch” for calling new or experimental API endpoints that don’t yet have official SDK methods.
The change adds rawRequest to both OpenFgaApi and OpenFgaClient, introduces new types (HttpMethod and ClientRawRequestParams), and includes a full test suite with 12 tests covering:
Closes #282
What problem is being solved?
Currently, the SDK provides typed methods for specific API endpoints (check, write, read, etc.). However, there is no way to call arbitrary OpenFGA API endpoints that may not yet have dedicated SDK methods. This creates a gap when:
New OpenFGA API endpoints are released but SDK updates have not been implemented yet
Experimental endpoints need to be tested
Developers need a temporary alternative while waiting for official SDK support
Without a raw request method, developers are forced to either wait for SDK updates, or fork and modify the SDK code themselves, or call the OpenFGA API directly without the SDK, losing the benefits of SDK authentication, configuration, and error handling patterns. This reduces developer agility and creates friction in adopting new OpenFGA features.
How is it being solved?
A new rawRequest method was added that takes an HTTP method, API path, and optional body and query parameters.
Internally, it uses the existing createRequestFunction, so it automatically handles:
This helps developers use new or experimental OpenFGA features quickly, while still following the same reliable patterns as the rest of the SDK.
What changes are made to solve it?
Added 12 tests covering all HTTP methods (GET, POST, PUT, DELETE, PATCH), query parameters, custom headers, authentication, and error handling.
References
Closes #282
Review Checklist
mainSummary by CodeRabbit
New Features
rawRequestmethod supports GET, POST, PUT, DELETE, and PATCH operations with custom bodies, query parameters, and headers.Tests