diff --git a/src/HttpClient.ts b/src/HttpClient.ts index 7970e332..3e8cb382 100644 --- a/src/HttpClient.ts +++ b/src/HttpClient.ts @@ -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 = { diff --git a/src/Request.ts b/src/Request.ts index 300eafd9..4a786ba7 100644 --- a/src/Request.ts +++ b/src/Request.ts @@ -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; + /** Alias for 'method'. */ + type?: HttpMethod | Lowercase; /** Data to be sent. Will be stringify automatically. */ data?: any; /** Manually set the content of payload. If set, data will be ignored. */ diff --git a/test/options.type.test.ts b/test/options.type.test.ts new file mode 100644 index 00000000..91ffaabc --- /dev/null +++ b/test/options.type.test.ts @@ -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'); + }); +});