feat: Development server for core + plugins (#2448)

This commit is contained in:
Carlos R. L. Rodrigues
2022-10-21 10:53:06 -03:00
committed by GitHub
parent 4de4f20b46
commit b88cef2b1f
23 changed files with 762 additions and 14 deletions

View File

@@ -0,0 +1,96 @@
import { AbstractFileService } from "@medusajs/medusa"
import stream from "stream"
import { resolve } from "path"
import * as fs from "fs"
import mkdirp from "mkdirp"
export default class LocalFileService extends AbstractFileService {
constructor({}, options) {
super({}, options)
this.upload_dir_ =
process.env.UPLOAD_DIR ?? options.upload_dir ?? "uploads/images"
if (!fs.existsSync(this.upload_dir_)) {
fs.mkdirSync(this.upload_dir_)
}
}
upload(file) {
return new Promise((resolvePromise, reject) => {
const path = resolve(this.upload_dir_, file.originalname)
let content = ""
if (file.filename) {
content = fs.readFileSync(
resolve(process.cwd(), "uploads", file.filename)
)
}
const pathSegments = path.split("/")
pathSegments.splice(-1)
const dirname = pathSegments.join("/")
mkdirp.sync(dirname, { recursive: true })
fs.writeFile(path, content.toString(), (err) => {
if (err) {
reject(err)
}
resolvePromise({ url: path })
})
})
}
delete({ fileKey }) {
return new Promise((resolvePromise, reject) => {
const path = resolve(this.upload_dir_, fileKey)
fs.unlink(path, (err) => {
if (err) {
reject(err)
}
resolvePromise("file unlinked")
})
})
}
async getUploadStreamDescriptor({ name, ext }) {
const fileKey = `${name}.${ext}`
const path = resolve(this.upload_dir_, fileKey)
const isFileExists = fs.existsSync(path)
if (!isFileExists) {
await this.upload({ originalname: fileKey })
}
const pass = new stream.PassThrough()
pass.pipe(fs.createWriteStream(path))
return {
writeStream: pass,
promise: Promise.resolve(),
url: `${this.upload_dir_}/${fileKey}`,
fileKey,
}
}
async getDownloadStream({ fileKey }) {
return new Promise((resolvePromise, reject) => {
try {
const path = resolve(this.upload_dir_, fileKey)
const data = fs.readFileSync(path)
const readable = stream.Readable()
readable._read = function () {}
readable.push(data.toString())
readable.push(null)
resolvePromise(readable)
} catch (e) {
reject(e)
}
})
}
async getPresignedDownloadUrl({ fileKey }) {
return `${this.upload_dir_}/${fileKey}`
}
}

View File

@@ -0,0 +1,53 @@
import { FulfillmentService } from "medusa-interfaces"
class TestFulService extends FulfillmentService {
static identifier = "test-ful"
constructor() {
super()
}
getFulfillmentOptions() {
return [
{
id: "manual-fulfillment",
},
]
}
validateFulfillmentData(data, cart) {
return data
}
validateOption(data) {
return true
}
canCalculate() {
return true
}
calculatePrice(data) {
return data.price
}
createOrder() {
// No data is being sent anywhere
return Promise.resolve({})
}
createReturn() {
return Promise.resolve({})
}
createFulfillment() {
// No data is being sent anywhere
return Promise.resolve({})
}
cancelFulfillment() {
return Promise.resolve({})
}
}
export default TestFulService

View File

@@ -0,0 +1,19 @@
import { NotificationService } from "medusa-interfaces"
class TestNotiService extends NotificationService {
static identifier = "test-not"
constructor() {
super()
}
async sendNotification() {
return Promise.resolve()
}
async resendNotification() {
return Promise.resolve()
}
}
export default TestNotiService

View File

@@ -0,0 +1,87 @@
import { AbstractPaymentService } from "@medusajs/medusa"
class TestPayService extends AbstractPaymentService {
static identifier = "test-pay"
constructor(_) {
super(_)
}
async getStatus(paymentData) {
return "authorized"
}
async retrieveSavedMethods(customer) {
return Promise.resolve([])
}
async createPayment(cart) {
const fields = [
"total",
"subtotal",
"tax_total",
"discount_total",
"shipping_total",
"gift_card_total",
]
const data = {}
for (const k of fields) {
data[k] = cart[k]
}
return data
}
async createPaymentNew(inputData) {
return inputData
}
async retrievePayment(data) {
return {}
}
async getPaymentData(sessionData) {
return {}
}
async authorizePayment(sessionData, context = {}) {
if (
sessionData.cart_id === "cart-id-tax-line-testing-for-pending-payment"
) {
return { data: {}, status: "pending" }
}
return { data: {}, status: "authorized" }
}
async updatePaymentData(sessionData, update) {
return {}
}
async updatePayment(sessionData, cart) {
return {}
}
async updatePaymentNew(sessionData) {
return sessionData
}
async deletePayment(payment) {
return {}
}
async capturePayment(payment) {
return {}
}
async refundPayment(payment, amountToRefund) {
return {}
}
async cancelPayment(payment) {
return {}
}
}
export default TestPayService