Skip to content

Commit

Permalink
feat(lookup-fn): added support for returning a single IP string inste…
Browse files Browse the repository at this point in the history
…ad of a tuple
  • Loading branch information
DigitalBrainJS committed Apr 25, 2023
1 parent 544d3b5 commit b6c089e
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 3 deletions.
2 changes: 1 addition & 1 deletion index.d.cts
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ declare namespace axios {
formSerializer?: FormSerializerOptions;
family?: 4 | 6 | undefined;
lookup?: ((hostname: string, options: object, cb: (err: Error | null, address: string, family: number) => void) => void) |
((hostname: string, options: object) => Promise<[address: string, family: number]>);
((hostname: string, options: object) => Promise<[address: string, family: number] | string>);
}

interface HeadersDefaults {
Expand Down
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ export interface AxiosRequestConfig<D = any> {
formSerializer?: FormSerializerOptions;
family?: 4 | 6 | undefined;
lookup?: ((hostname: string, options: object, cb: (err: Error | null, address: string, family: number) => void) => void) |
((hostname: string, options: object) => Promise<[address: string, family: number]>);
((hostname: string, options: object) => Promise<[address: string, family: number] | string>);
}

export interface HeadersDefaults {
Expand Down
4 changes: 3 additions & 1 deletion lib/adapters/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,9 @@ export default isHttpAdapterSupported && function httpAdapter(config) {

if (lookup && utils.isAsyncFn(lookup)) {
lookup = callbackify(lookup, (entry) => {
if (!utils.isArray(entry)) {
if(utils.isString(entry)) {
entry = [entry, entry.indexOf('.') < 0 ? 6 : 4]
} else if (!utils.isArray(entry)) {
throw new TypeError('lookup async function must return an array [ip: string, family: number]]')
}
return entry;
Expand Down
19 changes: 19 additions & 0 deletions test/unit/adapters/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -1983,5 +1983,24 @@ describe('supports http with nodejs', function () {

assert.strictEqual(data, payload);
});

it('should support custom DNS lookup function that returns only IP address (async)', async function () {
server = await startHTTPServer(SERVER_HANDLER_STREAM_ECHO);

const payload = 'test';

let isCalled = false;

const {data} = await axios.post(`http://fake-name.axios:4444`, payload,{
lookup: async (hostname, opt) => {
isCalled = true;
return '127.0.0.1';
}
});

assert.ok(isCalled);

assert.strictEqual(data, payload);
});
});
});

0 comments on commit b6c089e

Please sign in to comment.