Skip to content
Myr

prefix.ts

This file

Path

test/prefix.ts

88 lines · 4.0 KiB · TypeScript

Last changed by

TweaksSindre Sorhusbefore adoption3ded2aa

History of this file

Take it

Add `baseUrl` option, rename `prefixUrl` to `prefix`, and allow leading slashes in input (#606)Seth Holladaybefore adoption1f2ad7f

lines 1–4why, one step back
1import test from 'ava';
2import ky from '../source/index.js';3import {createHttpTestServer} from './helpers/create-http-test-server.js';4

Support readonly inputs and explicit undefined in TypeScript optionsSindre Sorhusbefore adoption51b3177

lines 5–12why, one step back
5test('undefined clears an inherited prefix without changing the parent', async t => {
6 const parent = ky.create({7 prefix: 'https://example.com/api/',8 fetch: async request => new Response(request.url),9 });10 const child = parent.extend({prefix: undefined});1112 t.is(await parent('users').text(), 'https://example.com/api/users');

TweaksSindre Sorhusbefore adoption3ded2aa

line 13why, one step back
13 await t.throwsAsync(child('users'), {instanceOf: TypeError});

Support readonly inputs and explicit undefined in TypeScript optionsSindre Sorhusbefore adoption51b3177

lines 14–17why, one step back
14 t.is(await child('https://example.org/users').text(), 'https://example.org/users');
15 t.is(await parent('users').text(), 'https://example.com/api/users');16});17

Add `baseUrl` option, rename `prefixUrl` to `prefix`, and allow leading slashes in input (#606)Seth Holladaybefore adoption1f2ad7f

lines 18–43why, one step back
18test('prefix option', async t => {
19 const server = await createHttpTestServer(t);20 server.get('/', (_request, response) => {21 response.end('/');22 });23 server.get('/foo', (_request, response) => {24 response.end('/foo');25 });26 server.get('/bar', (_request, response) => {27 response.end('/bar');28 });29 server.get('/foo/bar', (_request, response) => {30 response.end('/foo/bar');31 });3233 t.is(34 // @ts-expect-error {prefix: boolean} isn't officially supported35 await ky(`${server.url}/foo/bar`, {prefix: false}).text(),36 '/foo/bar',37 );38 t.is(await ky(`${server.url}/foo/bar`, {prefix: ''}).text(), '/foo/bar');39 t.is(await ky(new URL(`${server.url}/foo/bar`), {prefix: ''}).text(), '/foo/bar');40 t.is(await ky('foo/bar', {prefix: server.url}).text(), '/foo/bar');41 t.is(await ky('foo/bar', {prefix: new URL(server.url)}).text(), '/foo/bar');42 t.is(await ky('/bar', {prefix: `${server.url}/foo/`}).text(), '/foo/bar');43 t.is(await ky('/bar', {prefix: `${server.url}/foo`}).text(), '/foo/bar');

Minor tweaksSindre Sorhusbefore adoption4b37c96

lines 44–45why, one step back
44 t.is(await ky('//bar', {prefix: `${server.url}/foo`}).text(), '/foo/bar');
45 t.is(await ky('/bar', {prefix: `${server.url}/foo//`}).text(), '/foo/bar');

Add `baseUrl` option, rename `prefixUrl` to `prefix`, and allow leading slashes in input (#606)Seth Holladaybefore adoption1f2ad7f

lines 46–51why, one step back
46 t.is(await ky('bar', {prefix: `${server.url}/foo/`}).text(), '/foo/bar');
47 t.is(await ky('bar', {prefix: `${server.url}/foo`}).text(), '/foo/bar');48 t.is(await ky('bar', {prefix: new URL(`${server.url}/foo`)}).text(), '/foo/bar');49 t.is(await ky('', {prefix: server.url}).text(), '/');50 t.is(await ky('', {prefix: `${server.url}/`}).text(), '/');51 t.is(await ky('', {prefix: new URL(server.url)}).text(), '/');

Minor tweaksSindre Sorhusbefore adoption4b37c96

lines 52–61why, one step back
52 t.is(
53 await ky(54 'bar',55 Object.assign(56 Object.create({prefixUrl: `${server.url}/api`}),57 {prefix: `${server.url}/foo`},58 ),59 ).text(),60 '/foo/bar',61 );

TweaksSindre Sorhusbefore adoption3ded2aa

line 62why, one step back
62 await t.throwsAsync(ky(`${server.url}/unicorn`, {prefixUrl: ''}),

Minor tweaksSindre Sorhusbefore adoption4b37c96

lines 63–66why, one step back
63 {
64 message: 'The `prefixUrl` option has been renamed `prefix` in v2 and enhanced to allow slashes in input. See also the new `baseUrl` option for improved flexibility with standard URL resolution: https://github.com/sindresorhus/ky#baseurl',65 },66 );

TweaksSindre Sorhusbefore adoption3ded2aa

line 67why, one step back
67 await t.throwsAsync(ky(`${server.url}/unicorn`, {prefixUrl: undefined}),

Minor tweaksSindre Sorhusbefore adoption4b37c96

lines 68–71why, one step back
68 {
69 message: 'The `prefixUrl` option has been renamed `prefix` in v2 and enhanced to allow slashes in input. See also the new `baseUrl` option for improved flexibility with standard URL resolution: https://github.com/sindresorhus/ky#baseurl',70 },71 );

Add `baseUrl` option, rename `prefixUrl` to `prefix`, and allow leading slashes in input (#606)Seth Holladaybefore adoption1f2ad7f

line 72why, one step back
72

TweaksSindre Sorhusbefore adoption3ded2aa

line 73why, one step back
73 await t.throwsAsync(ky('/unicorn', {prefixUrl: `${server.url}/api`}),

Minor tweaksSindre Sorhusbefore adoption4b37c96

lines 74–77why, one step back
74 {
75 message: 'The `prefixUrl` option has been renamed `prefix` in v2 and enhanced to allow slashes in input. See also the new `baseUrl` option for improved flexibility with standard URL resolution: https://github.com/sindresorhus/ky#baseurl',76 },77 );

TweaksSindre Sorhusbefore adoption3ded2aa

line 78why, one step back
78 await t.throwsAsync(ky(new Request(`${server.url}/unicorn`), {prefixUrl: `${server.url}/api`}),

Minor tweaksSindre Sorhusbefore adoption4b37c96

lines 79–82why, one step back
79 {
80 message: 'The `prefixUrl` option has been renamed `prefix` in v2 and enhanced to allow slashes in input. See also the new `baseUrl` option for improved flexibility with standard URL resolution: https://github.com/sindresorhus/ky#baseurl',81 },82 );

TweaksSindre Sorhusbefore adoption3ded2aa

line 83why, one step back
83 await t.throwsAsync(ky(new URL(`${server.url}/unicorn`), {prefixUrl: `${server.url}/api`}),

Add `baseUrl` option, rename `prefixUrl` to `prefix`, and allow leading slashes in input (#606)Seth Holladaybefore adoption1f2ad7f

lines 84–88why, one step back
84 {
85 message: 'The `prefixUrl` option has been renamed `prefix` in v2 and enhanced to allow slashes in input. See also the new `baseUrl` option for improved flexibility with standard URL resolution: https://github.com/sindresorhus/ky#baseurl',86 },87 );88});