-
Notifications
You must be signed in to change notification settings - Fork 1
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: Add extractRelatedResourceByName helper #59
base: main
Are you sure you want to change the base?
Conversation
Warning There were issues while running some tools. Please review the errors and either fix the tool’s configuration or disable the tool if it’s a critical failure. 🔧 eslint
packages/json-api/src/lib/response/extractRelatedResourceByName.spec.tsOops! Something went wrong! :( ESLint: 8.57.1 ESLint couldn't find the plugin "@typescript-eslint/eslint-plugin". (The package "@typescript-eslint/eslint-plugin" was not found when loaded as a Node module from the directory "/packages/json-api".) It's likely that the plugin isn't installed correctly. Try reinstalling by running the following:
The plugin "@typescript-eslint/eslint-plugin" was referenced from the config file in "packages/json-api/.eslintrc.json » ../../.eslintrc.json#overrides[0] » ./packages/eslint-config/src/index.js". If you still can't figure out the problem, please stop by https://eslint.org/chat/help to chat with the team. WalkthroughThe pull request introduces a new utility function Changes
Sequence DiagramsequenceDiagram
participant Caller
participant extractRelatedResourceByName
participant Resource
participant IncludedResources
Caller->>extractRelatedResourceByName: Call with resource, included, relationshipName
extractRelatedResourceByName->>Resource: Check relationships exist
extractRelatedResourceByName->>IncludedResources: Check included resources defined
extractRelatedResourceByName->>Resource: Get relationship data
extractRelatedResourceByName->>IncludedResources: Match resources by type and id
extractRelatedResourceByName-->>Caller: Return matched resource(s) or undefined
Finishing Touches
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
3097fe1
to
5b254f4
Compare
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: 3
🧹 Nitpick comments (5)
packages/json-api/src/lib/types.ts (1)
80-80
: Consider using a more specific type for attributes.Instead of using
Record<string, any>
, consider creating a generic type parameter for attributes to improve type safety:- attributes?: Record<string, any>; + attributes?: Record<string, unknown>;🧰 Tools
🪛 GitHub Actions: CI
[error] Code formatting check failed. File needs to be formatted according to project standards.
packages/json-api/src/lib/response/extractRelatedResourceByName.ts (2)
33-34
: Improve type safety of filter operation.The filter operation could be more explicit about removing undefined values:
- .filter((element) => !!element); + .filter((element): element is JsonApiResource => element !== undefined);🧰 Tools
🪛 GitHub Actions: CI
[error] Code formatting check failed. File needs to be formatted according to project standards.
16-18
: Consider separating null checks.For better error handling and debugging, consider separating the null checks:
- if (!resource.relationships || !included) { + if (!resource.relationships) { + return undefined; + } + if (!included) { return undefined; - } + }🧰 Tools
🪛 GitHub Actions: CI
[error] Code formatting check failed. File needs to be formatted according to project standards.
packages/json-api/src/lib/response/extractRelatedResourceByName.spec.ts (2)
52-76
: Add test case for empty array relationship.Consider adding a test case for when the relationship data is an empty array:
it("should return an empty array when relationship data is an empty array", () => { const apiResponse = { data: { type: "post", id: "1", relationships: { comments: { data: [], }, }, }, included: [], }; const result = extractRelatedResourceByName( apiResponse.data, apiResponse.included, "comments" ); expect(result).toEqual([]); });
93-105
: Consider testing for mismatched type.Add a test case for when the included resource has matching ID but different type:
it("should return undefined if included resource has matching id but different type", () => { const apiResponse = { data: { type: "book", id: "1", relationships: { author: { data: { type: "person", id: "1" } } }, }, included: [{ type: "organization", id: "1", attributes: { name: "Org" } }], }; const result = extractRelatedResourceByName( apiResponse.data, apiResponse.included, "author" ); expect(result).toBeUndefined(); });
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (3)
packages/json-api/src/lib/response/extractRelatedResourceByName.spec.ts
(1 hunks)packages/json-api/src/lib/response/extractRelatedResourceByName.ts
(1 hunks)packages/json-api/src/lib/types.ts
(1 hunks)
🧰 Additional context used
🪛 GitHub Actions: CI
packages/json-api/src/lib/types.ts
[error] Code formatting check failed. File needs to be formatted according to project standards.
packages/json-api/src/lib/response/extractRelatedResourceByName.ts
[error] Code formatting check failed. File needs to be formatted according to project standards.
⏰ Context from checks skipped due to timeout of 90000ms (1)
- GitHub Check: Analyze (javascript-typescript)
export interface JsonApiResource { | ||
type: string; | ||
id: string; | ||
attributes?: Record<string, any>; | ||
relationships?: Record< | ||
string, | ||
{ data: { type: string; id: string } | Array<{ type: string; id: string }> } | ||
>; | ||
} |
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.
Fix code formatting according to project standards.
The CI pipeline indicates formatting issues. Please run the project's formatter on this file.
🧰 Tools
🪛 GitHub Actions: CI
[error] Code formatting check failed. File needs to be formatted according to project standards.
import { JsonApiResource } from "../types"; | ||
/** | ||
* Extracts a related resource from the included array of a JSON:API response | ||
* based on the relationship name. | ||
* | ||
* @param resource - The main resource containing the relationships. | ||
* @param included - The included array from a JSON:API response. | ||
* @param relationshipName - The name of the relationship to extract. | ||
* @returns The related resource if found, otherwise undefined. | ||
*/ | ||
export function extractRelatedResourceByName( | ||
resource: JsonApiResource, | ||
included: JsonApiResource[] | undefined, | ||
relationshipName: string | ||
): JsonApiResource | JsonApiResource[] | undefined { | ||
if (!resource.relationships || !included) { | ||
return undefined; | ||
} | ||
|
||
const relationship = resource.relationships[relationshipName]; | ||
if (!relationship?.data) { | ||
return undefined; | ||
} | ||
|
||
if (Array.isArray(relationship.data)) { | ||
return relationship.data | ||
.map((data) => | ||
included?.find( | ||
(includedResource) => | ||
includedResource.type === data.type && includedResource.id === data.id | ||
) | ||
) | ||
.filter((element) => !!element); | ||
} | ||
|
||
const { type, id } = relationship.data; | ||
|
||
return included.find( | ||
(includedResource) => includedResource.type === type && includedResource.id === id | ||
); | ||
} |
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.
Fix code formatting according to project standards.
The CI pipeline indicates formatting issues. Please run the project's formatter on this file.
🧰 Tools
🪛 GitHub Actions: CI
[error] Code formatting check failed. File needs to be formatted according to project standards.
@@ -0,0 +1,106 @@ | |||
import { extractRelatedResourceByName } from "./jsonApiHelpers"; |
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.
Fix incorrect import path.
The import statement appears to be incorrect:
- import { extractRelatedResourceByName } from "./jsonApiHelpers";
+ import { extractRelatedResourceByName } from "./extractRelatedResourceByName";
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
import { extractRelatedResourceByName } from "./jsonApiHelpers"; | |
import { extractRelatedResourceByName } from "./extractRelatedResourceByName"; |
Summary
This PR introduces the
extractRelatedResourceByName
function, which facilitates extracting relationship data from theincluded
key of aJSON:API
compliant API response using the relationship name.Key Benefits:
Changes
Videos/screenshots