apigen-ts 0.0.1 → 0.0.2

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.
package/dist/_template.ts CHANGED
@@ -28,32 +28,48 @@ export class ApiClient {
28
28
  return d
29
29
  }
30
30
 
31
- async Fetch<T>(method: string, path: string, config: apigen.Req = {}): Promise<T> {
32
- const fallback = globalThis.location?.origin ?? undefined
33
- const url = new URL(`${this.Config.baseUrl}/${path}`.replace(/\/+/g, "/"), fallback)
34
- for (const [k, v] of Object.entries(config?.search ?? {})) {
31
+ async ParseError(rep: Response) {
32
+ try {
33
+ // try to parse domain error from response body
34
+ return await rep.json()
35
+ } catch (e) {
36
+ // otherwise return response as is
37
+ throw rep
38
+ }
39
+ }
40
+
41
+ async Fetch<T>(method: string, path: string, opts: apigen.Req = {}): Promise<T> {
42
+ let base = this.Config.baseUrl
43
+ if (globalThis.location && (base === "" || base.startsWith("/"))) {
44
+ base = `${globalThis.location.origin}${base.endsWith("/") ? base : `/${base}`}`
45
+ }
46
+
47
+ const url = new URL(path, base)
48
+ for (const [k, v] of Object.entries(opts?.search ?? {})) {
35
49
  url.searchParams.append(k, Array.isArray(v) ? v.join(",") : (v as string))
36
50
  }
37
51
 
38
- const headers = new Headers({ ...this.Config.headers, ...config.headers })
52
+ const headers = new Headers({ ...this.Config.headers, ...opts.headers })
39
53
  const ct = headers.get("content-type") ?? "application/json"
40
54
 
41
- let body: FormData | string | undefined = undefined
42
- if (ct === "multipart/form-data") {
55
+ let body: FormData | URLSearchParams | string | undefined = undefined
56
+
57
+ if (ct === "multipart/form-data" || ct === "application/x-www-form-urlencoded") {
43
58
  headers.delete("content-type") // https://stackoverflow.com/a/61053359/3664464
44
- body = new FormData()
45
- for (const [k, v] of Object.entries(config.body as Record<string, string>)) {
59
+ body = ct === "multipart/form-data" ? new FormData() : new URLSearchParams()
60
+ for (const [k, v] of Object.entries(opts.body as Record<string, string>)) {
46
61
  body.append(k, v)
47
62
  }
48
63
  }
49
64
 
50
- if (ct === "application/json" && typeof config.body !== "string") {
65
+ if (ct === "application/json" && typeof opts.body !== "string") {
51
66
  headers.set("content-type", "application/json")
52
- body = JSON.stringify(config.body)
67
+ body = JSON.stringify(opts.body)
53
68
  }
54
69
 
55
- const rep = await fetch(url.toString(), { method, ...config, headers, body })
56
- if (!rep.ok) throw rep
70
+ const credentials = opts.credentials ?? "include"
71
+ const rep = await fetch(url.toString(), { method, ...opts, headers, body, credentials })
72
+ if (!rep.ok) throw this.ParseError(rep)
57
73
 
58
74
  const rs = await rep.text()
59
75
  try {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "type": "module",
3
3
  "name": "apigen-ts",
4
- "version": "0.0.1",
4
+ "version": "0.0.2",
5
5
  "license": "MIT",
6
6
  "author": "Vlad Pronsky <v.pronsky@gmail.com>",
7
7
  "repository": "vladkens/apigen-ts",
package/readme.md CHANGED
@@ -1,21 +1,13 @@
1
1
  # OpenAPI TypeScript client generator
2
2
 
3
3
  <div align="center">
4
- <a href="https://npmjs.org/package/apigen-ts">
5
- <img src="https://badgen.net/npm/v/apigen-ts" alt="version" />
6
- </a>
7
- <a href="https://github.com/vladkens/apigen-ts/actions">
8
- <img src="https://github.com/vladkens/apigen-ts/workflows/test/badge.svg" alt="test status" />
9
- </a>
10
- <a href="https://packagephobia.now.sh/result?p=apigen-ts">
11
- <img src="https://badgen.net/packagephobia/publish/apigen-ts" alt="size" />
12
- </a>
13
- <a href="https://npmjs.org/package/apigen-ts">
14
- <img src="https://badgen.net/npm/dm/apigen-ts" alt="downloads" />
15
- </a>
16
- <a href="https://github.com/vladkens/apigen-ts/blob/main/LICENSE">
17
- <img src="https://badgen.net/github/license/vladkens/apigen-ts" alt="license" />
18
- </a>
4
+
5
+ [<img src="https://badgen.net/npm/v/apigen-ts" alt="version" />](https://npmjs.org/package/apigen-ts)
6
+ [<img src="https://github.com/vladkens/apigen-ts/workflows/test/badge.svg" alt="test status" />](https://github.com/vladkens/apigen-ts/actions)
7
+ [<img src="https://badgen.net/packagephobia/publish/apigen-ts" alt="size" />](https://packagephobia.now.sh/result?p=apigen-ts)
8
+ [<img src="https://badgen.net/npm/dm/apigen-ts" alt="downloads" />](https://npmjs.org/package/apigen-ts)
9
+ [<img src="https://badgen.net/github/license/vladkens/apigen-ts" alt="license" />](https://github.com/vladkens/apigen-ts/blob/main/LICENSE)
10
+
19
11
  </div>
20
12
 
21
13
  ## Features