docs: add buffer example in workflows + tip for type error in admin global variables (#13124)

This commit is contained in:
Shahed Nasser
2025-08-01 17:15:47 +03:00
committed by GitHub
parent dd9a644272
commit 6ec530b2a5
5 changed files with 186 additions and 12 deletions
+91 -4
View File
@@ -6079,10 +6079,16 @@ If you receive a type error on `import.meta.env`, create the file `src/admin/vit
```ts title="src/admin/vite-env.d.ts"
/// <reference types="vite/client" />
declare const __BASE__: string
declare const __BACKEND_URL__: string
declare const __STOREFRONT_URL__: string
```
This file tells TypeScript to recognize the `import.meta.env` object and enhances the types of your custom environment variables.
Note that the `__BASE__`, `__BACKEND_URL__`, and `__STOREFRONT_URL__` variables are global variables available in your admin customizations. Learn more in the [Tips for Admin Customizations](https://docs.medusajs.com/learn/fundamentals/admin/tips#global-variables-in-admin-customizations/index.html.md) chapter.
***
## Check Node Environment in Admin Customizations
@@ -6137,6 +6143,16 @@ export const config = defineWidgetConfig({
export default ProductWidget
```
To fix possible type errors, create the file `src/admin/vite-env.d.ts` and add the global variables:
```ts title="src/admin/vite-env.d.ts"
/// <reference types="vite/client" />
declare const __BACKEND_URL__: string
declare const __BASE__: string
declare const __STOREFRONT_URL__: string
```
# Admin Development
@@ -6485,6 +6501,16 @@ In your admin customizations, you can use the following global variables:
- `__BACKEND_URL__`: The URL to the Medusa backend, as set in the [admin.backendUrl](https://docs.medusajs.com/learn/configurations/medusa-config#backendurl/index.html.md) configuration in `medusa-config.ts`.
- `__STOREFRONT_URL__`: The URL to the storefront, as set in the [admin.storefrontUrl](https://docs.medusajs.com/learn/configurations/medusa-config#storefrontUrl/index.html.md) configuration in `medusa-config.ts`.
If you get type errors while using these variables, you can create the file `src/admin/vite-env.d.ts` with the following content:
```ts title="src/admin/vite-env.d.ts"
/// <reference types="vite/client" />
declare const __BASE__: string
declare const __BACKEND_URL__: string
declare const __STOREFRONT_URL__: string
```
***
## Admin Translations
@@ -18546,11 +18572,9 @@ Instead, refer to the [Error Handling](https://docs.medusajs.com/learn/fundament
***
## Step Constraints
## Returned Value Constraints
### Returned Values
A step must only return serializable values, such as [primitive values](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#primitive_values) or an object.
Data returned from workflows and steps are serialized, allowing Medusa to store them in the database. So, you must only return serializable values, such as [primitive values](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#primitive_values) or an object, from workflows and steps.
Values of other types, such as Maps, aren't allowed.
@@ -18594,6 +18618,69 @@ const step1 = createStep(
)
```
### Buffer Example
In some cases, you may need to return a buffer. For example, when your workflow generates a file and you want to return it as a buffer.
In those cases, you can return an object containing the buffer as a property. Then, in customizations that execute the workflow, you can recreate the buffer from the serialized data.
For example, consider the following workflow that returns a buffer:
```ts
import {
createWorkflow,
createStep,
WorkflowResponse,
StepResponse,
} from "@medusajs/framework/workflows-sdk"
const step1 = createStep(
"step-1",
(_, { container }) => {
const buffer = Buffer.from("Hello, World!")
return new StepResponse({
buffer,
})
}
)
const myWorkflow = createWorkflow(
"hello-world",
function () {
const step1Response = step1()
return new WorkflowResponse({
buffer: step1Response.buffer,
})
}
)
```
Then, in an API route that executes this workflow, you can recreate the buffer from the serialized data using `Buffer.from`:
```ts
import type {
MedusaRequest,
MedusaResponse,
} from "@medusajs/framework/http"
import myWorkflow from "../../workflows/hello-world"
export async function GET(
req: MedusaRequest,
res: MedusaResponse
) {
const { result } = await myWorkflow(req.scope)
.run()
const buffer = Buffer.from(result.buffer)
res.setHeader("Content-Type", "application/octet-stream")
res.setHeader("Content-Disposition", "attachment; filename=hello.txt")
res.send(buffer)
}
```
# Error Handling in Workflows