typed-bridge 2.0.11 → 2.0.12

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.
@@ -10,45 +10,49 @@ const typescript_1 = __importDefault(require("typescript"));
10
10
  // Snippet to inject at the end
11
11
  const proxySnippet = () => `
12
12
  type typedBridgeConfig = {
13
- host: string
14
- headers: { [key: string]: string }
15
- onResponse: (res: Response) => void
13
+ host: string
14
+ headers: { [key: string]: string }
15
+ onResponse: (res: Response) => void
16
16
  }
17
17
 
18
18
  export const typedBridgeConfig: typedBridgeConfig = {
19
- host: '',
20
- headers: { 'Content-Type': 'application/json' },
21
- onResponse: (res: Response) => {}
19
+ host: '',
20
+ headers: { 'Content-Type': 'application/json' },
21
+ onResponse: (res: Response) => {}
22
22
  }
23
23
 
24
24
  export const typedBridge = new Proxy(
25
- {},
26
- {
27
- get(_, methodName: string) {
28
- return async (args: any) => {
29
- const response = await fetch(
30
- typedBridgeConfig.host + (typedBridgeConfig.host.endsWith('/') ? '' : '/') + methodName,
31
- {
32
- method: 'POST',
33
- headers: {
34
- 'Content-Type': 'application/json',
35
- ...typedBridgeConfig.headers
36
- },
37
- body: JSON.stringify(args)
38
- }
39
- )
25
+ {},
26
+ {
27
+ get(_, methodName: string) {
28
+ return async (args: any) => {
29
+ const response = await fetch(
30
+ typedBridgeConfig.host + (typedBridgeConfig.host.endsWith('/') ? '' : '/') + methodName,
31
+ {
32
+ method: 'POST',
33
+ headers: {
34
+ 'Content-Type': 'application/json',
35
+ ...typedBridgeConfig.headers
36
+ },
37
+ body: JSON.stringify(args)
38
+ }
39
+ )
40
40
 
41
- typedBridgeConfig.onResponse(response)
41
+ typedBridgeConfig.onResponse(response)
42
42
 
43
- if (!response.ok) {
44
- const errorText = await response.text()
45
- throw new Error(errorText)
46
- }
43
+ if (!response.ok) {
44
+ const errorText = await response.text()
45
+ console.error('REQ_FAILED', response.url, errorText)
46
+ throw new Error(errorText)
47
+ }
47
48
 
48
- return response.json()
49
- }
49
+ return response.json().catch(error => {
50
+ console.error('RES_NOT_JSON', response.url, error)
51
+ throw new Error(error.message)
52
+ })
53
+ }
54
+ }
50
55
  }
51
- }
52
56
  ) as typeof _default
53
57
 
54
58
  export default typedBridge
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "typed-bridge",
3
3
  "description": "Strictly typed server functions for typescript apps",
4
- "version": "2.0.11",
4
+ "version": "2.0.12",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
7
7
  "author": "neilveil",