@orpc/server-standard-fetch
Advanced tools
Comparing version 0.0.0-next.60b3782 to 0.0.0-next.63309dc
@@ -1,6 +0,124 @@ | ||
// src/index.ts | ||
var author = "unnoq"; | ||
// src/body.ts | ||
import { contentDisposition, parseContentDisposition } from "@orpc/server-standard"; | ||
async function toStandardBody(re) { | ||
if (!re.body) { | ||
return void 0; | ||
} | ||
const contentDisposition2 = re.headers.get("content-disposition"); | ||
if (contentDisposition2) { | ||
const fileName = parseContentDisposition(contentDisposition2).parameters.filename; | ||
if (typeof fileName === "string") { | ||
const blob2 = await re.blob(); | ||
return new File([blob2], fileName, { | ||
type: blob2.type | ||
}); | ||
} | ||
} | ||
const contentType = re.headers.get("content-type"); | ||
if (!contentType || contentType.startsWith("application/json")) { | ||
const text = await re.text(); | ||
if (!text) { | ||
return void 0; | ||
} | ||
return JSON.parse(text); | ||
} | ||
if (contentType.startsWith("multipart/form-data")) { | ||
return await re.formData(); | ||
} | ||
if (contentType.startsWith("application/x-www-form-urlencoded")) { | ||
return new URLSearchParams(await re.text()); | ||
} | ||
if (contentType.startsWith("text/")) { | ||
return await re.text(); | ||
} | ||
const blob = await re.blob(); | ||
return new File([blob], "blob", { | ||
type: blob.type | ||
}); | ||
} | ||
function toFetchBody(body, headers) { | ||
headers.delete("content-type"); | ||
headers.delete("content-disposition"); | ||
if (body === void 0) { | ||
return void 0; | ||
} | ||
if (body instanceof Blob) { | ||
headers.set("content-type", body.type); | ||
headers.set("content-length", body.size.toString()); | ||
headers.set( | ||
"content-disposition", | ||
contentDisposition(body instanceof File ? body.name : "blob", { type: "inline" }) | ||
); | ||
return body; | ||
} | ||
if (body instanceof FormData) { | ||
return body; | ||
} | ||
if (body instanceof URLSearchParams) { | ||
return body; | ||
} | ||
headers.set("content-type", "application/json"); | ||
return JSON.stringify(body); | ||
} | ||
// src/headers.ts | ||
function toStandardHeaders(headers, standardHeaders = {}) { | ||
for (const [key, value] of headers) { | ||
if (Array.isArray(standardHeaders[key])) { | ||
standardHeaders[key].push(value); | ||
} else if (standardHeaders[key] !== void 0) { | ||
standardHeaders[key] = [standardHeaders[key], value]; | ||
} else { | ||
standardHeaders[key] = value; | ||
} | ||
} | ||
return standardHeaders; | ||
} | ||
function toFetchHeaders(headers, fetchHeaders = new Headers()) { | ||
for (const [key, value] of Object.entries(headers)) { | ||
if (Array.isArray(value)) { | ||
for (const v of value) { | ||
fetchHeaders.append(key, v); | ||
} | ||
} else if (value !== void 0) { | ||
fetchHeaders.append(key, value); | ||
} | ||
} | ||
return fetchHeaders; | ||
} | ||
// src/request.ts | ||
import { once } from "@orpc/server-standard"; | ||
function toStandardRequest(request) { | ||
return { | ||
raw: { request }, | ||
url: new URL(request.url), | ||
signal: request.signal, | ||
method: request.method, | ||
body: once(() => toStandardBody(request)), | ||
get headers() { | ||
const headers = toStandardHeaders(request.headers); | ||
Object.defineProperty(this, "headers", { value: headers, writable: true }); | ||
return headers; | ||
}, | ||
set headers(value) { | ||
Object.defineProperty(this, "headers", { value, writable: true }); | ||
} | ||
}; | ||
} | ||
// src/response.ts | ||
function toFetchResponse(response) { | ||
const headers = toFetchHeaders(response.headers); | ||
const body = toFetchBody(response.body, headers); | ||
return new Response(body, { headers, status: response.status }); | ||
} | ||
export { | ||
author | ||
toFetchBody, | ||
toFetchHeaders, | ||
toFetchResponse, | ||
toStandardBody, | ||
toStandardHeaders, | ||
toStandardRequest | ||
}; | ||
//# sourceMappingURL=index.js.map |
@@ -1,3 +0,5 @@ | ||
/** unnoq */ | ||
export declare const author = "unnoq"; | ||
export * from './body'; | ||
export * from './headers'; | ||
export * from './request'; | ||
export * from './response'; | ||
//# sourceMappingURL=index.d.ts.map |
{ | ||
"name": "@orpc/server-standard-fetch", | ||
"type": "module", | ||
"version": "0.0.0-next.60b3782", | ||
"version": "0.0.0-next.63309dc", | ||
"license": "MIT", | ||
@@ -30,2 +30,5 @@ "homepage": "https://unnoq.com", | ||
], | ||
"dependencies": { | ||
"@orpc/server-standard": "0.0.0-next.63309dc" | ||
}, | ||
"scripts": { | ||
@@ -32,0 +35,0 @@ "build": "tsup --clean --sourcemap --entry.index=src/index.ts --format=esm --onSuccess='tsc -b --noCheck'", |
Trivial Package
Supply chain riskPackages less than 10 lines of code are easily copied into your own project and may not warrant the additional supply chain risk of an external dependency.
Found 1 instance in 1 package
6954
8
146
0
1
+ Added@orpc/server-standard@0.0.0-next.63309dc(transitive)
+ Added@tinyhttp/content-disposition@2.2.2(transitive)
+ Addedtype-fest@4.35.0(transitive)