Skip to content

Add createImageUrl utility function to api app#123

Open
femincan wants to merge 6 commits intoplayfulprogramming:mainfrom
femincan:refactor/image-url-utility
Open

Add createImageUrl utility function to api app#123
femincan wants to merge 6 commits intoplayfulprogramming:mainfrom
femincan:refactor/image-url-utility

Conversation

@femincan
Copy link
Contributor

@femincan femincan commented Mar 10, 2026

This PR introduces new createImageUrl utility function to api app to prevent duplicated inline image url creation in api routes.

Summary by CodeRabbit

  • Refactor
    • Consolidated image URL generation into a shared utility used across the API, removing duplicated URL-building logic.
    • Centralized URL construction yields the same image links and preserves existing API responses and routing behavior.
    • Simplifies future maintenance and reduces risk of inconsistent image URLs across endpoints.

@coderabbitai
Copy link

coderabbitai bot commented Mar 10, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 521ff66e-51ff-458a-bd1e-8f115a8b0c80

📥 Commits

Reviewing files that changed from the base of the PR and between fa23e73 and 9aa7145.

📒 Files selected for processing (1)
  • apps/api/src/utils.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • apps/api/src/utils.ts

📝 Walkthrough

Walkthrough

Added a shared createImageUrl(path: string) helper in apps/api/src/utils.ts and updated three route modules to import and use it instead of constructing S3 image URLs inline.

Changes

Cohort / File(s) Summary
Centralized S3 URL utility
apps/api/src/utils.ts
Added createImageUrl(path: string) that builds absolute S3 image URLs using env.S3_PUBLIC_URL and env.S3_BUCKET.
Routes updated to use utility
apps/api/src/routes/content/collections.ts, apps/api/src/routes/tasks/post-images.ts, apps/api/src/routes/tasks/url-metadata.ts
Replaced inline S3 URL construction with imports of createImageUrl(...) for coverImage, profileImage, banner, linkPreview, embed images, and other image URL fields; removed duplicate URL-building logic and env imports where applicable.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 66.67% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: adding a createImageUrl utility function to the api app, which is the core objective of centralizing image URL construction across multiple routes.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@apps/api/src/utils.ts`:
- Around line 4-5: The S3 URL builder currently concatenates env.S3_PUBLIC_URL
and env.S3_BUCKET into s3PublicUrl and then calls new URL(path, s3PublicUrl),
which can produce double slashes or drop the bucket when inputs contain extra
slashes; update the logic that builds s3PublicUrl and prepares path by trimming
any trailing '/' from env.S3_PUBLIC_URL and any leading '/' from the incoming
path (and ensure exactly one '/' between S3_PUBLIC_URL and S3_BUCKET), then call
new URL(cleanedPath, cleanedBase) (refer to s3PublicUrl and the new URL(path,
s3PublicUrl) call) so resolved object URLs are always correct.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 83f2c835-5a4a-4526-a907-abd24833ee76

📥 Commits

Reviewing files that changed from the base of the PR and between 43125fe and 6472efe.

📒 Files selected for processing (4)
  • apps/api/src/routes/content/collections.ts
  • apps/api/src/routes/tasks/post-images.ts
  • apps/api/src/routes/tasks/url-metadata.ts
  • apps/api/src/utils.ts

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

♻️ Duplicate comments (1)
apps/api/src/utils.ts (1)

9-10: ⚠️ Potential issue | 🟠 Major

Normalize the base URL and key before resolving.

This still has the previously reported edge case: if Line 9 receives a trailing / in env.S3_PUBLIC_URL or Line 10 receives a leading / in path, new URL() can either emit malformed URLs or resolve from the CDN root and drop env.S3_BUCKET.

Suggested fix
 export function createImageUrl(path: string): string {
-	const s3PublicUrl = `${env.S3_PUBLIC_URL}/${env.S3_BUCKET}/`;
-	return new URL(path, s3PublicUrl).toString();
+	const baseUrl = `${env.S3_PUBLIC_URL.replace(/\/+$/, "")}/${env.S3_BUCKET.replace(
+		/^\/+|\/+$/g,
+		"",
+	)}/`;
+	const normalizedPath = path.replace(/^\/+/, "");
+	return new URL(normalizedPath, baseUrl).toString();
 }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@apps/api/src/utils.ts` around lines 9 - 10, The s3PublicUrl and path must be
normalized before calling new URL to avoid losing the bucket when
env.S3_PUBLIC_URL has a trailing slash or path has a leading slash; trim
trailing slashes from env.S3_PUBLIC_URL and trim leading slashes from path (or
build a base as `${trimmedPublicUrl}/${trimmedBucket}/`) then call new
URL(cleanPath, cleanBase). Use the existing s3PublicUrl, env.S3_PUBLIC_URL,
env.S3_BUCKET and the path variable names to locate and update the construction
logic around the new URL(...) call.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Duplicate comments:
In `@apps/api/src/utils.ts`:
- Around line 9-10: The s3PublicUrl and path must be normalized before calling
new URL to avoid losing the bucket when env.S3_PUBLIC_URL has a trailing slash
or path has a leading slash; trim trailing slashes from env.S3_PUBLIC_URL and
trim leading slashes from path (or build a base as
`${trimmedPublicUrl}/${trimmedBucket}/`) then call new URL(cleanPath,
cleanBase). Use the existing s3PublicUrl, env.S3_PUBLIC_URL, env.S3_BUCKET and
the path variable names to locate and update the construction logic around the
new URL(...) call.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 963512ec-07a4-45bc-a53a-c018eec37c0b

📥 Commits

Reviewing files that changed from the base of the PR and between 6472efe and fa23e73.

📒 Files selected for processing (1)
  • apps/api/src/utils.ts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant