types.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. import {
  2. AssetNamingStrategy,
  3. AssetPreviewStrategy,
  4. AssetStorageStrategy,
  5. RequestContext,
  6. } from '@vendure/core';
  7. import { ImageTransformStrategy } from './config/image-transform-strategy';
  8. export type ImageTransformFormat = 'jpg' | 'jpeg' | 'png' | 'webp' | 'avif';
  9. /**
  10. * @description
  11. * Specifies the way in which an asset preview image will be resized to fit in the
  12. * proscribed dimensions:
  13. *
  14. * * crop: crops the image to cover both provided dimensions
  15. * * resize: Preserving aspect ratio, resizes the image to be as large as possible
  16. * while ensuring its dimensions are less than or equal to both those specified.
  17. *
  18. * @docsCategory core plugins/AssetServerPlugin
  19. */
  20. export type ImageTransformMode = 'crop' | 'resize';
  21. /**
  22. * @description
  23. * A configuration option for an image size preset for the AssetServerPlugin.
  24. *
  25. * Presets allow a shorthand way to generate a thumbnail preview of an asset. For example,
  26. * the built-in "tiny" preset generates a 50px x 50px cropped preview, which can be accessed
  27. * by appending the string `preset=tiny` to the asset url:
  28. *
  29. * `http://localhost:3000/assets/some-asset.jpg?preset=tiny`
  30. *
  31. * is equivalent to:
  32. *
  33. * `http://localhost:3000/assets/some-asset.jpg?w=50&h=50&mode=crop`
  34. *
  35. * @docsCategory core plugins/AssetServerPlugin
  36. */
  37. export interface ImageTransformPreset {
  38. name: string;
  39. width: number;
  40. height: number;
  41. mode: ImageTransformMode;
  42. }
  43. /**
  44. * @description
  45. * A configuration option for the Cache-Control header in the AssetServerPlugin asset response.
  46. *
  47. * @docsCategory core plugins/AssetServerPlugin
  48. */
  49. export type CacheConfig = {
  50. /**
  51. * @description
  52. * The max-age=N response directive indicates that the response remains fresh until N seconds after the response is generated.
  53. */
  54. maxAge: number;
  55. /**
  56. * @description
  57. * The `private` response directive indicates that the response can be stored only in a private cache (e.g. local caches in browsers).
  58. * The `public` response directive indicates that the response can be stored in a shared cache.
  59. */
  60. restriction?: 'public' | 'private';
  61. };
  62. /**
  63. * @description
  64. * The configuration options for the AssetServerPlugin.
  65. *
  66. * @docsCategory core plugins/AssetServerPlugin
  67. */
  68. export interface AssetServerOptions {
  69. /**
  70. * @description
  71. * The route to the asset server.
  72. */
  73. route: string;
  74. /**
  75. * @description
  76. * The local directory to which assets will be uploaded when using the {@link LocalAssetStorageStrategy}.
  77. */
  78. assetUploadDir: string; // TODO: this is strategy-specific and should be moved out of the global options
  79. /**
  80. * @description
  81. * The complete URL prefix of the asset files. For example, "https://demo.vendure.io/assets/". A
  82. * function can also be provided to handle more complex cases, such as serving multiple domains
  83. * from a single server. In this case, the function should return a string url prefix.
  84. *
  85. * If not provided, the plugin will attempt to guess based off the incoming
  86. * request and the configured route. However, in all but the simplest cases,
  87. * this guess may not yield correct results.
  88. */
  89. assetUrlPrefix?: string | ((ctx: RequestContext, identifier: string) => string);
  90. /**
  91. * @description
  92. * The max width in pixels of a generated preview image.
  93. *
  94. * @default 1600
  95. * @deprecated Use `previewStrategy: new SharpAssetPreviewStrategy({ maxWidth })` instead
  96. */
  97. previewMaxWidth?: number;
  98. /**
  99. * @description
  100. * The max height in pixels of a generated preview image.
  101. *
  102. * @default 1600
  103. * @deprecated Use `previewStrategy: new SharpAssetPreviewStrategy({ maxHeight })` instead
  104. */
  105. previewMaxHeight?: number;
  106. /**
  107. * @description
  108. * An array of additional {@link ImageTransformPreset} objects.
  109. */
  110. presets?: ImageTransformPreset[];
  111. /**
  112. * @description
  113. * The strategy or strategies to use to determine the parameters for transforming an image.
  114. * This can be used to implement custom image transformation logic, for example to
  115. * limit transform parameters to a known set of presets.
  116. *
  117. * If multiple strategies are provided, they will be executed in the order in which they are defined.
  118. * If a strategy throws an error, the image transformation will be aborted and the error
  119. * will be logged, with an HTTP 400 response sent to the client.
  120. *
  121. * @since 3.1.0
  122. * @default []
  123. */
  124. imageTransformStrategy?: ImageTransformStrategy | ImageTransformStrategy[];
  125. /**
  126. * @description
  127. * Defines how asset files and preview images are named before being saved.
  128. *
  129. * @default HashedAssetNamingStrategy
  130. */
  131. namingStrategy?: AssetNamingStrategy;
  132. /**
  133. * @description
  134. * Defines how previews are generated for a given Asset binary. By default, this uses
  135. * the {@link SharpAssetPreviewStrategy}
  136. *
  137. * @since 1.7.0
  138. */
  139. previewStrategy?: AssetPreviewStrategy;
  140. /**
  141. * @description
  142. * A function which can be used to configure an {@link AssetStorageStrategy}. This is useful e.g. if you wish to store your assets
  143. * using a cloud storage provider. By default, the {@link LocalAssetStorageStrategy} is used.
  144. *
  145. * @default () => LocalAssetStorageStrategy
  146. */
  147. storageStrategyFactory?: (
  148. options: AssetServerOptions,
  149. ) => AssetStorageStrategy | Promise<AssetStorageStrategy>;
  150. /**
  151. * @description
  152. * Configures the `Cache-Control` directive for response to control caching in browsers and shared caches (e.g. Proxies, CDNs).
  153. * Defaults to publicly cached for 6 months.
  154. *
  155. * @default 'public, max-age=15552000'
  156. * @since 1.9.3
  157. */
  158. cacheHeader?: CacheConfig | string;
  159. }