request-got-adapter 0.1.1 → 0.1.3

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.
package/README.md CHANGED
@@ -112,6 +112,7 @@ One deliberate improvement over the reference: passing an object `body` without
112
112
  | `agent` / `agentOptions` / `forever` / `.forever()` | ✅ |
113
113
  | `jar` / `request.jar()` / `request.cookie()` (tough-cookie) | ✅ |
114
114
  | `transform` / `transform2xxOnly` | ✅ |
115
+ | `response.caseless` (case-insensitive header helper) | ✅ |
115
116
  | `localAddress` / `family` / `lookup` | ✅ |
116
117
  | `.defaults()` (chainable) | ✅ |
117
118
  | Callback style `(err, response, body)` alongside promises | ✅ |
package/dist/core.js CHANGED
@@ -46,6 +46,23 @@ function toRequestError(err, prepared) {
46
46
  }
47
47
  return new errors_1.RequestError(cause, prepared.rpOptions, undefined);
48
48
  }
49
+ const errorListenerAttached = Symbol.for('request-got-adapter.errorListenerAttached');
50
+ const swallowLateErrors = () => { };
51
+ // request kept 'error' listeners on the request and its socket for their whole
52
+ // lifetime; without this, late socket errors emitted after got settles (seen with
53
+ // keep-alive reuse and mocked sockets) become unhandled 'error' events.
54
+ function attachDefensiveErrorListeners(pending) {
55
+ const promiseWithEvents = pending;
56
+ promiseWithEvents.on?.('request', (request) => {
57
+ request.on('error', swallowLateErrors);
58
+ request.on('socket', (socket) => {
59
+ if (!socket[errorListenerAttached]) {
60
+ socket[errorListenerAttached] = true;
61
+ socket.on('error', swallowLateErrors);
62
+ }
63
+ });
64
+ });
65
+ }
49
66
  async function gotCall(prepared, extraHeaders) {
50
67
  const gotModule = await (0, got_loader_1.loadGot)();
51
68
  const got = gotModule.default;
@@ -56,7 +73,9 @@ async function gotCall(prepared, extraHeaders) {
56
73
  }
57
74
  : prepared.gotOptions;
58
75
  try {
59
- return await got(prepared.url, options);
76
+ const pending = got(prepared.url, options);
77
+ attachDefensiveErrorListeners(pending);
78
+ return await pending;
60
79
  }
61
80
  catch (err) {
62
81
  throw toRequestError(err, prepared);
package/dist/errors.d.ts CHANGED
@@ -1,24 +1,24 @@
1
1
  export declare class StatusCodeError extends Error {
2
2
  name: "StatusCodeError";
3
3
  statusCode: number;
4
- error: unknown;
5
- options: unknown;
6
- response: unknown;
4
+ error: any;
5
+ options: any;
6
+ response: any;
7
7
  constructor(statusCode: number, body: unknown, options?: unknown, response?: unknown);
8
8
  }
9
9
  export declare class RequestError extends Error {
10
10
  name: "RequestError";
11
- cause: unknown;
12
- error: unknown;
13
- options: unknown;
14
- response: unknown;
11
+ cause: any;
12
+ error: any;
13
+ options: any;
14
+ response: any;
15
15
  constructor(cause: unknown, options?: unknown, response?: unknown);
16
16
  }
17
17
  export declare class TransformError extends Error {
18
18
  name: "TransformError";
19
- cause: unknown;
20
- error: unknown;
21
- options: unknown;
22
- response: unknown;
19
+ cause: any;
20
+ error: any;
21
+ options: any;
22
+ response: any;
23
23
  constructor(cause: unknown, options?: unknown, response?: unknown);
24
24
  }
package/dist/errors.js CHANGED
@@ -1,7 +1,18 @@
1
1
  "use strict";
2
- // Error classes matching request-promise-core's errors 1:1 (names, fields, message formats).
3
2
  Object.defineProperty(exports, "__esModule", { value: true });
4
3
  exports.TransformError = exports.RequestError = exports.StatusCodeError = void 0;
4
+ // Error classes matching request-promise-core's errors 1:1 (names, fields, message formats).
5
+ // request-promise-core assigns `this.message` in plain constructors, making it an
6
+ // ENUMERABLE own property — consumers rely on `{ ...err }` spreads and JSON.stringify
7
+ // keeping the message, so mirror that explicitly (ES class super() makes it non-enumerable).
8
+ function exposeMessage(error, message) {
9
+ Object.defineProperty(error, 'message', {
10
+ value: message,
11
+ enumerable: true,
12
+ writable: true,
13
+ configurable: true
14
+ });
15
+ }
5
16
  class StatusCodeError extends Error {
6
17
  name = 'StatusCodeError';
7
18
  statusCode;
@@ -9,7 +20,9 @@ class StatusCodeError extends Error {
9
20
  options;
10
21
  response;
11
22
  constructor(statusCode, body, options, response) {
12
- super(`${statusCode} - ${JSON.stringify(body)}`);
23
+ const message = `${statusCode} - ${JSON.stringify(body)}`;
24
+ super(message);
25
+ exposeMessage(this, message);
13
26
  this.statusCode = statusCode;
14
27
  this.error = body;
15
28
  this.options = options;
@@ -25,7 +38,9 @@ class RequestError extends Error {
25
38
  options;
26
39
  response;
27
40
  constructor(cause, options, response) {
28
- super(String(cause));
41
+ const message = String(cause);
42
+ super(message);
43
+ exposeMessage(this, message);
29
44
  this.cause = cause;
30
45
  this.error = cause;
31
46
  this.options = options;
@@ -41,7 +56,9 @@ class TransformError extends Error {
41
56
  options;
42
57
  response;
43
58
  constructor(cause, options, response) {
44
- super(String(cause));
59
+ const message = String(cause);
60
+ super(message);
61
+ exposeMessage(this, message);
45
62
  this.cause = cause;
46
63
  this.error = cause;
47
64
  this.options = options;
package/dist/index.d.ts CHANGED
@@ -1,10 +1,10 @@
1
- import { type RequestJar, jar as createJar, cookie as parseCookieString } from './cookies';
1
+ import { jar as createJar, cookie as parseCookieString } from './cookies';
2
2
  import { RequestError as RequestErrorClass, StatusCodeError as StatusCodeErrorClass, TransformError as TransformErrorClass } from './errors';
3
3
  import type { FullResponse as FullResponseShape } from './response';
4
4
  import type { AuthOptions, OAuthOptions } from './rp-options';
5
5
  declare function requestGotAdapter(uriOrOptions: string | requestGotAdapter.Options, optionsOrCallback?: requestGotAdapter.RequestPromiseOptions | requestGotAdapter.RequestCallback, callback?: requestGotAdapter.RequestCallback): requestGotAdapter.RequestPromise;
6
6
  declare namespace requestGotAdapter {
7
- type RequestCallback = (error: unknown, response: FullResponse, body: unknown) => void;
7
+ type RequestCallback = (error: any, response: any, body: any) => void;
8
8
  type FullResponse = FullResponseShape;
9
9
  interface RequestPromise<T = any> extends Promise<T> {
10
10
  promise(): Promise<T>;
@@ -29,7 +29,7 @@ declare namespace requestGotAdapter {
29
29
  time?: boolean;
30
30
  simple?: boolean;
31
31
  resolveWithFullResponse?: boolean;
32
- followRedirect?: boolean;
32
+ followRedirect?: boolean | ((response: any) => boolean);
33
33
  followAllRedirects?: boolean;
34
34
  followOriginalHttpMethod?: boolean;
35
35
  maxRedirects?: number;
@@ -62,7 +62,11 @@ declare namespace requestGotAdapter {
62
62
  };
63
63
  }
64
64
  type Options = OptionsWithUri | OptionsWithUrl;
65
- type CookieJar = RequestJar;
65
+ interface CookieJar {
66
+ setCookie: (cookieOrString: any, uri: string, options?: any) => any;
67
+ getCookieString: (uri: string) => string;
68
+ getCookies: (uri: string) => any[];
69
+ }
66
70
  interface RequestPromiseCall {
67
71
  (options: Options, callback?: RequestCallback): RequestPromise;
68
72
  (uri: string, options?: RequestPromiseOptions, callback?: RequestCallback): RequestPromise;
@@ -85,17 +89,17 @@ declare namespace requestGotAdapter {
85
89
  RequestError: typeof RequestErrorClass;
86
90
  TransformError: typeof TransformErrorClass;
87
91
  }
88
- const get: RequestPromiseCall;
89
- const head: RequestPromiseCall;
90
- const options: RequestPromiseCall;
91
- const post: RequestPromiseCall;
92
- const put: RequestPromiseCall;
93
- const patch: RequestPromiseCall;
94
- const del: RequestPromiseCall;
95
- const defaults: (options: RequestPromiseOptions) => RequestPromiseAPI;
96
- const forever: (agentOptions?: Record<string, any>) => RequestPromiseAPI;
97
- const jar: typeof createJar;
98
- const cookie: typeof parseCookieString;
92
+ let get: RequestPromiseCall;
93
+ let head: RequestPromiseCall;
94
+ let options: RequestPromiseCall;
95
+ let post: RequestPromiseCall;
96
+ let put: RequestPromiseCall;
97
+ let patch: RequestPromiseCall;
98
+ let del: RequestPromiseCall;
99
+ let defaults: (options: RequestPromiseOptions) => RequestPromiseAPI;
100
+ let forever: (agentOptions?: Record<string, any>) => RequestPromiseAPI;
101
+ let jar: typeof createJar;
102
+ let cookie: typeof parseCookieString;
99
103
  const StatusCodeError: typeof StatusCodeErrorClass;
100
104
  const RequestError: typeof RequestErrorClass;
101
105
  const TransformError: typeof TransformErrorClass;
package/dist/index.js CHANGED
@@ -35,6 +35,8 @@ function requestGotAdapter(uriOrOptions, optionsOrCallback, callback) {
35
35
  }
36
36
  // Mirrors @types/request-promise-native's export= namespace shape
37
37
  (function (requestGotAdapter) {
38
+ // `let` on purpose: @types/request-promise-native models these as mutable
39
+ // interface members, and tests commonly assign mocks (rp.post = jest.fn())
38
40
  requestGotAdapter.get = baseApi.get;
39
41
  requestGotAdapter.head = baseApi.head;
40
42
  requestGotAdapter.options = baseApi.options;
@@ -24,6 +24,13 @@ interface GotResponseLike {
24
24
  };
25
25
  };
26
26
  }
27
+ export interface Caseless {
28
+ get: (name: string) => unknown;
29
+ has: (name: string) => string | false;
30
+ set: (name: string, value: unknown) => void;
31
+ del: (name: string) => void;
32
+ swap: (name: string) => void;
33
+ }
27
34
  export interface FullResponse {
28
35
  statusCode: number;
29
36
  statusMessage?: string;
@@ -31,6 +38,7 @@ export interface FullResponse {
31
38
  rawHeaders?: string[];
32
39
  httpVersion?: string;
33
40
  body: unknown;
41
+ caseless: Caseless;
34
42
  request: {
35
43
  uri: ReturnType<typeof parseLegacyUrl>;
36
44
  method: string;
package/dist/response.js CHANGED
@@ -2,6 +2,37 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.buildFullResponse = buildFullResponse;
4
4
  const node_url_1 = require("node:url");
5
+ // request responses carry a `caseless` helper for case-insensitive header access
6
+ function makeCaseless(headers) {
7
+ const findKey = (name) => {
8
+ const lower = name.toLowerCase();
9
+ return Object.keys(headers).find((key) => key.toLowerCase() === lower);
10
+ };
11
+ return {
12
+ get(name) {
13
+ const key = findKey(name);
14
+ return key === undefined ? undefined : headers[key];
15
+ },
16
+ has(name) {
17
+ return findKey(name) ?? false;
18
+ },
19
+ set(name, value) {
20
+ headers[findKey(name) ?? name] = value;
21
+ },
22
+ del(name) {
23
+ const key = findKey(name);
24
+ if (key !== undefined)
25
+ delete headers[key];
26
+ },
27
+ swap(name) {
28
+ const key = findKey(name);
29
+ if (key === undefined || key === name)
30
+ return;
31
+ headers[name] = headers[key];
32
+ delete headers[key];
33
+ }
34
+ };
35
+ }
5
36
  function buildFullResponse(gotResponse, prepared, body) {
6
37
  const finalMethod = gotResponse.request?.options?.method ?? prepared.method;
7
38
  const requestPart = {
@@ -16,6 +47,7 @@ function buildFullResponse(gotResponse, prepared, body) {
16
47
  rawHeaders: gotResponse.rawHeaders,
17
48
  httpVersion: gotResponse.httpVersion,
18
49
  body,
50
+ caseless: makeCaseless(gotResponse.headers),
19
51
  request: requestPart,
20
52
  toJSON() {
21
53
  return {
package/dist/translate.js CHANGED
@@ -224,6 +224,14 @@ function prepare(rpOptions) {
224
224
  body = `${body}&${(0, oauth_1.toOAuthQuery)(oauthParams)}`;
225
225
  }
226
226
  }
227
+ // request computes content-length (as a number) for string/Buffer bodies and
228
+ // records it on the outgoing headers — visible via response.request.headers
229
+ if (body !== undefined && !(0, headers_1.hasHeader)(headers, 'content-length')) {
230
+ if (typeof body === 'string')
231
+ headers['content-length'] = Buffer.byteLength(body);
232
+ else if (Buffer.isBuffer(body))
233
+ headers['content-length'] = body.length;
234
+ }
227
235
  const gotHeaders = { ...headers };
228
236
  // got injects its own user-agent unless explicitly disabled; request sends none
229
237
  if (!(0, headers_1.hasHeader)(gotHeaders, 'user-agent'))
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "request-got-adapter",
3
- "version": "0.1.1",
3
+ "version": "0.1.3",
4
4
  "description": "Drop-in replacement for request-promise-native, backed by got. Same API, same options, same errors — no deprecated request-family dependencies.",
5
5
  "license": "MIT",
6
6
  "repository": {