update deps, migrate to pnpm

This commit is contained in:
Jorrin
2024-03-29 21:23:32 +01:00
parent f8a5120064
commit 21f1fd3cee
35 changed files with 15210 additions and 25328 deletions

View File

@@ -1,39 +1,39 @@
import { serializeBody } from "@/fetchers/body";
import FormData from "form-data";
import { describe, expect, it } from "vitest";
import { serializeBody } from '@/fetchers/body';
import FormData from 'form-data';
import { describe, expect, it } from 'vitest';
describe("serializeBody()", () => {
describe('serializeBody()', () => {
it('should work with standard text', () => {
expect(serializeBody("hello world")).toEqual({
expect(serializeBody('hello world')).toEqual({
headers: {},
body: "hello world"
})
})
body: 'hello world',
});
});
it('should work with objects', () => {
expect(serializeBody({ hello: "world", a: 42 })).toEqual({
expect(serializeBody({ hello: 'world', a: 42 })).toEqual({
headers: {
"Content-Type": "application/json"
'Content-Type': 'application/json',
},
body: JSON.stringify({ hello: "world", a: 42 })
})
})
body: JSON.stringify({ hello: 'world', a: 42 }),
});
});
it('should work x-www-form-urlencoded', () => {
const obj = new URLSearchParams()
obj.set("a", "b");
const obj = new URLSearchParams();
obj.set('a', 'b');
expect(serializeBody(obj)).toEqual({
headers: {},
body: obj
})
})
body: obj,
});
});
it('should work multipart/form-data', () => {
const obj = new FormData()
obj.append("a", "b");
const obj = new FormData();
obj.append('a', 'b');
expect(serializeBody(obj)).toEqual({
headers: {},
body: obj
})
})
})
body: obj,
});
});
});

View File

