Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

storage.get(key, { type }) #528

Open
1 task done
pi0 opened this issue Dec 18, 2024 · 1 comment
Open
1 task done

storage.get(key, { type }) #528

pi0 opened this issue Dec 18, 2024 · 1 comment
Assignees
Labels
discussion enhancement New feature or request

Comments

@pi0
Copy link
Member

pi0 commented Dec 18, 2024

Describe the feature

Currently, unstorage has two ways to access items:

  • getItem(key): Returns a json parsed value if parsable or fallback to text
  • `getItemRaw(key): Returns raw storage value (Typically arrayBuffer but unpredictable)

This is an unpredictable behavior and also introduced two sets of methods.

We could support storage.get(key, { type: "text" | "json" | "bytes" | "stream" | "blob" }) API to unify this and also make behavior more predictable.

await storage.get("key") // Defaults to json > text as before using destr
await storage.get("key", { type: "text" )  // Avoid serialization
await storage.get("key", { type: "json" )  // Strictly use JSON.parse
await storage.get("key", { type: "bytes" ) // Return Buffer or Unit8Array
await storage.get("key", { type: "bytes" ) // Return a readable stream

undio can potentially be integrated or we might end-up directly adding small utils in unstorage core to convert

Additional information

  • Would you be willing to help implement this feature?
@pi0 pi0 added enhancement New feature or request discussion labels Dec 18, 2024
@pi0 pi0 self-assigned this Dec 18, 2024
@cjpearson
Copy link
Contributor

This looks like a nice improvement to me. I suppose the typing would also be improved by looking at the type option? Does it make sense to also include blob as a type?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discussion enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants