Skip to content
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ node_modules
dist
.env
.npmrc
coverage/
2 changes: 1 addition & 1 deletion .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"recommendations": [
"orta.vscode-jest",
"vitest.explorer",
"dbaeumer.vscode-eslint",
"streetsidesoftware.code-spell-checker"
]
Expand Down
18 changes: 14 additions & 4 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,25 +1,31 @@
{
"files.exclude": {
".*": true,
"*.sh": true,
"*.json": true,
"CODEOWNERS": true,
"dist": true,
"README.md": true,
"LICENSE": true,
"tsconfig.*": true,
"node_modules": true,
"*.code-workspace": true
"*.code-workspace": true,
"*.config.ts": true,
"coverage": true
},
"search.exclude": {
".*": false,
"*.sh": false,
"*.json": false,
"CODEOWNERS": false,
"dist": false,
"README.md": false,
"LICENSE": false,
"tsconfig.*": false,
"node_modules": false,
"*.code-workspace": false
"*.code-workspace": false,
"*.config.ts": false,
"coverage": false
},
"files.insertFinalNewline": true,
"editor.tabSize": 2,
Expand All @@ -41,6 +47,7 @@
"cryptly",
"flagly",
"gracely",
"isly",
"isoly",
"langly",
"paramly",
Expand All @@ -51,6 +58,9 @@
"smoothly",
"tidily",
"transactly",
"uply"
]
"typedly",
"uply",
"Renamer",
],
"typescript.tsdk": "node_modules/typescript/lib"
}
7 changes: 0 additions & 7 deletions Authorization/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
import { Blob } from "fetch-blob"
import { File } from "fetch-blob/file"
import { FormData as Form } from "formdata-polyfill/esm.min.js"
import { http } from "../index"

globalThis.FormData = Form
globalThis.Blob = Blob
globalThis.File = File