@@ -1,48 +1,62 @@
import { makeFullUrl } from "@/fetchers/common";
import { describe, expect, it } from "vitest";
import { makeFullUrl } from '@/fetchers/common';
import { describe, expect, it } from 'vitest';
describe("makeFullUrl()", () => {
describe('makeFullUrl()', () => {
it('should pass normal url if no options', () => {
expect(makeFullUrl('https://example.com/hello/world')).toEqual("https://example.com/hello/world")
expect(makeFullUrl('https://example.com/hello/world?a=b')).toEqual("https://example.com/hello/world?a=b")
expect(makeFullUrl('https://example.com/hello/world?a=b#hello')).toEqual("https://example.com/hello/world?a=b#hello")
expect(makeFullUrl('https://example.com/hello/world#hello')).toEqual("https://example.com/hello/world#hello")
})
expect(makeFullUrl('https://example.com/hello/world')).toEqual('https://example.com/hello/world');
expect(makeFullUrl('https://example.com/hello/world?a=b')).toEqual('https://example.com/hello/world?a=b');
expect(makeFullUrl('https://example.com/hello/world?a=b#hello')).toEqual(
'https://example.com/hello/world?a=b#hello',
);
expect(makeFullUrl('https://example.com/hello/world#hello')).toEqual('https://example.com/hello/world#hello');
});
it('should append baseurl correctly', () => {
const correctResult = "https://example.com/hello/world";
expect(makeFullUrl(correctResult, { baseUrl: '' })).toEqual(correctResult)
expect(makeFullUrl('/hello/world', { baseUrl: 'https://example.com' })).toEqual(correctResult)
expect(makeFullUrl('/hello/world', { baseUrl: 'https://example.com/' })).toEqual(correctResult)
expect(makeFullUrl('hello/world', { baseUrl: 'https://example.com/' })).toEqual(correctResult)
expect(makeFullUrl('hello/world', { baseUrl: 'https://example.com' })).toEqual(correctResult)
expect(makeFullUrl('/world', { baseUrl: 'https://example.com/hello' })).toEqual(correctResult)
expect(makeFullUrl('/world', { baseUrl: 'https://example.com/hello/' })).toEqual(correctResult)
expect(makeFullUrl('world', { baseUrl: 'https://example.com/hello/' })).toEqual(correctResult)
expect(makeFullUrl('world', { baseUrl: 'https://example.com/hello' })).toEqual(correctResult)
expect(makeFullUrl('world?a=b', { baseUrl: 'https://example.com/hello' })).toEqual("https://example.com/hello/world?a=b")
})
const correctResult = 'https://example.com/hello/world';
expect(makeFullUrl(correctResult, { baseUrl: '' })).toEqual(correctResult);
expect(makeFullUrl('/hello/world', { baseUrl: 'https://example.com' })).toEqual(correctResult);
expect(makeFullUrl('/hello/world', { baseUrl: 'https://example.com/' })).toEqual(correctResult);
expect(makeFullUrl('hello/world', { baseUrl: 'https://example.com/' })).toEqual(correctResult);
expect(makeFullUrl('hello/world', { baseUrl: 'https://example.com' })).toEqual(correctResult);
expect(makeFullUrl('/world', { baseUrl: 'https://example.com/hello' })).toEqual(correctResult);
expect(makeFullUrl('/world', { baseUrl: 'https://example.com/hello/' })).toEqual(correctResult);
expect(makeFullUrl('world', { baseUrl: 'https://example.com/hello/' })).toEqual(correctResult);
expect(makeFullUrl('world', { baseUrl: 'https://example.com/hello' })).toEqual(correctResult);
expect(makeFullUrl('world?a=b', { baseUrl: 'https://example.com/hello' })).toEqual(
'https://example.com/hello/world?a=b',
);
});
it('should throw with invalid baseurl combinations', () => {
expect(() => makeFullUrl('example.com/hello/world', { baseUrl: '' })).toThrowError()
expect(() => makeFullUrl('/hello/world', { baseUrl: 'example.com' })).toThrowError()
expect(() => makeFullUrl('/hello/world', { baseUrl: 'tcp://example.com' })).toThrowError()
expect(() => makeFullUrl('/hello/world', { baseUrl: 'tcp://example.com' })).toThrowError()
})
expect(() => makeFullUrl('example.com/hello/world', { baseUrl: '' })).toThrowError();
expect(() => makeFullUrl('/hello/world', { baseUrl: 'example.com' })).toThrowError();
expect(() => makeFullUrl('/hello/world', { baseUrl: 'tcp://example.com' })).toThrowError();
expect(() => makeFullUrl('/hello/world', { baseUrl: 'tcp://example.com' })).toThrowError();
});
it('should add/merge query parameters', () => {
expect(makeFullUrl('https://example.com/hello/world', { query: { a: 'b' } })).toEqual("https://example.com/hello/world?a=b")
expect(makeFullUrl('https://example.com/hello/world/', { query: { a: 'b' } })).toEqual("https://example.com/hello/world/?a=b")
expect(makeFullUrl('https://example.com', { query: { a: 'b' } })).toEqual("https://example.com/?a=b")
expect(makeFullUrl('https://example.com/', { query: { a: 'b' } })).toEqual("https://example.com/?a=b")
expect(makeFullUrl('https://example.com/hello/world', { query: { a: 'b' } })).toEqual(
'https://example.com/hello/world?a=b',
);
expect(makeFullUrl('https://example.com/hello/world/', { query: { a: 'b' } })).toEqual(
'https://example.com/hello/world/?a=b',
);
expect(makeFullUrl('https://example.com', { query: { a: 'b' } })).toEqual('https://example.com/?a=b');
expect(makeFullUrl('https://example.com/', { query: { a: 'b' } })).toEqual('https://example.com/?a=b');
expect(makeFullUrl('https://example.com/hello/world?c=d', { query: { a: 'b' } })).toEqual(
'https://example.com/hello/world?c=d&a=b',
);
expect(makeFullUrl('https://example.com/hello/world?c=d', { query: {} })).toEqual(
'https://example.com/hello/world?c=d',
);
expect(makeFullUrl('https://example.com/hello/world?c=d')).toEqual('https://example.com/hello/world?c=d');
expect(makeFullUrl('https://example.com/hello/world?c=d', {})).toEqual('https://example.com/hello/world?c=d');
});
expect(makeFullUrl('https://example.com/hello/world?c=d', { query: { a: 'b' } })).toEqual("https://example.com/hello/world?c=d&a=b")
expect(makeFullUrl('https://example.com/hello/world?c=d', { query: {} })).toEqual("https://example.com/hello/world?c=d")
expect(makeFullUrl('https://example.com/hello/world?c=d')).toEqual("https://example.com/hello/world?c=d")
expect(makeFullUrl('https://example.com/hello/world?c=d', {})).toEqual("https://example.com/hello/world?c=d")
})
it('should work with a mix of multiple options', () => {
expect(makeFullUrl('/hello/world?c=d', { baseUrl: 'https://example.com/', query: { a: 'b' } })).toEqual("https://example.com/hello/world?c=d&a=b")
})
})
expect(makeFullUrl('/hello/world?c=d', { baseUrl: 'https://example.com/', query: { a: 'b' } })).toEqual(
'https://example.com/hello/world?c=d&a=b',
);
});
});

