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

[Feature Request] Is there a way to handle binary response? #390

Open
hanayashiki opened this issue Mar 22, 2023 · 8 comments
Open

[Feature Request] Is there a way to handle binary response? #390

hanayashiki opened this issue Mar 22, 2023 · 8 comments
Labels
pinned issues that should not be closed by bot

Comments

@hanayashiki
Copy link

I find it possible to pass request body in binary, but is it also possible to handle binary response, e.g. converting them to blob?
Thanks in advance.

@hanayashiki
Copy link
Author

Anyway, there must be a way to scramble with plugins. This is my way to get around with the limitation.

const blobPlugin = (): ZodiosPlugin => {
  const exampleBlob = new Blob();

  return {
    name: 'blob',
    request: async (api, config) => {
      const endpoint = api.find((e) => e.method === config.method && e.path === config.url);

      if (!endpoint) return config;

      // We have to guess whether the response schema is `z.instanceof(Blob)` because the info is not reflected
      let isLikelyInstanceOfBlob = false;

      try {
        if (endpoint.response.safeParse(exampleBlob).success) {
          isLikelyInstanceOfBlob = true;
        }
      } catch (e) {}

      if (!isLikelyInstanceOfBlob) return config;

      return {
        ...config,
        responseType: 'blob',
      };
    },
  };
};

Then you simply define your endpoint with response: z.instanceof(Blob) to achieve both blob handling and type-safety

@ecyrbe
Copy link
Owner

ecyrbe commented Mar 23, 2023

if you need a blob result, you don't need a plugin, just do :

client.download({ responseType: 'blob', });

if you pefer using your plugin, don't attach it globally but only on the route you need it, so no need to safeParse a fake blob:

client.use('download', blobPlugin());

@hanayashiki
Copy link
Author

hanayashiki commented Mar 23, 2023

if you need a blob result, you don't need a plugin, just do :

client.download({ responseType: 'blob', });

if you pefer using your plugin, don't attach it globally but only on the route you need it, so no need to safeParse a fake blob:

client.use('download', blobPlugin());

Thank you for your advice. My concern is this should probably done in the API Definition level instead of the client. Because the binary response is actually determined by the API contract instead of the client or server.

About the guessing, probably Zod should implement z.instanceof using a class object instead of a z.custom (which results in an impenetrable z.ZodEffects), just like z.string, z.number, z.object, etc, giving instanceof first class support. If so, the plugin would be perfect

And I wonder where is

client.download({ responseType: 'blob', });

in the documentatation. Am I missing anything?

@ecyrbe
Copy link
Owner

ecyrbe commented Mar 23, 2023

There is not yet exemples for binary downloads in the docs. it can definitely be added.

Blob download is just described in client options:
http://www.zodios.org/docs/client#request-options

i agree, that we could leverage the api binary option to force a blob or stream.
will think about a way to declare both for zodios v11

@stale
Copy link

stale bot commented Apr 22, 2023

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix This will not be worked on label Apr 22, 2023
@stale stale bot closed this as completed Apr 29, 2023
@ecyrbe ecyrbe reopened this Apr 29, 2023
@stale stale bot removed the wontfix This will not be worked on label Apr 29, 2023
@stale
Copy link

stale bot commented May 29, 2023

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix This will not be worked on label May 29, 2023
@ecyrbe ecyrbe added the pinned issues that should not be closed by bot label May 30, 2023
@stale stale bot removed the wontfix This will not be worked on label May 30, 2023
@jcdb95
Copy link

jcdb95 commented Sep 25, 2023

Is there an update on this example? passing { responseType: 'blob' } as a second parameter of a method generated from an API is not working for me.

@hanayashiki
Copy link
Author

Is there an update on this example? passing { responseType: 'blob' } as a second parameter of a method generated from an API is not working for me.

guess you have to write a middleware yourself

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pinned issues that should not be closed by bot
Projects
None yet
Development

No branches or pull requests

3 participants