describe("Authorization", () => {
it("serialize Basic", async () =>
expect(http.Authorization.serialize({ user: "user", password: "password" })).toEqual("Basic dXNlcjpwYXNzd29yZA=="))
Expand Down
6 changes: 0 additions & 6 deletions CamelCase.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
import { Blob } from "fetch-blob"
import { File } from "fetch-blob/file"
import { FormData as Form } from "formdata-polyfill/esm.min.js"
import * as CamelCase from "./CamelCase"

globalThis.Blob = Blob
globalThis.File = File
globalThis.FormData = Form
describe("CamelCase", () => {
it("from contentType", async () => expect(CamelCase.from("contentType")).toEqual("Content-Type"))
it("to Content-Type", async () => expect(CamelCase.to("Content-Type")).toEqual("contentType"))
Expand Down
6 changes: 0 additions & 6 deletions Continuable.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
import { Blob } from "fetch-blob"
import { File } from "fetch-blob/file"
import { FormData as Form } from "formdata-polyfill/esm.min.js"
import { http } from "./index"

globalThis.Blob = Blob
globalThis.File = File
globalThis.FormData = Form
describe("Continuable", () => {
const array = [1, 2, 3, 4, 5, 6, 7, 8, 9]
it("is", async () => {
Expand Down
5 changes: 2 additions & 3 deletions Continuable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,10 @@ export namespace Continuable {
thisArg?: any
): Continuable<S> => create(filter.bind(result)(predicate, thisArg), cursor)
const slice = result.slice
result.slice = (start?: number, end?: number, thisArg?: any): Continuable<T> =>
create(slice.bind(result)(start, end, thisArg), cursor)
result.slice = (start?: number, end?: number): Continuable<T> => create(slice.bind(result)(start, end), cursor)
const splice = result.splice
result.splice = (start: number, deleteCount?: number, ...items: T[]): Continuable<T> =>
create(splice.bind(result)(start, deleteCount, ...items), cursor)
create(splice.bind(result)(start, deleteCount ?? 0, ...items), cursor)
const concat = result.concat
result.concat = (...items: ConcatArray<T>[]): Continuable<T> => create(concat.bind(result)(...items), cursor)
}
Expand Down
12 changes: 4 additions & 8 deletions FormData.spec.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
import { Blob } from "fetch-blob"
import { File } from "fetch-blob/file"
import { FormData as Form } from "formdata-polyfill/esm.min.js"
import { http } from "./index"

globalThis.Blob = Blob
globalThis.File = File
globalThis.FormData = Form
describe("form data", () => {
const file = new Blob([JSON.stringify({ test: "testing", tester: "potato" }, null, 2)], { type: "application/json" })
it("to", async () => {
const result = http.FormData.to({ value: "value", file: file, test: "test", tester: "tester" })
expect(result.get("")).toEqual(
new File([new TextEncoder().encode(JSON.stringify({ value: "value", test: "test", tester: "tester" }))], "blob")
new File([new TextEncoder().encode(JSON.stringify({ value: "value", test: "test", tester: "tester" }))], "blob", {
type: "application/json; charset=utf-8",
})
)
expect(result.get("file") instanceof Blob).toBeTruthy
expect(result.get("file") instanceof Blob).toBeTruthy()
})
it("from", async () => {
const data = new FormData()
Expand Down
55 changes: 29 additions & 26 deletions FormData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,25 @@ export namespace FormData {
form: globalThis.FormData
): { [key: string]: unknown } {
return Object.entries(data).reduce<Record<string, unknown>>((result, [key, value]) => {
value instanceof Blob
? form.append(name ? `${name}.${key}` : key, value)
: typeof value == "object" && value
? Array.isArray(value)
? (result[key] = toHelperArray(value, name ? `${name}.${key}` : key, form))
: (result[key] = toHelperObject(value as Record<string, unknown>, name ? `${name}.${key}` : key, form))
: (result[key] = value)
if (value instanceof Blob)
form.append(name ? `${name}.${key}` : key, value)
else if (typeof value == "object" && value)
result[key] = Array.isArray(value)
? toHelperArray(value, name ? `${name}.${key}` : key, form)
: toHelperObject(value as Record<string, unknown>, name ? `${name}.${key}` : key, form)
else
result[key] = value
return result
}, {})
}
function toHelperArray(data: unknown[], name: string, form: globalThis.FormData) {
return data.reduce<unknown[]>((result, value) => {
typeof value == "object" && value
? Array.isArray(value)
? result.push(toHelperArray(value, name, form))
: result.push(toHelperObject(value as Record<string, unknown>, name, form))
: result.push(value)
if (typeof value != "object" || !value)
result.push(value)
else if (!Array.isArray(value))
result.push(toHelperObject(value as Record<string, unknown>, name, form))
else
result.push(toHelperArray(value, name, form))
return result
}, [])
}
Expand All @@ -42,20 +44,21 @@ export namespace FormData {
return result
}
async function set(data: Record<string, unknown>, [head, ...tail]: string[], value: string | Blob) {
if (tail.length == 0)
value instanceof Blob && value.type.startsWith("application/json")
? !head
? merge(data, JSON.parse(await value.text()))
: typeof data[head] == "object" && data[head]
? merge(data[head] as Record<string, unknown>, JSON.parse(await value.text()))
: (data[head] = JSON.parse(await value.text()))
: (data[head] = value)
else
set(
typeof data[head] == "object" && data[head] ? (data[head] as Record<string, unknown>) : (data[head] = {}),
tail,
value
)
if (tail.length == 0) {
if (value instanceof Blob && value.type.startsWith("application/json"))
if (!head)
merge(data, JSON.parse(await value.text()))
else if (typeof data[head] == "object" && data[head])
merge(data[head] as Record<string, unknown>, JSON.parse(await value.text()))
else
data[head] = JSON.parse(await value.text())
else
data[head] = value
} else {
if (typeof data[head] != "object" || !data[head])
data[head] = {}
set(data[head] as Record<string, unknown>, tail, value)
}
}
function merge(target: Record<string, unknown>, ...sources: Record<string, unknown>[]) {
if (sources.length) {
Expand Down
6 changes: 0 additions & 6 deletions Middleware.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
import { Blob } from "fetch-blob"
import { File } from "fetch-blob/file"
import { FormData as Form } from "formdata-polyfill/esm.min.js"
import { http } from "./index"

globalThis.Blob = Blob
globalThis.File = File
globalThis.FormData = Form
describe("http.Middleware", () => {
it("server object", async () => {
expect(
Expand Down
7 changes: 3 additions & 4 deletions Parser.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { bind } from "./bind"
import type { Request } from "./Request"
import type { Response } from "./Response"

Expand Down Expand Up @@ -26,9 +25,9 @@ export class Parser<T = any | Promise<any>> {
return new Parser(this)
}
private static parser = new Parser()
static add = bind(this.parser.add, this.parser)
static parse = bind(this.parser.parse, this.parser)
static extend = bind(this.parser.extend, this.parser)
static add = this.parser.add.bind(this.parser)
static parse = this.parser.parse.bind(this.parser)
static extend = this.parser.extend.bind(this.parser)
static is<T = any | Promise<any>>(value: any | Parser<T>): value is Parser<T> {
return value instanceof Parser
}
Expand Down
7 changes: 0 additions & 7 deletions Request/Header.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
import "isomorphic-fetch"
import { Blob } from "fetch-blob"
import { File } from "fetch-blob/file"
import { FormData as Form } from "formdata-polyfill/esm.min.js"
import { http } from "../index"

globalThis.Blob = Blob
globalThis.File = File
globalThis.FormData = Form
describe("Request.Header", () => {
it("to", async () =>
expect(
Expand Down
7 changes: 0 additions & 7 deletions Request/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
import "isomorphic-fetch"
import { Blob } from "fetch-blob"
import { File } from "fetch-blob/file"
import { FormData } from "formdata-polyfill/esm.min"
import { http } from "../index"

globalThis.Blob = Blob
globalThis.File = File
globalThis.FormData = FormData
describe("Request", () => {
const output = {
method: "GET",
Expand Down
7 changes: 0 additions & 7 deletions Response/Header.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
import "isomorphic-fetch"
import { Blob } from "fetch-blob"
import { File } from "fetch-blob/file"
import { FormData as Form } from "formdata-polyfill/esm.min.js"
import { http } from "../index"

globalThis.Blob = Blob
globalThis.File = File
globalThis.FormData = Form
describe("Response.Header", () => {
it("from array", async () =>
expect(
Expand Down
12 changes: 0 additions & 12 deletions Response/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,5 @@
import "isomorphic-fetch"
import { Blob } from "fetch-blob"
import { File } from "fetch-blob/file"
import { FormData } from "formdata-polyfill/esm.min"
import WebSocket from "jest-websocket-mock"
import { http } from "../index"

globalThis.Blob = Blob
globalThis.File = File
globalThis.FormData = FormData
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-ignore
global.WebSocket = WebSocket

describe("Response", () => {
const output = {
status: 200,
Expand Down
9 changes: 5 additions & 4 deletions Response/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ export namespace Response {
contentType = (typeof contentType == "string" ? { contentType } : contentType) ?? {}
if (Response.Like.is(response)) {
const header: Response.Header = { ...response.header, ...contentType }
!header.contentType && (header.contentType = ContentType.deduce(response.body))
if (!header.contentType)
header.contentType = ContentType.deduce(response.body)
result = {
status: response.status ?? 200,
header,
Expand All @@ -81,7 +82,8 @@ export namespace Response {
? response
: (({ header, ...body }) => body)(response)
const header = Header.fromAny(response, contentType)
!header.contentType && (header.contentType = ContentType.deduce(body))
if (!header.contentType)
header.contentType = ContentType.deduce(body)
result = {
status: (typeof response == "object" && typeof response.status == "number" && response.status) || 200,
header,
Expand All @@ -92,8 +94,7 @@ export namespace Response {
}
return result
}
export type Header = ResponseHeader
export const Header = ResponseHeader
export import Header = ResponseHeader
export type Like<T = any | Promise<any>> = ResponseLike<T>
export const Like = ResponseLike
}
6 changes: 0 additions & 6 deletions Search.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
import { Blob } from "fetch-blob"
import { File } from "fetch-blob/file"
import { FormData as Form } from "formdata-polyfill/esm.min.js"
import { http } from "./index"

globalThis.Blob = Blob
globalThis.File = File
globalThis.FormData = Form
describe("http.Search", () => {
it("stringify", async () => {
const result = http.Search.stringify({ value: 1337, nested: { value: 42, string: "The power of attraction." } })
Expand Down
6 changes: 0 additions & 6 deletions Serializer.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
import { Blob } from "fetch-blob"
import { File } from "fetch-blob/file"
import { FormData } from "formdata-polyfill/esm.min"
import { http } from "./index"

globalThis.FormData = FormData
globalThis.Blob = Blob
globalThis.File = File
describe("Serializer", () => {
it("send standard form data", async () => {
const body = new FormData()
Expand Down
7 changes: 3 additions & 4 deletions Serializer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { bind } from "./bind"
import type { Request } from "./Request"
import type { Response } from "./Response"

Expand Down Expand Up @@ -30,9 +29,9 @@ export class Serializer {
return new Serializer(this)
}
private static serializer = new Serializer()
static add = bind(this.serializer.add, this.serializer)
static serialize = bind(this.serializer.serialize, this.serializer)
static extend = bind(this.serializer.extend, this.serializer)
static add = this.serializer.add.bind(this.serializer)
static serialize = this.serializer.serialize.bind(this.serializer)
static extend = this.serializer.extend.bind(this.serializer)
static is(value: any | Serializer): value is Serializer {
return value instanceof Serializer
}
Expand Down
Loading