@hapaul/api 0.1.10 → 0.1.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.
Files changed (3) hide show
  1. package/dist/index.d.ts +334 -1154
  2. package/dist/index.js +34 -15
  3. package/package.json +8 -7
package/dist/index.js CHANGED
@@ -18,16 +18,19 @@ var Client = class {
18
18
  requestConfig = await this.beforeRequest({ config: requestConfig });
19
19
  method = requestConfig.method;
20
20
  const baseUrl = requestConfig.baseUrl ?? this.baseUrl;
21
- const url = baseUrl + path;
21
+ let url = baseUrl + path;
22
+ const query = requestConfig.params?.query;
23
+ if (query && typeof query === "object" && Object.entries(query).filter(([_, v]) => v !== void 0).length > 0) url = url + "?" + defaultQuerySerializer(query);
22
24
  const headersOptions = requestConfig.headers;
23
25
  const headers = mergeHeaders(headersOptions);
24
26
  const contentType = headers.get("Content-Type");
25
27
  const body = requestConfig.body;
26
28
  if (!contentType) headers.set("Content-Type", "application/json");
27
29
  try {
30
+ const serializedBody = defaultBodySerializer(body, headers);
28
31
  let response = await fetch(url, {
29
32
  ...requestConfig,
30
- body: defaultBodySerializer(body, headers),
33
+ body: serializedBody,
31
34
  method: method.toUpperCase(),
32
35
  headers
33
36
  });
@@ -46,19 +49,17 @@ var Client = class {
46
49
  if (status >= 200 && status < 300) isOk = true;
47
50
  else if (status >= 400) isOk = false;
48
51
  const contentType$1 = response.headers.get("Content-Type") || "application/json";
49
- switch (contentType$1) {
50
- case "application/json":
51
- assign(await response.json());
52
- break;
53
- case "text":
54
- assign(await response.text());
55
- break;
56
- case "arrayBuffer":
57
- assign(await response.arrayBuffer());
58
- break;
59
- default: assign(await response.bytes());
52
+ if (contentType$1.includes("application/json")) assign(await response.json());
53
+ else if (contentType$1.includes("text/plain")) assign(await response.text());
54
+ else assign(await response.arrayBuffer());
55
+ if (!isOk) {
56
+ let message = response.statusText;
57
+ if (typeof error === "object" && "message" in error) message = error.message;
58
+ for (const middleware of this.middlewares) middleware.onError?.(requestConfig, response, {
59
+ status,
60
+ message
61
+ });
60
62
  }
61
- if (!isOk) for (const middleware of this.middlewares) middleware.onError?.(requestConfig, error);
62
63
  return {
63
64
  data,
64
65
  error,
@@ -105,11 +106,29 @@ function mergeHeaders(...allHeaders) {
105
106
  }
106
107
  return finalHeaders;
107
108
  }
109
+ function defaultQuerySerializer(query) {
110
+ return new URLSearchParams(query).toString();
111
+ }
108
112
  function defaultBodySerializer(body, headers) {
109
113
  if (body instanceof FormData) return body;
110
114
  if (headers) {
111
115
  const contentType = headers.get("Content-Type");
112
- if (contentType === "application/x-www-form-urlencoded") return new URLSearchParams(body).toString();
116
+ switch (contentType) {
117
+ case "application/x-www-form-urlencoded": return new URLSearchParams(body).toString();
118
+ case "multipart/form-data": {
119
+ headers.delete("Content-Type");
120
+ const formData = new FormData();
121
+ Object.entries(body).forEach(([k, v]) => {
122
+ if (isArray(v)) v.forEach((x) => {
123
+ formData.append(k, x);
124
+ });
125
+ else if (isObject(v)) formData.append(k, JSON.stringify(v));
126
+ else if (isDate(v)) formData.append(k, v.toISOString());
127
+ else formData.append(k, v);
128
+ });
129
+ return formData;
130
+ }
131
+ }
113
132
  }
114
133
  return JSON.stringify(body);
115
134
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hapaul/api",
3
- "version": "0.1.10",
3
+ "version": "0.1.11",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "files": [
@@ -15,19 +15,20 @@
15
15
  },
16
16
  "types": "./dist/index.d.ts",
17
17
  "scripts": {
18
- "generate": "openapi-typescript http://localhost:4000/api/openapi.yaml -o ./schema.d.ts",
18
+ "generate": "openapi-typescript http://localhost:4000/docs/openapi.yaml -o ./schema.d.ts",
19
+ "prebuild": "npm run generate",
19
20
  "build": "rolldown -c rolldown.config.ts"
20
21
  },
21
22
  "keywords": [],
22
23
  "author": "",
23
24
  "license": "ISC",
24
- "packageManager": "pnpm@10.13.1",
25
+ "packageManager": "pnpm@10.23.0+sha512.21c4e5698002ade97e4efe8b8b4a89a8de3c85a37919f957e7a0f30f38fbc5bbdd05980ffe29179b2fb6e6e691242e098d945d1601772cad0fef5fb6411e2a4b",
25
26
  "devDependencies": {
26
- "@hapaul/dev": "^0.0.2",
27
- "openapi-typescript": "^7.8.0",
27
+ "@hapaul/dev": "^0.0.3",
28
+ "openapi-typescript": "^7.10.1",
28
29
  "openapi-typescript-helpers": "^0.0.15",
29
30
  "rolldown": "1.0.0-beta.30",
30
- "rolldown-plugin-dts": "^0.14.2",
31
- "typescript": "^5.8.3"
31
+ "rolldown-plugin-dts": "^0.14.3",
32
+ "typescript": "^5.9.3"
32
33
  }
33
34
  }