types.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import { AssetNamingStrategy, AssetStorageStrategy, RequestContext } from '@vendure/core';
  2. /**
  3. * @description
  4. * Specifies the way in which an asset preview image will be resized to fit in the
  5. * proscribed dimensions:
  6. *
  7. * * crop: crops the image to cover both provided dimensions
  8. * * resize: Preserving aspect ratio, resizes the image to be as large as possible
  9. * while ensuring its dimensions are less than or equal to both those specified.
  10. *
  11. * @docsCategory AssetServerPlugin
  12. */
  13. export type ImageTransformMode = 'crop' | 'resize';
  14. /**
  15. * @description
  16. * A configuration option for an image size preset for the AssetServerPlugin.
  17. *
  18. * Presets allow a shorthand way to generate a thumbnail preview of an asset. For example,
  19. * the built-in "tiny" preset generates a 50px x 50px cropped preview, which can be accessed
  20. * by appending the string `preset=tiny` to the asset url:
  21. *
  22. * `http://localhost:3000/assets/some-asset.jpg?preset=tiny`
  23. *
  24. * is equivalent to:
  25. *
  26. * `http://localhost:3000/assets/some-asset.jpg?w=50&h=50&mode=crop`
  27. *
  28. * @docsCategory AssetServerPlugin
  29. */
  30. export interface ImageTransformPreset {
  31. name: string;
  32. width: number;
  33. height: number;
  34. mode: ImageTransformMode;
  35. }
  36. /**
  37. * @description
  38. * The configuration options for the AssetServerPlugin.
  39. *
  40. * @docsCategory AssetServerPlugin
  41. */
  42. export interface AssetServerOptions {
  43. /**
  44. * @description
  45. * The route to the asset server.
  46. */
  47. route: string;
  48. /**
  49. * @description
  50. * The local directory to which assets will be uploaded when using the {@link LocalAssetStorageStrategy}.
  51. */
  52. assetUploadDir: string; // TODO: this is strategy-specific and should be moved out of the global options
  53. /**
  54. * @description
  55. * The complete URL prefix of the asset files. For example, "https://demo.vendure.io/assets/". A
  56. * function can also be provided to handle more complex cases, such as serving multiple domains
  57. * from a single server. In this case, the function should return a string url prefix.
  58. *
  59. * If not provided, the plugin will attempt to guess based off the incoming
  60. * request and the configured route. However, in all but the simplest cases,
  61. * this guess may not yield correct results.
  62. */
  63. assetUrlPrefix?: string | ((ctx: RequestContext, identifier: string) => string);
  64. /**
  65. * @description
  66. * The max width in pixels of a generated preview image.
  67. *
  68. * @default 1600
  69. */
  70. previewMaxWidth?: number;
  71. /**
  72. * @description
  73. * The max height in pixels of a generated preview image.
  74. *
  75. * @default 1600
  76. */
  77. previewMaxHeight?: number;
  78. /**
  79. * @description
  80. * An array of additional {@link ImageTransformPreset} objects.
  81. */
  82. presets?: ImageTransformPreset[];
  83. /**
  84. * @description
  85. * Defines how asset files and preview images are named before being saved.
  86. *
  87. * @default HashedAssetNamingStrategy
  88. */
  89. namingStrategy?: AssetNamingStrategy;
  90. /**
  91. * @description
  92. * A function which can be used to configure an {@link AssetStorageStrategy}. This is useful e.g. if you wish to store your assets
  93. * using a cloud storage provider. By default, the {@link LocalAssetStorageStrategy} is used.
  94. *
  95. * @default () => LocalAssetStorageStrategy
  96. */
  97. storageStrategyFactory?: (
  98. options: AssetServerOptions,
  99. ) => AssetStorageStrategy | Promise<AssetStorageStrategy>;
  100. }