apigen-ts 0.0.1 → 0.0.3

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
@@ -1,5 +1,6 @@
1
1
  // Use uppercase for names in ApiClient to avoid conflict with the generated code
2
2
 
3
+ // eslint-disable-next-line @typescript-eslint/no-namespace
3
4
  namespace apigen {
4
5
  export type Config = { baseUrl: string; headers: Record<string, string> }
5
6
  export type Req = Omit<RequestInit, "body"> & {
@@ -28,32 +29,48 @@ export class ApiClient {
28
29
  return d
29
30
  }
30
31
 
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 ?? {})) {
32
+ async ParseError(rep: Response) {
33
+ try {
34
+ // try to parse domain error from response body
35
+ return await rep.json()
36
+ } catch (e) {
37
+ // otherwise return response as is
38
+ throw rep
39
+ }
40
+ }
41
+
42
+ async Fetch<T>(method: string, path: string, opts: apigen.Req = {}): Promise<T> {
43
+ let base = this.Config.baseUrl
44
+ if (globalThis.location && (base === "" || base.startsWith("/"))) {
45
+ base = `${globalThis.location.origin}${base.endsWith("/") ? base : `/${base}`}`
46
+ }
47
+
48
+ const url = new URL(path, base)
49
+ for (const [k, v] of Object.entries(opts?.search ?? {})) {
35
50
  url.searchParams.append(k, Array.isArray(v) ? v.join(",") : (v as string))
36
51
  }
37
52
 
38
- const headers = new Headers({ ...this.Config.headers, ...config.headers })
53
+ const headers = new Headers({ ...this.Config.headers, ...opts.headers })
39
54
  const ct = headers.get("content-type") ?? "application/json"
40
55
 
41
- let body: FormData | string | undefined = undefined
42
- if (ct === "multipart/form-data") {
56
+ let body: FormData | URLSearchParams | string | undefined = undefined
57
+
58
+ if (ct === "multipart/form-data" || ct === "application/x-www-form-urlencoded") {
43
59
  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>)) {
60
+ body = ct === "multipart/form-data" ? new FormData() : new URLSearchParams()
61
+ for (const [k, v] of Object.entries(opts.body as Record<string, string>)) {
46
62
  body.append(k, v)
47
63
  }
48
64
  }
49
65
 
50
- if (ct === "application/json" && typeof config.body !== "string") {
66
+ if (ct === "application/json" && typeof opts.body !== "string") {
51
67
  headers.set("content-type", "application/json")
52
- body = JSON.stringify(config.body)
68
+ body = JSON.stringify(opts.body)
53
69
  }
54
70
 
55
- const rep = await fetch(url.toString(), { method, ...config, headers, body })
56
- if (!rep.ok) throw rep
71
+ const credentials = opts.credentials ?? "include"
72
+ const rep = await fetch(url.toString(), { method, ...opts, headers, body, credentials })
73
+ if (!rep.ok) throw await this.ParseError(rep)
57
74
 
58
75
  const rs = await rep.text()
59
76
  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.3",
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