Skip to content

Commit 08d7a0f

Browse files
committed
Fix assertions when cloning raw options
1 parent 0fb6ec6 commit 08d7a0f

File tree

2 files changed

+24
-22
lines changed

2 files changed

+24
-22
lines changed

source/core/options.ts

+22-20
Original file line numberDiff line numberDiff line change
@@ -757,81 +757,83 @@ const cloneRaw = (raw: OptionsInit) => {
757757

758758
const result: OptionsInit = {...raw};
759759

760-
if (raw.context) {
760+
if (is.object(raw.context)) {
761761
result.context = {...raw.context};
762762
}
763763

764-
if (raw.cacheOptions) {
764+
if (is.object(raw.cacheOptions)) {
765765
result.cacheOptions = {...raw.cacheOptions};
766766
}
767767

768-
if (raw.https) {
768+
if (is.object(raw.https)) {
769769
result.https = {...raw.https};
770770
}
771771

772-
if (raw.cacheOptions) {
772+
if (is.object(raw.cacheOptions)) {
773773
result.cacheOptions = {...result.cacheOptions};
774774
}
775775

776-
if (raw.agent) {
776+
if (is.object(raw.agent)) {
777777
result.agent = {...raw.agent};
778778
}
779779

780-
if (raw.headers) {
780+
if (is.object(raw.headers)) {
781781
result.headers = {...raw.headers};
782782
}
783783

784-
if (retry) {
784+
if (is.object(retry)) {
785785
result.retry = {...retry};
786786

787-
if (retry.errorCodes) {
787+
if (is.array(retry.errorCodes)) {
788788
result.retry.errorCodes = [...retry.errorCodes];
789789
}
790790

791-
if (retry.methods) {
791+
if (is.array(retry.methods)) {
792792
result.retry.methods = [...retry.methods];
793793
}
794794

795-
if (retry.statusCodes) {
795+
if (is.array(retry.statusCodes)) {
796796
result.retry.statusCodes = [...retry.statusCodes];
797797
}
798798
}
799799

800-
if (raw.timeout) {
800+
if (is.object(raw.timeout)) {
801801
result.timeout = {...raw.timeout};
802802
}
803803

804-
if (hooks) {
805-
result.hooks = {};
804+
if (is.object(hooks)) {
805+
result.hooks = {
806+
...hooks,
807+
};
806808

807-
if (hooks.init) {
809+
if (is.array(hooks.init)) {
808810
result.hooks.init = [...hooks.init];
809811
}
810812

811-
if (hooks.beforeRequest) {
813+
if (is.array(hooks.beforeRequest)) {
812814
result.hooks.beforeRequest = [...hooks.beforeRequest];
813815
}
814816

815-
if (hooks.beforeError) {
817+
if (is.array(hooks.beforeError)) {
816818
result.hooks.beforeError = [...hooks.beforeError];
817819
}
818820

819-
if (hooks.beforeRedirect) {
821+
if (is.array(hooks.beforeRedirect)) {
820822
result.hooks.beforeRedirect = [...hooks.beforeRedirect];
821823
}
822824

823-
if (hooks.beforeRetry) {
825+
if (is.array(hooks.beforeRetry)) {
824826
result.hooks.beforeRetry = [...hooks.beforeRetry];
825827
}
826828

827-
if (hooks.afterResponse) {
829+
if (is.array(hooks.afterResponse)) {
828830
result.hooks.afterResponse = [...hooks.afterResponse];
829831
}
830832
}
831833

832834
// TODO: raw.searchParams
833835

834-
if (raw.pagination) {
836+
if (is.object(raw.pagination)) {
835837
result.pagination = {...raw.pagination};
836838
}
837839

test/arguments.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ test('can omit `url` option if using `prefixUrl`', withServer, async (t, server,
201201
await t.notThrowsAsync(got({}));
202202
});
203203

204-
test.failing('throws when `options.hooks` is not an object', async t => {
204+
test('throws when `options.hooks` is not an object', async t => {
205205
await t.throwsAsync(
206206
// @ts-expect-error Error tests
207207
got('https://example.com', {hooks: 'not object'}),
@@ -230,7 +230,7 @@ test('throws when known `options.hooks` array item is not a function', async t =
230230
);
231231
});
232232

233-
test.failing('does not allow extra keys in `options.hooks`', withServer, async (t, server, got) => {
233+
test('does not allow extra keys in `options.hooks`', withServer, async (t, server, got) => {
234234
server.get('/test', echoUrl);
235235

236236
// @ts-expect-error Error tests

0 commit comments

Comments
 (0)