CodingSans / billingo-client

MIT License
6 stars 0 forks source link

Using this library for multiple Billingo accounts #18

Open geiszla opened 1 year ago

geiszla commented 1 year ago

We are creating a Billingo integration and serving multiple Billingo clients at the same time. As far as I can see, since we need to set OpenAPI.HEADERS to authenticate a client using this library, this can cause race conditions with multiple parallel requests from different clients (i.e., setting the global headers for one account, then switching to an async context corresponding to a different account, then executing a query might perform undesired actions for the former account).

Would it be possible to expose per-client headers to make multiple requests with different authentication headers at the same time? Or is this already possible in some way?

Thanks.

geiszla commented 1 year ago

For anyone having the same issue, our current workaround is using the request function directly from @codingsans/billingo-client/lib/client/core/request.js and copying the request call from node_modules/@codingsans/billingo-client/lib/client/services/<service name>.js while repacing the global OpenAPI object with a local one.

E.g., creating a document would look like this:

import { request } from '@codingsans/billingo-client/lib/client/core/request.js';

/**
 * @param {string} apiKey
 * @param {import('@codingsans/billingo-client').DocumentInsert} requestBody
 *
 * @returns {import('@codingsans/billingo-client').CancelablePromise<
 *   import('@codingsans/billingo-client').Document
 * >}
 */
export function createDocument(apiKey, requestBody) {
  return request({ ...OpenAPI, HEADERS: { 'X-API-KEY': apiKey } }, {
    method: 'POST',
    url: '/documents',
    body: requestBody,
    mediaType: 'application/json',
    errors: {
      400: `The request is malformed.`,
      401: `Authorization information is missing or invalid.`,
      402: `Authenticated user doesn't have subscription.`,
      403: `Authenticated user doesn't have access to the resource.`,
      422: `Validation errors occured.`,
      429: `Too many requests`,
      500: `Internal server error.`,
    },
  });
}

However, it would be nice to have a way to avoid copying these services.