-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add boilerplate for the file module
- Loading branch information
Showing
35 changed files
with
882 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from "./steps" | ||
export * from "./workflows" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { ModuleRegistrationName } from "@medusajs/modules-sdk" | ||
import { IFileModuleService } from "@medusajs/types" | ||
import { StepResponse, createStep } from "@medusajs/workflows-sdk" | ||
|
||
export const deleteFilesStepId = "delete-files" | ||
export const deleteFilesStep = createStep( | ||
{ name: deleteFilesStepId, noCompensation: true }, | ||
async (ids: string[], { container }) => { | ||
const service = container.resolve<IFileModuleService>( | ||
ModuleRegistrationName.FILE | ||
) | ||
|
||
await service.delete(ids) | ||
return new StepResponse(void 0) | ||
}, | ||
async () => {} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from "./upload-files" | ||
export * from "./delete-files" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { ModuleRegistrationName } from "@medusajs/modules-sdk" | ||
import { IFileModuleService } from "@medusajs/types" | ||
import { StepResponse, createStep } from "@medusajs/workflows-sdk" | ||
|
||
type UploadFilesStepInput = { | ||
files: { | ||
filename: string | ||
mimeType: string | ||
content: Blob | ||
}[] | ||
} | ||
|
||
export const uploadFilesStepId = "upload-files" | ||
export const uploadFilesStep = createStep( | ||
uploadFilesStepId, | ||
async (data: UploadFilesStepInput, { container }) => { | ||
const service = container.resolve<IFileModuleService>( | ||
ModuleRegistrationName.FILE | ||
) | ||
const created = await service.create(data.files) | ||
return new StepResponse( | ||
created, | ||
created.map((file) => file.id) | ||
) | ||
}, | ||
async (createdIds, { container }) => { | ||
if (!createdIds?.length) { | ||
return | ||
} | ||
|
||
const service = container.resolve<IFileModuleService>( | ||
ModuleRegistrationName.FILE | ||
) | ||
|
||
await service.delete(createdIds) | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk" | ||
import { deleteFilesStep } from "../steps" | ||
|
||
type WorkflowInput = { ids: string[] } | ||
|
||
export const deleteFilesWorkflowId = "delete-files" | ||
export const deleteFilesWorkflow = createWorkflow( | ||
deleteFilesWorkflowId, | ||
(input: WorkflowData<WorkflowInput>): WorkflowData<void> => { | ||
deleteFilesStep(input.ids) | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from "./upload-files" | ||
export * from "./delete-files" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { FileDTO } from "@medusajs/types" | ||
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk" | ||
import { uploadFilesStep } from "../steps" | ||
|
||
type WorkflowInput = { | ||
files: { | ||
filename: string | ||
mimeType: string | ||
content: Blob | ||
}[] | ||
} | ||
|
||
export const uploadFilesWorkflowId = "upload-files" | ||
export const uploadFilesWorkflow = createWorkflow( | ||
uploadFilesWorkflowId, | ||
(input: WorkflowData<WorkflowInput>): WorkflowData<FileDTO[]> => { | ||
return uploadFilesStep(input) | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
/dist | ||
node_modules | ||
.DS_store | ||
.env* | ||
.env | ||
*.sql |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# File Module |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
module.exports = { | ||
moduleNameMapper: { | ||
"^@models": "<rootDir>/src/models", | ||
"^@services": "<rootDir>/src/services", | ||
"^@repositories": "<rootDir>/src/repositories", | ||
"^@types": "<rootDir>/src/types", | ||
}, | ||
transform: { | ||
"^.+\\.[jt]s?$": [ | ||
"ts-jest", | ||
{ | ||
tsconfig: "tsconfig.spec.json", | ||
isolatedModules: true, | ||
}, | ||
], | ||
}, | ||
testEnvironment: `node`, | ||
moduleFileExtensions: [`js`, `ts`], | ||
modulePathIgnorePatterns: ["dist/"], | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
{ | ||
"name": "@medusajs/file", | ||
"version": "0.0.1", | ||
"description": "Medusa File module", | ||
"main": "dist/index.js", | ||
"types": "dist/index.d.ts", | ||
"files": [ | ||
"dist" | ||
], | ||
"engines": { | ||
"node": ">=16" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/medusajs/medusa", | ||
"directory": "packages/file" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"author": "Medusa", | ||
"license": "MIT", | ||
"scripts": { | ||
"watch": "tsc --build --watch", | ||
"watch:test": "tsc --build tsconfig.spec.json --watch", | ||
"prepublishOnly": "cross-env NODE_ENV=production tsc --build && tsc-alias -p tsconfig.json", | ||
"build": "rimraf dist && tsc --build && tsc-alias -p tsconfig.json", | ||
"test": "jest --passWithNoTests --runInBand --bail --forceExit -- src/**/__tests__/**/*.ts", | ||
"test:integration": "jest --passWithNoTests --forceExit -- integration-tests/**/__tests__/**/*.ts", | ||
"migration:generate": " MIKRO_ORM_CLI=./mikro-orm.config.dev.ts mikro-orm migration:generate", | ||
"migration:initial": " MIKRO_ORM_CLI=./mikro-orm.config.dev.ts mikro-orm migration:create --initial", | ||
"migration:create": " MIKRO_ORM_CLI=./mikro-orm.config.dev.ts mikro-orm migration:create", | ||
"migration:up": " MIKRO_ORM_CLI=./mikro-orm.config.dev.ts mikro-orm migration:up", | ||
"orm:cache:clear": " MIKRO_ORM_CLI=./mikro-orm.config.dev.ts mikro-orm cache:clear" | ||
}, | ||
"devDependencies": { | ||
"@mikro-orm/cli": "5.9.7", | ||
"cross-env": "^5.2.1", | ||
"faker": "^6.6.6", | ||
"jest": "^29.6.3", | ||
"medusa-test-utils": "^1.1.43", | ||
"pg-god": "^1.0.12", | ||
"rimraf": "^3.0.2", | ||
"ts-jest": "^29.1.1", | ||
"ts-node": "^10.9.1", | ||
"tsc-alias": "^1.8.6", | ||
"typescript": "^5.1.6" | ||
}, | ||
"dependencies": { | ||
"@medusajs/modules-sdk": "^1.12.10", | ||
"@medusajs/types": "^1.11.15", | ||
"@medusajs/utils": "^1.11.8", | ||
"@mikro-orm/core": "5.9.7", | ||
"@mikro-orm/migrations": "5.9.7", | ||
"@mikro-orm/postgresql": "5.9.7", | ||
"awilix": "^8.0.0", | ||
"dotenv": "^16.4.5", | ||
"knex": "2.4.2", | ||
"lodash": "^4.17.21" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { | ||
moduleDefinition, | ||
revertMigration, | ||
runMigrations, | ||
} from "./module-definition" | ||
|
||
export default moduleDefinition | ||
export { revertMigration, runMigrations } | ||
|
||
export * from "./services" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { Modules } from "@medusajs/modules-sdk" | ||
import { ModuleJoinerConfig } from "@medusajs/types" | ||
import { MapToConfig } from "@medusajs/utils" | ||
|
||
export const LinkableKeys = {} | ||
|
||
const entityLinkableKeysMap: MapToConfig = {} | ||
export const entityNameToLinkableKeysMap: MapToConfig = entityLinkableKeysMap | ||
|
||
export const joinerConfig: ModuleJoinerConfig = { | ||
serviceName: Modules.FILE, | ||
primaryKeys: ["id"], | ||
linkableKeys: LinkableKeys, | ||
alias: [ | ||
{ | ||
name: ["file", "files"], | ||
args: { | ||
entity: "File", | ||
}, | ||
}, | ||
], | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { ModuleExports } from "@medusajs/types" | ||
import { FileModuleService } from "@services" | ||
export const runMigrations = () => { | ||
return Promise.resolve() | ||
} | ||
export const revertMigration = () => { | ||
return Promise.resolve() | ||
} | ||
|
||
const service = FileModuleService | ||
const loaders = [] as any | ||
|
||
export const moduleDefinition: ModuleExports = { | ||
service, | ||
loaders, | ||
runMigrations, | ||
revertMigration, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
describe("File service", function () { | ||
it("noop", async function () {}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { | ||
Context, | ||
CreateFileDTO, | ||
FileDTO, | ||
ModuleJoinerConfig, | ||
} from "@medusajs/types" | ||
|
||
import { joinerConfig } from "../joiner-config" | ||
|
||
export default class FileModuleService { | ||
constructor() { | ||
// @ts-ignore | ||
// eslint-disable-next-line prefer-rest-params | ||
super(...arguments) | ||
} | ||
|
||
__joinerConfig(): ModuleJoinerConfig { | ||
return joinerConfig | ||
} | ||
|
||
create(data: CreateFileDTO[], sharedContext?: Context): Promise<FileDTO[]> | ||
create(data: CreateFileDTO, sharedContext?: Context): Promise<FileDTO> | ||
|
||
async create( | ||
data: CreateFileDTO[] | CreateFileDTO | ||
): Promise<FileDTO[] | FileDTO> { | ||
const input = Array.isArray(data) ? data : [data] | ||
const files = [] | ||
return Array.isArray(data) ? files : files[0] | ||
} | ||
|
||
async delete(ids: string[], sharedContext?: Context): Promise<void> | ||
async delete(id: string, sharedContext?: Context): Promise<void> | ||
async delete(ids: string[] | string): Promise<void> { | ||
return | ||
} | ||
|
||
async retrieve(id: string): Promise<FileDTO> | ||
async retrieve(id: string): Promise<FileDTO> { | ||
return {} as FileDTO | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default as FileModuleService } from "./file-module-service" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
{ | ||
"compilerOptions": { | ||
"lib": ["es2020"], | ||
"target": "es2020", | ||
"outDir": "./dist", | ||
"esModuleInterop": true, | ||
"declaration": true, | ||
"module": "commonjs", | ||
"moduleResolution": "node", | ||
"emitDecoratorMetadata": true, | ||
"experimentalDecorators": true, | ||
"sourceMap": false, | ||
"noImplicitReturns": true, | ||
"strictNullChecks": true, | ||
"strictFunctionTypes": true, | ||
"noImplicitThis": true, | ||
"allowJs": true, | ||
"skipLibCheck": true, | ||
"downlevelIteration": true, // to use ES5 specific tooling | ||
"baseUrl": ".", | ||
"resolveJsonModule": true, | ||
"paths": { | ||
"@models": ["./src/models"], | ||
"@services": ["./src/services"], | ||
"@repositories": ["./src/repositories"], | ||
"@types": ["./src/types"] | ||
} | ||
}, | ||
"include": ["src"], | ||
"exclude": [ | ||
"dist", | ||
"./src/**/__tests__", | ||
"./src/**/__mocks__", | ||
"./src/**/__fixtures__", | ||
"node_modules" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"extends": "./tsconfig.json", | ||
"include": ["src", "integration-tests"], | ||
"exclude": ["node_modules", "dist"], | ||
"compilerOptions": { | ||
"sourceMap": true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { | ||
AuthenticatedMedusaRequest, | ||
MedusaResponse, | ||
} from "../../../../types/routing" | ||
import { deleteFilesWorkflow } from "@medusajs/core-flows" | ||
import { remoteQueryObjectFromString } from "@medusajs/utils" | ||
|
||
export const GET = async ( | ||
req: AuthenticatedMedusaRequest, | ||
res: MedusaResponse | ||
) => { | ||
const remoteQuery = req.scope.resolve("remoteQuery") | ||
const variables = { id: req.params.id } | ||
|
||
const queryObject = remoteQueryObjectFromString({ | ||
entryPoint: "file", | ||
variables, | ||
fields: req.remoteQueryConfig.fields, | ||
}) | ||
|
||
const [file] = await remoteQuery(queryObject) | ||
res.status(200).json({ file }) | ||
} | ||
|
||
export const DELETE = async ( | ||
req: AuthenticatedMedusaRequest, | ||
res: MedusaResponse | ||
) => { | ||
const id = req.params.id | ||
|
||
const { errors } = await deleteFilesWorkflow(req.scope).run({ | ||
input: { ids: [id] }, | ||
throwOnError: false, | ||
}) | ||
|
||
if (Array.isArray(errors) && errors[0]) { | ||
throw errors[0].error | ||
} | ||
|
||
res.status(200).json({ | ||
id, | ||
object: "file", | ||
deleted: true, | ||
}) | ||
} |
Oops, something went wrong.