feat: Add support for loading file providers to file module (#7016)
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
import { moduleProviderLoader } from "@medusajs/modules-sdk"
|
||||
import { LoaderOptions, ModuleProvider, ModulesSdkTypes } from "@medusajs/types"
|
||||
import { FileProviderService } from "@services"
|
||||
import { FileProviderIdentifierRegistrationName } from "@types"
|
||||
import { Lifetime, asFunction, asValue } from "awilix"
|
||||
|
||||
const registrationFn = async (klass, container, pluginOptions) => {
|
||||
Object.entries(pluginOptions.config || []).map(([name, config]) => {
|
||||
const key = FileProviderService.getRegistrationIdentifier(klass, name)
|
||||
|
||||
container.register({
|
||||
["file_" + key]: asFunction((cradle) => new klass(cradle, config), {
|
||||
lifetime: klass.LIFE_TIME || Lifetime.SINGLETON,
|
||||
}),
|
||||
})
|
||||
|
||||
container.registerAdd(FileProviderIdentifierRegistrationName, asValue(key))
|
||||
})
|
||||
}
|
||||
|
||||
export default async ({
|
||||
container,
|
||||
options,
|
||||
}: LoaderOptions<
|
||||
(
|
||||
| ModulesSdkTypes.ModuleServiceInitializeOptions
|
||||
| ModulesSdkTypes.ModuleServiceInitializeCustomDataLayerOptions
|
||||
) & { provider: ModuleProvider }
|
||||
>): Promise<void> => {
|
||||
container.registerAdd(
|
||||
FileProviderIdentifierRegistrationName,
|
||||
asValue(undefined)
|
||||
)
|
||||
|
||||
await moduleProviderLoader({
|
||||
container,
|
||||
providers: options?.provider ? [options?.provider] : [],
|
||||
registerServiceFn: registrationFn,
|
||||
})
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
import { ModuleExports } from "@medusajs/types"
|
||||
import { FileModuleService } from "@services"
|
||||
import loadProviders from "./loaders/providers"
|
||||
|
||||
export const runMigrations = () => {
|
||||
return Promise.resolve()
|
||||
}
|
||||
@@ -8,7 +10,7 @@ export const revertMigration = () => {
|
||||
}
|
||||
|
||||
const service = FileModuleService
|
||||
const loaders = [] as any
|
||||
const loaders = [loadProviders] as any
|
||||
|
||||
export const moduleDefinition: ModuleExports = {
|
||||
service,
|
||||
|
||||
@@ -6,12 +6,16 @@ import {
|
||||
} from "@medusajs/types"
|
||||
|
||||
import { joinerConfig } from "../joiner-config"
|
||||
import FileProviderService from "./file-provider-service"
|
||||
|
||||
type InjectedDependencies = {
|
||||
fileProviderService: FileProviderService
|
||||
}
|
||||
|
||||
export default class FileModuleService {
|
||||
constructor() {
|
||||
// @ts-ignore
|
||||
// eslint-disable-next-line prefer-rest-params
|
||||
super(...arguments)
|
||||
protected readonly fileProviderService_: FileProviderService
|
||||
constructor({ fileProviderService }: InjectedDependencies) {
|
||||
this.fileProviderService_ = fileProviderService
|
||||
}
|
||||
|
||||
__joinerConfig(): ModuleJoinerConfig {
|
||||
@@ -25,18 +29,37 @@ export default class FileModuleService {
|
||||
data: CreateFileDTO[] | CreateFileDTO
|
||||
): Promise<FileDTO[] | FileDTO> {
|
||||
const input = Array.isArray(data) ? data : [data]
|
||||
const files = []
|
||||
return Array.isArray(data) ? files : files[0]
|
||||
const files = await Promise.all(
|
||||
input.map((file) => this.fileProviderService_.upload(file))
|
||||
)
|
||||
const result = files.map((file) => ({
|
||||
id: file.key,
|
||||
url: file.url,
|
||||
}))
|
||||
|
||||
return Array.isArray(data) ? result : result[0]
|
||||
}
|
||||
|
||||
async delete(ids: string[], sharedContext?: Context): Promise<void>
|
||||
async delete(id: string, sharedContext?: Context): Promise<void>
|
||||
async delete(ids: string[] | string): Promise<void> {
|
||||
const input = Array.isArray(ids) ? ids : [ids]
|
||||
await Promise.all(
|
||||
input.map((id) => this.fileProviderService_.delete({ fileKey: id }))
|
||||
)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
async retrieve(id: string): Promise<FileDTO>
|
||||
async retrieve(id: string): Promise<FileDTO> {
|
||||
return {} as FileDTO
|
||||
const res = await this.fileProviderService_.getPresignedDownloadUrl({
|
||||
fileKey: id,
|
||||
})
|
||||
|
||||
return {
|
||||
id,
|
||||
url: res,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
import { Constructor, DAL, FileTypes } from "@medusajs/types"
|
||||
import { MedusaError } from "medusa-core-utils"
|
||||
|
||||
type InjectedDependencies = {
|
||||
[key: `file_${string}`]: FileTypes.IFileProvider
|
||||
}
|
||||
|
||||
export default class FileProviderService {
|
||||
protected readonly fileProvider_: FileTypes.IFileProvider
|
||||
|
||||
constructor(container: InjectedDependencies) {
|
||||
if (Object.keys(container).length !== 1) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.INVALID_DATA,
|
||||
`File module should only be initialized with one provider`
|
||||
)
|
||||
}
|
||||
|
||||
this.fileProvider_ = Object.values(container)[0]
|
||||
}
|
||||
|
||||
static getRegistrationIdentifier(
|
||||
providerClass: Constructor<FileTypes.IFileProvider>,
|
||||
optionName?: string
|
||||
) {
|
||||
return `${(providerClass as any).identifier}_${optionName}`
|
||||
}
|
||||
|
||||
upload(
|
||||
file: FileTypes.ProviderUploadFileDTO
|
||||
): Promise<FileTypes.ProviderFileResultDTO> {
|
||||
return this.fileProvider_.upload(file)
|
||||
}
|
||||
|
||||
delete(fileData: FileTypes.ProviderDeleteFileDTO): Promise<void> {
|
||||
return this.fileProvider_.delete(fileData)
|
||||
}
|
||||
|
||||
getPresignedDownloadUrl(
|
||||
fileData: FileTypes.ProviderGetFileDTO
|
||||
): Promise<string> {
|
||||
return this.fileProvider_.getPresignedDownloadUrl(fileData)
|
||||
}
|
||||
}
|
||||
@@ -1 +1,2 @@
|
||||
export { default as FileModuleService } from "./file-module-service"
|
||||
export { default as FileProviderService } from "./file-provider-service"
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
import {
|
||||
ModuleProviderExports,
|
||||
ModuleServiceInitializeOptions,
|
||||
} from "@medusajs/types"
|
||||
|
||||
export const FileProviderIdentifierRegistrationName =
|
||||
"file_providers_identifier"
|
||||
|
||||
export type FileModuleOptions = Partial<ModuleServiceInitializeOptions> & {
|
||||
/**
|
||||
* Providers to be registered
|
||||
*/
|
||||
provider?: {
|
||||
/**
|
||||
* The module provider to be registered
|
||||
*/
|
||||
resolve: string | ModuleProviderExports
|
||||
options: {
|
||||
/**
|
||||
* key value pair of the provider name and the configuration to be passed to the provider constructor
|
||||
*/
|
||||
config: Record<string, unknown>
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user