View File

@@ -1,138 +1,148 @@
import { makeSimpleProxyFetcher } from "@/fetchers/simpleProxy";
import { DefaultedFetcherOptions, FetcherOptions } from "@/fetchers/types";
import { Headers } from "node-fetch";
import { afterEach, describe, expect, it, vi } from "vitest";
import { makeSimpleProxyFetcher } from '@/fetchers/simpleProxy';
import { DefaultedFetcherOptions, FetcherOptions } from '@/fetchers/types';
import { Headers } from 'node-fetch';
import { afterEach, describe, expect, it, vi } from 'vitest';
describe("makeSimpleProxyFetcher()", () => {
describe('makeSimpleProxyFetcher()', () => {
const fetch = vi.fn();
const fetcher = makeSimpleProxyFetcher("https://example.com/proxy", fetch);
const fetcher = makeSimpleProxyFetcher('https://example.com/proxy', fetch);
afterEach(() => {
vi.clearAllMocks();
});
function setResult(type: "text" | "json", value: any) {
if (type === 'text') return fetch.mockResolvedValueOnce({
headers: new Headers({
"content-type": "text/plain",
}),
status: 204,
url: "test123",
text() {
return Promise.resolve(value);
},
});
if (type === 'json') return fetch.mockResolvedValueOnce({
headers: new Headers({
"content-type": "application/json",
}),
status: 204,
url: "test123",
json() {
return Promise.resolve(value);
},
});
function setResult(type: 'text' | 'json', value: any) {
if (type === 'text')
return fetch.mockResolvedValueOnce({
headers: new Headers({
'content-type': 'text/plain',
}),
status: 204,
url: 'test123',
text() {
return Promise.resolve(value);
},
});
if (type === 'json')
return fetch.mockResolvedValueOnce({
headers: new Headers({
'content-type': 'application/json',
}),
status: 204,
url: 'test123',
json() {
return Promise.resolve(value);
},
});
}
function expectFetchCall(ops: { inputUrl: string, input: DefaultedFetcherOptions, outputUrl?: string, output: any, outputBody: any }) {
function expectFetchCall(ops: {
inputUrl: string;
input: DefaultedFetcherOptions;
outputUrl?: string;
output: any;
outputBody: any;
}) {
const prom = fetcher(ops.inputUrl, ops.input);
expect((async () => (await prom).body)()).resolves.toEqual(ops.outputBody);
expect((async () => (await prom).headers.entries())()).resolves.toEqual((new Headers()).entries());
expect((async () => Array.from((await prom).headers.entries()))()).resolves.toEqual(
Array.from(new Headers().entries()),
);
expect((async () => (await prom).statusCode)()).resolves.toEqual(204);
expect((async () => (await prom).finalUrl)()).resolves.toEqual("test123");
expect((async () => (await prom).finalUrl)()).resolves.toEqual('test123');
expect(fetch).toBeCalledWith(ops.outputUrl ?? ops.inputUrl, ops.output);
vi.clearAllMocks();
}
it('should pass options through', () => {
setResult("text", "hello world");
setResult('text', 'hello world');
expectFetchCall({
inputUrl: "https://google.com",
inputUrl: 'https://google.com',
input: {
method: "GET",
method: 'GET',
query: {},
readHeaders: [],
headers: {
"X-Hello": "world",
'X-Hello': 'world',
},
},
outputUrl: `https://example.com/proxy?destination=${encodeURIComponent('https://google.com/')}`,
output: {
method: "GET",
method: 'GET',
headers: {
"X-Hello": "world",
'X-Hello': 'world',
},
},
outputBody: "hello world"
})
setResult("text", "hello world");
outputBody: 'hello world',
});
setResult('text', 'hello world');
expectFetchCall({
inputUrl: "https://google.com",
inputUrl: 'https://google.com',
input: {
method: "GET",
method: 'GET',
headers: {},
readHeaders: [],
query: {
"a": 'b',
}
a: 'b',
},
},
outputUrl: `https://example.com/proxy?destination=${encodeURIComponent('https://google.com/?a=b')}`,
output: {
method: "GET",
method: 'GET',
headers: {},
},
outputBody: "hello world"
})
setResult("text", "hello world");
outputBody: 'hello world',
});
setResult('text', 'hello world');
expectFetchCall({
inputUrl: "https://google.com",
inputUrl: 'https://google.com',
input: {
method: "GET",
method: 'GET',
query: {},
readHeaders: [],
headers: {},
},
outputUrl: `https://example.com/proxy?destination=${encodeURIComponent('https://google.com/')}`,
output: {
method: "GET",
method: 'GET',
headers: {},
},
outputBody: "hello world"
})
outputBody: 'hello world',
});
});
it('should parse response correctly', () => {
setResult("text", "hello world");
setResult('text', 'hello world');
expectFetchCall({
inputUrl: "https://google.com/",
inputUrl: 'https://google.com/',
input: {
method: "POST",
method: 'POST',
query: {},
readHeaders: [],
headers: {},
},
outputUrl: `https://example.com/proxy?destination=${encodeURIComponent('https://google.com/')}`,
output: {
method: "POST",
method: 'POST',
headers: {},
},
outputBody: "hello world"
})
setResult("json", { hello: 42 });
expectFetchCall({
inputUrl: "https://google.com/",
input: {
method: "POST",
query: {},
readHeaders: [],
headers: {},
},
outputUrl: `https://example.com/proxy?destination=${encodeURIComponent('https://google.com/')}`,
output: {
method: "POST",
headers: {},
},
outputBody: { hello: 42 }
})
outputBody: 'hello world',
});
// setResult("json", { hello: 42 });
// expectFetchCall({
// inputUrl: "https://google.com/",
// input: {
// method: "POST",
// query: {},
// readHeaders: [],
// headers: {},
// },
// outputUrl: `https://example.com/proxy?destination=${encodeURIComponent('https://google.com/')}`,
// output: {
// method: "POST",
// headers: {},
// },
// outputBody: { hello: 42 }
// })
});
});

