yumauri / gotenberg-js-client

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

Gotenberg 7, WebHooks Headers and 204 response #47

Closed thaoula closed 2 years ago

thaoula commented 2 years ago

Hi,

Firstly, thanks so much for your client library.

We are attempting to use your library to make calls to Gotenberg 7.53. I have read the comments about making the necessary adjustments for version 7 and so far everything seems to work fine when Gotenberg returns a 200 response.

However, we would really like to use the WebHooks feature and we have the following issues -

  1. The WebHook header function seems to use the incorrect name - Gotenberg-Webhookurl-${name} instead of Gotenberg-Webhook-Url-${name}

We are currently working around this issue by setting the headers by hand in a custom function within the pipe method.

  1. It seems that Gotenberg return 204 No Content instead of 200 when the WebHook headers are set.

Gottenberg Docs

In your node.ts file it seems that the client is configured to reject all responses other than 200.

req.on('response', (res) => {
      if (res.statusCode === 200) {
        resolve(res)
      } else {
        let error = res.statusCode + ' ' + res.statusMessage

Do you know of anyway to override or workaround this issue this? Currently I am just catching the error and looking for the words '204 No Content'.

Regards, Tarek

yumauri commented 2 years ago

Hello! Glad you find it useful :)

I wonder, what if I change condition for successful response to 200 <= statusCode < 300, will it break something? 🤔

Unfortunately there is no workaround to adjust request like with header, but you have another option. It might be overkill, though :)

This is undocumented, but library allows you to use completely your own HTTP client instead of included. You can pass it as a second argument in function gotenberg.

HTTP client it is an object with methods post and optional get (for ping requests). Methods should return Promise<ReadableStream>. https://github.com/yumauri/gotenberg-js-client/blob/4167947a871f1d5f2bc9f06816de20c9dc33a809/src/_types.ts#L11-L20

As a second argument you can pass either client object, or function factory, or class. If you pass function factory or class, you also can add optional third argument with any configuration for your client.

gotenberg('<url>', client)
gotenberg('<url>', clientFactory, config)
gotenberg('<url>', ClientClass, config)

For example, you can use got library as a underlying HTTP client like this:

import got from 'got'

const client = {
  post: (url, body, headers) =>
    Promise.resolve(
      got.post({ url, body, headers, isStream: true })),
}

pipe(
  gotenberg(`http://localhost:3500/forms/chromium`, client),
  // ...
yumauri commented 2 years ago

I wonder, what if I change condition for successful response to 200 <= statusCode < 300, will it break something? 🤔

I decided it shouldn't break anything, and published small change in version 0.7.3, you can try it :)

thaoula commented 2 years ago

Hi @yumauri,

Apologies for not getting back earlier. I just updated to 0.7.3 and removed my try catch for the 204 and it worked fine.

Thanks for make the change.

Regards, Tarek