@dimah-s3/server 1.5.10 → 1.5.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,16 @@
1
- /** Build a synthetic Request for server-side `api` calls. */
1
+ /** Headers-only request for `s3.put` and the middleware fallback. */
2
2
  export declare function requestFromHeaders(headers?: HeadersInit): Request;
3
+ /**
4
+ * Request for a direct `s3.api` call when the caller did not pass one.
5
+ * Copies scalar `query` values and a plain JSON `body` so hooks see the same
6
+ * request an HTTP call would. better-call has no option for this: direct
7
+ * invocation passes `body` / `query` as objects and leaves `request` to us.
8
+ */
9
+ export declare function requestFromDirectCall(input: {
10
+ method: string;
11
+ pathname: string;
12
+ headers?: HeadersInit;
13
+ query?: unknown;
14
+ body?: unknown;
15
+ }): Request;
3
16
  //# sourceMappingURL=request.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"request.d.ts","sourceRoot":"","sources":["../../src/helpers/request.ts"],"names":[],"mappings":"AAAA,6DAA6D;AAC7D,wBAAgB,kBAAkB,CAAC,OAAO,CAAC,EAAE,WAAW,GAAG,OAAO,CAEjE"}
1
+ {"version":3,"file":"request.d.ts","sourceRoot":"","sources":["../../src/helpers/request.ts"],"names":[],"mappings":"AAGA,qEAAqE;AACrE,wBAAgB,kBAAkB,CAAC,OAAO,CAAC,EAAE,WAAW,GAAG,OAAO,CAEjE;AAED;;;;;GAKG;AACH,wBAAgB,qBAAqB,CAAC,KAAK,EAAE;IAC3C,MAAM,EAAE,MAAM,CAAC;IACf,QAAQ,EAAE,MAAM,CAAC;IACjB,OAAO,CAAC,EAAE,WAAW,CAAC;IACtB,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB,IAAI,CAAC,EAAE,OAAO,CAAC;CAChB,GAAG,OAAO,CAMV"}
package/dist/index.js CHANGED
@@ -9,8 +9,40 @@ import { createPresignedPost } from '@aws-sdk/s3-presigned-post';
9
9
  // src/dimah-s3.ts
10
10
 
11
11
  // src/helpers/request.ts
12
+ var SYNTHETIC_ORIGIN = "http://dimah-s3.local";
12
13
  function requestFromHeaders(headers) {
13
- return new Request("http://dimah-s3.local", { headers });
14
+ return new Request(SYNTHETIC_ORIGIN, { headers });
15
+ }
16
+ function requestFromDirectCall(input) {
17
+ const url = new URL(input.pathname, SYNTHETIC_ORIGIN);
18
+ appendQuery(url, input.query);
19
+ const headers = new Headers(input.headers);
20
+ const body = jsonBody(input.method, input.body, headers);
21
+ return new Request(url, { method: input.method, headers, body });
22
+ }
23
+ function appendQuery(url, query) {
24
+ if (query == null || typeof query !== "object" || Array.isArray(query))
25
+ return;
26
+ for (const [key, value] of Object.entries(query)) {
27
+ if (typeof value !== "string" && typeof value !== "number" && typeof value !== "boolean") {
28
+ continue;
29
+ }
30
+ url.searchParams.set(key, String(value));
31
+ }
32
+ }
33
+ function jsonBody(method, body, headers) {
34
+ if (body == null || method === "GET" || method === "HEAD" || !isPlainJson(body)) {
35
+ return;
36
+ }
37
+ headers.delete("content-length");
38
+ headers.delete("transfer-encoding");
39
+ headers.set("content-type", "application/json");
40
+ return JSON.stringify(body);
41
+ }
42
+ function isPlainJson(body) {
43
+ if (typeof body !== "object" || body === null) return false;
44
+ const proto = Object.getPrototypeOf(body);
45
+ return proto === Object.prototype || proto === Array.prototype;
14
46
  }
15
47
  async function deleteObjectBestEffort(client, bucket, key) {
16
48
  try {
@@ -735,6 +767,9 @@ function withS3Validation(options) {
735
767
  ...options,
736
768
  ...options.body !== void 0 ? { body: compileIfZod(options.body) } : {},
737
769
  ...options.query !== void 0 ? { query: compileIfZod(options.query) } : {},
770
+ // better-call reads the body before hooks. Clone first so
771
+ // `request.clone().json()` in guard / resolveScope still works.
772
+ ...options.body !== void 0 && options.cloneRequest === void 0 ? { cloneRequest: true } : {},
738
773
  onValidationError: options.onValidationError ?? (({ message }) => {
739
774
  throw errors.validationError(message);
740
775
  })
@@ -1721,17 +1756,22 @@ function applyPlugins(config) {
1721
1756
  function definePlugin(plugin) {
1722
1757
  return plugin;
1723
1758
  }
1724
-
1725
- // src/api/bind-endpoints.ts
1726
1759
  function bindEndpoints(endpoints, context) {
1727
1760
  const api = {};
1728
1761
  for (const [key, endpoint] of Object.entries(endpoints)) {
1729
1762
  const bound = (async (input = {}) => {
1730
1763
  const headers = input.headers;
1731
- const request = input.request ?? requestFromHeaders(headers);
1764
+ const provided = input.request;
1765
+ const request = provided ?? requestFromDirectCall({
1766
+ method: endpointMethod(endpoint),
1767
+ pathname: endpointPathname(endpoint, context.config.basePath),
1768
+ headers,
1769
+ query: input.query,
1770
+ body: input.body
1771
+ });
1732
1772
  return endpoint({
1733
1773
  ...input,
1734
- headers: headers ?? request.headers,
1774
+ headers: provided ? headers ?? request.headers : request.headers,
1735
1775
  request,
1736
1776
  context
1737
1777
  });
@@ -1742,6 +1782,18 @@ function bindEndpoints(endpoints, context) {
1742
1782
  }
1743
1783
  return api;
1744
1784
  }
1785
+ function endpointMethod(endpoint) {
1786
+ const method = endpoint.options?.method;
1787
+ if (Array.isArray(method)) return method[0] ?? "GET";
1788
+ if (typeof method === "string" && method !== "*") return method;
1789
+ return "GET";
1790
+ }
1791
+ function endpointPathname(endpoint, basePath) {
1792
+ const prefix = normalizeS3ApiBasePath(basePath ?? S3_API_BASE_PATH);
1793
+ const path = endpoint.path ?? "/";
1794
+ const suffix = path.startsWith("/") ? path : `/${path}`;
1795
+ return `${prefix}${suffix}` || "/";
1796
+ }
1745
1797
 
1746
1798
  // src/api/router.ts
1747
1799
  var NETWORK_CODES = /* @__PURE__ */ new Set([