primitive type returns in action

Is there a way to return a string in an action? I've tried just adding it to the return statement, but typescript complains. I've also tried adding a returns: { file: File, key: string } statment to my action (I'm already returning a File entity as well). Is this even possible, or do I need to create a separate entity just to return the string?
5 Replies
kapa.ai
kapa.ai2w ago
Hi @cmilazzo. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- Yes, you can return a string or any other primitive type in an action. However, you need to ensure that the return type of your action function matches the type of the value you're returning. In your case, if you want to return both a File entity and a string, you could define your action's return type as an object that includes both. Here's an example:
type Output = { file: File, key: string };
type Output = { file: File, key: string };
Then, in your action implementation, you would return an object of this type:
return { file: myFile, key: myString };
return { file: myFile, key: myString };
Remember that Wasp uses superjson (https://github.com/blitz-js/superjson) under the hood, which means you're not limited to only sending and receiving JSON payloads. You can send and receive any superjson-compatible payload (like Dates, Sets, Lists, circular references, etc.) and let Wasp handle the (de)serialization. For more information, you can refer to the Wasp documentation on actions (https://wasp-lang.dev/docs/data-model/operations/actions#implementing-actions-in-node). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @cmilazzo, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist: Perhaps try again in 15mins?
cmilazzo
cmilazzo2w ago
@kapa.ai where does the type Output line you referenced above go? Is that in the main.wasp file?
kapa.ai
kapa.ai2w ago
Let me check the knowledge base for you. One moment! :books: ---------------------- The Output type definition would go in your action implementation file, not in the main.wasp file. This file would be in your src directory, as specified in the fn field of your action declaration in main.wasp. Here's an example of what your action implementation file might look like:
import { File } from '../entities/file'

type Output = { file: File, key: string };

export const myAction = async (): Promise<Output> => {
// ... your action logic here ...

return { file: myFile, key: myString };
};
import { File } from '../entities/file'

type Output = { file: File, key: string };

export const myAction = async (): Promise<Output> => {
// ... your action logic here ...

return { file: myFile, key: myString };
};
In this file, you're defining the Output type and using it to specify the return type of your action function. This ensures that TypeScript knows what type of value your function is expected to return. Then, in your main.wasp file, you would have something like this:
action MyAction {
fn: import { myAction } from "@src/myAction.ts"
}
action MyAction {
fn: import { myAction } from "@src/myAction.ts"
}
This tells Wasp where to find the implementation of your action. Note that you don't need to specify the return type in main.wasp - that's handled in your action implementation file. For more information, you can refer to the Wasp documentation on actions (https://wasp-lang.dev/docs/data-model/operations/actions#implementing-actions-in-node). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @cmilazzo, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist: Perhaps try again in 15mins?
cmilazzo
cmilazzo2w ago
@kapa.ai. okay, that makes sense. My original action definition is: export const createFile: CreateFile<FileDescription, File> = async ({ fileType, name, bucket}, context) => {}. How would I change that to use the Output type you referenced?
kapa.ai
kapa.ai2w ago
Let me check the knowledge base for you. One moment! :books: ---------------------- You can modify your action definition to use the Output type by changing the second type argument of CreateFile from File to Output. Here's how you can do it:
import { File } from '../entities/file'
import { CreateFile } from 'wasp/server/operations'

type FileDescription = { fileType: string, name: string, bucket: string }
type Output = { file: File, key: string }

export const createFile: CreateFile<FileDescription, Output> = async ({ fileType, name, bucket}, context) => {
// ... your action logic here ...

return { file: myFile, key: myString }
}
import { File } from '../entities/file'
import { CreateFile } from 'wasp/server/operations'

type FileDescription = { fileType: string, name: string, bucket: string }
type Output = { file: File, key: string }

export const createFile: CreateFile<FileDescription, Output> = async ({ fileType, name, bucket}, context) => {
// ... your action logic here ...

return { file: myFile, key: myString }
}
In this code, Output is a type that represents the return type of your action. It's an object that includes a File entity and a string. The createFile function is then typed to return a Promise that resolves to an Output. Remember to replace myFile and myString with the actual values you want to return. For more information, you can refer to the Wasp documentation on actions (https://wasp-lang.dev/docs/data-model/operations/actions#implementing-actions-in-node). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @cmilazzo, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist: Perhaps try again in 15mins?
Want results from more Discord servers?
Add your server