types.ts 5.0 KB

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