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