yumauri / gotenberg-js-client

A simple JS/TS client for interacting with a Gotenberg API
MIT License
111 stars 9 forks source link

How to set headers for the gotenberg request? #26

Closed Setti7 closed 3 years ago

Setti7 commented 3 years ago

In the README there is an example on how to set the header for the request gotenberg does to an external URL, but there isn't any reference on how to set a header to the request to gotenberg itself.

Maybe if we could pass it to the gotenberg call like this?

const toPDF = pipe(
  gotenberg('https://cloud-run', { headers: { 'Authorization': 'Bearer abc' } }),
  convert,
  office,
  set(filename('result.pdf')),
  please
);
yumauri commented 3 years ago

Hm...

If you are using plain JavaScript, you can easily hack into pipe chain with you own function. I guess with TypeScript it will be a bit harder because of typings but should be possible as well.

For example (don't have possibility to check the code right now, but it should work):

const auth = (value) => (request) => ({
  ...request,
  headers: {
    ...request.headers,
    'Authorization': value,
  },
})

const toPDF = pipe(
  gotenberg('https://cloud-run'),
  auth('Bearer abc'),
  convert,
  office,
  set(filename('result.pdf')),
  please
);

Also:

This is undocumented, because I didn't have time then and then I just forgot ._. But gotenberg function accepts optional GotenbergClient as a second argument, and config as a third (see src).

GotenbergClient could be a plain object with fields post and optional get (get is used for ping request, so if you don't use them, you can omit it), or it could be a function, which will be called with third config argument, and should return object with fields post and optional get, or it could be a class, which will be instantiated with third config argument, and should have methods post and optional get.

You can check signature of post function here, for example.

GotenbergClient is responsible for HTTP requests to Gotenberg. Default client is utilizing node's http and https modules, but you can create and use completely your own client with custom logic. For example, on top of got library :)

yumauri commented 3 years ago

Honestly I don't want to add new functionality support to default GotenbergClient, but maybe introduce more convenient way to hack into chain, for advanced usage?

For example, some sort of function adjust, which will accept any object (with proper request format) and merge it with actual request?

Like

const toPDF = pipe(
  gotenberg('https://cloud-run'),
  adjust({ headers: { 'Authorization': 'Bearer abc' } }),
  convert,
  office,
  set(filename('result.pdf')),
  please
);

Which will work exactly like code snippet in my previous comment. It will also looks almost like your suggestion :)

What do you think?

yumauri commented 3 years ago

You know, I just checked my codes, and your code in your initial question even might work in some cases (maybe for URL conversions) :)

If gotenberg function's second argument doesn't have field post — it is considered as config for native GotenbergClient, and native client merges this config with actual request :)

But it will override default headers, so, I guess it will break files sent...

I think I can change this part so it will merge headers, not override them :)

Setti7 commented 3 years ago

The first solution worked perfectly! Thanks a lot!

The auth function in your first comment could be adapted to work just like your adjust function idea, but for me personally this solution is enough.

yumauri commented 3 years ago

Cool, but I've published new version 0.7.0 nonetheless :)

It fixes config merging and introduces new function adjust, so, with this new release your initial code should work as expected, without any changes and new handwritten functions.

As well as my code from this comment, with adjust function.