types.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 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 AssetServerPlugin
  35. */
  36. export interface ImageTransformPreset {
  37. name: string;
  38. width: number;
  39. height: number;
  40. mode: ImageTransformMode;
  41. }
  42. /**
  43. * @description
  44. * The configuration options for the AssetServerPlugin.
  45. *
  46. * @docsCategory AssetServerPlugin
  47. */
  48. export interface AssetServerOptions {
  49. /**
  50. * @description
  51. * The route to the asset server.
  52. */
  53. route: string;
  54. /**
  55. * @description
  56. * The local directory to which assets will be uploaded when using the {@link LocalAssetStorageStrategy}.
  57. */
  58. assetUploadDir: string; // TODO: this is strategy-specific and should be moved out of the global options
  59. /**
  60. * @description
  61. * The complete URL prefix of the asset files. For example, "https://demo.vendure.io/assets/". A
  62. * function can also be provided to handle more complex cases, such as serving multiple domains
  63. * from a single server. In this case, the function should return a string url prefix.
  64. *
  65. * If not provided, the plugin will attempt to guess based off the incoming
  66. * request and the configured route. However, in all but the simplest cases,
  67. * this guess may not yield correct results.
  68. */
  69. assetUrlPrefix?: string | ((ctx: RequestContext, identifier: string) => string);
  70. /**
  71. * @description
  72. * The max width in pixels of a generated preview image.
  73. *
  74. * @default 1600
  75. * @deprecated Use `previewStrategy: new SharpAssetPreviewStrategy({ maxWidth })` instead
  76. */
  77. previewMaxWidth?: number;
  78. /**
  79. * @description
  80. * The max height in pixels of a generated preview image.
  81. *
  82. * @default 1600
  83. * @deprecated Use `previewStrategy: new SharpAssetPreviewStrategy({ maxHeight })` instead
  84. */
  85. previewMaxHeight?: number;
  86. /**
  87. * @description
  88. * An array of additional {@link ImageTransformPreset} objects.
  89. */
  90. presets?: ImageTransformPreset[];
  91. /**
  92. * @description
  93. * Defines how asset files and preview images are named before being saved.
  94. *
  95. * @default HashedAssetNamingStrategy
  96. */
  97. namingStrategy?: AssetNamingStrategy;
  98. /**
  99. * @description
  100. * Defines how previews are generated for a given Asset binary. By default, this uses
  101. * the {@link SharpAssetPreviewStrategy}
  102. *
  103. * @since 1.7.0
  104. */
  105. previewStrategy?: AssetPreviewStrategy;
  106. /**
  107. * @description
  108. * A function which can be used to configure an {@link AssetStorageStrategy}. This is useful e.g. if you wish to store your assets
  109. * using a cloud storage provider. By default, the {@link LocalAssetStorageStrategy} is used.
  110. *
  111. * @default () => LocalAssetStorageStrategy
  112. */
  113. storageStrategyFactory?: (
  114. options: AssetServerOptions,
  115. ) => AssetStorageStrategy | Promise<AssetStorageStrategy>;
  116. }