@proofline/sdk 1.0.1 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -33,6 +33,6 @@ export declare class Transport {
33
33
  #private;
34
34
  constructor(options: ClientOptions);
35
35
  reviewUrl(orgId: string, reviewId: string): string;
36
- request<T>(method: string, template: string, parameters: Parameters, deadline?: number, body?: object): Promise<T>;
36
+ request<T>(method: string, template: string, parameters: Parameters, deadline?: number, body?: object, decode?: (text: string, headers: Headers) => unknown, accept?: string): Promise<T>;
37
37
  }
38
38
  export {};
package/dist/transport.js CHANGED
@@ -1,4 +1,4 @@
1
- const VERSION = "1.0.1";
1
+ const VERSION = "1.0.2";
2
2
  const MAX_RESPONSE_BYTES = 8 * 1024 * 1024;
3
3
  const MAX_RETRIES = 3;
4
4
  const MAX_RETRY_SECONDS = 60;
@@ -82,7 +82,7 @@ function positive(value, name) {
82
82
  }
83
83
  return value;
84
84
  }
85
- async function json(response, deadline, signal, waitTimeout) {
85
+ async function decoded(response, deadline, signal, waitTimeout, decode) {
86
86
  const reader = response.body?.getReader();
87
87
  if (!reader) {
88
88
  throw new ProoflineError(response.status, "urn:proofline:sdk:invalid-response", "the response has no JSON body");
@@ -107,7 +107,10 @@ async function json(response, deadline, signal, waitTimeout) {
107
107
  data.set(chunk, offset);
108
108
  offset += chunk.length;
109
109
  }
110
- return JSON.parse(new TextDecoder("utf-8", { fatal: true }).decode(data));
110
+ const text = new TextDecoder("utf-8", { fatal: true }).decode(data);
111
+ return response.ok
112
+ ? decode(text, response.headers)
113
+ : JSON.parse(text);
111
114
  }
112
115
  catch (error) {
113
116
  if (error instanceof ProoflineError)
@@ -185,7 +188,7 @@ export class Transport {
185
188
  reviewUrl(orgId, reviewId) {
186
189
  return new URL(`/orgs/by-id/${coordinate(orgId)}/reviews/${coordinate(reviewId)}`, this.#base).href;
187
190
  }
188
- async request(method, template, parameters, deadline, body) {
191
+ async request(method, template, parameters, deadline, body, decode = (text) => JSON.parse(text), accept = "application/json, application/problem+json") {
189
192
  const path = Object.entries(parameters.path ?? {}).reduce((value, [key, coordinateValue]) => value.replace(`{${key}}`, coordinate(coordinateValue)), template);
190
193
  if (/[{}]/.test(path))
191
194
  throw new TypeError("a required path coordinate is missing");
@@ -197,7 +200,7 @@ export class Transport {
197
200
  const requestBody = body === undefined ? undefined : JSON.stringify(body);
198
201
  for (let attempt = 0;; attempt += 1) {
199
202
  const headers = new Headers({
200
- Accept: "application/json, application/problem+json",
203
+ Accept: accept,
201
204
  "User-Agent": `proofline-typescript/${VERSION}`,
202
205
  });
203
206
  if (requestBody !== undefined)
@@ -227,7 +230,7 @@ export class Transport {
227
230
  throw new ProoflineError(0, "urn:proofline:sdk:timeout", "the review wait deadline expired");
228
231
  throw new ProoflineError(0, "urn:proofline:sdk:transport", "the request did not receive a response within its timeout");
229
232
  }
230
- const value = await json(response, requestDeadline, signal, waitTimeout);
233
+ const value = await decoded(response, requestDeadline, signal, waitTimeout, decode);
231
234
  if (response.ok)
232
235
  return value;
233
236
  const error = problem(response.status, value);