Skip to content

Commit

Permalink
fix: options.method alias options.type is invalid (#490)
Browse files Browse the repository at this point in the history
  • Loading branch information
autrol authored Feb 22, 2024
1 parent 5eb1297 commit 75c5989
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/HttpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ export class HttpClient extends EventEmitter {
}
}

const method = (options?.method ?? 'GET').toUpperCase() as HttpMethod;
const method = (options?.type || options?.method || 'GET').toUpperCase() as HttpMethod;
const originalHeaders = options?.headers;
const headers: IncomingHttpHeaders = {};
const args = {
Expand Down
2 changes: 2 additions & 0 deletions src/Request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ export type FixJSONCtlChars = boolean | FixJSONCtlCharsHandler;
export type RequestOptions = {
/** Request method, defaults to GET. Could be GET, POST, DELETE or PUT. Alias 'type'. */
method?: HttpMethod | Lowercase<HttpMethod>;
/** Alias for 'method'. */
type?: HttpMethod | Lowercase<HttpMethod>;
/** Data to be sent. Will be stringify automatically. */
data?: any;
/** Manually set the content of payload. If set, data will be ignored. */
Expand Down
36 changes: 36 additions & 0 deletions test/options.type.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { strict as assert } from 'node:assert';
import { describe, it, beforeAll, afterAll } from 'vitest';
import urllib from '../src';
import { startServer } from './fixtures/server';

describe('options.type.test.ts', () => {
let close: any;
let _url: string;
beforeAll(async () => {
const { closeServer, url } = await startServer();
close = closeServer;
_url = url;
});

afterAll(async () => {
await close();
});

it('should default set type GET', async () => {
const { status, data } = await urllib.request(_url, {
dataType: 'json',
type: 'GET',
});
assert.equal(status, 200);
assert.equal(data.method, 'GET');
});

it('should set type POST', async () => {
const { status, data } = await urllib.request(_url, {
dataType: 'json',
type: 'POST',
});
assert.equal(status, 200);
assert.equal(data.method, 'POST');
});
});

0 comments on commit 75c5989

Please sign in to comment.