feat(workflows): Improve typings (#4689)
Just improve some typings for simpler usage
This commit is contained in:
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
"@medusajs/workflows": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
feat(workflows): Improve typings
|
||||||
@@ -1,10 +1,10 @@
|
|||||||
import { InventoryItemDTO, ProductTypes } from "@medusajs/types"
|
import { InventoryItemDTO, ProductTypes } from "@medusajs/types"
|
||||||
|
|
||||||
import { InputAlias } from "../definitions"
|
import { InputAlias } from "../definitions"
|
||||||
import { PipelineHandlerResult, WorkflowArguments } from "../helper"
|
import { WorkflowArguments } from "../helper"
|
||||||
import { ProductVariantInventoryItem } from "@medusajs/medusa/src"
|
import { ProductVariantInventoryItem } from "@medusajs/medusa/src"
|
||||||
|
|
||||||
export async function attachInventoryItems<T = ProductVariantInventoryItem[]>({
|
export async function attachInventoryItems({
|
||||||
container,
|
container,
|
||||||
data,
|
data,
|
||||||
}: WorkflowArguments & {
|
}: WorkflowArguments & {
|
||||||
@@ -12,13 +12,13 @@ export async function attachInventoryItems<T = ProductVariantInventoryItem[]>({
|
|||||||
variant: ProductTypes.ProductVariantDTO
|
variant: ProductTypes.ProductVariantDTO
|
||||||
[InputAlias.InventoryItems]: InventoryItemDTO
|
[InputAlias.InventoryItems]: InventoryItemDTO
|
||||||
}[]
|
}[]
|
||||||
}): Promise<PipelineHandlerResult<T>> {
|
}): Promise<ProductVariantInventoryItem[]> {
|
||||||
const manager = container.resolve("manager")
|
const manager = container.resolve("manager")
|
||||||
const productVariantInventoryService = container
|
const productVariantInventoryService = container
|
||||||
.resolve("productVariantInventoryService")
|
.resolve("productVariantInventoryService")
|
||||||
.withTransaction(manager)
|
.withTransaction(manager)
|
||||||
|
|
||||||
return (await Promise.all(
|
return await Promise.all(
|
||||||
data
|
data
|
||||||
.filter((d) => d)
|
.filter((d) => d)
|
||||||
.map(async ({ variant, [InputAlias.InventoryItems]: inventoryItem }) => {
|
.map(async ({ variant, [InputAlias.InventoryItems]: inventoryItem }) => {
|
||||||
@@ -27,5 +27,5 @@ export async function attachInventoryItems<T = ProductVariantInventoryItem[]>({
|
|||||||
inventoryItem.id
|
inventoryItem.id
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
)) as PipelineHandlerResult<T>
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,18 +5,16 @@ import {
|
|||||||
} from "@medusajs/types"
|
} from "@medusajs/types"
|
||||||
|
|
||||||
import { InputAlias } from "../definitions"
|
import { InputAlias } from "../definitions"
|
||||||
import { PipelineHandlerResult, WorkflowArguments } from "../helper"
|
import { WorkflowArguments } from "../helper"
|
||||||
|
|
||||||
export async function createInventoryItems<
|
export async function createInventoryItems({
|
||||||
T = { variant: any; inventoryItem: InventoryItemDTO }[]
|
|
||||||
>({
|
|
||||||
container,
|
container,
|
||||||
data,
|
data,
|
||||||
}: WorkflowArguments & {
|
}: WorkflowArguments & {
|
||||||
data: {
|
data: {
|
||||||
[InputAlias.Products]: ProductTypes.ProductDTO[]
|
[InputAlias.Products]: ProductTypes.ProductDTO[]
|
||||||
}
|
}
|
||||||
}): Promise<PipelineHandlerResult<T>> {
|
}): Promise<{ variant: any; inventoryItem: InventoryItemDTO }[]> {
|
||||||
const manager = container.resolve("manager")
|
const manager = container.resolve("manager")
|
||||||
const inventoryService: IInventoryService =
|
const inventoryService: IInventoryService =
|
||||||
container.resolve("inventoryService")
|
container.resolve("inventoryService")
|
||||||
@@ -33,7 +31,7 @@ export async function createInventoryItems<
|
|||||||
[]
|
[]
|
||||||
)
|
)
|
||||||
|
|
||||||
return (await Promise.all(
|
return await Promise.all(
|
||||||
variants.map(async (variant) => {
|
variants.map(async (variant) => {
|
||||||
if (!variant.manage_inventory) {
|
if (!variant.manage_inventory) {
|
||||||
return
|
return
|
||||||
@@ -56,5 +54,5 @@ export async function createInventoryItems<
|
|||||||
|
|
||||||
return { variant, inventoryItem }
|
return { variant, inventoryItem }
|
||||||
})
|
})
|
||||||
)) as PipelineHandlerResult<T>
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,14 +1,14 @@
|
|||||||
import { InputAlias } from "../definitions"
|
import { InputAlias } from "../definitions"
|
||||||
import { ProductTypes } from "@medusajs/types"
|
import { ProductTypes } from "@medusajs/types"
|
||||||
import { PipelineHandlerResult, WorkflowArguments } from "../helper"
|
import { WorkflowArguments } from "../helper"
|
||||||
|
|
||||||
export async function createProducts<T = ProductTypes.ProductDTO[]>({
|
export async function createProducts({
|
||||||
container,
|
container,
|
||||||
context,
|
context,
|
||||||
data,
|
data,
|
||||||
}: WorkflowArguments & {
|
}: WorkflowArguments & {
|
||||||
data: { [InputAlias.Products]: ProductTypes.CreateProductDTO[] }
|
data: { [InputAlias.Products]: ProductTypes.CreateProductDTO[] }
|
||||||
}): Promise<PipelineHandlerResult<T>> {
|
}): Promise<ProductTypes.ProductDTO[]> {
|
||||||
const productModuleService = container.resolve("productModuleService")
|
const productModuleService = container.resolve("productModuleService")
|
||||||
|
|
||||||
return await productModuleService.create(data[InputAlias.Products], context)
|
return await productModuleService.create(data[InputAlias.Products], context)
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
import { InputAlias } from "../../definitions"
|
import { InputAlias } from "../../definitions"
|
||||||
import { PipelineHandlerResult } from "../../helper"
|
|
||||||
|
|
||||||
export function createProductsPrepareData({ data }): PipelineHandlerResult {
|
export function createProductsPrepareData({ data }) {
|
||||||
return {
|
return {
|
||||||
alias: InputAlias.Products as string,
|
alias: InputAlias.Products as string,
|
||||||
value: [{}],
|
value: [{}],
|
||||||
|
|||||||
@@ -1,26 +1,26 @@
|
|||||||
import { InventoryItemDTO } from "@medusajs/types"
|
import { InventoryItemDTO } from "@medusajs/types"
|
||||||
|
|
||||||
import { InputAlias } from "../definitions"
|
import { InputAlias } from "../definitions"
|
||||||
import { PipelineHandlerResult, WorkflowArguments } from "../helper"
|
import { WorkflowArguments } from "../helper"
|
||||||
|
|
||||||
export async function removeInventoryItems<T = void[]>({
|
export async function removeInventoryItems({
|
||||||
container,
|
container,
|
||||||
data,
|
data,
|
||||||
}: WorkflowArguments & {
|
}: WorkflowArguments & {
|
||||||
data: {
|
data: {
|
||||||
[InputAlias.InventoryItems]: InventoryItemDTO
|
[InputAlias.InventoryItems]: InventoryItemDTO
|
||||||
}[]
|
}[]
|
||||||
}): Promise<PipelineHandlerResult<T>> {
|
}) {
|
||||||
const manager = container.resolve("manager")
|
const manager = container.resolve("manager")
|
||||||
const inventoryService = container.resolve("inventoryService")
|
const inventoryService = container.resolve("inventoryService")
|
||||||
const context = { transactionManager: manager }
|
const context = { transactionManager: manager }
|
||||||
|
|
||||||
return (await Promise.all(
|
return await Promise.all(
|
||||||
data.map(async ({ [InputAlias.InventoryItems]: inventoryItem }) => {
|
data.map(async ({ [InputAlias.InventoryItems]: inventoryItem }) => {
|
||||||
return await inventoryService!.deleteInventoryItem(
|
return await inventoryService!.deleteInventoryItem(
|
||||||
inventoryItem.id,
|
inventoryItem.id,
|
||||||
context
|
context
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
)) as PipelineHandlerResult<T>
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,15 +1,15 @@
|
|||||||
import { InputAlias } from "../definitions"
|
import { InputAlias } from "../definitions"
|
||||||
import { ProductTypes } from "@medusajs/types"
|
import { ProductTypes } from "@medusajs/types"
|
||||||
import { PipelineHandlerResult, WorkflowArguments } from "../helper"
|
import { WorkflowArguments } from "../helper"
|
||||||
|
|
||||||
export async function removeProducts<T = any>({
|
export async function removeProducts({
|
||||||
container,
|
container,
|
||||||
data,
|
data,
|
||||||
}: WorkflowArguments & {
|
}: WorkflowArguments & {
|
||||||
data: {
|
data: {
|
||||||
[InputAlias.Products]: ProductTypes.ProductDTO[]
|
[InputAlias.Products]: ProductTypes.ProductDTO[]
|
||||||
}
|
}
|
||||||
}): Promise<PipelineHandlerResult<T>> {
|
}) {
|
||||||
const productModuleService = container.resolve("productModuleService")
|
const productModuleService = container.resolve("productModuleService")
|
||||||
return await productModuleService.softDelete(
|
return await productModuleService.softDelete(
|
||||||
data[InputAlias.Products].map((p) => p.id)
|
data[InputAlias.Products].map((p) => p.id)
|
||||||
|
|||||||
@@ -22,25 +22,23 @@ interface PipelineInput {
|
|||||||
compensate?: WorkflowStepMiddlewareInput | WorkflowStepMiddlewareInput[]
|
compensate?: WorkflowStepMiddlewareInput | WorkflowStepMiddlewareInput[]
|
||||||
}
|
}
|
||||||
|
|
||||||
export type WorkflowArguments = {
|
export type WorkflowArguments<T = any> = {
|
||||||
container: MedusaContainer
|
container: MedusaContainer
|
||||||
payload: unknown
|
payload: unknown
|
||||||
data: any
|
data: T
|
||||||
metadata: TransactionMetadata
|
metadata: TransactionMetadata
|
||||||
context: Context | SharedContext
|
context: Context | SharedContext
|
||||||
}
|
}
|
||||||
|
|
||||||
export type PipelineHandlerResult<T = undefined> = T extends undefined
|
|
||||||
? WorkflowStepMiddlewareReturn | WorkflowStepMiddlewareReturn[]
|
|
||||||
: T
|
|
||||||
|
|
||||||
export type PipelineHandler<T extends any = undefined> = (
|
export type PipelineHandler<T extends any = undefined> = (
|
||||||
args: WorkflowArguments
|
args: WorkflowArguments
|
||||||
) => PipelineHandlerResult<T> | Promise<PipelineHandlerResult<T>>
|
) => T extends undefined
|
||||||
|
? Promise<WorkflowStepMiddlewareReturn | WorkflowStepMiddlewareReturn[]>
|
||||||
|
: T
|
||||||
|
|
||||||
export function pipe(
|
export function pipe<T = undefined>(
|
||||||
input: PipelineInput,
|
input: PipelineInput,
|
||||||
...functions: PipelineHandler[]
|
...functions: [...PipelineHandler[], PipelineHandler<T>]
|
||||||
): WorkflowStepHandler {
|
): WorkflowStepHandler {
|
||||||
return async ({
|
return async ({
|
||||||
container,
|
container,
|
||||||
@@ -92,8 +90,10 @@ export function pipe(
|
|||||||
data[action.alias] = action.value
|
data[action.alias] = action.value
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if (result?.alias) {
|
} else if ((result as WorkflowStepMiddlewareReturn)?.alias) {
|
||||||
data[result.alias] = result.value
|
data[(result as WorkflowStepMiddlewareReturn).alias] = (
|
||||||
|
result as WorkflowStepMiddlewareReturn
|
||||||
|
).value
|
||||||
}
|
}
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|||||||
@@ -56,6 +56,9 @@ export const exportWorkflow = <TData = unknown, TResult = unknown>(
|
|||||||
resultFrom: defaultResult,
|
resultFrom: defaultResult,
|
||||||
}
|
}
|
||||||
) => {
|
) => {
|
||||||
|
resultFrom ??= defaultResult
|
||||||
|
throwOnError ??= true
|
||||||
|
|
||||||
const transaction = await originalRun(
|
const transaction = await originalRun(
|
||||||
context?.transactionId ?? ulid(),
|
context?.transactionId ?? ulid(),
|
||||||
input,
|
input,
|
||||||
|
|||||||
Reference in New Issue
Block a user