-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(asset-server-plugin): Allow assetUrlPrefix to be a function
Closes #766
- Loading branch information
1 parent
56bcbff
commit 10eb014
Showing
4 changed files
with
33 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { REQUEST_CONTEXT_KEY } from '@vendure/core/dist/common/constants'; | ||
import { Request } from 'express'; | ||
|
||
import { AssetServerOptions } from './types'; | ||
|
||
export function getAssetUrlPrefixFn(options: AssetServerOptions) { | ||
const { assetUrlPrefix, route } = options; | ||
if (assetUrlPrefix == null) { | ||
return (request: Request, identifier: string) => | ||
`${request.protocol}://${request.get('host')}/${route}/`; | ||
} | ||
if (typeof assetUrlPrefix === 'string') { | ||
return (...args: any[]) => assetUrlPrefix; | ||
} | ||
if (typeof assetUrlPrefix === 'function') { | ||
return (request: Request, identifier: string) => { | ||
const ctx = (request as any)[REQUEST_CONTEXT_KEY]; | ||
return assetUrlPrefix(ctx, identifier); | ||
}; | ||
} | ||
throw new Error(`The assetUrlPrefix option was of an unexpected type: ${JSON.stringify(assetUrlPrefix)}`); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters