Skip to content

Commit

Permalink
Support default domain (#16)
Browse files Browse the repository at this point in the history
* feat: support for relative paths

* refactor: comments

* chore: apply changeset

* feat: support for default domain

* refactor: shorten code

* docs: changeset message

---------

Co-authored-by: Phumrapee Limpianchop <[email protected]>
  • Loading branch information
epavanello and rayriffy authored Oct 31, 2023
1 parent 52450b8 commit 9eab2a6
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/nasty-spies-repair.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@urami/core": minor
---

added `defaultDomain` config as a way to define domain to fetch an image as a relative path
6 changes: 6 additions & 0 deletions apps/docs/src/core/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ List of domains that allowed to use the API, this will be checked via header `Re

Only applied when `process.env.NODE_ENV` is set to `production`. Unset this option will allow anywhere to request image from this API.

## defaultDomain

`string | undefined`

Default domain to use when domain is not specified in URL. Defaults to undefined

## ttl

`number`
Expand Down
3 changes: 3 additions & 0 deletions packages/core/src/@types/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ export interface Config {
// lists of allowed domain to use this API (will check via header `Referer`), not specifying anything will allow all domains to use this API
allowedDomains?: string[]

// default domain to use if no domain is specified
defaultDomain?: string

// directory to temporary store optimized images (default to .svelte-kit/images), paths will be relative to process.cwd()
storePath: string

Expand Down
14 changes: 7 additions & 7 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,18 @@ export const createRequestHandler =
const width = Number(fullRequestUrl.searchParams.get('w') ?? '')
const quality = Number(fullRequestUrl.searchParams.get('q') ?? '')

// Check if the URL is valid. If not, try to construct a new URL using defaultDomain or referer header.
try {
// attempt to construct a URL, a relative URL will throw an error
url = new URL(url).toString()
} catch (e) {
// if relative URL does not have a referer header, throw an error
const referer = request.headers.get('referer')
if (referer === null) {
throw error(400, 'missing url')
}
// if default domain is specified, construct a new URL using the default domain
const targetDomain =
mergedConfig.defaultDomain ?? request.headers.get('referer')

if (targetDomain === null) throw error(400, 'missing url')

// construct a new URL using the referer header
url = new URL(url, referer).toString()
url = new URL(url, mergedConfig.defaultDomain).toString()
}

// make sure that this url is allowed to optimize
Expand Down

0 comments on commit 9eab2a6

Please sign in to comment.