Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: options.method alias options.type is invalid #490

Merged
merged 1 commit into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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');
});
});
Loading