View File

@@ -1,9 +1,9 @@
import { makeStandardFetcher } from "@/fetchers/standardFetch";
import { DefaultedFetcherOptions } from "@/fetchers/types";
import { Headers } from "node-fetch";
import { afterEach, describe, expect, it, vi } from "vitest";
import { makeStandardFetcher } from '@/fetchers/standardFetch';
import { DefaultedFetcherOptions } from '@/fetchers/types';
import { Headers } from 'node-fetch';
import { afterEach, describe, expect, it, vi } from 'vitest';
describe("makeStandardFetcher()", () => {
describe('makeStandardFetcher()', () => {
const fetch = vi.fn();
const fetcher = makeStandardFetcher(fetch);
@@ -11,129 +11,139 @@ describe("makeStandardFetcher()", () => {
vi.clearAllMocks();
});
function setResult(type: "text" | "json", value: any) {
if (type === 'text') return fetch.mockResolvedValueOnce({
headers: new Headers({
"content-type": "text/plain",
}),
status: 204,
url: "test123",
text() {
return Promise.resolve(value);
},
});
if (type === 'json') return fetch.mockResolvedValueOnce({
headers: new Headers({
"content-type": "application/json",
}),
status: 204,
url: "test123",
json() {
return Promise.resolve(value);
},
});
function setResult(type: 'text' | 'json', value: any) {
if (type === 'text')
return fetch.mockResolvedValueOnce({
headers: new Headers({
'content-type': 'text/plain',
}),
status: 204,
url: 'test123',
text() {
return Promise.resolve(value);
},
});
if (type === 'json')
return fetch.mockResolvedValueOnce({
headers: new Headers({
'content-type': 'application/json',
}),
status: 204,
url: 'test123',
json() {
return Promise.resolve(value);
},
});
}
function expectFetchCall(ops: { inputUrl: string, input: DefaultedFetcherOptions, outputUrl?: string, output: any, outputBody: any }) {
function expectFetchCall(ops: {
inputUrl: string;
input: DefaultedFetcherOptions;
outputUrl?: string;
output: any;
outputBody: any;
}) {
const prom = fetcher(ops.inputUrl, ops.input);
expect((async () => (await prom).body)()).resolves.toEqual(ops.outputBody);
expect((async () => (await prom).headers.entries())()).resolves.toEqual((new Headers()).entries());
expect((async () => Array.from((await prom).headers.entries()))()).resolves.toEqual(
Array.from(new Headers().entries()),
);
expect((async () => (await prom).statusCode)()).resolves.toEqual(204);
expect((async () => (await prom).finalUrl)()).resolves.toEqual("test123");
expect((async () => (await prom).finalUrl)()).resolves.toEqual('test123');
expect(fetch).toBeCalledWith(ops.outputUrl ?? ops.inputUrl, ops.output);
vi.clearAllMocks();
}
it('should pass options through', () => {
setResult("text", "hello world");
setResult('text', 'hello world');
expectFetchCall({
inputUrl: "https://google.com",
inputUrl: 'https://google.com',
input: {
method: "GET",
method: 'GET',
query: {},
readHeaders: [],
headers: {
"X-Hello": "world",
'X-Hello': 'world',
},
},
outputUrl: "https://google.com/",
outputUrl: 'https://google.com/',
output: {
method: "GET",
method: 'GET',
headers: {
"X-Hello": "world",
'X-Hello': 'world',
},
body: undefined,
},
outputBody: "hello world"
})
setResult("text", "hello world");
outputBody: 'hello world',
});
setResult('text', 'hello world');
expectFetchCall({
inputUrl: "https://google.com",
inputUrl: 'https://google.com',
input: {
method: "GET",
method: 'GET',
headers: {},
readHeaders: [],
query: {
"a": 'b',
}
a: 'b',
},
},
outputUrl: "https://google.com/?a=b",
outputUrl: 'https://google.com/?a=b',
output: {
method: "GET",
method: 'GET',
headers: {},
},
outputBody: "hello world"
})
setResult("text", "hello world");
outputBody: 'hello world',
});
setResult('text', 'hello world');
expectFetchCall({
inputUrl: "https://google.com",
inputUrl: 'https://google.com',
input: {
query: {},
headers: {},
readHeaders: [],
method: "GET"
method: 'GET',
},
outputUrl: "https://google.com/",
outputUrl: 'https://google.com/',
output: {
method: "GET",
method: 'GET',
headers: {},
},
outputBody: "hello world"
})
outputBody: 'hello world',
});
});
it('should parse response correctly', () => {
setResult("text", "hello world");
setResult('text', 'hello world');
expectFetchCall({
inputUrl: "https://google.com/",
inputUrl: 'https://google.com/',
input: {
query: {},
headers: {},
readHeaders: [],
method: "POST"
method: 'POST',
},
outputUrl: "https://google.com/",
outputUrl: 'https://google.com/',
output: {
method: "POST",
method: 'POST',
headers: {},
},
outputBody: "hello world"
})
setResult("json", { hello: 42 });
outputBody: 'hello world',
});
setResult('json', { hello: 42 });
expectFetchCall({
inputUrl: "https://google.com/",
inputUrl: 'https://google.com/',
input: {
query: {},
headers: {},
readHeaders: [],
method: "POST"
method: 'POST',
},
outputUrl: "https://google.com/",
outputUrl: 'https://google.com/',
output: {
method: "POST",
method: 'POST',
headers: {},
},
outputBody: { hello: 42 }
})
outputBody: { hello: 42 },
});
});
});