feat: Add support for providers to validate their options at loading time (#8853)

* feat: Add support for providers to validate their options at loading time

* fix missing removal

* fix integration tests

* add tests
This commit is contained in:
Adrien de Peretti
2024-08-29 09:08:49 +02:00
committed by GitHub
parent b8572165cb
commit 77b874f272
11 changed files with 281 additions and 205 deletions
@@ -2,46 +2,46 @@ import { FileTypes, IFileProvider } from "@medusajs/types"
/**
* ### constructor
*
*
* The constructor allows you to access resources from the module's container using the first parameter,
* and the module's options using the second parameter.
*
*
* If you're creating a client or establishing a connection with a third-party service, do it in the constructor.
*
*
* #### Example
*
*
* ```ts
* import { Logger } from "@medusajs/types"
* import { AbstractFileProviderService } from "@medusajs/utils"
*
*
* type InjectedDependencies = {
* logger: Logger
* }
*
*
* type Options = {
* apiKey: string
* }
*
*
* class MyFileProviderService extends AbstractFileProviderService {
* protected logger_: Logger
* protected options_: Options
* // assuming you're initializing a client
* protected client
*
*
* constructor (
* { logger }: InjectedDependencies,
* options: Options
* ) {
* super()
*
*
* this.logger_ = logger
* this.options_ = options
*
*
* // assuming you're initializing a client
* this.client = new Client(options)
* }
* }
*
*
* export default MyFileProviderService
* ```
*/
@@ -51,6 +51,12 @@ export class AbstractFileProviderService implements IFileProvider {
*/
static identifier: string
/**
* Override this static method in order for the loader to validate the options provided to the module provider.
* @param options
*/
static validateOptions(options: Record<any, any>): void | never {}
/**
* @ignore
*/
@@ -60,10 +66,10 @@ export class AbstractFileProviderService implements IFileProvider {
/**
* This method uploads a file using your provider's custom logic.
*
*
* @param {FileTypes.ProviderUploadFileDTO} file - The file to upload
* @returns {Promise<FileTypes.ProviderFileResultDTO>} The uploaded file's details.
*
*
* @example
* class MyFileProviderService extends AbstractFileProviderService {
* // ...
@@ -72,7 +78,7 @@ export class AbstractFileProviderService implements IFileProvider {
* ): Promise<ProviderFileResultDTO> {
* // TODO upload file to third-party provider
* // or using custom logic
*
*
* return {
* url: "some-url.com",
* key: "file-name"
@@ -88,10 +94,10 @@ export class AbstractFileProviderService implements IFileProvider {
/**
* This method deletes the file from storage.
*
*
* @param {FileTypes.ProviderDeleteFileDTO} file - The details of the file to delete.
* @returns {Promise<void>} Resolves when the file is deleted.
*
*
* @example
* class MyFileProviderService extends AbstractFileProviderService {
* // ...
@@ -108,16 +114,16 @@ export class AbstractFileProviderService implements IFileProvider {
}
/**
* This method is used to retrieve a download URL of the file. For some providers,
* This method is used to retrieve a download URL of the file. For some providers,
* such as S3, a presigned URL indicates a temporary URL to get access to a file.
*
* If your provider doesnt perform or offer a similar functionality, you can
*
* If your provider doesnt perform or offer a similar functionality, you can
* return the URL to download the file.
*
* @param {FileTypes.ProviderGetFileDTO} fileData - The details of the file to get its
*
* @param {FileTypes.ProviderGetFileDTO} fileData - The details of the file to get its
* presigned URL.
* @returns {Promise<string>} The file's presigned URL.
*
*
* @example
* class MyFileProviderService extends AbstractFileProviderService {
* // ...